surface.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #ifndef SIMPLE_GRAPHICAL_SURFACE_H
  2. #define SIMPLE_GRAPHICAL_SURFACE_H
  3. #include <memory>
  4. #include "simple/support/function_utils.hpp"
  5. #include "simple/sdlcore/utils.hpp"
  6. #include "common_def.h"
  7. #include "pixel_format.h"
  8. #include "pixels.h"
  9. #include "color.h"
  10. namespace simple::graphical
  11. {
  12. using sdl_surface_wrapper = sdlcore::utils::object_wrapper<SDL_Surface>;
  13. class surface : public sdl_surface_wrapper
  14. {
  15. public:
  16. using byte = pixel_byte;
  17. explicit surface(const surface& other);
  18. surface(surface&& other) = default;
  19. surface& operator=(surface&& other) = default;
  20. // TODO: constructor with a pallete
  21. explicit surface(const char* filename);
  22. surface(int2 size, const pixel_format& format);
  23. surface(byte* pixels, int2 size, const pixel_format& format, int pitch = 0);
  24. surface(std::unique_ptr<byte[]> pixels, int2 size, const pixel_format& format, int pitch = 0);
  25. surface(std::unique_ptr<byte[], void(*)(byte[])> pixels, int2 size, const pixel_format& format, int pitch = 0);
  26. #if SDL_VERSION_ATLEAST(2,0,5)
  27. surface(int2 size, pixel_format::type format);
  28. surface(byte* pixels, int2 size, pixel_format::type format, int pitch = 0);
  29. surface(std::unique_ptr<byte[]> pixels, int2 size, pixel_format::type format, int pitch = 0);
  30. surface(std::unique_ptr<byte[], void(*)(byte[])> pixels, int2 size, pixel_format::type format, int pitch = 0);
  31. #endif
  32. const pixel_format& format() const;
  33. int2 size() const;
  34. blend_mode blend() const noexcept;
  35. void blend(blend_mode new_value) const noexcept;
  36. uint8_t alpha() const noexcept;
  37. void alpha(uint8_t new_value) const noexcept;
  38. rgb_pixel color() const noexcept;
  39. void color(rgb_pixel new_value) const noexcept;
  40. // TODO: generic interface wrappers, implicitly visiting, like simple::motion::variant
  41. pixel_writer_variant pixels() const noexcept;
  42. void save(const char* filename) const;
  43. protected:
  44. explicit surface(SDL_Surface* guts, Deleter deleter = SDL_FreeSurface);
  45. private:
  46. using sdl_surface_wrapper::sdl_surface_wrapper;
  47. class free_pixel_format : public pixel_format
  48. {
  49. public:
  50. free_pixel_format(SDL_PixelFormat* guts);
  51. };
  52. free_pixel_format _format;
  53. std::unique_ptr<byte[], void(*)(byte*)> pixels_owner {nullptr, support::nop};
  54. friend bool fill(const surface&, class color);
  55. friend bool fill(const surface&, class color, const range2D&);
  56. friend bool blit(const surface&, range2D, const surface&, int2);
  57. friend bool blit(const surface&, range2D, const surface&, range2D);
  58. friend surface convert(const surface& source, const pixel_format& format);
  59. friend class renderer;
  60. friend class window;
  61. };
  62. pixel_writer_variant pixel_writer_from_format(pixel_byte* data, int2 size, int pitch, int bpp);
  63. } // namespace simple::graphical
  64. #endif /* end of include guard */