Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

animatedimage.cpp

Go to the documentation of this file.
00001 #include <SDL/SDL.h>
00002 #include <SDL/SDL_image.h>
00003 
00004 #include "constants.h"
00005 #include "animatedimage.h"
00006 
00007 AnimatedImage :: AnimatedImage( string file, int nFrames )
00008 {
00009         nrFrames = nFrames;
00010         currentFrame = 0;
00011 
00012         // Load image
00013         image = IMG_Load( (const char *)file.c_str() );
00014         if ( !image )
00015         {
00016                 nrFrames = 0;
00017                 image = 0;
00018                 return;
00019         }
00020 
00021         SDL_SetColorKey(image, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB( image->format, 0, 0, 0 ) );
00022         frameWidth = image->w/nrFrames;
00023         frameHeight = image->h;
00024 }
00025 
00026 AnimatedImage :: ~AnimatedImage()
00027 {
00028         if ( image != 0 )
00029                 SDL_FreeSurface( image );
00030 }
00031 
00032 bool AnimatedImage :: nextFrame()
00033 {
00034         bool rc = true;
00035         currentFrame ++;
00036         if ( currentFrame >= nrFrames )
00037         {
00038                 currentFrame --;
00039                 rc = false;
00040         }
00041 
00042         return rc;
00043 }
00044 
00045 void AnimatedImage :: draw( SDL_Surface *screen, int x, int y )
00046 {
00047         if ( !image )
00048                 return;
00049 
00050         SDL_Rect dst;
00051         dst.x = currentFrame * frameWidth;
00052         dst.y = 0;
00053         dst.w = frameWidth;
00054         dst.h = frameHeight;
00055 
00056         SDL_Rect dst2;
00057         dst2.x = x - (frameWidth/2);
00058         dst2.y = y - (frameHeight/2);;
00059         SDL_BlitSurface( image, &dst, screen, &dst2 );
00060 }
00061 
00062 bool AnimatedImage :: AtEnd()
00063 {
00064         if ( currentFrame +1 >= nrFrames || image == 0 )
00065                 return true;
00066         return false;
00067 }
00068 

Generated on Sat Nov 5 16:17:24 2005 for OPIE by  doxygen 1.4.2