Main Page | Namespace List | Class List | File List | Class Members | File Members

video_sprites.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   NGT (Neopontec Gaming Toolkit                                         *
00003  *   Distributed under the terms of the GNU LGPL License                   *
00004  *   Copyright (C) 2005 by Hector Blanco de Frutos                         *
00005  *   hectorblanco@neopontec.com                                            *
00006  *   Refer to the LICENSE file to read the full license agreement          *
00007  ***************************************************************************/
00008  
00009  /**************************************************************************
00010   * This code part was inspired by the SDL GFX tutorials at 
00011   * http://cone3d.gamedev.net , made by Marius Andra
00012   **************************************************************************/
00013   
00014 #ifndef NGT_ENGINE_H
00015         #include "../ngt.h"
00016 #endif
00017  
00018 int NGT_SpriteBase::init(char *dir)
00019 {
00020   char buffer[255];
00021   char filename[255];
00022   char name[255];
00023   int pause=0, r=0, g=0, b=0;
00024   FILE *fp;
00025 
00026   sprintf(filename, "%s/info", dir);
00027 
00028   if((fp=fopen(filename, "r")) == NULL)
00029   {
00030     printf("ERROR opening file %s\n\n", filename);
00031     return -1;
00032   }
00033 
00034   fgets(buffer, 255, fp);
00035   sscanf(buffer, "FILES: %d", &mNumframes);
00036   mAnim = new NGT_SpriteFrame[mNumframes];
00037 
00038   mBuilt = 1;
00039   int count = 0;
00040   printf("*NGT_SpriteBase: Load sprite '%s' - frames: %i:\n", filename, mNumframes);
00041   while(!feof(fp) && count<mNumframes)
00042   {
00043     fgets(buffer, 255, fp);
00044     if(buffer[0] != '#' && buffer[0] != '\r' && buffer[0] != '\0' && buffer[0] != '\n' && strlen(buffer) != 0)
00045     {
00046       sscanf(buffer, "%s %d %d %d %d", name, &pause, &r, &g, &b);
00047       sprintf(filename, "%s/%s", dir, name);
00048       SDL_Surface *temp;
00049         if((temp = NGT_SurfaceLoad(filename)) == NULL){
00050                 printf("\tframe %i: ERROR loading frame '%s'\n", count, filename);
00051                 return -1;
00052         }else{
00053                 // Print the number and name of the frame
00054                         printf("\tframe %i: %s\n",count,filename);
00055                         fflush(0);
00056         }
00057       
00058       if(r >= 0) SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, r, g, b));
00059       mAnim[count].image = SDL_DisplayFormat(temp);
00060       SDL_FreeSurface(temp);
00061 
00062       mAnim[count].pause = pause;
00063       if(!mW) mW = mAnim[count].image->w; if(!mH) mH = mAnim[count].image->w;
00064 
00065       count++;
00066     }
00067   }
00068   printf("*NGT_SpriteBase: Sprite frames loaded: %i\n",mNumframes);
00069   fclose(fp);
00070   return 0;
00071 }
00072 
00073 
00074 /*****************************************************************************
00075 * The NGT_Sprite class methods
00076 *****************************************************************************/
00077 
00078 int NGT_Sprite::init(NGT_SpriteBase *base, SDL_Surface *screen)
00079 {
00080   mSpriteBase = base;
00081   if(mSpriteBase->mBuilt)
00082   {
00083     if(mSpriteBase->mNumframes>1) mAnimating=1;
00084     mBackreplacement = SDL_DisplayFormat(mSpriteBase->mAnim[0].image);
00085   }
00086   mScreen = screen;
00087   return 0;
00088 }
00089 
00090 void NGT_Sprite::clearBG()
00091 {
00092   if(mDrawn==1)
00093   {
00094     SDL_Rect dest;
00095     dest.x = mOldX;
00096     dest.y = mOldY;
00097     dest.w = mSpriteBase->mW;
00098     dest.h = mSpriteBase->mH;
00099     SDL_BlitSurface(mBackreplacement, NULL, mScreen, &dest);
00100   }
00101 }
00102 
00103 void NGT_Sprite::updateBG()
00104 {
00105   SDL_Rect srcrect;
00106   srcrect.w = mSpriteBase->mW;
00107   srcrect.h = mSpriteBase->mH;
00108   srcrect.x = mX;
00109   srcrect.y = mY;
00110   mOldX=mX;mOldY=mY;
00111   SDL_BlitSurface(mScreen, &srcrect, mBackreplacement, NULL);
00112 }
00113 
00114 void NGT_Sprite::draw()
00115 {
00116   if(mAnimating == 1)
00117   {
00118     if(mLastupdate+mSpriteBase->mAnim[mFrame].pause*mSpeed<SDL_GetTicks())
00119     {
00120       mFrame++;
00121       if(mFrame>mSpriteBase->mNumframes-1) mFrame=0;
00122       mLastupdate = SDL_GetTicks();
00123     }
00124   }
00125 
00126   if(mDrawn==0) mDrawn=1;
00127 
00128   SDL_Rect dest;
00129   dest.x = mX; dest.y = mY;
00130   SDL_BlitSurface(mSpriteBase->mAnim[mFrame].image, NULL, mScreen, &dest);
00131   
00132 }
00133 
00134 short int Sprite_Collide(NGT_Sprite &object1, NGT_Sprite &object2)
00135 {
00136   // We store the coordinates of our reduced rectangles here
00137   double left1, left2;
00138   double right1, right2;
00139   double top1, top2;
00140   double bottom1, bottom2;
00141 
00142 
00143   // What we do now is store the coordinates of our
00144   // shrunken rectangle inside some variables.
00145   // We make 'left' and 'top' equal the position B on our sprite
00146   // (look at the "drawing") and 'right' and 'bottom' equal the
00147   // position C. (Remember, left1, top1, etc contain the coordinates
00148   // of the first sprite's reduced rectangle and left2, top2 contain
00149   // the coordinated of the second sprite's reduced rectangle)
00150   left1 =   object1.getx()+object1.getw()*0.1;
00151   left2 =   object2.getx()+object2.getw()*0.1;
00152   top1 =    object1.gety()+object1.geth()*0.1;
00153   top2 =    object2.gety()+object2.geth()*0.1;
00154 
00155   right1 =  object1.getx()+object1.getw()*0.9;
00156   right2 =  object2.getx()+object2.getw()*0.9;
00157   bottom1 = object1.gety()+object1.geth()*0.9;
00158   bottom2 = object2.gety()+object2.geth()*0.9;
00159 
00160   // We now do some little comparing and return 0 if the rectangles
00161   // aren't colliding and 1 if they are.
00162   if (bottom1 < top2) return 0;
00163   if (top1 > bottom2) return 0;
00164   if (right1 < left2) return 0;
00165   if (left1 > right2) return 0;
00166 
00167   return 1;
00168 };
00169 
00170 

Generated on Sat Feb 4 10:15:16 2006 for Neopntec Gaming Toolkit - API reference by  doxygen 1.4.4