daily pastebin goal
76%
SHARE
TWEET

Surface.h

Archon Dec 7th, 2010 86 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.    void draw(Surface &dst, Point dstPoint, SDL_Rect *srcRect = 0) const;
  79.    friend Surface &operator<<(Surface &dst, const Surface &src);
  80.  
  81.    //sets the surface's alpha value (0-ff)
  82.    void setAlpha(Uint8 alpha);
  83.  
  84.    //flips the screen buffer
  85.    void flip();
  86.  
  87.    //saves the surface as an image file
  88.    void saveToBitmap(std::string &fileName) const;
  89. };
  90.  
  91. #endif
RAW Paste Data
Top