Renderer backend simplification part 1: Draw.cpp and Software.cpp

By emulating the DirectDraw code more closely, I can simplify the
renderer backend API.
This commit is contained in:
Clownacy 2019-08-13 01:31:08 +00:00
parent e094f2ff68
commit 3f8ead09d6
6 changed files with 56 additions and 96 deletions

View file

@ -15,22 +15,18 @@ typedef struct Backend_Surface Backend_Surface;
typedef struct Backend_Glyph Backend_Glyph;
SDL_Window* Backend_CreateWindow(const char *title, int width, int height);
BOOL Backend_Init(SDL_Window *window);
Backend_Surface* Backend_Init(SDL_Window *window);
void Backend_Deinit(void);
void Backend_DrawScreen(void);
Backend_Surface* Backend_CreateSurface(unsigned int width, unsigned int height);
void Backend_FreeSurface(Backend_Surface *surface);
unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch);
void Backend_UnlockSurface(Backend_Surface *surface);
void Backend_BlitToSurface(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, long x, long y);
void Backend_BlitToScreen(Backend_Surface *source_surface, const RECT *rect, long x, long y, BOOL colour_key);
void Backend_ColourFillToSurface(Backend_Surface *surface, const RECT *rect, unsigned char red, unsigned char green, unsigned char blue);
void Backend_ColourFillToScreen(const RECT *rect, unsigned char red, unsigned char green, unsigned char blue);
void Backend_ScreenToSurface(Backend_Surface *surface, const RECT *rect);
void Backend_Blit(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, long x, long y, BOOL colour_key);
void Backend_ColourFill(Backend_Surface *surface, const RECT *rect, unsigned char red, unsigned char green, unsigned char blue);
BOOL Backend_SupportsSubpixelGlyph(void);
Backend_Glyph* Backend_LoadGlyph(const unsigned char *pixels, unsigned int width, unsigned int height, int pitch, unsigned char pixel_mode);
void Backend_UnloadGlyph(Backend_Glyph *glyph);
void Backend_DrawGlyphToSurface(Backend_Surface *surface, Backend_Glyph *glyph, long x, long y, const unsigned char *colours);
void Backend_DrawGlyphToScreen(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);
void Backend_HandleDeviceLoss(void);
void Backend_HandleWindowResize(void);

View file

@ -57,8 +57,10 @@ BOOL Backend_Init(SDL_Window *p_window)
return TRUE;
}
void Backend_Deinit(void)
void Backend_Deinit(Backend_Surface *framebuffer_surface)
{
(void)framebuffer_surface;
SDL_FreeSurface(framebuffer.sdl_surface);
}

View file

@ -30,9 +30,8 @@ typedef struct Backend_Glyph
} Backend_Glyph;
static SDL_Window *window;
static SDL_Surface *window_surface;
static SDL_Surface *screen_surface;
static SDL_Surface *window_sdlsurface;
static SDL_Surface *framebuffer_sdlsurface;
static Backend_Surface framebuffer;
SDL_Window* Backend_CreateWindow(const char *title, int width, int height)
@ -40,33 +39,33 @@ SDL_Window* Backend_CreateWindow(const char *title, int width, int height)
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);
screen_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 (screen_surface == NULL)
return FALSE;
if (framebuffer_sdlsurface == NULL)
return NULL;
framebuffer.pixels = (unsigned char*)screen_surface->pixels;
framebuffer.width = screen_surface->w;
framebuffer.height = screen_surface->h;
framebuffer.pitch = screen_surface->pitch;
framebuffer.pixels = (unsigned char*)framebuffer_sdlsurface->pixels;
framebuffer.width = framebuffer_sdlsurface->w;
framebuffer.height = framebuffer_sdlsurface->h;
framebuffer.pitch = framebuffer_sdlsurface->pitch;
return TRUE;
return &framebuffer;
}
void Backend_Deinit(void)
{
SDL_FreeSurface(screen_surface);
SDL_FreeSurface(framebuffer_sdlsurface);
}
void Backend_DrawScreen(void)
{
SDL_BlitSurface(screen_surface, NULL, window_surface, NULL);
SDL_BlitSurface(framebuffer_sdlsurface, NULL, window_sdlsurface, NULL);
SDL_UpdateWindowSurface(window);
}
@ -115,7 +114,7 @@ void Backend_UnlockSurface(Backend_Surface *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)
return;
@ -198,17 +197,7 @@ static void BlitCommon(Backend_Surface *source_surface, const RECT *rect, Backen
}
}
void Backend_BlitToSurface(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, long x, long y)
{
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)
void Backend_ColourFill(Backend_Surface *surface, const RECT *rect, unsigned char red, unsigned char green, unsigned char blue)
{
if (surface == NULL)
return;
@ -266,16 +255,6 @@ void Backend_ColourFillToSurface(Backend_Surface *surface, const RECT *rect, uns
}
}
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 TRUE; // It's a software renderer, baby
@ -352,7 +331,7 @@ void Backend_UnloadGlyph(Backend_Glyph *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)
return;
@ -419,11 +398,6 @@ void Backend_DrawGlyphToSurface(Backend_Surface *surface, Backend_Glyph *glyph,
}
}
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
@ -433,5 +407,5 @@ void Backend_HandleWindowResize(void)
{
// https://wiki.libsdl.org/SDL_GetWindowSurface
// We need to fetch a new surface pointer
window_surface = SDL_GetWindowSurface(window);
window_sdlsurface = SDL_GetWindowSurface(window);
}

