daily pastebin goal
28%
SHARE
TWEET

Surface.h

Archon Feb 3rd, 2011 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // (C) 2010 Tim Gurto
  2.  
  3. #ifndef SURFACE_H
  4. #define SURFACE_H
  5.  
  6. #include <string>
  7. #include "SDL.h"
  8. #include "SDL_ttf.h"
  9. #include "types.h"
  10. #include "globals.h"
  11.  
  12. class Surface;
  13. extern Surface screenBuf;
  14.  
  15. enum SpecialSurface{
  16.    SUR_UNINIT, //not yet a surface; needs to be set later with =
  17.    SUR_SCREEN, //the screen buffer
  18.    SUR_BLANK   //a blank surface
  19. };
  20.  
  21. //encapsulates graphics functionality
  22. //ideally, switching to OpenGL or DirectX should only involve changing this class.
  23. class Surface{
  24.  
  25.    //the actual surface pointer
  26.    SDL_Surface *surface_;
  27.  
  28.    //whether this surface is the screen buffer;
  29.    bool isScreen_;
  30.  
  31.    //Keeps track of allocated surfaces
  32.    static int surfacesLoaded_;
  33.  
  34.    //whether there is a screen buffer
  35.    static int screensSet_;
  36.  
  37. public:
  38.  
  39.    //file
  40.    Surface(const std::string fileName, bool alpha = false);
  41.    //file + transparent background
  42.    Surface(const std::string fileName, const SDL_Color &background,
  43.            bool alpha = false);
  44.    //special (blank, screen, or default uninitialized)
  45.    Surface(SpecialSurface special = SUR_UNINIT,
  46.            //-1 implies current screen res
  47.            int width = -1, int height = -1,
  48.            SDL_Color background = NO_COLOR);
  49.    //text
  50.    Surface(TTF_Font *font, std::string message, SDL_Color color);
  51.    //copy
  52.    Surface(const Surface &original);
  53.    
  54.    ~Surface();
  55.  
  56.    Surface &operator=(const Surface &rhs);
  57.    operator bool() const;
  58.    SDL_Surface *operator->();
  59.    const SDL_Surface *operator->() const;
  60.  
  61.    //need to be called manually, in main or wherever
  62.    static void init();
  63.    static void quit();
  64.  
  65.    //load a surface from an image file
  66.    void loadImage(const std::string &fileName, bool alpha);
  67.  
  68.    //set the surface's transparent color
  69.    void setColorKey(const SDL_Color &color);
  70.  
  71.    //fills a part of the surface with color
  72.    void fill(const SDL_Color color, SDL_Rect *rect = 0);
  73.    void fill(Uint32 color = BLACK_UINT, SDL_Rect *rect = 0);
  74.  
  75.    //draw onto another surface
  76.    void draw(Surface &dst = screenBuf,
  77.              SDL_Rect *dstRect = 0, SDL_Rect *srcRect = 0) const;
  78.    friend Surface &operator<<(Surface &dst, const Surface &src);
  79.  
  80.    //sets the surface's alpha value (0-ff)
  81.    void setAlpha(Uint8 alpha);
  82.  
  83.    //flips the screen buffer
  84.    void flip();
  85.  
  86.    //saves the surface as an image file
  87.    void saveToBitmap(std::string fileName) const;
  88.  
  89.    //dimensions of the surface
  90.    Point getDim() const;
  91.  
  92.    //pixel getters
  93.    Uint32 pixel(int index) const;
  94.    Uint32 pixel(int x, int y) const;
  95.  
  96.    //pixel setters
  97.    Uint32 &pixel(int index);
  98.    Uint32 &pixel(int x, int y);
  99. };
  100.  
  101. #endif
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top