SDLOutputSurface.cc 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "SDLOutputSurface.hh"
  2. #include "unreachable.hh"
  3. namespace openmsx {
  4. SDLDirectPixelAccess::SDLDirectPixelAccess(SDL_Surface* surface_)
  5. : surface(surface_)
  6. {
  7. assert(surface);
  8. if (SDL_MUSTLOCK(surface)) {
  9. // Note: we ignore the return value from SDL_LockSurface()
  10. SDL_LockSurface(surface);
  11. }
  12. }
  13. SDLDirectPixelAccess::~SDLDirectPixelAccess()
  14. {
  15. if (SDL_MUSTLOCK(surface)) {
  16. SDL_UnlockSurface(surface);
  17. }
  18. }
  19. void SDLOutputSurface::setSDLPixelFormat(const SDL_PixelFormat& format)
  20. {
  21. auto bpp = format.BitsPerPixel;
  22. auto Rmask = format.Rmask;
  23. auto Rshift = format.Rshift;
  24. auto Rloss = format.Rloss;
  25. auto Gmask = format.Gmask;
  26. auto Gshift = format.Gshift;
  27. auto Gloss = format.Gloss;
  28. auto Bmask = format.Bmask;
  29. auto Bshift = format.Bshift;
  30. auto Bloss = format.Bloss;
  31. auto Amask = format.Amask;
  32. auto Ashift = format.Ashift;
  33. auto Aloss = format.Aloss;
  34. // TODO is this still needed with SDL2?
  35. // SDL sets an alpha channel only for GL modes. We want an alpha channel
  36. // for all 32bpp output surfaces, so we add one ourselves if necessary.
  37. if (bpp == 32 && Amask == 0) {
  38. unsigned rgbMask = Rmask | Gmask | Bmask;
  39. if ((rgbMask & 0x000000FF) == 0) {
  40. Amask = 0x000000FF;
  41. Ashift = 0;
  42. Aloss = 0;
  43. } else if ((rgbMask & 0xFF000000) == 0) {
  44. Amask = 0xFF000000;
  45. Ashift = 24;
  46. Aloss = 0;
  47. } else {
  48. UNREACHABLE;
  49. }
  50. }
  51. setPixelFormat(PixelFormat(bpp,
  52. Rmask, Rshift, Rloss,
  53. Gmask, Gshift, Gloss,
  54. Bmask, Bshift, Bloss,
  55. Amask, Ashift, Aloss));
  56. }
  57. } // namespace openmsx