00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef ENCODER_INCLUDED
00033 #define ENCODER_INCLUDED
00034
00035 #include "SDL.h"
00036 #include <vector>
00037
00038 #include "../../tinyxml/tinyxml.h"
00039 #ifdef _MSC_VER
00040
00041 #pragma warning( disable : 4530 )
00042 #pragma warning( disable : 4786 )
00043 #endif
00044
00045 #include "../../grinliz/gltypes.h"
00046 #include "../engine/color.h"
00047 #include "../util/gllist.h"
00048 #include "kyraresource.h"
00049 #include "namefield.h"
00050 #include "vault.h"
00051
00052
00053 class KrRle;
00054 class KrCanvasResource;
00055 class KrPixelBlock;
00056 class KrCachedWrite;
00057 class KrConsole;
00058 class KrEngine;
00059
00060
00061 typedef SDL_Surface* (*ImageLoaderFunc)( const char* );
00062
00063
00064 class KrEncoder
00065 {
00066 public:
00067 KrEncoder( SDL_RWops* stream );
00068
00069
00070 bool StartDat();
00071
00072
00073
00074
00075
00076
00077 bool ProcessDoc( const TiXmlDocument& doc );
00078
00079
00080
00081
00082 bool EndDat();
00083
00084 void WriteHeader( const char* name,
00085 FILE* stream,
00086 const char* prefix );
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 enum
00099 {
00100 RGBA,
00101 UpperLeft,
00102 LowerLeft,
00103 UpperRight,
00104 LowerRight
00105 };
00106
00107 struct Transparent
00108 {
00109 int type;
00110 KrRGBA rgba;
00111 };
00112
00113 static SDL_Surface* Load32Surface( const char* filename,
00114 Transparent* transparent,
00115 int nTransparent,
00116 std::string* error );
00117
00118
00119
00120
00121
00122 static KrCanvasResource* Load32Canvas( const char* filename,
00123 const KrRGBA* transparent,
00124 int nTransparent,
00125 std::string* error );
00126
00127
00128
00129
00130
00131 static KrFontResource* CreateFixedFontResource( const char* resourceName,
00132 const U8* buffer,
00133 int bufferSize );
00134
00135
00136
00137 void StartTag( U32 tag );
00138 void EndTag();
00139 void WriteCached( const std::string& name ) { cachedWrite.Write( name ); }
00140 SDL_RWops* Stream() { return stream; }
00141 void AddCount( U32 _numLines, U32 _numSegments, U32 _numRGBA )
00142 {
00143 GLASSERT( _numRGBA >= _numSegments );
00144
00145 numLine += _numLines;
00146 numSegment += _numSegments;
00147 numRGBA += _numRGBA;
00148 GLASSERT( numRGBA >= numSegment );
00149
00150 }
00151 void KrEncoder::Save();
00152
00153 KrResourceVault* GetVault() { return &vault; }
00154
00155 private:
00156 static ImageLoaderFunc ImageLoader;
00157 static void GetImageLoader();
00158
00159
00160
00161 enum
00162 {
00163 TYPE_NONE,
00164 TYPE_SPRITE,
00165 TYPE_TILE,
00166 TYPE_FONT
00167 };
00168
00169 enum
00170 {
00171 SUBTYPE_FIXEDFONT,
00172 SUBTYPE_SFONT,
00173 };
00174
00175 struct AllInfo
00176 {
00177 AllInfo() : type( 0 ),
00178 subType( 0 ),
00179 name( "NONE" ), action( "NONE" ),
00180
00181 frameCount( 0 ),
00182 x( 0 ), y( 0 ),
00183 width( 0 ), height( 0 ),
00184
00185 hotx( 0 ), hoty( 0 ),
00186
00187 deltax( 0 ), deltay( 0 ),
00188 isoTargetWidth( 0 ),
00189 fontStart( 0 ),
00190 fontLength( 0 ),
00191 space( 0 ) { keyColor.Set( 0, 0, 0, 0 ); }
00192
00193 int type;
00194 int subType;
00195 std::string name;
00196 std::string action;
00197
00198
00199 int frameCount;
00200 int x, y;
00201 int width, height;
00202 int hotx, hoty;
00203 int deltax, deltay;
00204 int isoTargetWidth;
00205 int fontStart;
00206 int fontLength;
00207 int space;
00208 KrRGBA keyColor;
00209 std::vector< int > rotation;
00210 };
00211
00212 void CalcAllInfo( const TiXmlNode* node, AllInfo* info, SDL_Surface* );
00213
00214 void CreateIsoTile( KrPaintInfo* info,
00215 int x, int y,
00216 int width, int height,
00217 KrRle* rle,
00218 int isoIdealWidth,
00219 int rotation );
00220
00221 void IsoToSource( GlFixed x, GlFixed y, GlFixed isoWidth,
00222 GlFixed sourceW, GlFixed sourceH,
00223 GlFixed* sourceX, GlFixed* sourceY,
00224 int rotation, GlFixed increment );
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237 bool EncodeSFontFrames( SDL_Surface* surface, const AllInfo& allInfo, KrConsole* console );
00238 bool EncodeFixedFontFrames( SDL_Surface* surface, const AllInfo& allInfo, KrConsole* console );
00239
00240
00241 bool EncodeSprite( SDL_Surface* surface, const AllInfo& allInfo );
00242 bool EncodeFont( SDL_Surface* surface, const AllInfo& allInfo );
00243 bool EncodeTile( SDL_Surface* surface, const AllInfo& allInfo );
00244
00245
00246 bool EncodeColorKey( SDL_Surface* surface, const AllInfo& allInfo );
00247
00248
00249 bool EncodeBinary( const TiXmlElement* e );
00250 bool EncodeText( const TiXmlElement* e );
00251
00252
00253 void PrintSprite( const std::string& spriteName, const std::string& actionName,
00254 int frameNum, KrRle* rle );
00255 void PrintTile( const std::string& tileName, KrTileResource* tile );
00256
00257 SDL_Surface* LoadSurface( const TiXmlElement* definition, std::string* error );
00258
00259 U32 tagpos;
00260
00261
00262
00263 enum
00264 {
00265 SPRITE,
00266 TILE,
00267 ACTION,
00268 NUM_NAME_BINS,
00269 };
00270
00271 enum
00272 {
00273 DEFINITION,
00274 DIRECT
00275 };
00276
00277 struct Scan
00278 {
00279 Scan() { Init(); }
00280 void Init() { x = 0; y = 0; }
00281
00282 int x;
00283 int y;
00284 };
00285
00286 Scan scan;
00287
00288 int mode;
00289
00290
00291 int thisLine;
00292 int nextLine;
00293 int outputX;
00294 SDL_Surface* output;
00295
00296
00297 U32 numRGBA, numLine, numSegment;
00298 U32 numRlePos;
00299
00300 KrResourceVault vault;
00301
00302 KrCachedWrite cachedWrite;
00303 SDL_RWops* stream;
00304 };
00305
00306 #endif
00307