ref: 5f0249de989600f36f688176c6a284f13960df56
parent: 3f8ead09d6abba46b2bdff8cefc43d6545bcf6bc
author: Clownacy <Clownacy@users.noreply.github.com>
date: Mon Aug 12 21:44:36 EDT 2019
Render backend API simplification part 2: SDLSurface
--- a/src/Backends/Rendering/SDLSurface.cpp
+++ b/src/Backends/Rendering/SDLSurface.cpp
@@ -11,16 +11,16 @@
typedef struct Backend_Surface
{
- SDL_Surface *sdl_surface;
+ SDL_Surface *sdlsurface;
} Backend_Surface;
typedef struct Backend_Glyph
{
- SDL_Surface *sdl_surface;
+ SDL_Surface *sdlsurface;
} Backend_Glyph;
static SDL_Window *window;
-static SDL_Surface *window_surface;
+static SDL_Surface *window_sdlsurface;
static Backend_Surface framebuffer;
@@ -43,30 +43,28 @@
return SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, 0);
}
-BOOL Backend_Init(SDL_Window *p_window)
+Backend_Surface* Backend_Init(SDL_Window *p_window)
{
window = p_window;
- window_surface = SDL_GetWindowSurface(window);
+ window_sdlsurface = SDL_GetWindowSurface(window);
- framebuffer.sdl_surface = SDL_CreateRGBSurfaceWithFormat(0, window_surface->w, window_surface->h, 0, SDL_PIXELFORMAT_RGB24);
+ framebuffer.sdlsurface = SDL_CreateRGBSurfaceWithFormat(0, window_sdlsurface->w, window_sdlsurface->h, 0, SDL_PIXELFORMAT_RGB24);
- if (framebuffer.sdl_surface == NULL)
- return FALSE;
+ if (framebuffer.sdlsurface == NULL)
+ return NULL;
- return TRUE;
+ return &framebuffer;
}
-void Backend_Deinit(Backend_Surface *framebuffer_surface)
+void Backend_Deinit(void)
{
- (void)framebuffer_surface;
-
- SDL_FreeSurface(framebuffer.sdl_surface);
+ SDL_FreeSurface(framebuffer.sdlsurface);
}
void Backend_DrawScreen(void)
{
- SDL_BlitSurface(framebuffer.sdl_surface, NULL, window_surface, NULL);
+ SDL_BlitSurface(framebuffer.sdlsurface, NULL, window_sdlsurface, NULL);
SDL_UpdateWindowSurface(window);
}
@@ -77,9 +75,9 @@
if (surface == NULL)
return NULL;
- surface->sdl_surface = SDL_CreateRGBSurfaceWithFormat(0, width, height, 0, SDL_PIXELFORMAT_RGB24);
+ surface->sdlsurface = SDL_CreateRGBSurfaceWithFormat(0, width, height, 0, SDL_PIXELFORMAT_RGB24);
- if (surface->sdl_surface == NULL)
+ if (surface->sdlsurface == NULL)
{
free(surface);
return NULL;
@@ -93,7 +91,7 @@
if (surface == NULL)
return;
- SDL_FreeSurface(surface->sdl_surface);
+ SDL_FreeSurface(surface->sdlsurface);
free(surface);
}
@@ -102,8 +100,8 @@
if (surface == NULL)
return NULL;
- *pitch = surface->sdl_surface->pitch;
- return (unsigned char*)surface->sdl_surface->pixels;
+ *pitch = surface->sdlsurface->pitch;
+ return (unsigned char*)surface->sdlsurface->pixels;
}
void Backend_UnlockSurface(Backend_Surface *surface)
@@ -111,7 +109,7 @@
(void)surface;
}
-static void BlitCommon(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, long x, long y, BOOL colour_key)
+void Backend_Blit(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, long x, long y, BOOL colour_key)
{
if (source_surface == NULL || destination_surface == NULL)
return;
@@ -125,23 +123,13 @@
destination_rect.w = source_rect.w;
destination_rect.h = source_rect.h;
- SDL_SetColorKey(source_surface->sdl_surface, colour_key ? SDL_TRUE : SDL_FALSE, SDL_MapRGB(source_surface->sdl_surface->format, 0, 0, 0)); // Assumes the colour key will always be #000000 (black)
+ SDL_SetColorKey(source_surface->sdlsurface, colour_key ? SDL_TRUE : SDL_FALSE, SDL_MapRGB(source_surface->sdlsurface->format, 0, 0, 0)); // Assumes the colour key will always be #000000 (black)
- SDL_BlitSurface(source_surface->sdl_surface, &source_rect, destination_surface->sdl_surface, &destination_rect);
+ SDL_BlitSurface(source_surface->sdlsurface, &source_rect, destination_surface->sdlsurface, &destination_rect);
}
-void Backend_BlitToSurface(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, long x, long y)
+void Backend_ColourFill(Backend_Surface *surface, const RECT *rect, unsigned char red, unsigned char green, unsigned char blue)
{
- BlitCommon(source_surface, rect, destination_surface, x, y, TRUE);
-}
-
-void Backend_BlitToScreen(Backend_Surface *source_surface, const RECT *rect, long x, long y, BOOL colour_key)
-{
- BlitCommon(source_surface, rect, &framebuffer, x, y, colour_key);
-}
-
-void Backend_ColourFillToSurface(Backend_Surface *surface, const RECT *rect, unsigned char red, unsigned char green, unsigned char blue)
-{
if (surface == NULL)
return;
@@ -148,19 +136,9 @@
SDL_Rect destination_rect;
RectToSDLRect(rect, &destination_rect);
- SDL_FillRect(surface->sdl_surface, &destination_rect, SDL_MapRGB(surface->sdl_surface->format, red, green, blue));
+ SDL_FillRect(surface->sdlsurface, &destination_rect, SDL_MapRGB(surface->sdlsurface->format, red, green, blue));
}
-void Backend_ColourFillToScreen(const RECT *rect, unsigned char red, unsigned char green, unsigned char blue)
-{
- Backend_ColourFillToSurface(&framebuffer, rect, red, green, blue);
-}
-
-void Backend_ScreenToSurface(Backend_Surface *surface, const RECT *rect)
-{
- BlitCommon(&framebuffer, rect, surface, rect->left, rect->top, FALSE);
-}
-
BOOL Backend_SupportsSubpixelGlyph(void)
{
return FALSE; // SDL_Surfaces don't have per-component alpha
@@ -173,9 +151,9 @@
if (glyph == NULL)
return NULL;
- glyph->sdl_surface = SDL_CreateRGBSurfaceWithFormat(0, width, height, 0, SDL_PIXELFORMAT_RGBA32);
+ glyph->sdlsurface = SDL_CreateRGBSurfaceWithFormat(0, width, height, 0, SDL_PIXELFORMAT_RGBA32);
- if (glyph->sdl_surface == NULL)
+ if (glyph->sdlsurface == NULL)
{
free(glyph);
return NULL;
@@ -189,7 +167,7 @@
for (unsigned int y = 0; y < height; ++y)
{
const unsigned char *source_pointer = pixels + y * pitch;
- unsigned char *destination_pointer = (unsigned char*)glyph->sdl_surface->pixels + y * glyph->sdl_surface->pitch;
+ unsigned char *destination_pointer = (unsigned char*)glyph->sdlsurface->pixels + y * glyph->sdlsurface->pitch;
for (unsigned int x = 0; x < width; ++x)
{
@@ -206,7 +184,7 @@
for (unsigned int y = 0; y < height; ++y)
{
const unsigned char *source_pointer = pixels + y * pitch;
- unsigned char *destination_pointer = (unsigned char*)glyph->sdl_surface->pixels + y * glyph->sdl_surface->pitch;
+ unsigned char *destination_pointer = (unsigned char*)glyph->sdlsurface->pixels + y * glyph->sdlsurface->pitch;
for (unsigned int x = 0; x < width; ++x)
{
@@ -228,11 +206,11 @@
if (glyph == NULL)
return;
- SDL_FreeSurface(glyph->sdl_surface);
+ SDL_FreeSurface(glyph->sdlsurface);
free(glyph);
}
-void Backend_DrawGlyphToSurface(Backend_Surface *surface, Backend_Glyph *glyph, long x, long y, const unsigned char *colours)
+void Backend_DrawGlyph(Backend_Surface *surface, Backend_Glyph *glyph, long x, long y, const unsigned char *colours)
{
if (glyph == NULL || surface == NULL)
return;
@@ -240,19 +218,14 @@
SDL_Rect rect;
rect.x = x;
rect.y = y;
- rect.w = glyph->sdl_surface->w;
- rect.h = glyph->sdl_surface->h;
+ rect.w = glyph->sdlsurface->w;
+ rect.h = glyph->sdlsurface->h;
- SDL_SetSurfaceColorMod(glyph->sdl_surface, colours[0], colours[1], colours[2]);
+ SDL_SetSurfaceColorMod(glyph->sdlsurface, colours[0], colours[1], colours[2]);
- SDL_BlitSurface(glyph->sdl_surface, NULL, surface->sdl_surface, &rect);
+ SDL_BlitSurface(glyph->sdlsurface, NULL, surface->sdlsurface, &rect);
}
-void Backend_DrawGlyphToScreen(Backend_Glyph *glyph, long x, long y, const unsigned char *colours)
-{
- Backend_DrawGlyphToSurface(&framebuffer, glyph, x, y, colours);
-}
-
void Backend_HandleDeviceLoss(void)
{
// No problem for us
@@ -262,5 +235,5 @@
{
// https://wiki.libsdl.org/SDL_GetWindowSurface
// We need to fetch a new surface pointer
- window_surface = SDL_GetWindowSurface(window);
+ window_sdlsurface = SDL_GetWindowSurface(window);
}