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:
34  SDLSurfacePtr(const SDLSurfacePtr&) = delete;
35  SDLSurfacePtr& operator=(const SDLSurfacePtr&) = delete;
36 
39  SDLSurfacePtr(unsigned width, unsigned height, unsigned depth,
40  Uint32 rMask, Uint32 gMask, Uint32 bMask, Uint32 aMask)
41  {
42  assert((depth % 8) == 0);
43  unsigned pitch = width * (depth >> 3);
44  unsigned size = height * pitch;
45  buffer.resize(size);
46  surface = SDL_CreateRGBSurfaceFrom(
47  buffer.data(), width, height, depth, pitch,
48  rMask, gMask, bMask, aMask);
49  if (!surface) throw std::bad_alloc();
50  }
51 
52  explicit SDLSurfacePtr(SDL_Surface* surface_ = nullptr)
53  : surface(surface_)
54  {
55  }
56 
57  SDLSurfacePtr(SDLSurfacePtr&& other) noexcept
58  : surface(other.surface)
59  , buffer(std::move(other.buffer))
60  {
61  other.surface = nullptr;
62  }
63 
65  {
66  if (surface) SDL_FreeSurface(surface);
67  }
68 
69  void reset(SDL_Surface* surface_ = nullptr)
70  {
71  SDLSurfacePtr temp(surface_);
72  temp.swap(*this);
73  }
74 
75  SDL_Surface* get()
76  {
77  return surface;
78  }
79  const SDL_Surface* get() const
80  {
81  return surface;
82  }
83 
84  void swap(SDLSurfacePtr& other) noexcept
85  {
86  std::swap(surface, other.surface);
87  std::swap(buffer, other.buffer );
88  }
89 
91  {
92  std::swap(surface, other.surface);
93  std::swap(buffer, other.buffer);
94  return *this;
95  }
96 
97  SDL_Surface& operator*()
98  {
99  return *surface;
100  }
101  const SDL_Surface& operator*() const
102  {
103  return *surface;
104  }
105 
106  SDL_Surface* operator->()
107  {
108  return surface;
109  }
110  const SDL_Surface* operator->() const
111  {
112  return surface;
113  }
114 
115  explicit operator bool() const
116  {
117  return get() != nullptr;
118  }
119 
120  void* getLinePtr(unsigned y)
121  {
122  assert(y < unsigned(surface->h));
123  return static_cast<Uint8*>(surface->pixels) + y * surface->pitch;
124  }
125  const void* getLinePtr(unsigned y) const
126  {
127  return const_cast<SDLSurfacePtr*>(this)->getLinePtr(y);
128  }
129 
130 private:
131  SDL_Surface* surface;
133 };
134 
135 #endif
const SDL_Surface & operator*() const
const SDL_Surface * operator->() const
const void * getLinePtr(unsigned y) const
void swap(SDLSurfacePtr &other) noexcept
void * getLinePtr(unsigned y)
SDLSurfacePtr & operator=(const SDLSurfacePtr &)=delete
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:120
SDLSurfacePtr(const SDLSurfacePtr &)=delete
SDLSurfacePtr(SDL_Surface *surface_=nullptr)
SDL_Surface & operator*()
SDLSurfacePtr(SDLSurfacePtr &&other) noexcept
size_t size() const
SDLSurfacePtr & operator=(SDLSurfacePtr &&other) noexcept
const T * data() const
Returns pointer to the start of the memory buffer.
Definition: MemBuffer.hh:90