shithub: cstory

Download patch

ref: c4aa8e28bb201ed52ae6203aa396b762c88c229b
parent: 073712017f33838f6270a108fc11c5099d6a0783
author: Clownacy <Clownacy@users.noreply.github.com>
date: Wed Apr 1 10:57:07 EDT 2020

More refactoring

--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -17,7 +17,6 @@
 option(DEBUG_SAVE "Re-enable the ability to drag-and-drop save files onto the window" OFF)
 set(BACKEND_RENDERER "SDLTexture" CACHE STRING "Which renderer the game should use: 'OpenGL3' for an OpenGL 3.2 renderer, 'OpenGLES2' for an OpenGL ES 2.0 renderer, 'SDLTexture' for SDL2's hardware-accelerated Texture API, 'SDLSurface' for SDL2's software-rendered Surface API, or 'Software' for a handwritten software renderer")
 set(BACKEND_AUDIO "SDL2" CACHE STRING "Which audio backend the game should use: 'SDL2' or 'miniaudio'")
-set(BACKEND_CONTROLLER "SDL2" CACHE STRING "Which controller backend the game should use: 'SDL2' or 'GLFW3'")
 set(BACKEND_PLATFORM "SDL2" CACHE STRING "Which platform backend the game should use: 'SDL2' or 'GLFW3'")
 
 option(LTO "Enable link-time optimisation" OFF)
@@ -320,16 +319,10 @@
 	message(FATAL_ERROR "Invalid BACKEND_AUDIO selected")
 endif()
 
-if(BACKEND_CONTROLLER MATCHES "SDL2")
-	target_sources(CSE2 PRIVATE "src/Backends/Controller/SDL2.cpp")
-elseif(BACKEND_CONTROLLER MATCHES "GLFW3")
-	target_sources(CSE2 PRIVATE "src/Backends/Controller/GLFW3.cpp")
-endif()
-
 if(BACKEND_PLATFORM MATCHES "SDL2")
-	target_sources(CSE2 PRIVATE "src/Backends/Platform/SDL2.cpp")
+	target_sources(CSE2 PRIVATE "src/Backends/Platform/SDL2.cpp" "src/Backends/Controller/SDL2.cpp")
 elseif(BACKEND_PLATFORM MATCHES "GLFW3")
-	target_sources(CSE2 PRIVATE "src/Backends/Platform/GLFW3.cpp")
+	target_sources(CSE2 PRIVATE "src/Backends/Platform/GLFW3.cpp" "src/Backends/Controller/GLFW3.cpp")
 endif()
 
 if(BACKEND_PLATFORM MATCHES "SDL2" AND BACKEND_RENDERER MATCHES "OpenGL3")
--- a/src/Backends/Platform/GLFW3.cpp
+++ b/src/Backends/Platform/GLFW3.cpp
@@ -210,7 +210,7 @@
 	(void)width;
 	(void)height;
 
-	RenderBackend_HandleWindowResize();
+	Backend_HandleWindowResize();
 }
 
 void PlatformBackend_Init(void)
--- a/src/Backends/Platform/SDL2.cpp
+++ b/src/Backends/Platform/SDL2.cpp
@@ -2,7 +2,7 @@
 
 #include "SDL.h"
 
-#include "../Window.h"
+#include "../Rendering.h"
 
 #include "../../WindowsWrapper.h"
 
--- a/src/Backends/Rendering.h
+++ b/src/Backends/Rendering.h
@@ -5,22 +5,22 @@
 typedef struct Backend_Surface Backend_Surface;
 typedef struct Backend_Glyph Backend_Glyph;
 
