src/graphic/sprite.h

Go to the documentation of this file.
00001 /******************************************************************************
00002  *  Wormux is a convivial mass murder game.
00003  *  Copyright (C) 2001-2004 Lawrence Azzoug.
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00018  ******************************************************************************
00019  * Sprite:     Simple sprite management
00020  ******************************************************************************
00021  * 2005/09/21: Jean-Christophe Duberga (jcduberga@gmx.de)
00022  *             Initial version
00023  *****************************************************************************/
00024 
00025 #ifndef _SPRITE_H
00026 #define _SPRITE_H
00027 
00028 #include <SDL.h>
00029 #include <vector>
00030 #include "spriteframe.h"
00031 #include "spritecache.h"
00032 #include "spriteanimation.h"
00033 #include "include/base.h"
00034 
00035 typedef enum {
00036   top_left,
00037   top_center,
00038   top_right,
00039   left_center,
00040   center,
00041   right_center,
00042   bottom_left,
00043   bottom_center,
00044   bottom_right,
00045   user_defined // Sprite::rot_hotspot is set to this value, when the hotspot is set as a Point2i
00046 } Rotation_HotSpot;
00047 
00048 class Sprite
00049 {
00050 public:
00051   SpriteCache cache;
00052   SpriteAnimation animation;
00053 
00054 public:
00055   explicit Sprite();
00056   explicit Sprite( Surface surface);
00057   Sprite(const Sprite &other);
00058 
00059   void Init(Surface& surface, const Point2i &frameSize, int nb_frames_x, int nb_frames_y);
00060   Surface GetSurface();
00061 
00062   // Frame number
00063   unsigned int GetCurrentFrame() const;
00064   void SetCurrentFrame( unsigned int frame_no);
00065   unsigned int GetFrameCount();
00066 
00067   // Size
00068   unsigned int GetWidth() const;
00069   unsigned int GetWidthMax() const; // gives height of the surface
00070                                      // (takes rotations into acount)
00071   unsigned int GetHeight() const;
00072   unsigned int GetHeightMax() const; // gives height of the surface
00073                                      // (takes rotations into acount)
00074   Point2i GetSize() const;
00075   Point2i GetSizeMax() const;
00076 
00077   void GetScaleFactors( float &scale_x, float &scale_y);
00078   void SetSize(unsigned int w, unsigned int h);
00079   void SetSize(const Point2i &size);
00080   void Scale( float scale_x, float scale_y);
00081   void ScaleSize(int width, int height);
00082   void ScaleSize(Point2i size);
00083 
00084   // Rotation
00085   void SetRotation_rad( double angle_rad);
00086   const double &GetRotation_rad();
00087   void SetRotation_HotSpot( const Point2i new_hotspot);
00088   void SetRotation_HotSpot( const Rotation_HotSpot rhs) { rot_hotspot = rhs; };
00089   const Point2i& GetRotationPoint() { return rotation_point; };
00090 
00091   SpriteFrame& operator[] (unsigned int frame_no);
00092   const SpriteFrame& operator[] (unsigned int frame_no) const;
00093   const SpriteFrame& GetCurrentFrameObject() const;
00094 
00095   // Prepare animation
00096   void AddFrame( const Surface& surf, unsigned int delay = 100);
00097   void SetFrameSpeed(unsigned int nv_fs);
00098 
00099   // Animation
00100   void Start();
00101   void Update();
00102   void Finish();
00103   bool IsFinished() const;
00104 
00105   // Alpha
00106   void SetAlpha( float alpha); // Can't be combined with per pixel alpha
00107   float GetAlpha();
00108 
00109   // Cache
00110   void EnableRotationCache(unsigned int cache_size);
00111   void EnableFlippingCache();
00112 
00113   // Show flag
00114   void Show();
00115   void Hide();
00116 
00117   // Draw
00118   void Blit(Surface &dest, uint pox_x, uint pos_y);
00119   void Blit(Surface &dest, const Point2i &pos);
00120   void Blit(Surface &dest, const Rectanglei &srcRect, const Point2i &destPos);
00121   void Blit(Surface &dest, int pox_x, int pos_y, int src_x, int src_y, uint w, uint h);
00122   void Draw(const Point2i &pos);
00123   void DrawXY(const Point2i &pos);
00124 
00125    void RefreshSurface();
00126 
00127 private:
00128    Surface current_surface;
00129    bool show;
00130    // Frames
00131    unsigned int current_frame;
00132    int frame_width_pix,frame_height_pix;
00133    std::vector<SpriteFrame> frames;
00134 
00135    // Gfx
00136    float alpha;
00137    float scale_x,scale_y;
00138    double rotation_rad;
00139    Point2i rhs_pos;
00140    Rotation_HotSpot rot_hotspot;
00141    Point2i rotation_point;
00142 
00143 private:
00144    void Constructor();
00145    void Calculate_Rotation_Offset(Surface& tmp_surface);
00146 };
00147 
00148 #endif /* _SPRITE_H */

Generated on Mon Jan 1 13:10:56 2007 for Wormux by  doxygen 1.4.7