SDLOffScreenSurface.cc 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "SDLOffScreenSurface.hh"
  2. #include "SDLVisibleSurface.hh"
  3. #include <cstring>
  4. namespace openmsx {
  5. SDLOffScreenSurface::SDLOffScreenSurface(const SDL_Surface& proto)
  6. {
  7. gl::ivec2 size(proto.w, proto.h);
  8. calculateViewPort(size, size);
  9. setSDLPixelFormat(*proto.format);
  10. // SDL_CreateRGBSurface() allocates an internal buffer, on 32-bit
  11. // systems this buffer is only 8-bytes aligned. For some scalers (with
  12. // SSE(2) optimizations) we need a 16-byte aligned buffer. So now we
  13. // allocate the buffer ourselves and create the SDL_Surface with
  14. // SDL_CreateRGBSurfaceFrom().
  15. // Of course it would be better to get rid of SDL_Surface in the
  16. // OutputSurface interface.
  17. const PixelFormat& frmt = getPixelFormat();
  18. unsigned pitch2 = proto.w * frmt.getBytesPerPixel();
  19. assert((pitch2 % 16) == 0);
  20. unsigned bufSize = pitch2 * proto.h;
  21. buffer.resize(bufSize);
  22. memset(buffer.data(), 0, bufSize);
  23. surface.reset(SDL_CreateRGBSurfaceFrom(
  24. buffer.data(), proto.w, proto.h, frmt.getBpp(), pitch2,
  25. frmt.getRmask(), frmt.getGmask(), frmt.getBmask(), frmt.getAmask()));
  26. setSDLSurface(surface.get());
  27. // Used (only?) by 'screenshot -with-osd'.
  28. renderer.reset(SDL_CreateSoftwareRenderer(surface.get()));
  29. setSDLRenderer(renderer.get());
  30. }
  31. void SDLOffScreenSurface::saveScreenshot(const std::string& filename)
  32. {
  33. SDLVisibleSurface::saveScreenshotSDL(*this, filename);
  34. }
  35. void SDLOffScreenSurface::clearScreen()
  36. {
  37. memset(surface->pixels, 0, uint32_t(surface->pitch) * surface->h);
  38. }
  39. } // namespace openmsx