SDL_blendfillrect.c
Go to the documentation of this file.
/*
Simple DirectMedia Layer
Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#if !SDL_RENDER_DISABLED
#include "SDL_draw.h"
#include "SDL_blendfillrect.h"
static int
SDL_BlendFillRect_RGB555(SDL_Surface * dst, const SDL_Rect * rect,
SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
unsigned inva = 0xff - a;
switch (blendMode) {
case SDL_BLENDMODE_BLEND:
FILLRECT(Uint16, DRAW_SETPIXEL_BLEND_RGB555);
break;
case SDL_BLENDMODE_ADD:
FILLRECT(Uint16, DRAW_SETPIXEL_ADD_RGB555);
break;
case SDL_BLENDMODE_MOD:
FILLRECT(Uint16, DRAW_SETPIXEL_MOD_RGB555);
break;
default:
FILLRECT(Uint16, DRAW_SETPIXEL_RGB555);
break;
}
return 0;
}
static int
SDL_BlendFillRect_RGB565(SDL_Surface * dst, const SDL_Rect * rect,
SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
unsigned inva = 0xff - a;
switch (blendMode) {
case SDL_BLENDMODE_BLEND:
FILLRECT(Uint16, DRAW_SETPIXEL_BLEND_RGB565);
break;
case SDL_BLENDMODE_ADD:
FILLRECT(Uint16, DRAW_SETPIXEL_ADD_RGB565);
break;
case SDL_BLENDMODE_MOD:
FILLRECT(Uint16, DRAW_SETPIXEL_MOD_RGB565);
break;
default:
FILLRECT(Uint16, DRAW_SETPIXEL_RGB565);
break;
}
return 0;
}
static int
SDL_BlendFillRect_RGB888(SDL_Surface * dst, const SDL_Rect * rect,
SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
unsigned inva = 0xff - a;
switch (blendMode) {
case SDL_BLENDMODE_BLEND:
FILLRECT(Uint32, DRAW_SETPIXEL_BLEND_RGB888);
break;
case SDL_BLENDMODE_ADD:
FILLRECT(Uint32, DRAW_SETPIXEL_ADD_RGB888);
break;
case SDL_BLENDMODE_MOD:
FILLRECT(Uint32, DRAW_SETPIXEL_MOD_RGB888);
break;
default:
FILLRECT(Uint32, DRAW_SETPIXEL_RGB888);
break;
}
return 0;
}
static int
SDL_BlendFillRect_ARGB8888(SDL_Surface * dst, const SDL_Rect * rect,
SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
unsigned inva = 0xff - a;
switch (blendMode) {
case SDL_BLENDMODE_BLEND:
FILLRECT(Uint32, DRAW_SETPIXEL_BLEND_ARGB8888);
break;
case SDL_BLENDMODE_ADD:
FILLRECT(Uint32, DRAW_SETPIXEL_ADD_ARGB8888);
break;
case SDL_BLENDMODE_MOD:
FILLRECT(Uint32, DRAW_SETPIXEL_MOD_ARGB8888);
break;
default:
FILLRECT(Uint32, DRAW_SETPIXEL_ARGB8888);
break;
}
return 0;
}
static int
SDL_BlendFillRect_RGB(SDL_Surface * dst, const SDL_Rect * rect,
SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
SDL_PixelFormat *fmt = dst->format;
unsigned inva = 0xff - a;
switch (fmt->BytesPerPixel) {
case 2:
switch (blendMode) {
case SDL_BLENDMODE_BLEND:
FILLRECT(Uint16, DRAW_SETPIXEL_BLEND_RGB);
break;
case SDL_BLENDMODE_ADD:
FILLRECT(Uint16, DRAW_SETPIXEL_ADD_RGB);
break;
case SDL_BLENDMODE_MOD:
FILLRECT(Uint16, DRAW_SETPIXEL_MOD_RGB);
break;
default:
FILLRECT(Uint16, DRAW_SETPIXEL_RGB);
break;
}
return 0;
case 4:
switch (blendMode) {
case SDL_BLENDMODE_BLEND:
FILLRECT(Uint32, DRAW_SETPIXEL_BLEND_RGB);
break;
case SDL_BLENDMODE_ADD:
FILLRECT(Uint32, DRAW_SETPIXEL_ADD_RGB);
break;
case SDL_BLENDMODE_MOD:
FILLRECT(Uint32, DRAW_SETPIXEL_MOD_RGB);
break;
default:
FILLRECT(Uint32, DRAW_SETPIXEL_RGB);
break;
}
return 0;
default:
return SDL_Unsupported();
}
}
static int
SDL_BlendFillRect_RGBA(SDL_Surface * dst, const SDL_Rect * rect,
SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
SDL_PixelFormat *fmt = dst->format;
unsigned inva = 0xff - a;
switch (fmt->BytesPerPixel) {
case 4:
switch (blendMode) {
case SDL_BLENDMODE_BLEND:
FILLRECT(Uint32, DRAW_SETPIXEL_BLEND_RGBA);
break;
case SDL_BLENDMODE_ADD:
FILLRECT(Uint32, DRAW_SETPIXEL_ADD_RGBA);
break;
case SDL_BLENDMODE_MOD:
FILLRECT(Uint32, DRAW_SETPIXEL_MOD_RGBA);
break;
default:
FILLRECT(Uint32, DRAW_SETPIXEL_RGBA);
break;
}
return 0;
default:
return SDL_Unsupported();
}
}
int
SDL_BlendFillRect(SDL_Surface * dst, const SDL_Rect * rect,
SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
SDL_Rect clipped;
if (!dst) {
return SDL_SetError("Passed NULL destination surface");
}
/* This function doesn't work on surfaces < 8 bpp */
if (dst->format->BitsPerPixel < 8) {
return SDL_SetError("SDL_BlendFillRect(): Unsupported surface format");
}
/* If 'rect' == NULL, then fill the whole surface */
if (rect) {
/* Perform clipping */
if (!SDL_IntersectRect(rect, &dst->clip_rect, &clipped)) {
return 0;
}
rect = &clipped;
} else {
rect = &dst->clip_rect;
}
if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
r = DRAW_MUL(r, a);
g = DRAW_MUL(g, a);
b = DRAW_MUL(b, a);
}
switch (dst->format->BitsPerPixel) {
case 15:
switch (dst->format->Rmask) {
case 0x7C00:
return SDL_BlendFillRect_RGB555(dst, rect, blendMode, r, g, b, a);
}
break;
case 16:
switch (dst->format->Rmask) {
case 0xF800:
return SDL_BlendFillRect_RGB565(dst, rect, blendMode, r, g, b, a);
}
break;
case 32:
switch (dst->format->Rmask) {
case 0x00FF0000:
if (!dst->format->Amask) {
return SDL_BlendFillRect_RGB888(dst, rect, blendMode, r, g, b, a);
} else {
return SDL_BlendFillRect_ARGB8888(dst, rect, blendMode, r, g, b, a);
}
/* break; -Wunreachable-code-break */
}
break;
default:
break;
}
if (!dst->format->Amask) {
return SDL_BlendFillRect_RGB(dst, rect, blendMode, r, g, b, a);
} else {
return SDL_BlendFillRect_RGBA(dst, rect, blendMode, r, g, b, a);
}
}
int
SDL_BlendFillRects(SDL_Surface * dst, const SDL_Rect * rects, int count,
SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
SDL_Rect rect;
int i;
int (*func)(SDL_Surface * dst, const SDL_Rect * rect,
SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a) = NULL;
int status = 0;
if (!dst) {
return SDL_SetError("Passed NULL destination surface");
}
/* This function doesn't work on surfaces < 8 bpp */
if (dst->format->BitsPerPixel < 8) {
return SDL_SetError("SDL_BlendFillRects(): Unsupported surface format");
}
if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
r = DRAW_MUL(r, a);
g = DRAW_MUL(g, a);
b = DRAW_MUL(b, a);
}
/* FIXME: Does this function pointer slow things down significantly? */
switch (dst->format->BitsPerPixel) {
case 15:
switch (dst->format->Rmask) {
case 0x7C00:
func = SDL_BlendFillRect_RGB555;
}
break;
case 16:
switch (dst->format->Rmask) {
case 0xF800:
func = SDL_BlendFillRect_RGB565;
}
break;
case 32:
switch (dst->format->Rmask) {
case 0x00FF0000:
if (!dst->format->Amask) {
func = SDL_BlendFillRect_RGB888;
} else {
func = SDL_BlendFillRect_ARGB8888;
}
break;
}
break;
default:
break;
}
if (!func) {
if (!dst->format->Amask) {
func = SDL_BlendFillRect_RGB;
} else {
func = SDL_BlendFillRect_RGBA;
}
}
for (i = 0; i < count; ++i) {
/* Perform clipping */
if (!SDL_IntersectRect(&rects[i], &dst->clip_rect, &rect)) {
continue;
}
status = func(dst, &rect, blendMode, r, g, b, a);
}
return status;
}
#endif /* !SDL_RENDER_DISABLED */
/* vi: set ts=4 sw=4 expandtab: */
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
DRAW_SETPIXEL_ARGB8888
#define DRAW_SETPIXEL_ARGB8888
Definition: SDL_draw.h:187
SDL_PixelFormat::BytesPerPixel
Uint8 BytesPerPixel
Definition: SDL_pixels.h:320
blendMode
static SDL_BlendMode blendMode
Definition: testdraw2.c:34
SDL_Surface
A collection of pixels used in software blitting.
Definition: SDL_surface.h:70
DRAW_SETPIXEL_RGB
#define DRAW_SETPIXEL_RGB
Definition: SDL_draw.h:218
SDL_BlendFillRects
int SDL_BlendFillRects(SDL_Surface *dst, const SDL_Rect *rects, int count, SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
Definition: SDL_blendfillrect.c:263
SDL_BLENDMODE_ADD
@ SDL_BLENDMODE_ADD
Definition: SDL_blendmode.h:47
NULL
#define NULL
Definition: begin_code.h:167
SDL_PixelFormat::format
Uint32 format
Definition: SDL_pixels.h:317
b
GLboolean GLboolean GLboolean b
Definition: SDL_opengl_glext.h:1109
g
GLboolean GLboolean g
Definition: SDL_opengl_glext.h:1109
count
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1571
DRAW_SETPIXEL_BLEND_ARGB8888
#define DRAW_SETPIXEL_BLEND_ARGB8888
Definition: SDL_draw.h:190
DRAW_SETPIXEL_BLEND_RGB888
#define DRAW_SETPIXEL_BLEND_RGB888
Definition: SDL_draw.h:159
r
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
SDL_IntersectRect
#define SDL_IntersectRect
Definition: SDL_dynapi_overrides.h:294
DRAW_SETPIXEL_BLEND_RGB555
#define DRAW_SETPIXEL_BLEND_RGB555
Definition: SDL_draw.h:97
DRAW_SETPIXEL_BLEND_RGB565
#define DRAW_SETPIXEL_BLEND_RGB565
Definition: SDL_draw.h:128
SDL_BlendFillRect_ARGB8888
static int SDL_BlendFillRect_ARGB8888(SDL_Surface *dst, const SDL_Rect *rect, SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
Definition: SDL_blendfillrect.c:99
DRAW_SETPIXEL_RGB555
#define DRAW_SETPIXEL_RGB555
Definition: SDL_draw.h:94
a
GLboolean GLboolean GLboolean GLboolean a
Definition: SDL_opengl_glext.h:1109
func
GLenum func
Definition: SDL_opengl_glext.h:657
DRAW_SETPIXEL_BLEND_RGBA
#define DRAW_SETPIXEL_BLEND_RGBA
Definition: SDL_draw.h:265
DRAW_SETPIXEL_ADD_RGB
#define DRAW_SETPIXEL_ADD_RGB
Definition: SDL_draw.h:225
dst
GLenum GLenum dst
Definition: SDL_opengl_glext.h:1737
DRAW_SETPIXEL_MOD_ARGB8888
#define DRAW_SETPIXEL_MOD_ARGB8888
Definition: SDL_draw.h:198
SDL_BLENDMODE_MOD
@ SDL_BLENDMODE_MOD
Definition: SDL_blendmode.h:50
DRAW_SETPIXEL_MOD_RGB888
#define DRAW_SETPIXEL_MOD_RGB888
Definition: SDL_draw.h:167
SDL_BlendFillRect_RGB888
static int SDL_BlendFillRect_RGB888(SDL_Surface *dst, const SDL_Rect *rect, SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
Definition: SDL_blendfillrect.c:76
SDL_draw.h
DRAW_SETPIXEL_RGB565
#define DRAW_SETPIXEL_RGB565
Definition: SDL_draw.h:125
DRAW_SETPIXEL_ADD_RGB888
#define DRAW_SETPIXEL_ADD_RGB888
Definition: SDL_draw.h:163
rect
SDL_Rect rect
Definition: testrelative.c:27
SDL_BlendFillRect
int SDL_BlendFillRect(SDL_Surface *dst, const SDL_Rect *rect, SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
Definition: SDL_blendfillrect.c:196
SDL_BlendFillRect_RGB555
static int SDL_BlendFillRect_RGB555(SDL_Surface *dst, const SDL_Rect *rect, SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
Definition: SDL_blendfillrect.c:30
Uint16
uint16_t Uint16
Definition: SDL_stdinc.h:191
DRAW_SETPIXEL_ADD_RGB565
#define DRAW_SETPIXEL_ADD_RGB565
Definition: SDL_draw.h:132
DRAW_SETPIXEL_MOD_RGB
#define DRAW_SETPIXEL_MOD_RGB
Definition: SDL_draw.h:229
SDL_BlendFillRect_RGB
static int SDL_BlendFillRect_RGB(SDL_Surface *dst, const SDL_Rect *rect, SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
Definition: SDL_blendfillrect.c:122
DRAW_SETPIXEL_ADD_RGBA
#define DRAW_SETPIXEL_ADD_RGBA
Definition: SDL_draw.h:269
DRAW_SETPIXEL_MOD_RGB565
#define DRAW_SETPIXEL_MOD_RGB565
Definition: SDL_draw.h:136
FILLRECT
#define FILLRECT(type, op)
Definition: SDL_draw.h:545
SDL_PixelFormat
Definition: SDL_pixels.h:315
DRAW_SETPIXEL_BLEND_RGB
#define DRAW_SETPIXEL_BLEND_RGB
Definition: SDL_draw.h:221
DRAW_MUL
#define DRAW_MUL(_a, _b)
Definition: SDL_draw.h:29
SDL_BLENDMODE_BLEND
@ SDL_BLENDMODE_BLEND
Definition: SDL_blendmode.h:44
DRAW_SETPIXEL_ADD_ARGB8888
#define DRAW_SETPIXEL_ADD_ARGB8888
Definition: SDL_draw.h:194
DRAW_SETPIXEL_MOD_RGBA
#define DRAW_SETPIXEL_MOD_RGBA
Definition: SDL_draw.h:273
SDL_blendfillrect.h
SDL_BlendFillRect_RGBA
static int SDL_BlendFillRect_RGBA(SDL_Surface *dst, const SDL_Rect *rect, SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
Definition: SDL_blendfillrect.c:167
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30
SDL_Rect
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:77
DRAW_SETPIXEL_RGBA
#define DRAW_SETPIXEL_RGBA
Definition: SDL_draw.h:262
DRAW_SETPIXEL_RGB888
#define DRAW_SETPIXEL_RGB888
Definition: SDL_draw.h:156
SDL_Unsupported
#define SDL_Unsupported()
Definition: SDL_error.h:53
rects
EGLSurface EGLint * rects
Definition: eglext.h:282
SDL_BlendMode
SDL_BlendMode
The blend mode used in SDL_RenderCopy() and drawing operations.
Definition: SDL_blendmode.h:40
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
SDL_BlendFillRect_RGB565
static int SDL_BlendFillRect_RGB565(SDL_Surface *dst, const SDL_Rect *rect, SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
Definition: SDL_blendfillrect.c:53
DRAW_SETPIXEL_MOD_RGB555
#define DRAW_SETPIXEL_MOD_RGB555
Definition: SDL_draw.h:105
DRAW_SETPIXEL_ADD_RGB555
#define DRAW_SETPIXEL_ADD_RGB555
Definition: SDL_draw.h:101
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:179