openMSX
SDLSurfacePtr.hh
Go to the documentation of this file.
1 #ifndef SDLSURFACEPTR
2 #define SDLSURFACEPTR
3 
4 #include <SDL.h>
5 #include <algorithm>
6 #include <new>
7 #include <cassert>
8 #include <cstdlib>
9 
31 {
32 public:
35  SDLSurfacePtr(unsigned width, unsigned height, unsigned depth,
36  Uint32 rMask, Uint32 gMask, Uint32 bMask, Uint32 aMask)
37  {
38  assert((depth % 8) == 0);
39  unsigned pitch = width * (depth >> 3);
40  unsigned size = height * pitch;
41  buffer = malloc(size);
42  if (!buffer) throw std::bad_alloc();
43  surface = SDL_CreateRGBSurfaceFrom(
44  buffer, width, height, depth, pitch,
45  rMask, gMask, bMask, aMask);
46  if (!surface) {
47  free(buffer);
48  throw std::bad_alloc();
49  }
50  }
51 
52  // don't allow copy and assign
53  // msvc doesn't yet support deleted functions.
54  SDLSurfacePtr(const SDLSurfacePtr&) /*= delete*/;
55  SDLSurfacePtr& operator=(const SDLSurfacePtr&) /*= delete*/;
56 
57  explicit SDLSurfacePtr(SDL_Surface* surface_ = nullptr,
58  void* buffer_ = nullptr)
59  : surface(surface_)
60  , buffer(buffer_)
61  {
62  }
63 
65  : surface(other.surface)
66  , buffer(other.buffer)
67  {
68  other.surface = nullptr;
69  other.buffer = nullptr;
70  }
71 
73  {
74  if (surface) {
75  SDL_FreeSurface(surface);
76  }
77  free(buffer);
78  }
79 
80  void reset(SDL_Surface* surface_ = nullptr)
81  {
82  SDLSurfacePtr temp(surface_);
83  temp.swap(*this);
84  }
85 
86  SDL_Surface* get()
87  {
88  return surface;
89  }
90  const SDL_Surface* get() const
91  {
92  return surface;
93  }
94 
95  void swap(SDLSurfacePtr& other)
96  {
97  std::swap(surface, other.surface);
98  std::swap(buffer, other.buffer );
99  }
100 
102  {
103  SDLSurfacePtr tmp(other.surface, other.buffer);
104  other.surface = nullptr;
105  other.buffer = nullptr;
106  tmp.swap(*this);
107  return *this;
108  }
109 
110  SDL_Surface& operator*()
111  {
112  return *surface;
113  }
114  const SDL_Surface& operator*() const
115  {
116  return *surface;
117  }
118 
119  SDL_Surface* operator->()
120  {
121  return surface;
122  }
123  const SDL_Surface* operator->() const
124  {
125  return surface;
126  }
127 
128  explicit operator bool() const
129  {
130  return get() != nullptr;
131  }
132 
133  void* getLinePtr(unsigned y)
134  {
135  assert(y < unsigned(surface->h));
136  return static_cast<Uint8*>(surface->pixels) + y * surface->pitch;
137  }
138  const void* getLinePtr(unsigned y) const
139  {
140  return const_cast<SDLSurfacePtr*>(this)->getLinePtr(y);
141  }
142 
143 private:
144  SDL_Surface* surface;
145  void* buffer;
146 };
147 
148 #endif
const SDL_Surface & operator*() const
const SDL_Surface * operator->() const
const void * getLinePtr(unsigned y) const
void * getLinePtr(unsigned y)
SDLSurfacePtr & operator=(const SDLSurfacePtr &)
SDLSurfacePtr(unsigned width, unsigned height, unsigned depth, Uint32 rMask, Uint32 gMask, Uint32 bMask, Uint32 aMask)
Create a (software) surface with uninitialized pixel content.
void reset(SDL_Surface *surface_=nullptr)
SDL_Surface * operator->()
Wrapper around a SDL_Surface.
SDLSurfacePtr(SDLSurfacePtr &&other)
SDL_Surface & operator*()
size_t size() const
void swap(SDLSurfacePtr &other)
SDLSurfacePtr & operator=(SDLSurfacePtr &&other)
SDLSurfacePtr(SDL_Surface *surface_=nullptr, void *buffer_=nullptr)