shithub: cstory

Download patch

ref: 6a4f4e0df32eb1790986ac0871d4e8fd2c608c05
parent: b84661d88ae69f30e9ade7af6422870d5c590a85
author: Clownacy <Clownacy@users.noreply.github.com>
date: Mon Jul 15 13:47:22 EDT 2019

Added handlers for render target loss/window resize

These only really happen when you use exclusive fullscreen and
alt-tab out. Or, at least, it does on Windows with SDL2 in DirectX
mode.

--- a/src/Backends/Rendering.h
+++ b/src/Backends/Rendering.h
@@ -21,3 +21,5 @@
 void Backend_ScreenToSurface(Backend_Surface *surface, const RECT *rect);
 void Backend_DrawText(Backend_Surface *surface, FontObject *font, int x, int y, const char *text, unsigned long colour);
 void Backend_DrawTextToScreen(FontObject *font, int x, int y, const char *text, unsigned long colour);
+void Backend_HandleDeviceLoss(void);
+void Backend_HandleWindowResize(void);
--- a/src/Backends/Rendering/SDLTexture.cpp
+++ b/src/Backends/Rendering/SDLTexture.cpp
@@ -14,11 +14,16 @@
 	BOOL needs_syncing;
 	SDL_Surface *sdl_surface;
 	SDL_Texture *texture;
+
+	struct Backend_Surface *next;
+	struct Backend_Surface *prev;
 };
 
 static SDL_Renderer *renderer;
 static SDL_Texture *screen_texture;
 
+static Backend_Surface *surface_list_head;
+
 static void FlushSurface(Backend_Surface *surface)
 {
 	unsigned char *buffer = (unsigned char*)malloc(surface->sdl_surface->w * surface->sdl_surface->h * 4);
@@ -120,11 +125,21 @@
 
 	surface->needs_syncing = FALSE;
 
+	surface->next = surface_list_head;
+	if (surface->next)
+		surface->next->prev = surface;
+	surface_list_head = surface;
+
 	return surface;
 }
 
 void Backend_FreeSurface(Backend_Surface *surface)
 {
+	if (surface->next)
+		surface->next->prev = surface->prev;
+	if (surface->prev)
+		surface->prev->next = surface->next;
+
 	SDL_FreeSurface(surface->sdl_surface);
 	free(surface);
 }
@@ -273,4 +288,16 @@
 	SDL_FreeSurface(screen_surface);
 	SDL_RenderCopy(renderer, texture, NULL, NULL);
 	SDL_DestroyTexture(texture);
+}
+
+void Backend_HandleDeviceLoss(void)
+{
+	// All of our textures have been lost, so regenerate them
+	for (Backend_Surface *surface = surface_list_head; surface != NULL; surface = surface->next)
+		surface->needs_syncing = TRUE;
+}
+
+void Backend_HandleWindowResize(void)
+{
+	// No problem for us
 }
--- a/src/Backends/Rendering/Software.cpp
+++ b/src/Backends/Rendering/Software.cpp
@@ -83,10 +83,10 @@
 
 void Backend_LoadPixels(Backend_Surface *surface, const unsigned char *pixels, unsigned int width, unsigned int height, unsigned int pitch)
 {
-	for (unsigned int h = 0; h < height; ++h)
+	for (unsigned int i = 0; i < height; ++i)
 	{
-		const unsigned char *src_row = &pixels[h * pitch];
-		unsigned char *dst_row = &surface->pixels[h * surface->pitch];
+		const unsigned char *src_row = &pixels[i * pitch];
+		unsigned char *dst_row = &surface->pixels[i * surface->pitch];
 
 		memcpy(dst_row, src_row, width * 3);
 	}
@@ -231,10 +231,21 @@
 void Backend_DrawText(Backend_Surface *surface, FontObject *font, int x, int y, const char *text, unsigned long colour)
 {
 	DrawText(font, surface->pixels, surface->pitch, surface->width, surface->height, x, y, colour, text, strlen(text));
-	//surf[surf_no].needs_updating = TRUE;
 }
 
 void Backend_DrawTextToScreen(FontObject *font, int x, int y, const char *text, unsigned long colour)
 {
 	Backend_DrawText(&framebuffer, font, x, y, text, colour);
+}
+
+void Backend_HandleDeviceLoss(void)
+{
+	// No problem for us
+}
+
+void Backend_HandleWindowResize(void)
+{
+	// https://wiki.libsdl.org/SDL_GetWindowSurface
+	// We need to fetch a new surface pointer
+	window_surface = SDL_GetWindowSurface(window);
 }
--- a/src/Draw.cpp
+++ b/src/Draw.cpp
@@ -569,3 +569,13 @@
 	UnloadFont(gFont);
 	gFont = NULL;
 }
+
+void HandleDeviceLoss()
+{
+	Backend_HandleDeviceLoss();
+}
+
+void HandleWindowResize()
+{
+	Backend_HandleWindowResize();
+}
--- a/src/Draw.h
+++ b/src/Draw.h
@@ -72,3 +72,5 @@
 void PutText(int x, int y, const char *text, unsigned long color);
 void PutText2(int x, int y, const char *text, unsigned long color, Surface_Ids surf_no);
 void EndTextObject();
+void HandleDeviceLoss();
+void HandleWindowResize();
--- a/src/Main.cpp
+++ b/src/Main.cpp
@@ -463,9 +463,19 @@
 				return FALSE;
 				break;
 
+			case SDL_RENDER_TARGETS_RESET:
+			case SDL_RENDER_DEVICE_RESET:
+				HandleDeviceLoss();
+				break;
+
 			case SDL_WINDOWEVENT:
 				switch (event.window.event)
 				{
+					case SDL_WINDOWEVENT_RESIZED:
+					case SDL_WINDOWEVENT_SIZE_CHANGED:
+						HandleWindowResize();
+						break;
+
 					case SDL_WINDOWEVENT_FOCUS_GAINED:
 						focusGained = TRUE;
 						ActiveWindow();