C:/Documents and Settings/jegan/Desktop/projectX/Sprite.cpp

Go to the documentation of this file.
00001 #include "glfuncs.h"
00002 #include "Screen.h"
00003 #include "Sprite.h"
00004 #include "Parser.h"
00005 #include "AnimatedSprite.h"
00006 #include "ShowOneSprite.h"
00007 
00008 
00009 using namespace std;
00010 
00011 map<string,Sprite *> Sprite::SPRITES;
00012 
00013 Sprite* Sprite::instance()
00014 {
00015         return this;
00016 }
00017 
00018 Sprite::Sprite(std::string filename)
00019 :_filename(filename),_direction(LEFT)
00020 {
00021         _hTexture = glfuncs::instance()->loadTexture(_filename,_coords,_rect);
00022 
00023         _box = _rect;
00024 
00025         // By reversing the [0] and [2] texture mapping coordinates you make the texture face the other direction
00026         /*
00027         _coords[0] = 1;
00028         _coords[1] = 0;
00029         _coords[2] = 0;
00030         _coords[3] = 1;
00031         */
00032 }
00033 
00034 void Sprite::cleanSprites()
00035 {
00036         for( map<std::string,Sprite *>::iterator i = SPRITES.begin(); i != SPRITES.end(); i++)
00037                 (*i).second->clean();
00038 }
00039 
00040 Sprite::Sprite(std::string filename, int width, int height)
00041 :_filename(filename)
00042 {
00043         _hTexture = glfuncs::instance()->loadTexture(_filename,_coords,_rect);
00044         _rect.w = width;
00045         _rect.h = height;
00046 }
00047 
00048 Sprite::Sprite(Sprite *sprite)
00049 {
00050         _filename = sprite->_filename;
00051         _rect = sprite->_rect;
00052         _direction = _direction;
00053         _box = sprite->_box;
00054 }
00055 
00056 void Sprite::draw(int x, int y, bool origin)
00057 {
00058         double tempX;
00059 
00060         if(_direction == RIGHT) {
00061                 double temp = _coords[0];
00062                 _coords[0] = _coords[2];
00063                 _coords[2] = temp;
00064 
00065                 tempX = _rect.x;
00066                 _rect.x = _rect.w - (_rect.x+_box.w);
00067         }
00068         
00069         _rect.x = x - _rect.x;
00070         _rect.y = y - _rect.y;
00071 
00072         if(origin == LEVEL)
00073                 Screen::instance()->drawLevel(_hTexture,_coords,_rect);
00074         else if(origin == SCREEN)
00075                 Screen::instance()->drawScreen(_hTexture,_coords,_rect);
00076 
00077         _rect.x = x - _rect.x;
00078         _rect.y = y - _rect.y;
00079         
00080         if(_direction == RIGHT) {
00081                 double temp = _coords[0];
00082                 _coords[0] = _coords[2];
00083                 _coords[2] = temp;
00084                 _rect.x = tempX;
00085         }
00086 }
00087 
00088 void Sprite::drawBox(int x, int y, SDL_Rect drawBox)
00089 {
00090         double temp2 = _coords[2];
00091         double temp3 = _coords[3];
00092         int tempWidth = _rect.w;
00093         int tempHeight = _rect.h;
00094 
00095         _coords[2] *= drawBox.w / ((double)_rect.w);
00096         _coords[3] *= drawBox.h / ((double)_rect.h);
00097 
00098         _rect.w = drawBox.w;
00099         _rect.h = drawBox.h;
00100 
00101         draw(x,y);
00102 
00103         _rect.w = tempWidth;
00104         _rect.h = tempHeight;
00105 
00106         _coords[2] = temp2;
00107         _coords[3] = temp3;
00108 }
00109 
00110 Sprite* Sprite::loadSprite( std::string filename )
00111 {
00112         if(SPRITES.find(filename) != SPRITES.end()) {
00113                 //cerr << filename << endl;
00114                 return SPRITES[filename]->instance();
00115         }
00116 
00117         string ext = filename.substr(filename.size() - 3,3);
00118         if( ext == "png" )
00119         {
00120                 Sprite *sprite = new Sprite( filename );
00121                 SPRITES[filename] = sprite;
00122                 return sprite;
00123         }
00124         else if ( ext == "txt" )
00125         {
00126                 Sprite *sprite = Sprite::loadText( filename );
00127                 SPRITES[filename] = sprite;
00128                 return sprite;
00129         }
00130         
00131         cerr << "Invalid sprite extension" << endl;
00132         return NULL;
00133 }
00134 
00135 Sprite* Sprite::loadText( std::string filename )
00136 {
00137         List *spriteInfo = Parser::parse(filename);
00138 
00139         if(spriteInfo->firstString() == "animated") {
00140                 Sprite *newSprite = new AnimatedSprite(filename, spriteInfo->rest());
00141                 delete spriteInfo;
00142                 return newSprite;
00143         }
00144         else if(spriteInfo->firstString() == "showone") {
00145                 Sprite *newSprite = new ShowOneSprite(filename, spriteInfo->rest());
00146                 delete spriteInfo;
00147                 return newSprite;
00148         }
00149         else {
00150                 cerr << "Sprite type " << spriteInfo->firstString() << " unknown." << endl;
00151                 delete spriteInfo;
00152                 return NULL;
00153         }
00154 }

Generated on Fri May 5 00:20:19 2006 for ProjectX by  doxygen 1.4.6-NO