pixel_format.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <type_traits>
  2. #include "simple/support/enum.hpp"
  3. #include "pixel_format.h"
  4. #include "palette_view.h"
  5. namespace simple::graphical
  6. {
  7. void pixel_format::free_pixel_format(const SDL_PixelFormat* one) noexcept
  8. {
  9. SDL_FreeFormat(const_cast<SDL_PixelFormat*>(one));
  10. }
  11. pixel_format::pixel_format(type format_type)
  12. : sdl_pixel_format_wrapper
  13. (
  14. SDL_AllocFormat(static_cast<std::underlying_type_t<type>>(format_type)),
  15. free_pixel_format
  16. )
  17. {}
  18. int pixel_format::bits() const noexcept { return guts()->BitsPerPixel; }
  19. int pixel_format::bytes() const noexcept { return guts()->BytesPerPixel; }
  20. uint32_t pixel_format::red_mask() const noexcept { return guts()->Rmask; }
  21. uint32_t pixel_format::green_mask() const noexcept { return guts()->Gmask; }
  22. uint32_t pixel_format::blue_mask() const noexcept { return guts()->Bmask; }
  23. uint32_t pixel_format::alpha_mask() const noexcept { return guts()->Amask; }
  24. graphical::color pixel_format::color(const rgb_pixel& values) const noexcept
  25. {
  26. return graphical::color(SDL_MapRGB(guts().get(), values.r(), values.g(), values.b()));
  27. }
  28. graphical::color pixel_format::color(const rgba_pixel& values) const noexcept
  29. {
  30. return graphical::color(SDL_MapRGBA(guts().get(), values.r(), values.g(), values.b(), values.a()));
  31. }
  32. rgb_pixel pixel_format::rgb(const graphical::color& color) const noexcept
  33. {
  34. rgb_pixel rgb;
  35. SDL_GetRGB(static_cast<uint32_t>(color), guts().get(), &rgb.r(), &rgb.g(), &rgb.b());
  36. return rgb;
  37. }
  38. rgba_pixel pixel_format::rgba(const graphical::color& color) const noexcept
  39. {
  40. rgba_pixel rgba;
  41. SDL_GetRGBA(static_cast<uint32_t>(color), guts().get(),
  42. &rgba.r(), &rgba.g(), &rgba.b(), &rgba.a());
  43. return rgba;
  44. }
  45. std::optional<palette_view> pixel_format::palette() const noexcept
  46. {
  47. auto palette_ptr = guts()->palette;
  48. return palette_ptr ? std::make_optional(palette_view(palette_ptr)) : std::nullopt;
  49. }
  50. const char* pixel_format::name() const noexcept
  51. {
  52. return SDL_GetPixelFormatName(guts()->format);
  53. }
  54. const char* to_string(pixel_format::type type) noexcept
  55. {
  56. return SDL_GetPixelFormatName(support::to_integer(type));
  57. }
  58. } // namespace simple::graphical