SHARE
TWEET

Surface.cpp

Archon Nov 26th, 2010 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // (C) 2010 Tim Gurto
  2.  
  3. #include <string>
  4. #include <cassert>
  5.  
  6. #include "SDL.h"
  7. #include "SDL_image.h"
  8. #include "Surface.h"
  9. #include "Screen.h"
  10. #include "globals.h"
  11. #include "util.h"
  12.  
  13. int Surface::surfacesLoaded_ = 0;
  14. int Surface::screensSet_ = 0;
  15.  
  16. //constructor - from file
  17. Surface::Surface(const std::string fileName, bool alpha):
  18. isScreen_(false),
  19. surface_(0){
  20.    loadImage(fileName, alpha);
  21.    ++surfacesLoaded_;
  22. }
  23.  
  24. //constructor - from file, with transparent background
  25. Surface::Surface(const std::string fileName, const SDL_Color &background, bool alpha):
  26. isScreen_(false),
  27. surface_(0){
  28.    loadImage(fileName, alpha);
  29.    setColorKey(background);
  30.    ++surfacesLoaded_;
  31. }
  32.  
  33. //constructor - special (blank, screen, or default uninitialized)
  34. Surface::Surface(SpecialSurface special, int width, int height,
  35.                  SDL_Color background):
  36. isScreen_(false),
  37. surface_(0){
  38.    switch(special){
  39.  
  40.    case SUR_SCREEN:
  41.       if (screensSet_ != 0) //don't make duplicate screen buffers
  42.          break;
  43.  
  44.       surface_ = SDL_SetVideoMode(Screen::getScreenRes().x,
  45.                                   Screen::getScreenRes().y,
  46.                                   SCREEN_BPP,
  47.                                   SDL_HWSURFACE | (Screen::getWindowedMode() ?
  48.                                   0 :
  49.                                   SDL_FULLSCREEN));
  50.       SDL_WM_SetCaption("Game", 0);
  51.       isScreen_ = true;
  52.       assert (screensSet_ == 0);
  53.       screensSet_ = 1;
  54.       break;
  55.  
  56.    case SUR_BLANK:
  57.       if (width == -1)
  58.          width = Screen::getScreenRes().x;
  59.       if (height == -1)
  60.          height = Screen::getScreenRes().y;
  61.       surface_ = SDL_CreateRGBSurface(SDL_HWSURFACE,
  62.                                    width, height, SCREEN_BPP,
  63.                                    0, 0, 0, 0);
  64.       setColorKey(background);
  65.       break;
  66.    }
  67.    if (surface_ && !isScreen_)
  68.       ++surfacesLoaded_;
  69. }
  70.  
  71. //constructor - text
  72. Surface::Surface(TTF_Font *font, std::string message, SDL_Color color):
  73. surface_(TTF_RenderText_Solid(font, message.c_str(), color)),
  74. isScreen_(false){
  75.    if (surface_)
  76.       ++surfacesLoaded_;
  77. }
  78.  
  79. Surface::Surface(const Surface &original):
  80. surface_(0),
  81. isScreen_(false){
  82.    if (!original)
  83.       return;
  84.  
  85.    else
  86.       if (original.isScreen_){
  87.          surface_ = original.surface_;
  88.          isScreen_ = true;
  89.          ++screensSet_;
  90.       }else{
  91.          surface_ = SDL_ConvertSurface(original.surface_,
  92.                                        original.surface_->format,
  93.                                        SDL_HWSURFACE);
  94.          ++surfacesLoaded_;
  95.       }
  96. }
  97.  
  98. Surface::~Surface(){
  99.    if (surface_){
  100.       if (isScreen_)
  101.          --screensSet_;
  102.       else{
  103.          SDL_FreeSurface(surface_);
  104.          --surfacesLoaded_;
  105.       }
  106.    }
  107. }
  108.  
  109. Surface &Surface::operator=(const Surface &rhs){
  110.    if (this == &rhs)
  111.       return *this;
  112.  
  113.    if (surface_){
  114.       SDL_FreeSurface(surface_);
  115.       --surfacesLoaded_;
  116.    }
  117.    
  118.    if (rhs.isScreen_){
  119.       surface_ = rhs.surface_;
  120.       isScreen_ = true;
  121.       ++screensSet_;
  122.    }else{
  123.       surface_ = SDL_ConvertSurface(rhs.surface_,
  124.                                     rhs.surface_->format,
  125.                                     SDL_HWSURFACE);
  126.       ++surfacesLoaded_;
  127.    }
  128.    return *this;
  129. }
  130.  
  131. Surface::operator bool() const{
  132.    //return surface_;
  133.    return surface_ != 0;
  134. }
  135.  
  136. SDL_Surface *Surface::operator->(){
  137.    return surface_;
  138. }
  139.  
  140. const SDL_Surface *Surface::operator->() const{
  141.    return surface_;
  142. }
  143.  
  144. //needs to be called once, in main or wherever
  145. void Surface::init(){}
  146.  
  147. //needs to be called once, in main or wherever
  148. void Surface::quit(){
  149.    assert(surfacesLoaded_ == 0);
  150. }
  151.  
  152. //load a surface from an image file
  153. void Surface::loadImage(const std::string &fileName, bool alpha){
  154.    SDL_Surface *load = IMG_Load(fileName.c_str());
  155.    //assert(load);
  156.    surface_ = alpha ?
  157.       SDL_DisplayFormatAlpha(load) :
  158.       SDL_DisplayFormat(load);
  159.    assert(surface_);
  160.    SDL_FreeSurface(load);
  161. }
  162.  
  163. //set the surface's transparent color
  164. void Surface::setColorKey(const SDL_Color &color){
  165.    if (color != NO_COLOR)
  166.       SDL_SetColorKey(surface_,
  167.                       SDL_SRCCOLORKEY,
  168.                       SDL_MapRGB(surface_->format,
  169.                                  color.r,
  170.                                  color.g,
  171.                                  color.b));
  172. }
  173.  
  174. //fills a part of the surface with color
  175. void Surface::fill(const SDL_Color color, SDL_Rect *rect){
  176.    fill(colorToUInt(color), rect);
  177. }
  178.  
  179. //fills a part of the surface with color
  180. void Surface::fill(Uint32 color, SDL_Rect *rect){
  181.    SDL_FillRect(surface_, rect, color);
  182. }
  183.  
  184. //draw onto another surface
  185. void Surface::draw(Surface &dst,
  186.                    SDL_Rect *dstRect, SDL_Rect *srcRect) const{
  187.    SDL_BlitSurface(surface_, srcRect, dst.surface_, dstRect);
  188. }
  189.  
  190. void Surface::draw(Surface &dst, Point dstPoint, SDL_Rect *srcRect) const{
  191.    SDL_Rect rect = makeRect(dstPoint);
  192.    draw(dst, &rect, srcRect);
  193. }
  194.  
  195. //dst << src: draw
  196. Surface &operator<<(Surface &dst, const Surface &src){
  197.    src.draw(dst);
  198.    return dst;
  199. }
  200.  
  201. //sets the surface's alpha value (0-ff)
  202. void Surface::setAlpha(Uint8 alpha){
  203.    SDL_SetAlpha(surface_, SDL_SRCALPHA, alpha);
  204. }
  205.  
  206. //flips the screen buffer
  207. void Surface::flip(){
  208.    assert(isScreen_);
  209.    bool test;
  210.    test = SDL_Flip(surface_) == 0;
  211.    assert(test);
  212. }
  213.  
  214. //saves the surface as an image file
  215. void Surface::saveToBitmap(std::string &fileName) const{
  216.    SDL_SaveBMP(surface_, fileName.c_str());
  217. }
RAW Paste Data
Top