Render backend API simplification part 2: SDLSurface
This commit is contained in:
parent
3f8ead09d6
commit
5f0249de98
1 changed files with 33 additions and 60 deletions
|
@ -11,16 +11,16 @@
|
||||||
|
|
||||||
typedef struct Backend_Surface
|
typedef struct Backend_Surface
|
||||||
{
|
{
|
||||||
SDL_Surface *sdl_surface;
|
SDL_Surface *sdlsurface;
|
||||||
} Backend_Surface;
|
} Backend_Surface;
|
||||||
|
|
||||||
typedef struct Backend_Glyph
|
typedef struct Backend_Glyph
|
||||||
{
|
{
|
||||||
SDL_Surface *sdl_surface;
|
SDL_Surface *sdlsurface;
|
||||||
} Backend_Glyph;
|
} Backend_Glyph;
|
||||||
|
|
||||||
static SDL_Window *window;
|
static SDL_Window *window;
|
||||||
static SDL_Surface *window_surface;
|
static SDL_Surface *window_sdlsurface;
|
||||||
|
|
||||||
static Backend_Surface framebuffer;
|
static Backend_Surface framebuffer;
|
||||||
|
|
||||||
|
@ -43,30 +43,28 @@ SDL_Window* Backend_CreateWindow(const char *title, int width, int height)
|
||||||
return SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, 0);
|
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 = 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)
|
if (framebuffer.sdlsurface == NULL)
|
||||||
return FALSE;
|
return NULL;
|
||||||
|
|
||||||
return TRUE;
|
return &framebuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Backend_Deinit(Backend_Surface *framebuffer_surface)
|
void Backend_Deinit(void)
|
||||||
{
|
{
|
||||||
(void)framebuffer_surface;
|
SDL_FreeSurface(framebuffer.sdlsurface);
|
||||||
|
|
||||||
SDL_FreeSurface(framebuffer.sdl_surface);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Backend_DrawScreen(void)
|
void Backend_DrawScreen(void)
|
||||||
{
|
{
|
||||||
SDL_BlitSurface(framebuffer.sdl_surface, NULL, window_surface, NULL);
|
SDL_BlitSurface(framebuffer.sdlsurface, NULL, window_sdlsurface, NULL);
|
||||||
SDL_UpdateWindowSurface(window);
|
SDL_UpdateWindowSurface(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,9 +75,9 @@ Backend_Surface* Backend_CreateSurface(unsigned int width, unsigned int height)
|
||||||
if (surface == NULL)
|
if (surface == NULL)
|
||||||
return 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);
|
free(surface);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -93,7 +91,7 @@ void Backend_FreeSurface(Backend_Surface *surface)
|
||||||
if (surface == NULL)
|
if (surface == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
SDL_FreeSurface(surface->sdl_surface);
|
SDL_FreeSurface(surface->sdlsurface);
|
||||||
free(surface);
|
free(surface);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,8 +100,8 @@ unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch
|
||||||
if (surface == NULL)
|
if (surface == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
*pitch = surface->sdl_surface->pitch;
|
*pitch = surface->sdlsurface->pitch;
|
||||||
return (unsigned char*)surface->sdl_surface->pixels;
|
return (unsigned char*)surface->sdlsurface->pixels;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Backend_UnlockSurface(Backend_Surface *surface)
|
void Backend_UnlockSurface(Backend_Surface *surface)
|
||||||
|
@ -111,7 +109,7 @@ void Backend_UnlockSurface(Backend_Surface *surface)
|
||||||
(void)surface;
|
(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)
|
if (source_surface == NULL || destination_surface == NULL)
|
||||||
return;
|
return;
|
||||||
|
@ -125,22 +123,12 @@ static void BlitCommon(Backend_Surface *source_surface, const RECT *rect, Backen
|
||||||
destination_rect.w = source_rect.w;
|
destination_rect.w = source_rect.w;
|
||||||
destination_rect.h = source_rect.h;
|
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)
|
if (surface == NULL)
|
||||||
return;
|
return;
|
||||||
|
@ -148,17 +136,7 @@ void Backend_ColourFillToSurface(Backend_Surface *surface, const RECT *rect, uns
|
||||||
SDL_Rect destination_rect;
|
SDL_Rect destination_rect;
|
||||||
RectToSDLRect(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)
|
BOOL Backend_SupportsSubpixelGlyph(void)
|
||||||
|
@ -173,9 +151,9 @@ Backend_Glyph* Backend_LoadGlyph(const unsigned char *pixels, unsigned int width
|
||||||
if (glyph == NULL)
|
if (glyph == NULL)
|
||||||
return 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);
|
free(glyph);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -189,7 +167,7 @@ Backend_Glyph* Backend_LoadGlyph(const unsigned char *pixels, unsigned int width
|
||||||
for (unsigned int y = 0; y < height; ++y)
|
for (unsigned int y = 0; y < height; ++y)
|
||||||
{
|
{
|
||||||
const unsigned char *source_pointer = pixels + y * pitch;
|
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)
|
for (unsigned int x = 0; x < width; ++x)
|
||||||
{
|
{
|
||||||
|
@ -206,7 +184,7 @@ Backend_Glyph* Backend_LoadGlyph(const unsigned char *pixels, unsigned int width
|
||||||
for (unsigned int y = 0; y < height; ++y)
|
for (unsigned int y = 0; y < height; ++y)
|
||||||
{
|
{
|
||||||
const unsigned char *source_pointer = pixels + y * pitch;
|
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)
|
for (unsigned int x = 0; x < width; ++x)
|
||||||
{
|
{
|
||||||
|
@ -228,11 +206,11 @@ void Backend_UnloadGlyph(Backend_Glyph *glyph)
|
||||||
if (glyph == NULL)
|
if (glyph == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
SDL_FreeSurface(glyph->sdl_surface);
|
SDL_FreeSurface(glyph->sdlsurface);
|
||||||
free(glyph);
|
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)
|
if (glyph == NULL || surface == NULL)
|
||||||
return;
|
return;
|
||||||
|
@ -240,17 +218,12 @@ void Backend_DrawGlyphToSurface(Backend_Surface *surface, Backend_Glyph *glyph,
|
||||||
SDL_Rect rect;
|
SDL_Rect rect;
|
||||||
rect.x = x;
|
rect.x = x;
|
||||||
rect.y = y;
|
rect.y = y;
|
||||||
rect.w = glyph->sdl_surface->w;
|
rect.w = glyph->sdlsurface->w;
|
||||||
rect.h = glyph->sdl_surface->h;
|
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)
|
void Backend_HandleDeviceLoss(void)
|
||||||
|
@ -262,5 +235,5 @@ void Backend_HandleWindowResize(void)
|
||||||
{
|
{
|
||||||
// https://wiki.libsdl.org/SDL_GetWindowSurface
|
// https://wiki.libsdl.org/SDL_GetWindowSurface
|
||||||
// We need to fetch a new surface pointer
|
// We need to fetch a new surface pointer
|
||||||
window_surface = SDL_GetWindowSurface(window);
|
window_sdlsurface = SDL_GetWindowSurface(window);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue