SDLOutputSurface.hh 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #ifndef SDLOUTPUTSURFACE_HH
  2. #define SDLOUTPUTSURFACE_HH
  3. #include "OutputSurface.hh"
  4. #include <cassert>
  5. #include <SDL.h>
  6. namespace openmsx {
  7. class SDLDirectPixelAccess
  8. {
  9. public:
  10. SDLDirectPixelAccess(SDL_Surface* surface_);
  11. ~SDLDirectPixelAccess();
  12. template<typename Pixel>
  13. Pixel* getLinePtr(unsigned y) {
  14. return reinterpret_cast<Pixel*>(static_cast<char*>(surface->pixels) + y * surface->pitch);
  15. }
  16. private:
  17. SDL_Surface* surface;
  18. };
  19. /** A frame buffer where pixels can be written to.
  20. * It could be an in-memory buffer or a video buffer visible to the user
  21. * (see VisibleSurface subclass).
  22. */
  23. class SDLOutputSurface : public OutputSurface
  24. {
  25. public:
  26. SDLOutputSurface(const SDLOutputSurface&) = delete;
  27. SDLOutputSurface& operator=(const SDLOutputSurface&) = delete;
  28. SDL_Surface* getSDLSurface() const { return surface; }
  29. SDL_Renderer* getSDLRenderer() const { return renderer; }
  30. /** Return a SDLDirectPixelAccess object. Via this object pointers to
  31. * individual Pixel lines can be obtained. Those pointer only remain
  32. * valid for as long as the SDLDirectPixelAccess object is kept alive.
  33. * And that object should be kept for at most the duration of one
  34. * frame. (This allows the implementation to lock/unlock the underlying
  35. * SDL surface).
  36. *
  37. * Note that direct pixel access is not always supported (e.g. not for
  38. * openGL based surfaces). TODO can we move this method down the class
  39. * hierarchy so that it's only available on classes that do support it?
  40. */
  41. SDLDirectPixelAccess getDirectPixelAccess()
  42. {
  43. return SDLDirectPixelAccess(getSDLSurface());
  44. }
  45. /** Copy frame buffer to display buffer.
  46. * The default implementation does nothing.
  47. */
  48. virtual void flushFrameBuffer() {}
  49. /** Clear frame buffer (paint it black).
  50. * The default implementation does nothing.
  51. */
  52. virtual void clearScreen() {}
  53. protected:
  54. SDLOutputSurface() = default;
  55. void setSDLPixelFormat(const SDL_PixelFormat& format);
  56. void setSDLSurface(SDL_Surface* surface_) { surface = surface_; }
  57. void setSDLRenderer(SDL_Renderer* r) { renderer = r; }
  58. private:
  59. SDL_Surface* surface = nullptr;
  60. SDL_Renderer* renderer = nullptr;
  61. };
  62. } // namespace openmsx
  63. #endif