-Backend_Surface* RenderBackend_Init(int screen_width, int screen_height);
-void RenderBackend_Deinit(void);
-void RenderBackend_DrawScreen(void);
-void RenderBackend_ClearScreen(void);
-Backend_Surface* RenderBackend_CreateSurface(unsigned int width, unsigned int height);
-void RenderBackend_FreeSurface(Backend_Surface *surface);
-BOOL RenderBackend_IsSurfaceLost(Backend_Surface *surface);
-void RenderBackend_RestoreSurface(Backend_Surface *surface);
-unsigned char* RenderBackend_LockSurface(Backend_Surface *surface, unsigned int *pitch, unsigned int width, unsigned int height);
-void RenderBackend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigned int height);
-void RenderBackend_Blit(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, long x, long y, BOOL colour_key);
-void RenderBackend_ColourFill(Backend_Surface *surface, const RECT *rect, unsigned char red, unsigned char green, unsigned char blue);
-Backend_Glyph* RenderBackend_LoadGlyph(const unsigned char *pixels, unsigned int width, unsigned int height, int pitch);
-void RenderBackend_UnloadGlyph(Backend_Glyph *glyph);
-void RenderBackend_PrepareToDrawGlyphs(Backend_Surface *destination_surface, const unsigned char *colour_channels);
-void RenderBackend_DrawGlyph(Backend_Glyph *glyph, long x, long y);
-void RenderBackend_FlushGlyphs(void);
-void RenderBackend_HandleRenderTargetLoss(void);
-void RenderBackend_HandleWindowResize(void);
+Backend_Surface* Backend_Init(const char *window_title, int screen_width, int screen_height, BOOL fullscreen);
+void Backend_Deinit(void);
+void Backend_DrawScreen(void);
+void Backend_ClearScreen(void);
+Backend_Surface* Backend_CreateSurface(unsigned int width, unsigned int height);
+void Backend_FreeSurface(Backend_Surface *surface);
+BOOL Backend_IsSurfaceLost(Backend_Surface *surface);
+void Backend_RestoreSurface(Backend_Surface *surface);
+unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch, unsigned int width, unsigned int height);
+void Backend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigned int height);
+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);
+Backend_Glyph* Backend_LoadGlyph(const unsigned char *pixels, unsigned int width, unsigned int height, int pitch);
+void Backend_UnloadGlyph(Backend_Glyph *glyph);
+void Backend_PrepareToDrawGlyphs(Backend_Surface *destination_surface, const unsigned char *colour_channels);
+void Backend_DrawGlyph(Backend_Glyph *glyph, long x, long y);
+void Backend_FlushGlyphs(void);
+void Backend_HandleRenderTargetLoss(void);
+void Backend_HandleWindowResize(void);
--- a/src/Backends/Rendering/OpenGL3.cpp
+++ b/src/Backends/Rendering/OpenGL3.cpp
@@ -513,108 +513,111 @@
 // Render-backend initialisation
 // ====================
 
