Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

CSTile.h

Go to the documentation of this file.
00001 #ifndef CSTILE_H
00002 #define CSTILE_H
00003 
00004 // USES SDL_Surface
00005 
00006 #include <string>
00007 #include <vector>
00008 #include "CSTypes.h"
00009 #include "SDLMain.h"
00010 #include "CSAnimation.h"
00011 #include "CSLoadable.h"
00012 #include "CSLog.h"
00013 
00014 class CSTileLoader;
00015 class CSTile;
00016 typedef std::vector<CSTile *> CSTiles;
00017 
00018 class CSTileData
00019 {
00020     public:
00021         char *id;                   //!< defining 
00022         unsigned char idChar;       //!< defining 
00023         char *animationName;        //!< animations
00024         int type;
00025         int subtype;
00026 
00027         CSTileData()
00028         {
00029             id = 0;
00030             animationName = 0;
00031             type = 0;
00032             subtype = 0;
00033         }
00034 
00035         ~CSTileData()
00036         {
00037             if (id != 0)
00038             {
00039                 free (id);
00040                 id = 0;
00041             }
00042             if (animationName != 0)
00043             {
00044                 free (animationName);
00045                 animationName = 0;
00046             }   
00047         }
00048 };
00049 
00050 /**
00051     This file defines CSTile:
00052 <PRE>
00053       CSTile can only be created thru a loader (CSTileLoader).
00054       CSTile are defined by (now ONLY thru) an ini-File, like:
00055 
00056             ID                          "PAC_LEFT_ACTION"
00057             IS_REPEATABLE               ACTION_INVERSE_ONCE
00058             PHASES                      0
00059             ANIMATION                   "animation\\pac_left.ani"
00060       
00061       ID           - supposed a unique identifier
00062       REAPEATABLE  - one of:
00063                      ACTION_NOT_REPEATABLE      0
00064                      ACTION_REPEATABLE          1
00065                      ACTION_INVERSE_ONCE        2
00066                      ACTION_INVERSE_REPEATABLE  3
00067                      ACTION_RANDOM              4
00068 
00069       PHASES       - many phases, each phase is represented by a number
00070                      the number corresponse to the ANIMATION that is
00071                      loaded with the ini - file (numbered from top to
00072                      bottom.
00073                      NOTE:
00074                         REAPEATABLE         ACTION_INVERSE_ONCE
00075                         PHASES              0
00076                         PHASES              1
00077                         PHASES              2
00078                         PHASES              3
00079                         PHASES              4
00080                 
00081                      gives the same result as:
00082                         REAPEATABLE         ACTION_NOT_REPEATABLE
00083                         PHASES              0
00084                         PHASES              1
00085                         PHASES              2
00086                         PHASES              3
00087                         PHASES              4
00088                         PHASES              3
00089                         PHASES              2
00090                         PHASES              1
00091                         PHASES              0
00092 
00093       ANIMATION    - many animations are possible!
00094                      describes the animation to be loaded,
00095                      parameter gives the ini-file for a CSAnimation
00096 
00097 
00098       Only two methods are really needed:
00099             virtual bool next() // if finished and not repeat -> false, true otherwise
00100 </PRE>
00101     */
00102 
00103 class CSTile
00104 {
00105     friend CSTileLoader;
00106     private:
00107         char *mId;                          //!< defining 
00108         int mType;
00109         int mSubtype;
00110         unsigned char mIdChar;              //!< defining 
00111         CSAnimation *mAnimation;
00112         AnimationState mAnimationState;
00113         bool mChanged;
00114         bool mIsSolid;
00115         //! constructor using "*.act" file
00116         CSTile(const std::string &filename);
00117         static void loadTileData(const std::string &filename, CSTileData &data);
00118         unsigned int mMapPosX;
00119         unsigned int mMapPosY;
00120         void initialize(const CSTileData &data);
00121         void initialize();
00122 
00123     public:
00124         CSTile(const CSTile &tile);
00125         unsigned char getIDChar() {return mIdChar;}
00126         virtual ~CSTile();
00127 
00128         static const char *CLASS;
00129         virtual std::string getType() {return (std::string) CLASS;}
00130 
00131         unsigned int getX() {return mAnimationState.mXPos;}
00132         unsigned int getY() {return mAnimationState.mYPos;}
00133         void setX(unsigned int x) {mAnimationState.mXPos = x;}
00134         void setY(unsigned int y) {mAnimationState.mYPos = y;}
00135 
00136         unsigned int getWitdh() {return mAnimation->getMaxX();}
00137         unsigned int getHeight() {return mAnimation->getMaxY();}
00138         SDL_Surface *getScaledTile(double factor)
00139         {
00140             return mAnimation->getScaledAnimation(mAnimationState, factor);
00141         }
00142 
00143         void setMapPosition(unsigned int x, unsigned int y){mMapPosX = x;mMapPosY = y;}
00144         unsigned int getMapPositionX(){return mMapPosX;} //!< in pixel of World
00145         unsigned int getMapPositionY(){return mMapPosY;} //!< in pixel of World
00146         int getTileType(){return mType;}
00147         int getSubtype(){return mSubtype;}
00148         void setChanged(bool b){mChanged = b;}
00149         bool isChanged(){return mChanged;}
00150         virtual bool next(const CSDisplayParams &displayParams)
00151         {
00152             mAnimationState.mDisplayParams = displayParams;
00153             bool finished = false;
00154             finished = !(mAnimation->next(mAnimationState));
00155 
00156             if (finished)
00157             {
00158                 mAnimation->resetState(mAnimationState);
00159             } 
00160 
00161             return !finished;
00162         }
00163 
00164         virtual void display()
00165         {
00166             mAnimation->display(mAnimationState);
00167         }
00168 
00169         void setSolid(bool b);
00170         bool isSolid() {return mIsSolid;}
00171         void setSecPerFrame(float secPerFrame);
00172         AnimationState *getAnimationState() {return &mAnimationState;}
00173         void scale(int width, int tilesX, int height, int tilesY);
00174 };      
00175 
00176 class CSTileLoader : public Loadable<CSTile>
00177 {
00178     protected:
00179         virtual bool isSingleCreate(void) const
00180         {
00181             return false;
00182         }
00183 
00184         virtual CSTile *create(const std::string &filename)
00185         {
00186             return new CSTile(filename);
00187         }
00188 
00189     public:
00190         static CSTileLoader INSTANCE;
00191 };
00192 
00193 #endif // CSTILE_H

Generated on Wed Jul 14 00:43:43 2004 for CSLib by doxygen 1.3.6