openMSX
SDLSurfacePtr.hh
Go to the documentation of this file.
1 #ifndef SDLSURFACEPTR
2 #define SDLSURFACEPTR
3 
4 #include "MemBuffer.hh"
5 #include <SDL.h>
6 #include <algorithm>
7 #include <new>
8 #include <cassert>
9 #include <cstdlib>
10 
32 {
33 public:
36  SDLSurfacePtr(unsigned width, unsigned height, unsigned depth,
37  Uint32 rMask, Uint32 gMask, Uint32 bMask, Uint32 aMask)
38  {
39  assert((depth % 8) == 0);
40  unsigned pitch = width * (depth >> 3);
41  unsigned size = height * pitch;
42  buffer.resize(size);
43  surface = SDL_CreateRGBSurfaceFrom(
44  buffer.data(), width, height, depth, pitch,
45  rMask, gMask, bMask, aMask);
46  if (!surface) throw std::bad_alloc();
47  }
48 
49  // don't allow copy and assign
50  // msvc doesn't yet support deleted functions.
51  SDLSurfacePtr(const SDLSurfacePtr&) /*= delete*/;
52  SDLSurfacePtr& operator=(const SDLSurfacePtr&) /*= delete*/;
53 
54  explicit SDLSurfacePtr(SDL_Surface* surface_ = nullptr)
55  : surface(surface_)
56  {
57  }
59  : surface(other.surface)
60  , buffer(std::move(other.buffer))
61  {
62  other.surface = nullptr;
63  }
64 
66  {
67  if (surface) SDL_FreeSurface(surface);
68  }
69 
70  void reset(SDL_Surface* surface_ = nullptr)
71  {
72  SDLSurfacePtr temp(surface_);
73  temp.swap(*this);
74  }
75 
76  SDL_Surface* get()
77  {
78  return surface;
79  }
80  const SDL_Surface* get() const
81  {
82  return surface;
83  }
84 
85  void swap(SDLSurfacePtr& other)
86  {
87  std::swap(surface, other.surface);
88  std::swap(buffer, other.buffer );
89  }
90 
92  {
93  std::swap(surface, other.surface);
94  std::swap(buffer, other.buffer);
95  return *this;
96  }
97 
98  SDL_Surface& operator*()
99  {
100  return *surface;
101  }
102  const SDL_Surface& operator*() const
103  {
104  return *surface;
105  }
106 
107  SDL_Surface* operator->()
108  {
109  return surface;
110  }
111  const SDL_Surface* operator->() const
112  {
113  return surface;
114  }
115 
116  explicit operator bool() const
117  {
118  return get() != nullptr;
119  }
120 
121  void* getLinePtr(unsigned y)
122  {
123  assert(y < unsigned(surface->h));
124  return static_cast<Uint8*>(surface->pixels) + y * surface->pitch;
125  }
126  const void* getLinePtr(unsigned y) const
127  {
128  return const_cast<SDLSurfacePtr*>(this)->getLinePtr(y);
129  }
130 
131 private:
132  SDL_Surface* surface;
134 };
135 
136 #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->()
STL namespace.
Wrapper around a SDL_Surface.
void resize(size_t size)
Grow or shrink the memory block.
Definition: MemBuffer.hh:121
SDLSurfacePtr(SDL_Surface *surface_=nullptr)
SDLSurfacePtr(SDLSurfacePtr &&other)
SDL_Surface & operator*()
size_t size() const
void swap(SDLSurfacePtr &other)
const T * data() const
Returns pointer to the start of the memory buffer.
Definition: MemBuffer.hh:91
SDLSurfacePtr & operator=(SDLSurfacePtr &&other)