-Backend_Surface* RenderBackend_Init(int screen_width, int screen_height)
+Backend_Surface* Backend_Init(const char *window_title, int screen_width, int screen_height, BOOL fullscreen)
 {
-	printf("GL_VENDOR = %s\n", glGetString(GL_VENDOR));
-	printf("GL_RENDERER = %s\n", glGetString(GL_RENDERER));
-	printf("GL_VERSION = %s\n", glGetString(GL_VERSION));
+	if (WindowBackend_OpenGL_CreateWindow(window_title, screen_width, screen_height, fullscreen))
+	{
+		printf("GL_VENDOR = %s\n", glGetString(GL_VENDOR));
+		printf("GL_RENDERER = %s\n", glGetString(GL_RENDERER));
+		printf("GL_VERSION = %s\n", glGetString(GL_VERSION));
 
-	// Set up blending (only used for font-rendering)
-	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+		// Set up blending (only used for font-rendering)
+		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
-	//glEnable(GL_DEBUG_OUTPUT);
-	//glDebugMessageCallback(MessageCallback, 0);
+		//glEnable(GL_DEBUG_OUTPUT);
+		//glDebugMessageCallback(MessageCallback, 0);
 
-	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
-	glClear(GL_COLOR_BUFFER_BIT);
+		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+		glClear(GL_COLOR_BUFFER_BIT);
 
-#ifndef USE_OPENGLES2
-	// Set up Vertex Array Object
-	glGenVertexArrays(1, &vertex_array_id);
-	glBindVertexArray(vertex_array_id);
-#endif
+	#ifndef USE_OPENGLES2
+		// Set up Vertex Array Object
+		glGenVertexArrays(1, &vertex_array_id);
+		glBindVertexArray(vertex_array_id);
+	#endif
 
-	// Set up Vertex Buffer Objects
-	glGenBuffers(TOTAL_VBOS, vertex_buffer_ids);
+		// Set up Vertex Buffer Objects
+		glGenBuffers(TOTAL_VBOS, vertex_buffer_ids);
 
-	// Set up the vertex attributes
-	glEnableVertexAttribArray(ATTRIBUTE_INPUT_VERTEX_COORDINATES);
+		// Set up the vertex attributes
+		glEnableVertexAttribArray(ATTRIBUTE_INPUT_VERTEX_COORDINATES);
 
-	// Set up our shaders
-	program_texture = CompileShader(vertex_shader_texture, fragment_shader_texture);
-	program_texture_colour_key = CompileShader(vertex_shader_texture, fragment_shader_texture_colour_key);
-	program_colour_fill = CompileShader(vertex_shader_plain, fragment_shader_colour_fill);
-	program_glyph = CompileShader(vertex_shader_texture, fragment_shader_glyph);
+		// Set up our shaders
+		program_texture = CompileShader(vertex_shader_texture, fragment_shader_texture);
+		program_texture_colour_key = CompileShader(vertex_shader_texture, fragment_shader_texture_colour_key);
+		program_colour_fill = CompileShader(vertex_shader_plain, fragment_shader_colour_fill);
+		program_glyph = CompileShader(vertex_shader_texture, fragment_shader_glyph);
 
-	if (program_texture != 0 && program_texture_colour_key != 0 && program_colour_fill != 0 && program_glyph != 0)
-	{
-		// Get shader uniforms
-		program_colour_fill_uniform_colour = glGetUniformLocation(program_colour_fill, "colour");
-		program_glyph_uniform_colour = glGetUniformLocation(program_glyph, "colour");
+		if (program_texture != 0 && program_texture_colour_key != 0 && program_colour_fill != 0 && program_glyph != 0)
+		{
+			// Get shader uniforms
+			program_colour_fill_uniform_colour = glGetUniformLocation(program_colour_fill, "colour");
+			program_glyph_uniform_colour = glGetUniformLocation(program_glyph, "colour");
 
-		// Set up framebuffer (used for surface-to-surface blitting)
-		glGenFramebuffers(1, &framebuffer_id);
-		glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id);
+			// Set up framebuffer (used for surface-to-surface blitting)
+			glGenFramebuffers(1, &framebuffer_id);
+			glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id);
 
-		// Set up framebuffer screen texture (used for screen-to-surface blitting)
-		glGenTextures(1, &framebuffer.texture_id);
-		glBindTexture(GL_TEXTURE_2D, framebuffer.texture_id);
-	#ifdef USE_OPENGLES2
-		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, screen_width, screen_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
-	#else
-		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, screen_width, screen_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
-	#endif
-		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
-		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-	#ifndef USE_OPENGLES2
-		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
-	#endif
+			// Set up framebuffer screen texture (used for screen-to-surface blitting)
+			glGenTextures(1, &framebuffer.texture_id);
+			glBindTexture(GL_TEXTURE_2D, framebuffer.texture_id);
+		#ifdef USE_OPENGLES2
+			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, screen_width, screen_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
+		#else
+			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, screen_width, screen_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
+		#endif
+			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+		#ifndef USE_OPENGLES2
+			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
+		#endif
 
-		framebuffer.width = screen_width;
-		framebuffer.height = screen_height;
+			framebuffer.width = screen_width;
+			framebuffer.height = screen_height;
 
-		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, framebuffer.texture_id, 0);
-		glViewport(0, 0, framebuffer.width, framebuffer.height);
+			glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, framebuffer.texture_id, 0);
+			glViewport(0, 0, framebuffer.width, framebuffer.height);
 
