cpp-sdl2
C++ header-only SDL2 wrapper
pixel.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "color.hpp"
4 #include "exception.hpp"
5 #include <SDL_pixels.h>
6 
7 namespace sdl
8 {
10 class Pixel
11 {
12 public:
14  Pixel(void* target, SDL_PixelFormat const& fmt) : target_{target}, fmt_{fmt} {}
15 
17  Pixel& operator=(Color const& c)
18  {
19  set_color(c);
20  return *this;
21  }
23  void set_color(Color const& c) { set_raw(c.as_uint(fmt_)); }
24 
26  Color color() const { return Color{get_raw(), fmt_}; }
27 
29  Uint8 r() const { return color().r; }
31  Uint8 g() const { return color().g; }
33  Uint8 b() const { return color().b; }
35  Uint8 a() const { return color().a; }
36 
38  void set_r(Uint8 r)
39  {
40  auto c = color();
41  c.r = r;
42  set_color(c);
43  }
45  void set_g(Uint8 g)
46  {
47  auto c = color();
48  c.g = g;
49  set_color(c);
50  }
52  void set_b(Uint8 b)
53  {
54  auto c = color();
55  c.b = b;
56  set_color(c);
57  }
59  void set_a(Uint8 a)
60  {
61  auto c = color();
62  c.a = a;
63  set_color(c);
64  }
65 
66 private:
68  void set_raw(Uint32 raw)
69  {
70  switch (fmt_.BytesPerPixel)
71  {
72  case 1: *static_cast<Uint8*>(target_) = static_cast<Uint8>(raw); break;
73  case 2: *static_cast<Uint16*>(target_) = static_cast<Uint16>(raw); break;
74  case 3:
75  if constexpr (SDL_BYTEORDER == SDL_BIG_ENDIAN)
76  {
77  static_cast<Uint8*>(target_)[0] = static_cast<Uint8>((raw >> 16) & 0xFF);
78  static_cast<Uint8*>(target_)[1] = static_cast<Uint8>((raw >> 8) & 0xFF);
79  static_cast<Uint8*>(target_)[2] = static_cast<Uint8>(raw & 0xFF);
80  }
81  else
82  {
83  static_cast<Uint8*>(target_)[0] = static_cast<Uint8>(raw & 0xFF);
84  static_cast<Uint8*>(target_)[1] = static_cast<Uint8>((raw >> 8) & 0xFF);
85  static_cast<Uint8*>(target_)[2] = static_cast<Uint8>((raw >> 16) & 0xFF);
86  }
87  break;
88  case 4: *static_cast<Uint32*>(target_) = raw; break;
89  }
90  }
92  Uint32 get_raw() const
93  {
94  switch (fmt_.BytesPerPixel)
95  {
96  case 1: return *static_cast<Uint8*>(target_);
97  case 2: return *static_cast<Uint16*>(target_);
98  case 3:
99  if constexpr (SDL_BYTEORDER == SDL_BIG_ENDIAN)
100  {
101  return static_cast<Uint8*>(target_)[0] << 16 | static_cast<Uint8*>(target_)[1] << 8
102  | static_cast<Uint8*>(target_)[2];
103  }
104  else
105  {
106  return static_cast<Uint8*>(target_)[0] | static_cast<Uint8*>(target_)[1] << 8
107  | static_cast<Uint8*>(target_)[2] << 16;
108  }
109  case 4: return *static_cast<Uint32*>(target_);
110  }
111 
112  // TODO decide if landing here is an important error and throw, or just return a black pixel
113  return 0;
114  }
115 
117  void* target_;
119  const SDL_PixelFormat& fmt_;
120 };
121 
122 } // namespace sdl
Pixel & operator=(Color const &c)
Set color to this pixel via operator =.
Definition: pixel.hpp:17
Uint8 g() const
Get green channel as byte.
Definition: pixel.hpp:31
void set_b(Uint8 b)
Set blue channel from given byte.
Definition: pixel.hpp:52
void set_g(Uint8 g)
Set green channel from given byte.
Definition: pixel.hpp:45
Uint8 r() const
Get red channel as byte.
Definition: pixel.hpp:29
void set_raw(Uint32 raw)
Set this pixel from a 32byte value.
Definition: pixel.hpp:68
Pixel(void *target, SDL_PixelFormat const &fmt)
Construct a pixel structure.
Definition: pixel.hpp:14
void set_r(Uint8 r)
Set red channel from given byte.
Definition: pixel.hpp:38
C++ wrapping around the SDL_Color structure.
Definition: color.hpp:10
Uint32 get_raw() const
Get pixel as 32 bit value.
Definition: pixel.hpp:92
Define to deactivate exception support.
Definition: color.hpp:7
const SDL_PixelFormat & fmt_
Used pixel format on this format.
Definition: pixel.hpp:119
Represent a 4 channel pixel.
Definition: pixel.hpp:10
void set_color(Color const &c)
Set color to this pixel.
Definition: pixel.hpp:23
void * target_
pointer to target structure
Definition: pixel.hpp:117
Uint8 b() const
Get blue channel as byte.
Definition: pixel.hpp:33
Color color() const
Get color.
Definition: pixel.hpp:26
Uint8 a() const
Get alpha channel as byte.
Definition: pixel.hpp:35
Uint32 as_uint(SDL_PixelFormat const &format) const
Return the color of the current object as a 32bit number (4 bytes)
Definition: color.hpp:55
void set_a(Uint8 a)
Set alpha channel from given byte.
Definition: pixel.hpp:59