00001 #ifndef H_SDLPP_GRAPHICS
00002 #define H_SDLPP_GRAPHICS
00003
00004 #include "sdlpp_common.h"
00005
00006 namespace SDLPP {
00007
00014 class Bitmap : public RefCount
00015 {
00016 public:
00019 Bitmap(const xstring& filename="");
00022 Bitmap(int width, int height, bool video_memory);
00025 Bitmap(const iVec2& size, bool video_memory);
00027 Bitmap(std::istream& is);
00029 Bitmap(ResourceFile& rf, const xstring& name);
00032 Bitmap(SDL_Surface* surface);
00033 virtual ~Bitmap();
00034 Bitmap(const Bitmap& rhs);
00035 Bitmap& operator= (const Bitmap& rhs);
00036
00041 SDL_Surface* detach();
00042
00045 Bitmap clone();
00046
00048 void save(const xstring& filename);
00049
00051 Bitmap cut(const iRect2& r);
00052
00054 void draw(int x, int y);
00056 void draw(const iVec2& p);
00060 void draw(Bitmap& target, int x, int y);
00061 void draw(Bitmap& target, const iVec2& p) { draw(target,p.x,p.y); }
00062
00064 void draw(Bitmap& target, const iRect2& src_rect, const iVec2& dest_point);
00065
00067 void fill(const iRect2& r, Uint32 color);
00068
00070 void fill(Uint32 color);
00071
00073 iVec2 get_size() const;
00074 iRect2 get_rect() const { return iRect2(iVec2(0,0),get_size()); }
00075 int get_width() const { return get_size().x; }
00076 int get_height() const { return get_size().y; }
00077 int get_bits_per_pixel() const { return m_Surface->format->BitsPerPixel; }
00078
00079 Uint32 get_colorkey() const { return m_Surface->format->colorkey; }
00080 Uint8 get_alpha() const { return m_Surface->format->alpha; }
00081 void set_pixel_32(int x, int y, Uint32 color);
00082 void set_pixel_32(const iVec2& p, Uint32 color);
00083 Uint32 get_pixel_32(int x, int y);
00084 Uint32 get_pixel_32(const iVec2& p) { return get_pixel_32(p.x,p.y); }
00085 Uint32* get_row_32(int y);
00086 const Uint32* get_row_32(int y) const;
00087
00088 void set_colorkey(Uint32 colorkey);
00089 void disable_colorkey();
00090 void set_alpha(Uint8 alpha);
00091 void disable_alpha();
00092
00093 SDL_Surface* get_surface() { return m_Surface; }
00094 bool valid() const { return (m_Surface!=0); }
00095 protected:
00096 void destroy();
00097 private:
00098 void load(const xstring& filename);
00099 SDL_Surface* m_Surface;
00100 };
00101
00102 class BitmapLoader : public Loader<Bitmap>
00103 {
00104 public:
00105 virtual void load(Bitmap&,const xstring& name, ResourceFile* rf);
00106 };
00107
00108 class BitmapCache : public Cache<Bitmap>
00109 {
00110 public:
00111 static BitmapCache* instance()
00112 {
00113 static std::auto_ptr<BitmapCache> ptr(new BitmapCache);
00114 return ptr.get();
00115 }
00116
00117 static BitmapLoader& get_loader()
00118 {
00119 static BitmapLoader sl;
00120 return sl;
00121 }
00122
00123 private:
00124 friend class std::auto_ptr<BitmapCache>;
00125 BitmapCache() : Cache<Bitmap>(get_loader()) {}
00126 ~BitmapCache() {}
00127 BitmapCache(const BitmapCache&) : Cache<Bitmap>(get_loader()) {}
00128 };
00129
00130
00131
00132 struct TTF_Font;
00133
00134 class Font
00135 {
00136 TTF_Font* m_TTF_Font;
00137 std::istream* m_FontStream;
00138 SDL_RWops* m_RWops;
00139
00140 friend class FontManager;
00141 Font(TTF_Font* font, std::istream* stream, SDL_RWops* rw) : m_TTF_Font(font), m_FontStream(stream), m_RWops(rw) {}
00142 void destroy();
00143 public:
00144
00145
00146 Font() : m_TTF_Font(0), m_FontStream(0), m_RWops(0) {}
00147 Font(const Font& rhs)
00148 : m_TTF_Font(rhs.m_TTF_Font),
00149 m_FontStream(rhs.m_FontStream),
00150 m_RWops(rhs.m_RWops)
00151 {}
00152 Font& operator= (const Font& rhs)
00153 {
00154 m_TTF_Font=rhs.m_TTF_Font;
00155 m_FontStream=rhs.m_FontStream;
00156 m_RWops=rhs.m_RWops;
00157 return *this;
00158 }
00159 virtual ~Font();
00160
00161 iVec2 get_size(const xstring& text);
00162 Bitmap get_bitmap(const xstring& text, Uint32 color);
00163 iVec2 draw(int x, int y, const xstring& text, Uint32 color, int align=0);
00164 iVec2 draw(const iVec2& pos, const xstring& text, Uint32 color, int align=0);
00165 iVec2 draw(Bitmap target, int x, int y, const xstring& text, Uint32 color, int align=0);
00166 iVec2 draw(Bitmap target, const iVec2& pos, const xstring& text, Uint32 color, int align=0);
00167 };
00168
00169 Font& get_font(const xstring& name, int point_size);
00170 Font& get_font(ResourceFile& rf, const xstring& name, int point_size);
00171
00175 class GraphicsManager : public Singleton
00176 {
00177 public:
00179 static GraphicsManager* instance(bool destroy=false)
00180 {
00181 static GraphicsManager* ptr=0;
00182 if (!ptr && !destroy) ptr=new GraphicsManager;
00183 else
00184 if (ptr && destroy) { delete ptr; ptr=0; }
00185 return ptr;
00186 }
00187
00189 virtual void shutdown() { instance(true); }
00190
00192 void initialize(int width, int height, bool full_screen)
00193 {
00194 const SDL_VideoInfo* video_info=SDL_GetVideoInfo();
00195 if (!video_info->hw_available) m_NoFlip=true;
00196 Uint32 flags=SDL_ANYFORMAT|SDL_DOUBLEBUF|SDL_SWSURFACE;
00197 if (full_screen) flags|=SDL_FULLSCREEN;
00198 SDL_Surface* screen=SDL_SetVideoMode(width,height,32,flags);
00199
00200 if (!screen) THROW("Invalid graphics format");
00201 m_Screen=Bitmap(screen);
00202 }
00203
00204 void flip()
00205 {
00206 SDL_Flip(m_Screen.get_surface());
00207 if (m_NoFlip) m_Screen.fill(0);
00208 }
00209
00210 Uint32 MapRGB(Uint8 r, Uint8 g, Uint8 b)
00211 {
00212 return SDL_MapRGB(m_Screen.get_surface()->format,r,g,b);
00213 }
00214
00215 void GetRGB(Uint32 color, Uint8& r, Uint8& g, Uint8& b)
00216 {
00217 SDL_GetRGB(color,m_Screen.get_surface()->format,&r,&g,&b);
00218 }
00219
00220 Bitmap& get_screen() { return m_Screen; }
00221 private:
00222 GraphicsManager() : m_NoFlip(false) {}
00223 ~GraphicsManager()
00224 {
00225 m_Screen.detach();
00226 }
00227 GraphicsManager(const GraphicsManager&) {}
00228
00229 Bitmap m_Screen;
00230
00231 bool m_NoFlip;
00232 };
00233
00234 inline Bitmap& get_screen() { return GraphicsManager::instance()->get_screen(); }
00235 inline Uint32 MapRGB(Uint8 r, Uint8 g, Uint8 b) { return GraphicsManager::instance()->MapRGB(r,g,b); }
00236 inline void GetRGB(Uint32 color, Uint8& r, Uint8& g, Uint8& b) { GraphicsManager::instance()->GetRGB(color,r,g,b); }
00237 inline void flip() { GraphicsManager::instance()->flip(); }
00239 Bitmap rotate(Bitmap bmp, double angle);
00243 Bitmap recolor(Bitmap bmp, Uint32 from_color, Uint32 to_color);
00244
00245 }
00246
00247
00248 #endif // H_SDLPP_GRAPHICS
00249