ManaPlus
Functions
sdlpixel.h File Reference

(986a3bf)

#include "gui/color.h"
#include <SDL.h>

Go to the source code of this file.

Functions

void SDLputPixel (SDL_Surface *surface, int x, int y, const Color &color)
 
unsigned int SDLAlpha32 (const unsigned int src, const unsigned int dst, const unsigned char a)
 
unsigned short SDLAlpha16 (const unsigned short src, const unsigned short dst, const unsigned char a, const SDL_PixelFormat *const f)
 
void SDLputPixelAlpha (SDL_Surface *surface, int x, int y, const Color &color)
 

Function Documentation

◆ SDLAlpha16()

unsigned short SDLAlpha16 ( const unsigned short  src,
const unsigned short  dst,
const unsigned char  a,
const SDL_PixelFormat *const  f 
)
inline

Blends two 16 bit colors together.

Parameters
srcthe source color.
dstthe destination color.
aalpha.

Definition at line 170 of file sdlpixel.h.

174 {
175  unsigned int b = ((src & f->Rmask) * a + (dst & f->Rmask)
176  * (255 - a)) >> 8;
177  unsigned int g = ((src & f->Gmask) * a + (dst & f->Gmask)
178  * (255 - a)) >> 8;
179  unsigned int r = ((src & f->Bmask) * a + (dst & f->Bmask)
180  * (255 - a)) >> 8;
181 
182  return static_cast<unsigned short>((b & f->Rmask)
183  | (g & f->Gmask) | (r & f->Bmask));
184 }

Referenced by SDLputPixelAlpha().

◆ SDLAlpha32()

unsigned int SDLAlpha32 ( const unsigned int  src,
const unsigned int  dst,
const unsigned char  a 
)
inline

Blends two 32 bit colors together.

Parameters
srcthe source color.
dstthe destination color.
aalpha.

Definition at line 145 of file sdlpixel.h.

148 {
149  const unsigned int b = ((src & 0xff) * a + (dst & 0xff) * (255 - a)) >> 8;
150  const unsigned int g = ((src & 0xff00) * a + (dst & 0xff00)
151  * (255 - a)) >> 8;
152  const unsigned int r = ((src & 0xff0000) * a + (dst & 0xff0000)
153  * (255 - a)) >> 8;
154 
155  return (b & 0xff) | (g & 0xff00) | (r & 0xff0000);
156 }

Referenced by SDLputPixelAlpha().

◆ SDLputPixel()

void SDLputPixel ( SDL_Surface *  surface,
int  x,
int  y,
const Color color 
)
inline

Puts a pixel on an SDL_Surface.

Parameters
xthe x coordinate on the surface.
ythe y coordinate on the surface.
colorthe color the pixel should be in.

Definition at line 86 of file sdlpixel.h.

88 {
89  if (surface == nullptr)
90  return;
91 
92  const int bpp = surface->format->BytesPerPixel;
93 
94  SDL_LockSurface(surface);
95 
96  Uint8 *const p = static_cast<uint8_t*>(surface->pixels)
97  + CAST_SIZE(y * surface->pitch + x * bpp);
98 
99  const Uint32 pixel = SDL_MapRGB(surface->format,
100  CAST_U8(color.r), CAST_U8(color.g),
101  CAST_U8(color.b));
102 
103  switch (bpp)
104  {
105  case 1:
106  *p = CAST_U8(pixel);
107  break;
108 
109  case 2:
110  *reinterpret_cast<uint16_t*>(p) = CAST_U16(pixel);
111  break;
112 
113  case 3:
114 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
115  p[0] = CAST_U8((pixel >> 16) & 0xff);
116  p[1] = CAST_U8((pixel >> 8) & 0xff);
117  p[2] = CAST_U8((pixel) & 0xff);
118 #else // SDL_BYTEORDER == SDL_BIG_ENDIAN
119 
120  p[0] = CAST_U8((pixel) & 0xff);
121  p[1] = CAST_U8((pixel >> 8) & 0xff);
122  p[2] = CAST_U8((pixel >> 16) & 0xff);
123 #endif // SDL_BYTEORDER == SDL_BIG_ENDIAN
124 
125  break;
126 
127  case 4:
128  *reinterpret_cast<Uint32*>(p) = pixel;
129  break;
130 
131  default:
132  break;
133  }
134 
135  SDL_UnlockSurface(surface);
136 }
#define CAST_U16
Definition: cast.h:29
#define CAST_SIZE
Definition: cast.h:34
#define CAST_U8
Definition: cast.h:27
unsigned int b
Definition: color.h:245
unsigned int r
Definition: color.h:235
unsigned int g
Definition: color.h:240