-		// Set-up glyph-batcher
-		spritebatch_config_t config;
-		spritebatch_set_default_config(&config);
-		config.pixel_stride = 1;
-		config.atlas_width_in_pixels = 256;
-		config.atlas_height_in_pixels = 256;
-		config.lonely_buffer_count_till_flush = 4; // Start making atlases immediately
-		config.batch_callback = GlyphBatch_Draw;
-		config.get_pixels_callback = GlyphBatch_GetPixels;
-		config.generate_texture_callback = GlyphBatch_CreateTexture;
-		config.delete_texture_callback = GlyphBatch_DestroyTexture;
-		spritebatch_init(&glyph_batcher, &config, NULL);
+			// Set-up glyph-batcher
+			spritebatch_config_t config;
+			spritebatch_set_default_config(&config);
+			config.pixel_stride = 1;
+			config.atlas_width_in_pixels = 256;
+			config.atlas_height_in_pixels = 256;
+			config.lonely_buffer_count_till_flush = 4; // Start making atlases immediately
+			config.batch_callback = GlyphBatch_Draw;
+			config.get_pixels_callback = GlyphBatch_GetPixels;
+			config.generate_texture_callback = GlyphBatch_CreateTexture;
+			config.delete_texture_callback = GlyphBatch_DestroyTexture;
+			spritebatch_init(&glyph_batcher, &config, NULL);
 
-		return &framebuffer;
-	}
+			return &framebuffer;
+		}
 
-	if (program_glyph != 0)
-		glDeleteProgram(program_glyph);
+		if (program_glyph != 0)
+			glDeleteProgram(program_glyph);
 
-	if (program_colour_fill != 0)
-		glDeleteProgram(program_colour_fill);
+		if (program_colour_fill != 0)
+			glDeleteProgram(program_colour_fill);
 
-	if (program_texture_colour_key != 0)
-		glDeleteProgram(program_texture_colour_key);
+		if (program_texture_colour_key != 0)
+			glDeleteProgram(program_texture_colour_key);
 
-	if (program_texture != 0)
-		glDeleteProgram(program_texture);
+		if (program_texture != 0)
+			glDeleteProgram(program_texture);
 
-	glDeleteBuffers(TOTAL_VBOS, vertex_buffer_ids);
-#ifndef USE_OPENGLES2
-	glDeleteVertexArrays(1, &vertex_array_id);
-#endif
+		glDeleteBuffers(TOTAL_VBOS, vertex_buffer_ids);
+	#ifndef USE_OPENGLES2
+		glDeleteVertexArrays(1, &vertex_array_id);
+	#endif
+	}
 
 	return NULL;
 }
 
-void RenderBackend_Deinit(void)
+void Backend_Deinit(void)
 {
 	free(local_vertex_buffer);
 
@@ -630,9 +633,11 @@
 #ifndef USE_OPENGLES2
 	glDeleteVertexArrays(1, &vertex_array_id);
 #endif
+
+	WindowBackend_OpenGL_DestroyWindow();
 }
 
-void RenderBackend_DrawScreen(void)
+void Backend_DrawScreen(void)
 {
 	spritebatch_tick(&glyph_batcher);
 
@@ -688,15 +693,14 @@
 
 	FlushVertexBuffer();
 
-	// Switch back to our framebuffer
-	glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id);
-}
+	WindowBackend_OpenGL_Display();
 
-void RenderBackend_ClearScreen(void)
-{
 	// According to https://www.khronos.org/opengl/wiki/Common_Mistakes#Swap_Buffers
 	// the buffer should always be cleared, even if it seems unnecessary
 	glClear(GL_COLOR_BUFFER_BIT);
+
+	// Switch back to our framebuffer
+	glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id);
 }
 
 // ====================
@@ -703,7 +707,7 @@
 // Surface management
 // ====================
 
-Backend_Surface* RenderBackend_CreateSurface(unsigned int width, unsigned int height)
+Backend_Surface* Backend_CreateSurface(unsigned int width, unsigned int height)
 {
 	Backend_Surface *surface = (Backend_Surface*)malloc(sizeof(Backend_Surface));
 
@@ -733,7 +737,7 @@
 	return surface;
 }
 
-void RenderBackend_FreeSurface(Backend_Surface *surface)
+void Backend_FreeSurface(Backend_Surface *surface)
 {
 	if (surface == NULL)
 		return;
@@ -746,7 +750,7 @@
 	free(surface);
 }
 
