SDL++
C++wrapperfortheSDLlibrary.

video/Surface.h

Go to the documentation of this file.
00001 
00021 #ifndef SDL_VIDEO_SURFACE_H
00022 #define SDL_VIDEO_SURFACE_H
00023 
00024 #include <string>
00025 #include <stdexcept>
00026 
00027 #include <boost/shared_ptr.hpp>
00028 
00029 #include <SDL.h>
00030 
00031 #include "sdlpp/misc/Rect.h"
00032 #include "sdlpp/misc/Color.h"
00033 
00034 namespace sdl {
00035 namespace video {
00036     using namespace std;
00037     using namespace misc;
00038 
00043     class Surface {
00044         public:
00050             Surface () : surface_ (SDL_GetVideoSurface (), &SDL_FreeSurface) {
00051                 if (surface_ == NULL)
00052                     throw runtime_error (SDL_GetError ());
00053             };
00054 
00062             Surface (SDL_Surface* surface) : surface_ (surface, &SDL_FreeSurface) {
00063                 if (surface_ == NULL)
00064                     throw runtime_error (SDL_GetError ());
00065             };
00066 
00074             Surface (const string& fileName) : surface_ (SDL_LoadBMP (fileName.c_str ()), &SDL_FreeSurface) {
00075                 if (surface_ == NULL)
00076                     throw runtime_error (SDL_GetError ());
00077             };
00078 
00089             Surface (int height, int width, int bpp, unsigned int flags)
00090               : surface_ (SDL_SetVideoMode (height, width, bpp, flags)) {
00091                 if (surface_ == NULL)
00092                     throw runtime_error (SDL_GetError ());
00093             };
00094 
00105             Surface (Uint32 flags, const Rect& rect, int bpp, const Color& mask)
00106               : surface_ (SDL_CreateRGBSurface (flags, rect.width (), rect.height (), bpp, 
00107                                                 mask.red (), mask.green (), mask.blue (), mask.alpha ()), 
00108                                                 &SDL_FreeSurface) {
00109                 if (surface_ == NULL)
00110                     throw runtime_error (SDL_GetError ());
00111             };
00112 
00124             Surface (void *pixels, const Rect& rect, int bpp, int pitch, const Color& mask)
00125               : surface_ (SDL_CreateRGBSurfaceFrom (pixels, rect.width (), rect.height (), bpp, pitch, 
00126                                                     mask.red (), mask.green (), mask.blue (), mask.alpha ()), 
00127                                                     &SDL_FreeSurface) {
00128                 if (surface_ == NULL)
00129                     throw runtime_error (SDL_GetError ());
00130             };
00131 
00135             ~Surface () {};
00136 
00142             Surface (const Surface& rhs) : surface_ (rhs.surface_) {};
00143             
00151             Surface& operator= (const Surface& rhs) {
00152                 if (this != &rhs) 
00153                     surface_ = rhs.surface_;
00154                 return *this;
00155             };
00156 
00168             Surface& blit (Surface& surface, const Rect& srcRect, const Rect& dstRect) {
00169                 SDL_Rect src;
00170                 src.w = srcRect.width ();
00171                 src.h = srcRect.height ();
00172                 src.x = srcRect.x ();
00173                 src.y = srcRect.y ();
00174                 SDL_Rect dst;
00175                 dst.w = dstRect.width ();
00176                 dst.h = dstRect.height ();
00177                 dst.x = dstRect.x ();
00178                 dst.y = dstRect.y ();
00179                 if (SDL_BlitSurface (surface.to_c (), &src, surface_.get (), &dst) == -1)
00180                     throw runtime_error (SDL_GetError ());
00181                 return *this;
00182             };
00183 
00189             int height () { return surface_->h; };
00190 
00196             int width () { return surface_->w; };
00197 
00203             int bpp () { return surface_->format->BitsPerPixel; };
00204 
00210             bool lock () { return SDL_LockSurface (surface_.get ()) == 0; };
00211 
00215             void unlock () { SDL_UnlockSurface (surface_.get ()); };
00216 
00224             bool save (const string& fileName) { return SDL_SaveBMP (surface_.get (), fileName.c_str ()) == 0; };
00225              
00231             SDL_Surface* const to_c () const { return surface_.get (); };
00232 
00233         private:
00237             boost::shared_ptr<SDL_Surface> surface_;
00238     }; //Surface
00239 }; //video
00240 }; //sdl
00241 
00242 #endif //SDL_VIDEO_SURFACE_H
00243 
 All Classes Namespaces Files Functions Variables Typedefs