Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

sprite.h

Go to the documentation of this file.
00001 /***************************************************************************
00002                sprite.h  -  Sprite types, and class definition
00003                          -------------------
00004     begin                : Wed Mar 5 2003
00005     copyright            : (C) 2003 by Tyler Montbriand
00006     email                : tsm@accesscomm.ca
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #ifndef SPRITE_H
00019 #define SPRITE_H
00020 #include "RotateImage.h"
00021 #include "Fxp.h"
00022 #include "rect.h"
00023 #include "bitmask.h"
00024 
00029 #include <SDL/begin_code.h>
00030 
00061 
00067 typedef Uint16  SpriteFlag;
00068 
00070 typedef Uint8  ColType;
00071 
00073 #define COL_LAYER(x)  (0xff&(1<<x))
00074 
00075 
00081 #define SPRITE_NONE       0x0000  
00082 
00090 #define SPRITE_WAIT       0x0001
00091 
00092 #define SPRITE_MOVED      0x0002
00093 
00094 #define SPRITE_EXPIRED    0x0004
00095 
00096 #define SPRITE_DELETEABLE 0x0008
00097 
00098 #define SPRITE_OWNSURFACE 0x0010
00099 
00100 #define SPRITE_OWNBITMASK 0x0020
00101 
00102 
00116 class DECLSPEC Sprite
00117 {
00119   friend class SpriteList;
00121   friend class SpriteManager;
00122   
00123 public:
00124 
00126   Sprite();
00127 
00138   virtual void Update(Sint32 dtick);
00140   virtual void OnCollide(const Sprite *spr, ColType index);
00142   virtual int GetFrame();
00144   virtual Sprite *Clone();
00146   virtual void Event(int code, void *data1=NULL, void *data2=NULL);
00148 
00154   inline SfxpCoord Pos()  const {return pos;  }
00156   inline SfxpCoord Vel()  const {return vel;  }
00158   inline void ReverseXVelocity()
00159   { vel.SetFxp(  -vel.Xfxp(), vel.Yfxp()  );  }
00161   inline void ReverseYVelocity()
00162   { vel.SetFxp(   vel.Xfxp(),-vel.Yfxp()  );  }
00164   inline void Preserve()
00165   { prevpos=pos;  }
00167   inline void MoveTo(Sfxp x, Sfxp y)
00168   {  pos.SetFxp(x,y);  }
00170   inline void SetVel(SfxpCoord velin)
00171   {  vel=velin;  }
00173   Rect PrevRect() const
00174   {  return(Rect(prevpos,srcrect.w,srcrect.h)); }
00176   Rect CurRect() const
00177   {  return(Rect(pos, srcrect.w,srcrect.h));    }
00179   inline Sfxp VXfxp() const { return(vel.Xfxp()); }
00181   inline Sfxp VYfxp() const { return(vel.Yfxp()); }
00183   inline Sfxp Xfxp()  const { return pos.Xfxp(); };
00185   inline Sfxp Yfxp()  const { return pos.Yfxp(); }  
00187 
00193   inline void Remove()
00194   {  SetFlags(SPRITE_EXPIRED);  }
00196   inline void Tag()
00197   {  SetFlags(SPRITE_MOVED);  }
00199   inline int OwnSurface() const
00200   {  return(flag&SPRITE_OWNSURFACE);  }
00202   inline int OwnBitmask() const
00203   {  return(flag&SPRITE_OWNBITMASK);  }
00205   inline SpriteFlag SetFlags(const SpriteFlag flags)
00206   {
00207     flag|=flags;
00208     return(flag);
00209   }
00211   inline int Deleteable() const
00212   {  return(flag&SPRITE_DELETEABLE);  }
00214   inline int New() const
00215   {  return((flag&SPRITE_WAIT)&&!(flag&SPRITE_EXPIRED));  }
00217   inline int Moved() const
00218   {  return(flag&(SPRITE_MOVED|SPRITE_EXPIRED));  }
00220   inline void Expire()
00221   { SetFlags(SPRITE_EXPIRED); }
00223   inline int Expired() const
00224   {  return(flag&SPRITE_EXPIRED);  }
00226   inline const SpriteFlag ClearFlags(const SpriteFlag flags)
00227   {
00228     flag&=~flags;
00229     return(flag);
00230   }
00232   inline const SpriteFlag GetFlags() const
00233   { return(flag);  }  
00235 
00240 
00242   inline bool CanCollide(Sprite *spr)
00243   {
00244     if(spr==NULL) return false;
00245     Rect r1=CurRect(),r2=spr->CurRect();
00246 
00247     if((clayer&spr->cindex)&&(cindex&spr->clayer))
00248     if(r1.Overlaps(r2))
00249     {
00250       bitmask *b1=GetBitmask(),*b2=spr->GetBitmask();
00251 
00252       if((b1!=NULL)&&(b2!=NULL))
00253       {
00254         int dx=r2.x-r1.x,dy=r2.y-r1.y;
00255         if(bitmask_overlap(b1,b2,dx,dy))/* Bitmask collision */
00256           return(true);
00257         else
00258           return(false);
00259       }
00260       else  /* Bounding box collision only */
00261         return(true);
00262     }
00263 
00264     return false;
00265   }    
00266 
00268   bitmask *GetBitmask();
00270   inline Uint8 const GetColIndex()  { return(cindex); }
00272   inline ColType const GetColLayer(){ return(clayer); }
00273   
00275 
00281   inline void SetFrameRate(Sfxp frate)
00282   { framerate=frate;  }
00284   inline Sint16 DrawLayer() const
00285   { return(drawlayer);  }
00287   inline void CalcFrame()
00288   {
00289     int f=GetFrame();
00290     int xf=f%frames.x;
00291     int yf=(f/frames.x)%frames.y;
00292 
00293     srcrect.x=xf*frames.w;
00294     srcrect.y=yf*frames.h;
00295     srcrect.w=frames.w;
00296     srcrect.h=frames.h;
00297   }
00299 
00300 
00307   void Draw(SDL_Surface *screen);
00309   void Draw(SDL_Surface *screen, Rect portion);
00311   void Init_Surface(SDL_Surface *surface=NULL, bool os_in=false);
00313   void Init_Layers(Sint16 layer=0);
00315   void Init_List(Sprite *previn=NULL, Sprite *nextin=NULL);
00317   void Init_Flags(SpriteFlag flags=0);
00319   void Init_Frames(int xframes=-1, int yframes=-1);
00321   void Init_Collision(ColType c_index=0, ColType c_layer=0);
00323   void Init_Bitmask(bool enable=false, bitmask **bmask_in=NULL);
00325 
00327   virtual ~Sprite();
00328   
00329 protected:
00338   bitmask **bmask;
00340   ColType clayer;
00342   ColType cindex;  
00344 
00351   Sfxp frame;
00353   Sfxp framerate;
00355   Rect   frames;
00357   SDL_Surface *surface;
00359   Rect srcrect;
00361   Sint16 drawlayer;  
00363 
00370   SfxpCoord pos;
00372   SfxpCoord prevpos;
00374   SfxpCoord vel;
00376 
00382   Sprite *prev,*next;
00384 
00386   SpriteFlag flag;
00387 };
00388 
00394 DECLSPEC SDL_Surface * SDLCALL LoadConvertBMP(const char *, Uint8 tr=255, Uint8 tg=0,
00395                             Uint8 tb=255                            );
00397 DECLSPEC bitmask * SDLCALL MakeBitMask(SDL_Surface *surf, Rect rect);
00399 
00402 #include <SDL/close_code.h>
00403 
00404 #endif/*SPRITE_H*/
00405 
00406 

Generated on Sat Oct 11 13:19:25 2003 for Spritelib by doxygen 1.3.4