-BOOL RenderBackend_IsSurfaceLost(Backend_Surface *surface)
+BOOL Backend_IsSurfaceLost(Backend_Surface *surface)
 {
 	(void)surface;
 
@@ -753,12 +757,12 @@
 	return FALSE;
 }
 
-void RenderBackend_RestoreSurface(Backend_Surface *surface)
+void Backend_RestoreSurface(Backend_Surface *surface)
 {
 	(void)surface;
 }
 
-unsigned char* RenderBackend_LockSurface(Backend_Surface *surface, unsigned int *pitch, unsigned int width, unsigned int height)
+unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch, unsigned int width, unsigned int height)
 {
 	if (surface == NULL)
 		return NULL;
@@ -768,7 +772,7 @@
 	return surface->pixels;
 }
 
-void RenderBackend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigned int height)
+void Backend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigned int height)
 {
 	if (surface == NULL)
 		return;
@@ -788,7 +792,7 @@
 // Drawing
 // ====================
 
-void RenderBackend_Blit(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;
@@ -864,7 +868,7 @@
 	vertex_buffer_slot->vertices[1][2].vertex_coordinate.y = vertex_bottom;
 }
 
-void RenderBackend_ColourFill(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)
 {
 	static unsigned char last_red;
 	static unsigned char last_green;
@@ -929,7 +933,7 @@
 // Glyph management
 // ====================
 
-Backend_Glyph* RenderBackend_LoadGlyph(const unsigned char *pixels, unsigned int width, unsigned int height, int pitch)
+Backend_Glyph* Backend_LoadGlyph(const unsigned char *pixels, unsigned int width, unsigned int height, int pitch)
 {
 	Backend_Glyph *glyph = (Backend_Glyph*)malloc(sizeof(Backend_Glyph));
 
@@ -960,7 +964,7 @@
 	return NULL;
 }
 
-void RenderBackend_UnloadGlyph(Backend_Glyph *glyph)
+void Backend_UnloadGlyph(Backend_Glyph *glyph)
 {
 	if (glyph == NULL)
 		return;
@@ -969,7 +973,7 @@
 	free(glyph);
 }
 
-void RenderBackend_PrepareToDrawGlyphs(Backend_Surface *destination_surface, const unsigned char *colour_channels)
+void Backend_PrepareToDrawGlyphs(Backend_Surface *destination_surface, const unsigned char *colour_channels)
 {
 	glyph_destination_surface = destination_surface;
 
@@ -976,12 +980,12 @@
 	memcpy(glyph_colour_channels, colour_channels, sizeof(glyph_colour_channels));
 }
 
-void RenderBackend_DrawGlyph(Backend_Glyph *glyph, long x, long y)
+void Backend_DrawGlyph(Backend_Glyph *glyph, long x, long y)
 {
 	spritebatch_push(&glyph_batcher, (SPRITEBATCH_U64)glyph, glyph->pitch, glyph->height, x, y, 1.0f, 1.0f, 0.0f, 0.0f, 0);
 }
 
-void RenderBackend_FlushGlyphs(void)
+void Backend_FlushGlyphs(void)
 {
 	spritebatch_defrag(&glyph_batcher);
 	spritebatch_flush(&glyph_batcher);
@@ -991,12 +995,12 @@
 // Misc.
 // ====================
 
-void RenderBackend_HandleRenderTargetLoss(void)
+void Backend_HandleRenderTargetLoss(void)
 {
 	// No problem for us
 }
 
-void RenderBackend_HandleWindowResize(void)
+void Backend_HandleWindowResize(void)
 {
 	// No problem for us
 }
--- a/src/Backends/Window.h
+++ b/src/Backends/Window.h
@@ -2,24 +2,6 @@
 
 #include "../WindowsWrapper.h"
 
-#include "Rendering.h"
-
-Backend_Surface* Backend_Init(const char *window_title, int screen_width, int screen_height, BOOL fullscreen);
-void Backend_Deinit(void);
-void Backend_DrawScreen(void);
-void Backend_ClearScreen(void);
-Backend_Surface* Backend_CreateSurface(unsigned int width, unsigned int height);
-void Backend_FreeSurface(Backend_Surface *surface);
-BOOL Backend_IsSurfaceLost(Backend_Surface *surface);
-void Backend_RestoreSurface(Backend_Surface *surface);
-unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch, unsigned int width, unsigned int height);
-void Backend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigned int height);
-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);
-Backend_Glyph* Backend_LoadGlyph(const unsigned char *pixels, unsigned int width, unsigned int height, int pitch);
-void Backend_UnloadGlyph(Backend_Glyph *glyph);
-void Backend_PrepareToDrawGlyphs(Backend_Surface *destination_surface, const unsigned char *colour_channels);
-void Backend_DrawGlyph(Backend_Glyph *glyph, long x, long y);
-void Backend_FlushGlyphs(void);
-void Backend_HandleRenderTargetLoss(void);
-void Backend_HandleWindowResize(void);
+BOOL WindowBackend_OpenGL_CreateWindow(const char *window_title, int screen_width, int screen_height, BOOL fullscreen);
+void WindowBackend_OpenGL_DestroyWindow(void);
+void WindowBackend_OpenGL_Display(void);
--- a/src/Backends/Window/GLFW3-OpenGL3.cpp
+++ b/src/Backends/Window/GLFW3-OpenGL3.cpp
@@ -22,7 +22,7 @@
 void WindowFocusCallback(GLFWwindow *window, int focused);
 void WindowSizeCallback(GLFWwindow *window, int width, int height);
 
