shithub: cstory

Download patch

ref: 4e239c3175e80e39bdc6346cf46d274fe02ba7b2
parent: 0dc9bb6b1bb6cd6d41428e42014ac9bbd70cf556
author: Clownacy <Clownacy@users.noreply.github.com>
date: Fri Sep 6 15:07:49 EDT 2019

Restore the rendering backend callbacks

Now the SDLSurface backend survives window resizes (also triggered by
alt-tabbing while in fullscreen), and the SDLTexture backend properly
regenerates its textures after a fullscreen alt-tab in DirectX mode.

--- a/src/Backends/Rendering.h
+++ b/src/Backends/Rendering.h
@@ -20,6 +20,7 @@
 void Backend_DrawScreen(void);
 Backend_Surface* Backend_CreateSurface(unsigned int width, unsigned int height);
 void Backend_FreeSurface(Backend_Surface *surface);
+BOOL Backend_IsSurfaceLost(Backend_Surface *surface);
 unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch);
 void Backend_UnlockSurface(Backend_Surface *surface);
 void Backend_Blit(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, long x, long y, BOOL colour_key);
@@ -28,5 +29,5 @@
 Backend_Glyph* Backend_LoadGlyph(const unsigned char *pixels, unsigned int width, unsigned int height, int pitch, FontPixelMode pixel_mode);
 void Backend_UnloadGlyph(Backend_Glyph *glyph);
 void Backend_DrawGlyph(Backend_Surface *surface, Backend_Glyph *glyph, long x, long y, const unsigned char *colours);
-void Backend_HandleDeviceLoss(void);
+void Backend_HandleRenderTargetLoss(void);
 void Backend_HandleWindowResize(void);
--- a/src/Backends/Rendering/OpenGL3.cpp
+++ b/src/Backends/Rendering/OpenGL3.cpp
@@ -463,6 +463,11 @@
 	free(surface);
 }
 
+BOOL Backend_IsSurfaceLost(Backend_Surface *surface)
+{
+	return FALSE;
+}
+
 unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch)
 {
 	if (surface == NULL)
@@ -786,7 +791,7 @@
 	vertex_buffer_slot->vertices[1][2].vertex_coordinate.y = vertex_bottom;
 }
 
-void Backend_HandleDeviceLoss(void)
+void Backend_HandleRenderTargetLoss(void)
 {
 	// No problem for us
 }
--- a/src/Backends/Rendering/SDLSurface.cpp
+++ b/src/Backends/Rendering/SDLSurface.cpp
@@ -93,6 +93,11 @@
 	free(surface);
 }
 
+BOOL Backend_IsSurfaceLost(Backend_Surface *surface)
+{
+	return FALSE;
+}
+
 unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch)
 {
 	if (surface == NULL)
@@ -226,7 +231,7 @@
 	SDL_BlitSurface(glyph->sdlsurface, NULL, surface->sdlsurface, &rect);
 }
 
-void Backend_HandleDeviceLoss(void)
+void Backend_HandleRenderTargetLoss(void)
 {
 	// No problem for us
 }
--- a/src/Backends/Rendering/SDLTexture.cpp
+++ b/src/Backends/Rendering/SDLTexture.cpp
@@ -18,6 +18,10 @@
 	unsigned char *pixels;
 	unsigned int width;
 	unsigned int height;
+	BOOL lost;
+
+	struct Backend_Surface *next;
+	struct Backend_Surface *prev;
 } Backend_Surface;
 
 typedef struct Backend_Glyph
@@ -31,6 +35,8 @@
 
 static Backend_Surface framebuffer;
 
+static Backend_Surface *surface_list_head;
+
 static void RectToSDLRect(const RECT *rect, SDL_Rect *sdl_rect)
 {
 	sdl_rect->x = (int)rect->left;
@@ -103,7 +109,13 @@
 
 	surface->width = width;
 	surface->height = height;
+	surface->lost = FALSE;
 
+	// Add to linked-list
+	surface->prev = NULL;
+	surface->next = surface_list_head;
+	surface_list_head = surface;
+
 	return surface;
 }
 
@@ -112,10 +124,21 @@
 	if (surface == NULL)
 		return;
 
