SHARE
TWEET

Surface.cpp assignment

Archon Feb 3rd, 2011 99 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 name here ]", 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.    assert (rhs.surface_);
  119.    if (rhs.isScreen_){
  120.       surface_ = rhs.surface_;
  121.       isScreen_ = true;
  122.       ++screensSet_;
  123.    }else if (rhs.surface_){
  124.       surface_ = SDL_ConvertSurface(rhs.surface_,
  125.                                     rhs.surface_->format,
  126.                                     SDL_HWSURFACE);
  127.       ++surfacesLoaded_;
  128.    }
  129.    return *this;
  130. }
  131.  
  132. Surface::operator bool() const{
  133.    //return surface_;
  134.    return surface_ != 0;
  135. }
  136.  
  137. SDL_Surface *Surface::operator->(){
  138.    return surface_;
  139. }
  140.  
  141. const SDL_Surface *Surface::operator->() const{
  142.    return surface_;
  143. }
  144.  
  145. //needs to be called once, in main or wherever
  146. void Surface::init(){}
  147.  
  148. //needs to be called once, in main or wherever
  149. void Surface::quit(){
  150.    assert(surfacesLoaded_ == 0);
  151. }
  152.  
  153. //load a surface from an image file
  154. void Surface::loadImage(const std::string &fileName, bool alpha){
  155.    SDL_Surface *load = IMG_Load(fileName.c_str());
  156.    //assert(load);
  157.    surface_ = alpha ?
  158.       SDL_DisplayFormatAlpha(load) :
  159.       SDL_DisplayFormat(load);
  160.    assert(surface_);
  161.    SDL_FreeSurface(load);
  162. }
  163.  
  164. //set the surface's transparent color
  165. void Surface::setColorKey(const SDL_Color &color){
  166.    if (color != NO_COLOR)
  167.       SDL_SetColorKey(surface_,
  168.                       SDL_SRCCOLORKEY,
  169.                       SDL_MapRGB(surface_->format,
  170.                                  color.r,
  171.                                  color.g,
  172.                                  color.b));
  173. }
  174.  
  175. //fills a part of the surface with color
  176. void Surface::fill(const SDL_Color color, SDL_Rect *rect){
  177.    fill(colorToUInt(color), rect);
  178. }
  179.  
  180. //fills a part of the surface with color
  181. void Surface::fill(Uint32 color, SDL_Rect *rect){
  182.    SDL_FillRect(surface_, rect, color);
  183. }
  184.  
  185. //draw onto another surface
  186. void Surface::draw(Surface &dst,
  187.                    SDL_Rect *dstRect, SDL_Rect *srcRect) const{
  188.    SDL_BlitSurface(surface_, srcRect, dst.surface_, dstRect);
  189. }
  190.  
  191. //dst << src: draw
  192. Surface &operator<<(Surface &dst, const Surface &src){
  193.    src.draw(dst);
  194.    return dst;
  195. }
  196.  
  197. //sets the surface's alpha value (0-ff)
  198. void Surface::setAlpha(Uint8 alpha){
  199.    SDL_SetAlpha(surface_, SDL_SRCALPHA, alpha);
  200. }
  201.  
  202. //flips the screen buffer
  203. void Surface::flip(){
  204.    assert(isScreen_);
  205.    bool test;
  206.    test = SDL_Flip(surface_) == 0;
  207.    assert(test);
  208. }
  209.  
  210. //saves the surface as an image file
  211. void Surface::saveToBitmap(std::string fileName) const{
  212.    SDL_SaveBMP(surface_, fileName.c_str());
  213. }
  214.  
  215. Point Surface::getDim() const{
  216.    return Point(surface_->w, surface_->h);
  217. }
  218.  
  219. //pixel getters
  220. Uint32 Surface::pixel(int index) const{
  221.    return ((Uint32 *)surface_->pixels) [index];
  222. }
  223. Uint32 Surface::pixel(int x, int y) const{
  224.    return ((Uint32 *)surface_->pixels) [y * surface_->w + x];
  225. }
  226.  
  227. //pixel setters
  228. Uint32 &Surface::pixel(int index){
  229.    return ((Uint32 *)surface_->pixels) [index];
  230. }
  231. Uint32 &Surface::pixel(int x, int y){
  232.    return ((Uint32 *)surface_->pixels) [y * surface_->w + x];
  233. }
RAW Paste Data
Want to get better at C++?
Learn to code C++ in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top