-Backend_Surface* Backend_Init(const char *window_title, int screen_width, int screen_height, BOOL fullscreen)
+BOOL WindowBackend_OpenGL_CreateWindow(const char *window_title, int screen_width, int screen_height, BOOL fullscreen)
 {
 #ifdef USE_OPENGLES2
 	glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
@@ -49,8 +49,16 @@
 		SDL_FreeSurface(icon_surface);
 	#endif
 */
+
 		if (fullscreen)
-			glfwSetWindowMonitor(window, glfwGetPrimaryMonitor(), 0, 0, screen_width, screen_height, GLFW_DONT_CARE);
+		{
+		  GLFWmonitor* monitor = glfwGetPrimaryMonitor();
+            if (monitor)
+            {
+                const GLFWvidmode* mode = glfwGetVideoMode(monitor);
+                glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
+            }
+		}
 
 		glfwMakeContextCurrent(window);
 
@@ -65,7 +73,7 @@
 						glfwSetWindowFocusCallback(window, WindowFocusCallback);
 						glfwSetWindowSizeCallback(window, WindowSizeCallback);
 
-						return RenderBackend_Init(screen_width, screen_height);
+						return TRUE;
 			#ifndef USE_OPENGLES2
 					}
 					else
@@ -86,97 +94,15 @@
 		PlatformBackend_ShowMessageBox("Fatal error (OpenGL rendering backend)", "Could not create window");
 	}
 
-	return NULL;
+	return FALSE;
 }
 
-void Backend_Deinit(void)
+void WindowBackend_OpenGL_DestroyWindow(void)
 {
-	RenderBackend_Deinit();
-
 	glfwDestroyWindow(window);
 }
 
