ulfben

Sample "Big Five" implementation for SDL_Surface

Oct 14th, 2017
112
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2. #include "SDL.h"
  3. class Text;
  4. class Surface{
  5. public:
  6.     Surface(Text& text);       
  7.     SDL_Surface* Surface::getSurface() const {
  8.         return _ptr;
  9.     }
  10.     //Destructor
  11.     Surface::~Surface() noexcept {
  12.         destroy();
  13.     }
  14.     // Copy constructor
  15.     Surface(const Surface& other){
  16.         _ptr = other.getSurface();
  17.         if (_ptr) {
  18.             _ptr->refcount++;
  19.         }      
  20.     }
  21.     // Move constructor
  22.     Surface(Surface&& other) noexcept {
  23.         _ptr = other._ptr;
  24.         other._ptr = nullptr;                          
  25.     }
  26.     // Copy assignment operator
  27.     Surface& operator= (const Surface& other){
  28.         destroy();//delete previous value      
  29.         _ptr = other.getSurface();
  30.         if (_ptr) {
  31.             _ptr->refcount++;
  32.         }      
  33.         return *this;      
  34.     }
  35.     // Move assignment operator
  36.     Surface& operator= (Surface&& other) noexcept {    
  37.         destroy();
  38.         _ptr = other.getSurface();
  39.         other._ptr = nullptr;      
  40.         return *this;
  41.     }
  42. protected:
  43.     void destroy() noexcept {
  44.         if (_ptr) {
  45.             SDL_FreeSurface(_ptr);
  46.             _ptr = nullptr;
  47.         }
  48.     }
  49. private:
  50.     SDL_Surface* _ptr = nullptr;
  51. };
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×