+	// Remove from linked list
+	if (surface->next != NULL)
+		surface->next->prev = surface->prev;
+	if (surface->prev != NULL)
+		surface->prev->next = surface->next;
+
 	SDL_DestroyTexture(surface->texture);
 	free(surface);
 }
 
+BOOL Backend_IsSurfaceLost(Backend_Surface *surface)
+{
+	return surface->lost;
+}
+
 unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch)
 {
 	if (surface == NULL)
@@ -305,14 +328,10 @@
 	SDL_RenderCopy(renderer, glyph->texture, NULL, &destination_rect);
 }
 
-void Backend_HandleDeviceLoss(void)
+void Backend_HandleRenderTargetLoss(void)
 {
-	// TODO - RestoreSurfaces
-	// All of our target-textures have been lost, so regenerate them
-//	RestoreSurfaces();
-//	RestoreStripper();
-//	RestoreMapName();
-//	RestoreTextScript();
+	for (Backend_Surface *surface = surface_list_head; surface != NULL; surface = surface->next)
+		surface->lost = TRUE;
 }
 
 void Backend_HandleWindowResize(void)
--- a/src/Backends/Rendering/Software.cpp
+++ b/src/Backends/Rendering/Software.cpp
@@ -100,6 +100,11 @@
 	free(surface);
 }
 
+BOOL Backend_IsSurfaceLost(Backend_Surface *surface)
+{
+	return FALSE;
+}
+
 unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch)
 {
 	if (surface == NULL)
@@ -398,7 +403,7 @@
 	}
 }
 
-void Backend_HandleDeviceLoss(void)
+void Backend_HandleRenderTargetLoss(void)
 {
 	// No problem for us
 }
--- a/src/Draw.cpp
+++ b/src/Draw.cpp
@@ -523,7 +523,7 @@
 
 	Backend_ColourFill(surf[surf_no], &dst_rect, red, green, blue);
 }
-/*
+
 BOOL DummiedOutLogFunction(int unknown)
 {
 	char unknown2[0x100];
@@ -545,23 +545,12 @@
 	RECT rect;
 	int surfaces_regenerated = 0;
 
-	if (frontbuffer == NULL)
+	if (framebuffer == NULL)
 		return surfaces_regenerated;
 
-	if (backbuffer == NULL)
-		return surfaces_regenerated;
-
-	if (frontbuffer->IsLost() == DDERR_SURFACELOST)
+	if (Backend_IsSurfaceLost(framebuffer))
 	{
 		++surfaces_regenerated;
-		frontbuffer->Restore();
-		DummiedOutLogFunction(0x66);
-	}
-
-	if (backbuffer->IsLost() == DDERR_SURFACELOST)
-	{
-		++surfaces_regenerated;
-		backbuffer->Restore();
 		DummiedOutLogFunction(0x62);
 	}
 
@@ -569,10 +558,9 @@
 	{
 		if (surf[s] != NULL)
 		{
-			if (surf[s]->IsLost() == DDERR_SURFACELOST)
+			if (Backend_IsSurfaceLost(surf[s]))
 			{
 				++surfaces_regenerated;
-				surf[s]->Restore();
 				DummiedOutLogFunction(0x30 + s);
 
 				if (!surface_metadata[s].bSystem)
@@ -602,7 +590,7 @@
 
 	return surfaces_regenerated;
 }
-*/
+
 // TODO - Inaccurate stack frame
 void InitTextObject(const char *name)
 {
--- a/src/Main.cpp
+++ b/src/Main.cpp
@@ -8,6 +8,7 @@
 
 #include "WindowsWrapper.h"
 
+#include "Backends/Rendering.h"
 #include "CommonDefines.h"
 #include "Config.h"
 #include "Draw.h"
@@ -558,6 +559,11 @@
 					case SDL_WINDOWEVENT_FOCUS_GAINED:
 						ActiveWindow();
 						break;
+
+					case SDL_WINDOWEVENT_RESIZED:
+					case SDL_WINDOWEVENT_SIZE_CHANGED:
+						Backend_HandleWindowResize();
+						break;
 				}
 
 				break;
@@ -565,6 +571,11 @@
 			case SDL_QUIT:
 				StopOrganyaMusic();
 				return FALSE;
+
+			case SDL_RENDER_TARGETS_RESET:
+				Backend_HandleRenderTargetLoss();
+				break;
+
 		}
 	}
 
--