-void Backend_DrawScreen(void)
+void WindowBackend_OpenGL_Display(void)
 {
-	RenderBackend_DrawScreen();
-
 	glfwSwapBuffers(window);
-
-	RenderBackend_ClearScreen();
-}
-
-Backend_Surface* Backend_CreateSurface(unsigned int width, unsigned int height)
-{
-	return RenderBackend_CreateSurface(width, height);
-
-}
-
-void Backend_FreeSurface(Backend_Surface *surface)
-{
-	RenderBackend_FreeSurface(surface);
-}
-
-BOOL Backend_IsSurfaceLost(Backend_Surface *surface)
-{
-	return RenderBackend_IsSurfaceLost(surface);
-}
-
-void Backend_RestoreSurface(Backend_Surface *surface)
-{
-	RenderBackend_RestoreSurface(surface);
-}
-
-unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch, unsigned int width, unsigned int height)
-{
-	return RenderBackend_LockSurface(surface, pitch, width, height);
-}
-
-void Backend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigned int height)
-{
-	RenderBackend_UnlockSurface(surface, width, height);
-}
-
-void Backend_Blit(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, long x, long y, BOOL colour_key)
-{
-	RenderBackend_Blit(source_surface, rect, destination_surface, x, y, colour_key);
-}
-
-void Backend_ColourFill(Backend_Surface *surface, const RECT *rect, unsigned char red, unsigned char green, unsigned char blue)
-{
-	RenderBackend_ColourFill(surface, rect, red, green, blue);
-}
-
-Backend_Glyph* Backend_LoadGlyph(const unsigned char *pixels, unsigned int width, unsigned int height, int pitch)
-{
-	return RenderBackend_LoadGlyph(pixels, width, height, pitch);
-}
-
-void Backend_UnloadGlyph(Backend_Glyph *glyph)
-{
-	RenderBackend_UnloadGlyph(glyph);
-}
-
-void Backend_PrepareToDrawGlyphs(Backend_Surface *destination_surface, const unsigned char *colour_channels)
-{
-	RenderBackend_PrepareToDrawGlyphs(destination_surface, colour_channels);
-}
-
-void Backend_DrawGlyph(Backend_Glyph *glyph, long x, long y)
-{
-	RenderBackend_DrawGlyph(glyph, x, y);
-}
-
-void Backend_FlushGlyphs(void)
-{
-	RenderBackend_FlushGlyphs();
-}
-
-void Backend_HandleRenderTargetLoss(void)
-{
-	RenderBackend_HandleRenderTargetLoss();
-}
-
-void Backend_HandleWindowResize(void)
-{
-	RenderBackend_HandleWindowResize();
 }
--- a/src/Backends/Window/SDL2-OpenGL3.cpp
+++ b/src/Backends/Window/SDL2-OpenGL3.cpp
@@ -17,7 +17,7 @@
 static SDL_Window *window;
 static SDL_GLContext context;
 
-Backend_Surface* Backend_Init(const char *window_title, int screen_width, int screen_height, BOOL fullscreen)
+BOOL WindowBackend_OpenGL_CreateWindow(const char *window_title, int screen_width, int screen_height, BOOL fullscreen)
 {
 	puts("Available SDL2 video drivers:");
 
@@ -67,7 +67,7 @@
 					if (GLAD_GL_VERSION_3_2)
 					{
 			#endif
-						return RenderBackend_Init(screen_width, screen_height);
+						return TRUE;
 			#ifndef USE_OPENGLES2
 					}
 					else
@@ -100,98 +100,16 @@
 		SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error (OpenGL rendering backend)", "Could not create window", NULL);
 	}
 
-	return NULL;
+	return FALSE;
 }
 
-void Backend_Deinit(void)
+void WindowBackend_OpenGL_DestroyWindow(void)
 {
-	RenderBackend_Deinit();
-
 	SDL_GL_DeleteContext(context);
 	SDL_DestroyWindow(window);
 }
 