References Color::b, CAST_SIZE, CAST_U16, CAST_U8, Color::g, Color::r, x, and y.

◆ SDLputPixelAlpha()

void SDLputPixelAlpha ( SDL_Surface *  surface,
int  x,
int  y,
const Color color 
)
inline

Puts a pixel on an SDL_Surface with alpha

Parameters
xthe x coordinate on the surface.
ythe y coordinate on the surface.
colorthe color the pixel should be in.

Definition at line 193 of file sdlpixel.h.

195 {
196  if (surface == nullptr)
197  return;
198  const int bpp = surface->format->BytesPerPixel;
199 
200  SDL_LockSurface(surface);
201 
202  Uint8 *const p = static_cast<uint8_t*>(surface->pixels)
203  + CAST_SIZE(y * surface->pitch + x * bpp);
204 
205  const Uint32 pixel = SDL_MapRGB(surface->format,
206  CAST_U8(color.r),
207  CAST_U8(color.g),
208  CAST_U8(color.b));
209 
210  switch (bpp)
211  {
212  case 1:
213  *p = CAST_U8(pixel);
214  break;
215 
216  case 2:
217  *reinterpret_cast<Uint16*>(p) = SDLAlpha16(
218  static_cast<unsigned short>(pixel),
219  *reinterpret_cast<unsigned short*>(p),
220  CAST_U8(color.a), surface->format);
221  break;
222 
223  case 3:
224 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
225  p[2] = CAST_U8((p[2] * (255 - color.a)
226  + color.b * color.a) >> 8);
227  p[1] = CAST_U8((p[1] * (255 - color.a)
228  + color.g * color.a) >> 8);
229  p[0] = CAST_U8((p[0] * (255 - color.a)
230  + color.r * color.a) >> 8);
231 #else // SDL_BYTEORDER == SDL_BIG_ENDIAN
232 
233  p[0] = CAST_U8((p[0] * (255 - color.a)
234  + color.b * color.a) >> 8);
235  p[1] = CAST_U8((p[1] * (255 - color.a)
236  + color.g * color.a) >> 8);
237  p[2] = CAST_U8((p[2] * (255 - color.a)
238  + color.r * color.a) >> 8);
239 #endif // SDL_BYTEORDER == SDL_BIG_ENDIAN
240 
241  break;
242 
243  case 4:
244  *reinterpret_cast<Uint32*>(p) = SDLAlpha32(pixel,
245  *reinterpret_cast<Uint32*>(p),
246  CAST_U8(color.a));
247  break;
248  default:
249  break;
250  }
251 
252  SDL_UnlockSurface(surface);
253 }
unsigned int a
Definition: color.h:251
unsigned int SDLAlpha32(const unsigned int src, const unsigned int dst, const unsigned char a)
Definition: sdlpixel.h:145
unsigned short SDLAlpha16(const unsigned short src, const unsigned short dst, const unsigned char a, const SDL_PixelFormat *const f)
Definition: sdlpixel.h:170

References Color::a, Color::b, CAST_SIZE, CAST_U8, Color::g, Color::r, SDLAlpha16(), SDLAlpha32(), x, and y.