openMSX
SDLOffScreenSurface.cc
Go to the documentation of this file.
1 #include "SDLOffScreenSurface.hh"
2 #include "SDLVisibleSurface.hh"
3 #include <cstring>
4 
5 namespace openmsx {
6 
7 SDLOffScreenSurface::SDLOffScreenSurface(const SDL_Surface& proto)
8 {
9  // SDL_CreateRGBSurface() allocates an internal buffer, on 32-bit
10  // systems this buffer is only 8-bytes aligned. For some scalers (with
11  // SSE(2) optimizations) we need a 16-byte aligned buffer. So now we
12  // allocate the buffer ourselves and create the SDL_Surface with
13  // SDL_CreateRGBSurfaceFrom().
14  // Of course it would be better to get rid of SDL_Surface in the
15  // OutputSurface interface.
16 
17  setSDLFormat(*proto.format);
18  const SDL_PixelFormat& frmt = getSDLFormat();
19 
20  unsigned pitch2 = proto.w * frmt.BitsPerPixel / 8;
21  assert((pitch2 % 16) == 0);
22  unsigned size = pitch2 * proto.h;
23  buffer.resize(size);
24  memset(buffer.data(), 0, size);
25  surface.reset(SDL_CreateRGBSurfaceFrom(
26  buffer.data(), proto.w, proto.h, frmt.BitsPerPixel, pitch2,
27  frmt.Rmask, frmt.Gmask, frmt.Bmask, frmt.Amask));
28 
29  setSDLSurface(surface.get());
30  setBufferPtr(static_cast<char*>(surface->pixels), surface->pitch);
31 
32  // Used (only?) by 'screenshot -with-osd'.
33  renderer.reset(SDL_CreateSoftwareRenderer(surface.get()));
34  setSDLRenderer(renderer.get());
35 }
36 
37 void SDLOffScreenSurface::saveScreenshot(const std::string& filename)
38 {
39  SDLVisibleSurface::saveScreenshotSDL(*this, filename);
40 }
41 
42 void SDLOffScreenSurface::clearScreen()
43 {
44  memset(surface->pixels, 0, uint32_t(surface->pitch) * surface->h);
45 }
46 
47 } // namespace openmsx
void setBufferPtr(char *data, unsigned pitch)
SDL_Surface * get()
SDLOffScreenSurface(const SDL_Surface &prototype)
void reset(SDL_Surface *surface_=nullptr)
size_t size(std::string_view utf8)
static void saveScreenshotSDL(const OutputSurface &output, const std::string &filename)
void setSDLFormat(const SDL_PixelFormat &format)
constexpr const char *const filename
void resize(size_t size)
Grow or shrink the memory block.
Definition: MemBuffer.hh:120
const T * data() const
Returns pointer to the start of the memory buffer.
Definition: MemBuffer.hh:90
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
void setSDLRenderer(SDL_Renderer *r)
void setSDLSurface(SDL_Surface *surface_)
const SDL_PixelFormat & getSDLFormat() const