-void Backend_DrawScreen(void)
+void WindowBackend_OpenGL_Display(void)
 {
-	RenderBackend_DrawScreen();
-
 	SDL_GL_SwapWindow(window);
-
-	RenderBackend_ClearScreen();
-}
-
-Backend_Surface* Backend_CreateSurface(unsigned int width, unsigned int height)
-{
-	return RenderBackend_CreateSurface(width, height);
-
-}
-
-void Backend_FreeSurface(Backend_Surface *surface)
-{
-	RenderBackend_FreeSurface(surface);
-}
-
-BOOL Backend_IsSurfaceLost(Backend_Surface *surface)
-{
-	return RenderBackend_IsSurfaceLost(surface);
-}
-
-void Backend_RestoreSurface(Backend_Surface *surface)
-{
-	RenderBackend_RestoreSurface(surface);
-}
-
-unsigned char* Backend_LockSurface(Backend_Surface *surface, unsigned int *pitch, unsigned int width, unsigned int height)
-{
-	return RenderBackend_LockSurface(surface, pitch, width, height);
-}
-
-void Backend_UnlockSurface(Backend_Surface *surface, unsigned int width, unsigned int height)
-{
-	RenderBackend_UnlockSurface(surface, width, height);
-}
-
-void Backend_Blit(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, long x, long y, BOOL colour_key)
-{
-	RenderBackend_Blit(source_surface, rect, destination_surface, x, y, colour_key);
-}
-
-void Backend_ColourFill(Backend_Surface *surface, const RECT *rect, unsigned char red, unsigned char green, unsigned char blue)
-{
-	RenderBackend_ColourFill(surface, rect, red, green, blue);
-}
-
-Backend_Glyph* Backend_LoadGlyph(const unsigned char *pixels, unsigned int width, unsigned int height, int pitch)
-{
-	return RenderBackend_LoadGlyph(pixels, width, height, pitch);
-}
-
-void Backend_UnloadGlyph(Backend_Glyph *glyph)
-{
-	RenderBackend_UnloadGlyph(glyph);
-}
-
-void Backend_PrepareToDrawGlyphs(Backend_Surface *destination_surface, const unsigned char *colour_channels)
-{
-	RenderBackend_PrepareToDrawGlyphs(destination_surface, colour_channels);
-}
-
-void Backend_DrawGlyph(Backend_Glyph *glyph, long x, long y)
-{
-	RenderBackend_DrawGlyph(glyph, x, y);
-}
-
-void Backend_FlushGlyphs(void)
-{
-	RenderBackend_FlushGlyphs();
-}
-
-void Backend_HandleRenderTargetLoss(void)
-{
-	RenderBackend_HandleRenderTargetLoss();
-}
-
-void Backend_HandleWindowResize(void)
-{
-	RenderBackend_HandleWindowResize();
 }
--- a/src/Draw.cpp
+++ b/src/Draw.cpp
@@ -8,7 +8,7 @@
 #include "WindowsWrapper.h"
 
 #include "Backends/Platform.h"
-#include "Backends/Window.h"
+#include "Backends/Rendering.h"
 #include "Bitmap.h"
 #include "CommonDefines.h"
 #include "Ending.h"
--- a/src/Font.cpp
+++ b/src/Font.cpp
@@ -13,7 +13,7 @@
 
 #include "Draw.h"
 #include "File.h"
-#include "Backends/Window.h"
+#include "Backends/Rendering.h"
 
 // Cave Story wasn't intended to use font anti-aliasing. It's only because Microsoft enabled it
 // by default from Windows Vista onwards that the game started using it.
--- a/src/Main.cpp
+++ b/src/Main.cpp
@@ -220,12 +220,22 @@
 			if (conf.display_mode == 1)
 			{
 				if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 0))
+				{
+					//SDL_FreeCursor(cursor);
+					//SDL_FreeSurface(cursor_surface);
+					PlatformBackend_Deinit();
 					return EXIT_FAILURE;
+				}
 			}
 			else
 			{
 				if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 1))
+				{
+					//SDL_FreeCursor(cursor);
+					//SDL_FreeSurface(cursor_surface);
+					PlatformBackend_Deinit();
 					return EXIT_FAILURE;
+				}
 			}
 		#else
 			// Doesn't handle StartDirectDraw failing
@@ -246,7 +256,12 @@
 
 		#ifdef FIX_BUGS
 			if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 2))
+			{
+				//SDL_FreeCursor(cursor);
+				//SDL_FreeSurface(cursor_surface);
+				PlatformBackend_Deinit();
 				return EXIT_FAILURE;
+			}
 		#else
 			// Doesn't handle StartDirectDraw failing
 			StartDirectDraw(lpWindowName, windowWidth, windowHeight, 2);
@@ -290,8 +305,9 @@
 	// Draw to screen
 	if (!Flip_SystemTask())
 	{
-//        SDL_FreeCursor(cursor);
- //       SDL_FreeSurface(cursor_surface);
+		//SDL_FreeCursor(cursor);
+		//SDL_FreeSurface(cursor_surface);
+		PlatformBackend_Deinit();
 		return EXIT_SUCCESS;
 	}
 
--