Main Page   Class Hierarchy   Compound List   File List   Compound Members  

encoder.h

00001 /*--License:
00002         Kyra Sprite Engine
00003         Copyright Lee Thomason (Grinning Lizard Software) 2001-2002
00004         www.grinninglizard.com/kyra
00005         www.sourceforge.net/projects/kyra
00006 
00007         Kyra is provided under 2 licenses:
00008 
00009         - The GPL, with no additional restrictions.
00010         - The LGPL, provided you display the Kyra splash screen, described below.
00011 
00012 
00013 --- GPL License --
00014         This program is free software; you can redistribute it and/or
00015         modify it under the terms of the GNU General Public License
00016         as published by the Free Software Foundation; either version 2
00017         of the License, or (at your option) any later version.
00018 
00019         This program is distributed in the hope that it will be useful,
00020         but WITHOUT ANY WARRANTY; without even the implied warranty of
00021         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022         GNU General Public License for more details.
00023 
00024         You should have received a copy of the GNU General Public License
00025         along with this program; if not, write to the Free Software
00026         Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00027 
00028         The full text of the license can be found in license.txt
00029 
00030 
00031 --- LGPL License --
00032   **Provided you kindly display the Kyra splash screen (details below), 
00033         you     may use the LGPL license:**
00034 
00035     This library is free software; you can redistribute it and/or
00036     modify it under the terms of the GNU Lesser General Public
00037     License as published by the Free Software Foundation; either
00038     version 2.1 of the License, or (at your option) any later version.
00039 
00040     This library is distributed in the hope that it will be useful,
00041     but WITHOUT ANY WARRANTY; without even the implied warranty of
00042     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00043     Lesser General Public License for more details.
00044 
00045     You should have received a copy of the GNU Lesser General Public
00046     License along with this library; if not, write to the Free Software
00047     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00048 
00049         The full text of the license can be found in lgpl.txt
00050 
00051 
00052 --- Kyra Splash Screen.
00053 
00054         It would be appreciate if you display the Kyra splash screen when using
00055         either license, however it is only required for the LGPL. All the
00056         resources for the splash are compiled into the library, and it can be
00057         accessed through the following API:
00058 
00059                 KrEngine::StartSplash
00060                 KrEngine::UpdateSplash
00061                 KrEngine::EndSplash
00062 
00063         Full documentation is provided with the KrEngine class. The splash screen
00064         should be displayed for 2 seconds.
00065 
00066         Thank you.
00067 */
00068 
00069 #ifndef ENCODER_INCLUDED
00070 #define ENCODER_INCLUDED
00071 
00072 #include "SDL.h"
00073 #include "../tinyxml/tinyxml.h"
00074 #include "../util/gltypes.h"
00075 #include "../engine/color.h"
00076 #include "../util/gllist.h"
00077 #include "kyraresource.h"
00078 #include "namefield.h"
00079 #include "vault.h"
00080 
00081 
00082 class KrRle;
00083 class KrCanvasResource;
00084 class KrPixelBlock;
00085 class KrCachedWrite;
00086 class KrConsole;
00087 class KrEngine;
00088 
00089 
00090 typedef SDL_Surface* (*ImageLoaderFunc)( const char* );
00091 
00092 
00093 class KrEncoder
00094 {
00095   public:
00096         KrEncoder( SDL_RWops* stream );
00097 
00098         // Writes the header of the .dat file.
00099         bool StartDat();
00100 
00101         /*      Process an entire doc, write the result of the
00102                 encoding to stream. Normally called between a
00103                 'StartDat' and 'EndDat'. If the 'screen' is non-0
00104                 then the surface will be displayed as we go.
00105         */
00106         bool ProcessDoc( const TiXmlDocument& doc, 
00107                                          KrEngine*  engine,
00108                                          KrConsole* output );
00109 
00110         // Writes the tail of the .dat and completes the header.
00111         bool EndDat();
00112 
00113         void WriteHeader( const char* name, 
00114                                           FILE* stream,
00115                                           const char* prefix );
00116 
00117         /*  A utility function that loads a surface. Will be converted
00118                 to 32 bit regardless of the source format.
00119 
00120                 @param filename         The filename of the surface to load.
00121                 @param transparent      An array of colors that will be interpreted     
00122                                                         as transparent. (Only used for surfaces
00123                                                         less than 32 bit.)
00124                 @param nTransparent     The number of entries in the transparent array.
00125                 @param error            If an error occurs, the despcription will go here.
00126         */
00127         enum
00128         {
00129                 RGBA,
00130                 UpperLeft,
00131                 LowerLeft,
00132                 UpperRight,
00133                 LowerRight
00134         };
00135 
00136         struct Transparent
00137         {
00138                 int type;                       // one of the enums, above.
00139                 KrRGBA rgba;            // if RGBA, value is here.
00140         };
00141 
00142         /*      Very, very strange limitation. It is important to not link the engine
00143                 library with SDL_Image...because it doesn't use it. But static linking
00144                 is confusing the linker, so it looks for the unused IMG_Load function.
00145                 If a tool wishes to make that function available, its address is passed
00146                 here.
00147         */
00148         static void SetImageLoader( ImageLoaderFunc i )         { ImageLoader = i; }
00149 
00150         static SDL_Surface* Load32Surface(      const char* filename,
00151                                                                                 Transparent* transparent,
00152                                                                                 int nTransparent,
00153                                                                                 std::string* error );
00154 
00155         /*  Exactly the same as Load32Surface, except that it loads to 
00156                 a canvas. A transition function on the road to wrapping
00157                 the video layer.
00158         */
00159         static KrCanvasResource* Load32Canvas(  const char* filename,
00160                                                                                         const KrRGBA* transparent,
00161                                                                                         int nTransparent,
00162                                                                                         std::string* error );
00163 
00164         /*  Useful, if odd, function to create a fixed font
00165                 from a BMP file in a buffer. It assumes
00166                 ascii: start 32, length 95.
00167         */
00168         static KrFontResource* CreateFixedFontResource( const char* resourceName,
00169                                                                                                         const U8* buffer,
00170                                                                                                         int bufferSize );
00171                                                                                                         
00172 
00173         // Used by the tags to Save() themselves.
00174         void StartTag( U32 tag );
00175         void EndTag();
00176         void WriteCached( const std::string& name )             { cachedWrite.Write( name ); }
00177         SDL_RWops* Stream()                                                             { return stream; }
00178         void AddCount( U32 _numLines, U32 _numSegments, U32 _numRGBA )                                          
00179                                                                                                         {       
00180                                                                                                                 GLASSERT( _numRGBA >= _numSegments );
00181 //                                                                                                              GLASSERT( _numSegments >= _numLines );
00182                                                                                                                 numLine         += _numLines;
00183                                                                                                                 numSegment      += _numSegments;
00184                                                                                                                 numRGBA         += _numRGBA;
00185                                                                                                                 GLASSERT( numRGBA >= numSegment );
00186 //                                                                                                              GLASSERT( numSegment >= numLine );
00187                                                                                                          }
00188         void KrEncoder::Save();
00189 
00190         KrResourceVault* GetVault()                                             { return &vault; }
00191 
00192   private:
00193         static ImageLoaderFunc ImageLoader;
00194 
00195 //      void InitOutput( SDL_Surface* screen );
00196 
00197         enum
00198         {
00199                 TYPE_NONE, 
00200                 TYPE_SPRITE, 
00201                 TYPE_TILE, 
00202                 TYPE_FONT
00203         };
00204 
00205         enum
00206         {
00207                 SUBTYPE_FIXEDFONT,              // The default value.
00208                 SUBTYPE_SFONT,
00209         };
00210 
00211         struct AllInfo
00212         {
00213                 AllInfo() :     type( 0 ),
00214                                         subType( 0 ),
00215                                         name( "NONE" ), action( "NONE" ), 
00216                                         useEntireImage( false ),
00217                                         frameCount( 0 ),
00218                                         x( 0 ), y( 0 ), 
00219                                         width( 0 ), height( 0 ), 
00220                                         //hasHotspot( false ),
00221                                         hotx( 0 ), hoty( 0 ),
00222                                         //hasDelta( false ),
00223                                         deltax( 0 ), deltay( 0 ),
00224                                         isoTargetWidth( 0 ),
00225                                         fontStart( 0 ),
00226                                         fontLength( 0 ),
00227                                         space( 0 )                      { keyColor.Set( 0, 0, 0, 0 ); }
00228 
00229                 int                     type;                           // SPRITE, TILE, ...
00230                 int                     subType;
00231                 //std::string filename;                 // The filename that goes with this.
00232                 std::string name;                               // Sprite, Tile, Font, etc.
00233                 std::string action;
00234                 //int                   frame;                  // Automatically calculated
00235                 bool            useEntireImage;         // If true, width, height, x, y don't matter
00236                 int                     frameCount;                     // Limit for # frames to be read by color key
00237                 int                     x, y;
00238                 int                     width, height;          // Width and Height set for tiles
00239                 int                     hotx, hoty;
00240                 int                     deltax, deltay;
00241                 int                     isoTargetWidth;
00242                 int                     fontStart;                      // Glyph number that starts the font
00243                 int                     fontLength;
00244                 int                     space;                          // Space glyph number for a font
00245                 KrRGBA          keyColor;                       // Color key color, if used.
00246                 GlDynArray< int > rotation;             // All the rotation angles specified.
00247         };
00248         
00249         void CalcAllInfo( TiXmlNode* node, AllInfo* info, SDL_Surface* );
00250 
00251         void CreateIsoTile(     KrPaintInfo* info, 
00252                                                 KrConsole* console,
00253                                                 int x, int y,
00254                                                 int width, int height,
00255                                                 KrRle* rle,
00256                                                 int isoIdealWidth,
00257                                                 int rotation );
00258 
00259         void IsoToSource( GlFixed x, GlFixed y, GlFixed isoWidth, 
00260                                           GlFixed sourceW, GlFixed  sourceH,
00261                                           GlFixed* sourceX, GlFixed* sourceY,
00262                                           int rotation, GlFixed increment );
00263 
00264 
00265         /*      Process a particular frame to the stream.
00266                 @param frame    input: node that describes the frame
00267                 @param surface  input: the surface where the data is 
00268                 @param rle              output: where to write the frame data
00269         */
00270 //      bool ProcessFrame(      const AllInfo& allInfo,
00271 //                                              SDL_Surface* surface, 
00272 //                                              KrRle* rle );
00273 
00274         // Font encoding
00275         bool EncodeSFontFrames( SDL_Surface* surface, const AllInfo& allInfo, KrConsole* console );
00276         bool EncodeFixedFontFrames( SDL_Surface* surface, const AllInfo& allInfo, KrConsole* console );
00277 
00278         // Image encoding
00279         bool EncodeSprite( SDL_Surface* surface, const AllInfo& allInfo, KrConsole* console );
00280         bool EncodeFont( SDL_Surface* surface, const AllInfo& allInfo, KrConsole* console );
00281         bool EncodeTile( SDL_Surface* surface, const AllInfo& allInfo, KrConsole* console );
00282 
00283         // Direct encoding
00284         bool EncodeColorKey( SDL_Surface* surface, const AllInfo& allInfo, KrConsole* console );
00285 //      bool EncodeImage( SDL_Surface* surface, TiXmlElement* e, KrConsole* console );
00286 
00287         bool EncodeBinary( TiXmlElement* e, KrConsole* console );
00288         bool EncodeText( TiXmlElement* e, KrConsole* console );
00289 
00290         // Printing
00291         void PrintSprite( KrConsole*, const std::string& spriteName, const std::string& actionName,
00292                                                                   int frameNum, KrRle* rle );
00293         void PrintTile(   KrConsole*, const std::string& tileName, KrTileResource* tile );
00294 
00295         SDL_Surface* LoadSurface( TiXmlElement* definition, std::string* error );
00296 
00297         U32 tagpos;
00298 
00299         // For storing the 'constant' information used to write the
00300         // header file.
00301         enum
00302         {
00303                 SPRITE,
00304                 TILE,
00305                 ACTION,
00306                 NUM_NAME_BINS,
00307         };
00308 
00309         enum 
00310         {
00311                 DEFINITION,
00312                 DIRECT
00313         };
00314 
00315         struct Scan
00316         {
00317                 Scan()  { Init(); }
00318                 void Init()             { x = 0; y = 0; }
00319 
00320                 int x;
00321                 int y;
00322         };
00323 
00324         Scan scan;
00325 
00326         int mode;               // definiton or direct
00327 
00328         // Output information.
00329         int thisLine;
00330         int nextLine;
00331         int outputX;
00332         SDL_Surface* output;
00333 
00334         // Count for information
00335         U32 numRGBA, numLine, numSegment;
00336         U32 numRlePos;  // position to write count information.
00337 
00338         KrResourceVault vault;
00339 
00340         KrCachedWrite cachedWrite;
00341         SDL_RWops* stream;
00342 };
00343 
00344 #endif
00345 

Generated on Mon Sep 15 12:01:10 2003 for Kyra by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001