shithub: cstory

Download patch

ref: 3f8ead09d6abba46b2bdff8cefc43d6545bcf6bc
parent: e094f2ff6818c0b05cbd559e668295cd307c3e6c
author: Clownacy <Clownacy@users.noreply.github.com>
date: Mon Aug 12 21:31:08 EDT 2019

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

By emulating the DirectDraw code more closely, I can simplify the
renderer backend API.

--- a/src/Backends/Rendering.h
+++ b/src/Backends/Rendering.h
@@ -15,7 +15,7 @@
 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);
@@ -22,15 +22,11 @@
 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);
--- a/src/Backends/Rendering/SDLSurface.cpp
+++ b/src/Backends/Rendering/SDLSurface.cpp
@@ -57,8 +57,10 @@
 	return TRUE;
 }
 
-void Backend_Deinit(void)
+void Backend_Deinit(Backend_Surface *framebuffer_surface)
 {
+	(void)framebuffer_surface;
+
 	SDL_FreeSurface(framebuffer.sdl_surface);
 }
 
--- a/src/Backends/Rendering/Software.cpp
+++ b/src/Backends/Rendering/Software.cpp
@@ -30,9 +30,8 @@
 } 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 @@
 	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)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,18 +197,8 @@
 	}
 }
 
-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;
 
@@ -266,16 +255,6 @@
 	}
 }
 
-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 @@
 	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_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 @@
 {
 	// https://wiki.libsdl.org/SDL_GetWindowSurface
 	// We need to fetch a new surface pointer
-	window_surface = SDL_GetWindowSurface(window);
+	window_sdlsurface = SDL_GetWindowSurface(window);
 }
--- a/src/Draw.cpp
+++ b/src/Draw.cpp
@@ -26,16 +26,11 @@
 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];
+static FontObject *gFont;
 
-FontObject *gFont;
-
 #define FRAMERATE 20
 
 BOOL Flip_SystemTask(HWND hWnd)
@@ -99,8 +94,9 @@
 
 	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 @@
 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 @@
 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,7 +158,7 @@
 	}
 	else
 	{
-		if (surf[surf_no].in_use)
+		if (surf[surf_no])
 		{
 			printf("Tried to create drawable surface at occupied slot (%d)\n", surf_no);
 		}
@@ -163,17 +165,12 @@
 		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;
-			}
 		}
 	}
 
@@ -190,7 +187,7 @@
 	}
 	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 @@
 					{
 						// 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 @@
 							}
 						}
 
-						Backend_UnlockSurface(surf[surf_no].backend);
+						Backend_UnlockSurface(surf[surf_no]);
 						SDL_FreeSurface(converted_surface);
 						success = TRUE;
 					}
@@ -362,7 +359,7 @@
 	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 @@
 	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 @@
 	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 @@
 	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 @@
 	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 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()
--- a/src/Draw.h
+++ b/src/Draw.h
@@ -50,10 +50,6 @@
 	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);
--- a/src/Font.cpp
+++ b/src/Font.cpp
@@ -1154,12 +1154,7 @@
 				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;
 			}