encoder.h

00001 /*--License:
00002         Kyra Sprite Engine
00003         Copyright Lee Thomason (Grinning Lizard Software) 2001-2005
00004         www.grinninglizard.com/kyra
00005         www.sourceforge.net/projects/kyra
00006 
00007         Kyra is provided under the LGPL. 
00008         
00009         I kindly request you display a splash screen (provided in the HTML documentation)
00010         to promote Kyra and acknowledge the software and everyone who has contributed to it, 
00011         but it is not required by the license.
00012 
00013 --- LGPL License --
00014 
00015     This library is free software; you can redistribute it and/or
00016     modify it under the terms of the GNU Lesser General Public
00017     License as published by the Free Software Foundation; either
00018     version 2.1 of the License, or (at your option) any later version.
00019 
00020     This library is distributed in the hope that it will be useful,
00021     but WITHOUT ANY WARRANTY; without even the implied warranty of
00022     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00023     Lesser General Public License for more details.
00024 
00025     You should have received a copy of the GNU Lesser General Public
00026     License along with this library; if not, write to the Free Software
00027     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028 
00029         The full text of the license can be found in lgpl.txt
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 // Disable the no-exception handling warning.
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         // Writes the header of the .dat file.
00070         bool StartDat();
00071 
00072         /*      Process an entire doc, write the result of the
00073                 encoding to stream. Normally called between a
00074                 'StartDat' and 'EndDat'. If the 'screen' is non-0
00075                 then the surface will be displayed as we go.
00076         */
00077         bool ProcessDoc( const TiXmlDocument& doc ); 
00078 //                                       KrEngine*  engine,
00079 //                                       KrConsole* output );
00080 
00081         // Writes the tail of the .dat and completes the header.
00082         bool EndDat();
00083 
00084         void WriteHeader( const char* name, 
00085                                           FILE* stream,
00086                                           const char* prefix );
00087 
00088         /*  A utility function that loads a surface. Will be converted
00089                 to 32 bit regardless of the source format.
00090 
00091                 @param filename         The filename of the surface to load.
00092                 @param transparent      An array of colors that will be interpreted     
00093                                                         as transparent. (Only used for surfaces
00094                                                         less than 32 bit.)
00095                 @param nTransparent     The number of entries in the transparent array.
00096                 @param error            If an error occurs, the despcription will go here.
00097         */
00098         enum
00099         {
00100                 RGBA,
00101                 UpperLeft,
00102                 LowerLeft,
00103                 UpperRight,
00104                 LowerRight
00105         };
00106 
00107         struct Transparent
00108         {
00109                 int type;                       // one of the enums, above.
00110                 KrRGBA rgba;            // if RGBA, value is here.
00111         };
00112 
00113         static SDL_Surface* Load32Surface(      const char* filename,
00114                                                                                 Transparent* transparent,
00115                                                                                 int nTransparent,
00116                                                                                 std::string* error );
00117 
00118         /*  Exactly the same as Load32Surface, except that it loads to 
00119                 a canvas. A transition function on the road to wrapping
00120                 the video layer.
00121         */
00122         static KrCanvasResource* Load32Canvas(  const char* filename,
00123                                                                                         const KrRGBA* transparent,
00124                                                                                         int nTransparent,
00125                                                                                         std::string* error );
00126 
00127         /*  Useful, if odd, function to create a fixed font
00128                 from a BMP file in a buffer. It assumes
00129                 ascii: start 32, length 95.
00130         */
00131         static KrFontResource* CreateFixedFontResource( const char* resourceName,
00132                                                                                                         const U8* buffer,
00133                                                                                                         int bufferSize );
00134                                                                                                         
00135 
00136         // Used by the tags to Save() themselves.
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 //                                                                                                              GLASSERT( _numSegments >= _numLines );
00145                                                                                                                 numLine         += _numLines;
00146                                                                                                                 numSegment      += _numSegments;
00147                                                                                                                 numRGBA         += _numRGBA;
00148                                                                                                                 GLASSERT( numRGBA >= numSegment );
00149 //                                                                                                              GLASSERT( numSegment >= numLine );
00150                                                                                                          }
00151         void KrEncoder::Save();
00152 
00153         KrResourceVault* GetVault()                                             { return &vault; }
00154 
00155   private:
00156         static ImageLoaderFunc ImageLoader;
00157         static void GetImageLoader();
00158 
00159 //      void InitOutput( SDL_Surface* screen );
00160 
00161         enum
00162         {
00163                 TYPE_NONE, 
00164                 TYPE_SPRITE, 
00165                 TYPE_TILE, 
00166                 TYPE_FONT
00167         };
00168 
00169         enum
00170         {
00171                 SUBTYPE_FIXEDFONT,              // The default value.
00172                 SUBTYPE_SFONT,
00173         };
00174 
00175         struct AllInfo
00176         {
00177                 AllInfo() :     type( 0 ),
00178                                         subType( 0 ),
00179                                         name( "NONE" ), action( "NONE" ), 
00180                                         //useEntireImage( false ),
00181                                         frameCount( 0 ),
00182                                         x( 0 ), y( 0 ), 
00183                                         width( 0 ), height( 0 ), 
00184                                         //hasHotspot( false ),
00185                                         hotx( 0 ), hoty( 0 ),
00186                                         //hasDelta( false ),
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;                           // SPRITE, TILE, ...
00194                 int                     subType;                        // SFONT or FIXEDFONT
00195                 std::string name;                               // Sprite, Tile, Font, etc.
00196                 std::string action;
00197                 //int                   frame;                  // Automatically calculated
00198                 //bool          useEntireImage;         // If true, width, height, x, y don't matter
00199                 int                     frameCount;                     // Limit for # frames to be read by color key
00200                 int                     x, y;
00201                 int                     width, height;          // Width and Height set for tiles
00202                 int                     hotx, hoty;
00203                 int                     deltax, deltay;
00204                 int                     isoTargetWidth;
00205                 int                     fontStart;                      // Glyph number that starts the font
00206                 int                     fontLength;
00207                 int                     space;                          // Space glyph number for a font
00208                 KrRGBA          keyColor;                       // Color key color, if used.
00209                 std::vector< int > rotation;            // All the rotation angles specified.
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         /*      Process a particular frame to the stream.
00228                 @param frame    input: node that describes the frame
00229                 @param surface  input: the surface where the data is 
00230                 @param rle              output: where to write the frame data
00231         */
00232 //      bool ProcessFrame(      const AllInfo& allInfo,
00233 //                                              SDL_Surface* surface, 
00234 //                                              KrRle* rle );
00235 
00236         // Font encoding
00237         bool EncodeSFontFrames( SDL_Surface* surface, const AllInfo& allInfo, KrConsole* console );
00238         bool EncodeFixedFontFrames( SDL_Surface* surface, const AllInfo& allInfo, KrConsole* console );
00239 
00240         // Image encoding
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         // Direct encoding
00246         bool EncodeColorKey( SDL_Surface* surface, const AllInfo& allInfo );
00247 //      bool EncodeImage( SDL_Surface* surface, TiXmlElement* e, KrConsole* console );
00248 
00249         bool EncodeBinary( const TiXmlElement* e );
00250         bool EncodeText( const TiXmlElement* e );
00251 
00252         // Printing
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         // For storing the 'constant' information used to write the
00262         // header file.
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;               // definiton or direct
00289 
00290         // Output information.
00291         int thisLine;
00292         int nextLine;
00293         int outputX;
00294         SDL_Surface* output;
00295 
00296         // Count for information
00297         U32 numRGBA, numLine, numSegment;
00298         U32 numRlePos;  // position to write count information.
00299 
00300         KrResourceVault vault;
00301 
00302         KrCachedWrite cachedWrite;
00303         SDL_RWops* stream;
00304 };
00305 
00306 #endif
00307 

Generated on Thu Jul 20 20:45:31 2006 for Kyra by  doxygen 1.4.7