View file

@ -26,15 +26,10 @@ RECT grcFull = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
int magnification;
BOOL fullscreen;
struct SURFACE
{
BOOL in_use;
Backend_Surface *backend;
};
static Backend_Surface *surf[SURFACE_ID_MAX];
static Backend_Surface *framebuffer;
SURFACE surf[SURFACE_ID_MAX];
FontObject *gFont;
static FontObject *gFont;
#define FRAMERATE 20
@ -99,8 +94,9 @@ BOOL StartDirectDraw(int lMagnification, int lColourDepth)
rgb24_pixel_format = SDL_AllocFormat(SDL_PIXELFORMAT_RGB24);
// Create renderer
if (!Backend_Init(gWindow))
framebuffer = Backend_Init(gWindow);
if (framebuffer == NULL)
return FALSE;
return TRUE;
@ -109,8 +105,14 @@ BOOL StartDirectDraw(int lMagnification, int lColourDepth)
void EndDirectDraw()
{
// Release all surfaces
for (int i = 0; i < SURFACE_ID_MAX; i++)
ReleaseSurface(i);
for (int i = 0; i < SURFACE_ID_MAX; ++i)
{
if (surf[i])
{
Backend_FreeSurface(surf[i]);
surf[i] = NULL;
}
}
Backend_Deinit();
@ -133,10 +135,10 @@ static BOOL IsEnableBitmap(SDL_RWops *fp)
void ReleaseSurface(int s)
{
// Release the surface we want to release
if (surf[s].in_use)
if (surf[s])
{
Backend_FreeSurface(surf[s].backend);
surf[s].in_use = FALSE;
Backend_FreeSurface(surf[s]);
surf[s] = NULL;
}
}
@ -156,26 +158,21 @@ BOOL MakeSurface_Generic(int bxsize, int bysize, Surface_Ids surf_no, BOOL bSyst
}
else
{
if (surf[surf_no].in_use)
if (surf[surf_no])
{
printf("Tried to create drawable surface at occupied slot (%d)\n", surf_no);
}
else
{
// Create surface
surf[surf_no].backend = Backend_CreateSurface(bxsize * magnification, bysize * magnification);
surf[surf_no] = Backend_CreateSurface(bxsize * magnification, bysize * magnification);
if (surf[surf_no].backend == NULL)
{
if (surf[surf_no] == NULL)
printf("Failed to create backend surface %d\n", surf_no);
}
else
{
surf[surf_no].in_use = TRUE;
success = TRUE;
}
}
}
return success;
}
@ -190,7 +187,7 @@ static BOOL LoadBitmap(SDL_RWops *fp, Surface_Ids surf_no, BOOL create_surface)
}
else
{
if (create_surface && surf[surf_no].in_use)
if (create_surface && surf[surf_no])
{
printf("Tried to create drawable surface at occupied slot (%d)\n", surf_no);
}
@ -217,7 +214,7 @@ static BOOL LoadBitmap(SDL_RWops *fp, Surface_Ids surf_no, BOOL create_surface)
{
// IF YOU WANT TO ADD HD SPRITES, THIS IS THE CODE YOU SHOULD EDIT
unsigned int pitch;
unsigned char *pixels = Backend_LockSurface(surf[surf_no].backend, &pitch);
unsigned char *pixels = Backend_LockSurface(surf[surf_no], &pitch);
if (magnification == 1)
{
@ -258,7 +255,7 @@ static BOOL LoadBitmap(SDL_RWops *fp, Surface_Ids surf_no, BOOL create_surface)
}
}
Backend_UnlockSurface(surf[surf_no].backend);
Backend_UnlockSurface(surf[surf_no]);
SDL_FreeSurface(converted_surface);
success = TRUE;
}
@ -362,7 +359,7 @@ void BackupSurface(Surface_Ids surf_no, const RECT *rect)
RECT frameRect;
ScaleRect(rect, &frameRect);
Backend_ScreenToSurface(surf[surf_no].backend, &frameRect);
Backend_Blit(surf[surf_no], &frameRect, framebuffer, frameRect.left, frameRect.top, FALSE);
}
static void DrawBitmap(const RECT *rcView, int x, int y, const RECT *rect, Surface_Ids surf_no, BOOL transparent)
@ -402,7 +399,7 @@ static void DrawBitmap(const RECT *rcView, int x, int y, const RECT *rect, Surfa
frameRect.bottom *= magnification;
// Draw to screen
Backend_BlitToScreen(surf[surf_no].backend, &frameRect, x * magnification, y * magnification, transparent);
Backend_Blit(surf[surf_no], &frameRect, framebuffer, x * magnification, y * magnification, transparent);
}
void PutBitmap3(const RECT *rcView, int x, int y, const RECT *rect, Surface_Ids surf_no) // Transparency
@ -421,7 +418,7 @@ void Surface2Surface(int x, int y, const RECT *rect, int to, int from)
RECT frameRect;
ScaleRect(rect, &frameRect);
Backend_BlitToSurface(surf[from].backend, &frameRect, surf[to].backend, x * magnification, y * magnification);
Backend_Blit(surf[from], &frameRect, surf[to], x * magnification, y * magnification, TRUE);
}
unsigned long GetCortBoxColor(unsigned long col)
@ -442,7 +439,7 @@ void CortBox(const RECT *rect, unsigned long col)
const unsigned char col_green = (unsigned char)((col >> 8) & 0xFF);
const unsigned char col_blue = (unsigned char)((col >> 16) & 0xFF);
Backend_ColourFillToScreen(&destRect, col_red, col_green, col_blue);
Backend_ColourFill(framebuffer, &destRect, col_red, col_green, col_blue);
}
void CortBox2(const RECT *rect, unsigned long col, Surface_Ids surf_no)
@ -456,7 +453,7 @@ void CortBox2(const RECT *rect, unsigned long col, Surface_Ids surf_no)
const unsigned char col_green = (unsigned char)((col >> 8) & 0xFF);
const unsigned char col_blue = (unsigned char)((col >> 16) & 0xFF);
Backend_ColourFillToSurface(surf[surf_no].backend, &destRect, col_red, col_green, col_blue);
Backend_ColourFill(surf[surf_no], &destRect, col_red, col_green, col_blue);
}
#ifdef WINDOWS
@ -560,12 +557,12 @@ void InitTextObject(const char *font_name)
void PutText(int x, int y, const char *text, unsigned long color)
{
DrawText(gFont, NULL, x * magnification, y * magnification, color, text, strlen(text));
DrawText(gFont, framebuffer, x * magnification, y * magnification, color, text, strlen(text));
}
void PutText2(int x, int y, const char *text, unsigned long color, Surface_Ids surf_no)
{
DrawText(gFont, surf[surf_no].backend, x * magnification, y * magnification, color, text, strlen(text));
DrawText(gFont, surf[surf_no], x * magnification, y * magnification, color, text, strlen(text));
}
void EndTextObject()

View file

@ -50,10 +50,6 @@ typedef enum Surface_Ids
SURFACE_ID_MAX = 40
} Surface_Ids;
struct SURFACE;
extern SURFACE surf[SURFACE_ID_MAX];
BOOL Flip_SystemTask(HWND hWnd);
SDL_Window* CreateWindow(const char *title, int width, int height);
BOOL StartDirectDraw(int lMagnification, int lColourDepth);

View file

@ -1154,12 +1154,7 @@ void DrawText(FontObject *font_object, Backend_Surface *surface, int x, int y, u
const int letter_y = y + glyph->y;
if (glyph->backend)
{
if (surface)
Backend_DrawGlyphToSurface(surface, glyph->backend, letter_x, letter_y, colours);
else
Backend_DrawGlyphToScreen(glyph->backend, letter_x, letter_y, colours);
}
Backend_DrawGlyph(surface, glyph->backend, letter_x, letter_y, colours);
pen_x += glyph->x_advance;
}