Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

sprite.cpp

Go to the documentation of this file.
00001 #include <assert.h>
00002 #include <iostream>
00003 #include "graphics.h"
00004 #include "sprite.h"
00005 #include "util.h"
00006 
00007 SpriteCache Sprite::cache;
00008 
00010 // Constructors, Destructors
00011 //
00012         
00013 // Creates a sprite with no frames
00014 Sprite::Sprite(string sprname)
00015 {
00016         SetName(sprname);
00017         init();
00018 }
00019 
00020         
00021 // Creates a sprite from filename named sprname
00022 Sprite::Sprite(string filename, string sprname)
00023 {
00024         LoadFrame(filename);
00025         SetName(sprname);
00026 
00027         init();
00028 }
00029 
00030         
00031 // Creates a sprite using filenames
00032 Sprite::Sprite(vector<string> &filenames, string sprname)
00033 {
00034         LoadFrames(filenames);
00035         SetName(sprname);
00036 
00037         init();
00038 }
00039 
00040         
00041 // Creates a sprite from an already allocated surface
00042 Sprite::Sprite(Surface *frame, string sprname)
00043 {
00044         LoadFrame(frame);
00045         SetName(sprname);
00046 
00047         init();
00048 }
00049 
00050         
00051 // Creates a sprite from a series of surfaces
00052 Sprite::Sprite(vector<Surface *> &sframes, string sprname)
00053 {
00054         LoadFrames(sframes);
00055         SetName(sprname);
00056 
00057         init();
00058 }
00059 
00060 
00061 // Copy Constructor
00062 Sprite::Sprite(const Sprite &tmp)
00063 {
00064         // do copy
00065 }
00066 
00067         
00068 // Virtual delete
00069 Sprite::~Sprite()
00070 {
00071         close();
00072 }
00073 
00074 void Sprite::init()
00075 {
00076         if(frames.size() > 0)
00077                 SetFrame(0);
00078         else
00079                 SetFrame(-1);
00080 
00081         
00082         EnableAnimation(false);
00083         
00084         SetPosition();
00085 
00086         Show(true);
00087 
00088         free_frames_on_exit = false;
00089 }
00090 
00091 void Sprite::close()
00092 {
00093         if(free_frames_on_exit)
00094                 EmptyFrames();
00095         else
00096                 frames.erase(frames.begin(), frames.end());
00097 }
00098         
00100 // application functions
00101 //
00102 
00103 // changes frame and other information
00104 void Sprite::animate()
00105 {
00106         int ncf, count;
00107 
00108         if (!animinfo.enabled)
00109                 return;
00110 
00111         count = Util_TimePassed(&animinfo.lastanimtime, animinfo.delay);
00112 
00113         if(!animinfo.reverse) {
00114                 ncf = current_frame + count;
00115                 if(ncf >= animinfo.end_frame)
00116                         ncf = animinfo.start_frame + (ncf % (animinfo.end_frame - animinfo.start_frame));
00117         } else {
00118                 ncf = current_frame - count;
00119                 if(ncf < animinfo.start_frame)
00120                         ncf = animinfo.end_frame - (animinfo.start_frame - ncf);
00121         }
00122         
00123         SetFrame(ncf);
00124 }
00125 
00126         
00127 // knows how to draw to a graphics context
00128 Rect Sprite::draw(Graphics &GD)
00129 {
00130         int rx = (int)(GetX() + 0.5f);
00131         int ry = (int)(GetY() + 0.5f);
00132 
00133         if(!visible || (current_frame < 0))
00134                 return Rect();
00135 
00136         assert((unsigned)current_frame < frames.size());
00137 
00138         GD.blit(frames[current_frame], rx, ry);
00139 
00140         Rect r; 
00141                 r.x = rx; 
00142                 r.y = ry; 
00143                 r.w = w; 
00144                 r.h = h;
00145         return r;
00146 }
00147         
00148 // Knows how to draw to a surface
00149 Rect Sprite::draw(Surface *surf)
00150 {
00151         int rx = (int)(GetX() + 0.5f);
00152         int ry = (int)(GetY() + 0.5f);
00153 
00154         if(!visible || (current_frame < 0))
00155                 return Rect();
00156 
00157         assert(surf != NULL);
00158         assert(current_frame < frames.size());
00159         
00160         Graphics::blit(surf, rx, ry, frames[current_frame]);
00161 
00162         Rect r; 
00163                 r.x = rx; 
00164                 r.y = ry; 
00165                 r.w = w; 
00166                 r.h = h;
00167         return r;
00168 }
00169 
00171 // Utility functions
00172 //
00173 
00174 // Enable/Disable reverse animation
00175 void Sprite::ReverseAnimation(bool tog)
00176 {
00177         animinfo.reverse = tog;
00178 }
00179 
00180         
00181 // Enable/Disable animation
00182 void Sprite::EnableAnimation(bool tog)
00183 {
00184         if(tog) {
00185                 animinfo.enabled = true;
00186                 animinfo.lastanimtime = Util_GetTime();
00187         } else {
00188                 animinfo.enabled = false;
00189         }
00190 }
00191 
00192         
00193 // Toggles animation
00194 void Sprite::ToggleAnimation()
00195 {
00196         EnableAnimation(!animinfo.enabled);
00197 }
00198 
00199         
00200 // Sets time per frame of animation
00201 void Sprite::SetAnimateTime(int ms)
00202 {
00203         animinfo.delay = ms;
00204 }
00205 
00206 
00207 // Sets the start frame for the animation
00208 void Sprite::SetAnimationStartFrame(int f)
00209 {
00210         animinfo.start_frame = f;
00211 }
00212 
00213         
00214 // Sets th stop frame for the animation (should be 1 + the last)
00215 void Sprite::SetAnimationEndFrame(int f)
00216 {
00217         animinfo.end_frame = f;
00218 }
00219 
00220 void Sprite::Show(bool tog)
00221 {
00222         visible = tog;
00223 }
00224 
00225 // Sets the frame to draw, when not in animation mode
00226 void Sprite::SetFrame(int f)
00227 {
00228         current_frame = f;
00229 
00230         if((current_frame >= 0) && (current_frame < frames.size())) {
00231                 w = frames[current_frame]->w;
00232                 h = frames[current_frame]->h;
00233         }
00234 }
00235         
00236 int Sprite::SetPosition(double sx, double sy)
00237 {
00238         x = sx;
00239         y = sy;
00240         return 1;
00241 }
00242                 
00243 // Returns current frame drawn
00244 int Sprite::GetFrame() const
00245 {
00246         return current_frame;
00247 }
00248 
00249         
00250 // Returns the amount of frames for this sprite
00251 int Sprite::CountFrames()
00252 {
00253         return (int)frames.size();
00254 }
00255 
00256         
00257 // Return the name of the sprite
00258 string Sprite::GetName() const
00259 {
00260         return name;
00261 }
00262 
00263         
00264 // Set the name
00265 void Sprite::SetName(string n)
00266 {
00267         name = n;
00268 }
00269         
00270 
00272 // Operators
00273 //
00274 
00275 // Increases drawn frame, will loop frames
00276 Sprite& Sprite::operator++()
00277 {
00278         int n = (GetFrame() + 1) % CountFrames();
00279         SetFrame(n);
00280         return *this;
00281 }
00282 
00283 Sprite Sprite::operator++(int)
00284 {
00285         Sprite t(*this);
00286         SetFrame((GetFrame() + 1) % CountFrames());
00287         return t;
00288 }
00289 
00290         
00291 // Decreases drawn frame, will loop
00292 Sprite& Sprite::operator--()
00293 {
00294         int n = (GetFrame() - 1);
00295         if(n < 0) 
00296                 n = CountFrames() - 1;
00297 
00298         SetFrame(n);
00299         return *this;
00300 }
00301 
00302 Sprite Sprite::operator--(int)
00303 {
00304         Sprite t(*this);
00305         int n = (GetFrame() - 1);
00306         if(n < 0)
00307                 n = CountFrames() - 1;
00308 
00309         SetFrame(n);
00310         return t;
00311 }
00312 
00313         
00314 // Sets the frame to draw (calls SetFrame)
00315 Sprite& Sprite::operator=(int val)
00316 {
00317         SetFrame(val);
00318         return *this;
00319 }
00320 
00322 // More Utilities (Protected)
00323 //
00324 
00325 // Loads frames in different manners
00326 int Sprite::LoadFrames(vector<Surface *>& sframes)
00327 {
00328         unsigned int i;
00329 
00330         EmptyFrames();
00331 
00332         for(i = 0; i < sframes.size(); i++)
00333                 if(!AddFrame(sframes[i]))
00334                         return i;
00335         return i;
00336 }
00337 
00338 int Sprite::LoadFrames(vector<string> &filenames)
00339 {
00340         unsigned int i;
00341 
00342         EmptyFrames();
00343 
00344         for(i = 0; i < filenames.size(); i++)
00345                 if(!AddFrame(filenames[i]))
00346                         return i;
00347         return i;
00348 }
00349 
00350 int Sprite::LoadFrame(Surface *frame)
00351 {
00352         EmptyFrames();
00353         return AddFrame(frame); 
00354 }
00355 
00356 int Sprite::LoadFrame(string filename)
00357 {
00358         EmptyFrames();
00359         return AddFrame(filename);
00360 }
00361 
00362 int Sprite::AddFrame(Surface *frame)
00363 {
00364         //Surface *n = Graphics::CreateSurfaceFrom(frame);
00365         //if(n) {
00366         //      frames.push_back(n);
00367         //      return 1;
00368         //} else { 
00369         //      cout << SDL_GetError() << endl; 
00370         //}
00371         frames.push_back(frame);
00372         
00373         return 1;
00374 }
00375 
00376 int Sprite::AddFrame(string filename)
00377 {
00378         Surface *n = Sprite::cache.Find(filename); 
00379         if(n) {
00380                 frames.push_back(n);
00381                 return 1;
00382         }
00383         return 0;
00384 }
00385 
00386         
00387 // Removes the frame at position
00388 bool Sprite::RemoveFrame(int val)
00389 {
00390         vector<Surface *>::iterator it = frames.begin() + val;
00391         if(it >= frames.end())
00392                 return false;
00393 
00394         Graphics::freeImage(*it);
00395 
00396         frames.erase(it);
00397         return true;
00398 }
00399 
00400         
00401 // Frees all frames
00402 bool Sprite::EmptyFrames()
00403 {
00404         // note to myself, test this. CM
00405         for(vector<Surface *>::iterator it = frames.begin();
00406             it != frames.end();) {
00407                 if(free_frames_on_exit)
00408                         Graphics::freeImage(*it);
00409                 frames.erase(it);
00410         }
00411         return true;
00412 }

Generated on Sun Dec 8 12:02:20 2002 for nnc by doxygen1.3-rc1