shithub: cstory

Download patch

ref: 6dac8254e2b9796f950fead27e701ebac6706ffb
parent: 3baba6a7273322cfc22c9ca010b202c072849544
author: Clownacy <[email protected]>
date: Wed Jan 22 17:19:55 EST 2020

Begin merge of Backend_Init/Backend_CreateWindow

OpenGL3+OpenGLES2 backend done so far

--- a/src/Backends/Rendering.h
+++ b/src/Backends/Rendering.h
@@ -14,8 +14,7 @@
 typedef struct Backend_Surface Backend_Surface;
 typedef struct Backend_Glyph Backend_Glyph;
 
-SDL_Window* Backend_CreateWindow(const char *title, int width, int height);
-Backend_Surface* Backend_Init(SDL_Window *window);
+Backend_Surface* Backend_Init(const char *title, int width, int height, BOOL fullscreen);
 void Backend_Deinit(void);
 void Backend_DrawScreen(void);
 Backend_Surface* Backend_CreateSurface(unsigned int width, unsigned int height);
--- a/src/Backends/Rendering/OpenGL3.cpp
+++ b/src/Backends/Rendering/OpenGL3.cpp
@@ -17,6 +17,8 @@
 
 #include "../../WindowsWrapper.h"
 
+#include "../../Resource.h"
+
 #define TOTAL_VBOS 2
 
 #define ATTRIBUTE_INPUT_VERTEX_COORDINATES 1
@@ -412,7 +414,7 @@
 	current_vertex_buffer_slot = 0;
 }
 
-SDL_Window* Backend_CreateWindow(const char *title, int width, int height)
+Backend_Surface* Backend_Init(const char *title, int width, int height, BOOL fullscreen)
 {
 #ifdef USE_OPENGLES2
 	SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
@@ -426,126 +428,138 @@
 	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
 #endif
 
-	return SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
-}
+	window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
 
-Backend_Surface* Backend_Init(SDL_Window *p_window)
-{
-	window = p_window;
+	if (window != NULL)
+	{
+	#ifndef _WIN32	// On Windows, we use native icons instead (so we can give the taskbar and window separate icons, like the original EXE does)
+		size_t resource_size;
+		const unsigned char *resource_data = FindResource("ICON_MINI", "ICON", &resource_size);
+		SDL_RWops *rwops = SDL_RWFromConstMem(resource_data, resource_size);
+		SDL_Surface *icon_surface = SDL_LoadBMP_RW(rwops, 1);
+		SDL_SetWindowIcon(window, icon_surface);
+		SDL_FreeSurface(icon_surface);
+	#endif
 
-	int window_width, window_height;
-	SDL_GetWindowSize(window, &window_width, &window_height);
+		if (fullscreen)
+			SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
 
-	context = SDL_GL_CreateContext(window);
+		int window_width, window_height;
+		SDL_GetWindowSize(window, &window_width, &window_height);
 
-	if (context != NULL)
-	{
-		if (SDL_GL_MakeCurrent(window, context) == 0)
+		context = SDL_GL_CreateContext(window);
+
+		if (context != NULL)
 		{
-		#ifndef USE_OPENGLES2
-			if (gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress))
-		#endif
+			if (SDL_GL_MakeCurrent(window, context) == 0)
 			{
 			#ifndef USE_OPENGLES2
-				// Check if the platform supports OpenGL 3.2
-				if (GLAD_GL_VERSION_3_2)
+				if (gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress))
 			#endif
 				{
-				#ifndef NDEBUG
-					printf("GL_VENDOR = %s\n", glGetString(GL_VENDOR));
-					printf("GL_RENDERER = %s\n", glGetString(GL_RENDERER));
-					printf("GL_VERSION = %s\n", glGetString(GL_VERSION));
+				#ifndef USE_OPENGLES2
+					// Check if the platform supports OpenGL 3.2
+					if (GLAD_GL_VERSION_3_2)
 				#endif
+					{
+					#ifndef NDEBUG
+						printf("GL_VENDOR = %s\n", glGetString(GL_VENDOR));
+						printf("GL_RENDERER = %s\n", glGetString(GL_RENDERER));
+						printf("GL_VERSION = %s\n", glGetString(GL_VERSION));
+					#endif
 
-					//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(1);
+						// Set up the vertex attributes
+						glEnableVertexAttribArray(1);
 
-					// 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_normal = CompileShader(vertex_shader_texture, fragment_shader_glyph_normal);
-					program_glyph_subpixel_part1 = CompileShader(vertex_shader_texture, fragment_shader_glyph_subpixel_part1);
-					program_glyph_subpixel_part2 = CompileShader(vertex_shader_texture, fragment_shader_glyph_subpixel_part2);
+						// 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_normal = CompileShader(vertex_shader_texture, fragment_shader_glyph_normal);
+						program_glyph_subpixel_part1 = CompileShader(vertex_shader_texture, fragment_shader_glyph_subpixel_part1);
+						program_glyph_subpixel_part2 = CompileShader(vertex_shader_texture, fragment_shader_glyph_subpixel_part2);
 
-					if (program_texture != 0 && program_texture_colour_key != 0 && program_colour_fill != 0 && program_glyph_normal != 0 && program_glyph_subpixel_part1 != 0 && program_glyph_subpixel_part2 != 0)
-					{
-						// Get shader uniforms
-						program_colour_fill_uniform_colour = glGetUniformLocation(program_colour_fill, "colour");
-						program_glyph_normal_uniform_colour = glGetUniformLocation(program_glyph_normal, "colour");
-						program_glyph_subpixel_part2_uniform_colour = glGetUniformLocation(program_glyph_subpixel_part2, "colour");
+						if (program_texture != 0 && program_texture_colour_key != 0 && program_colour_fill != 0 && program_glyph_normal != 0 && program_glyph_subpixel_part1 != 0 && program_glyph_subpixel_part2 != 0)
+						{
+							// Get shader uniforms
+							program_colour_fill_uniform_colour = glGetUniformLocation(program_colour_fill, "colour");
+							program_glyph_normal_uniform_colour = glGetUniformLocation(program_glyph_normal, "colour");
+							program_glyph_subpixel_part2_uniform_colour = glGetUniformLocation(program_glyph_subpixel_part2, "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, window_width, window_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
-					#else
-						glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, window_width, window_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, window_width, window_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
+						#else
+							glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, window_width, window_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 = window_width;
-						framebuffer.height = window_height;
+							framebuffer.width = window_width;
+							framebuffer.height = window_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);
 
-						return &framebuffer;
-					}
+							return &framebuffer;
+						}
 
-					if (program_glyph_subpixel_part2 != 0)
-						glDeleteProgram(program_glyph_subpixel_part2);
+						if (program_glyph_subpixel_part2 != 0)
+							glDeleteProgram(program_glyph_subpixel_part2);
 
-					if (program_glyph_subpixel_part1 != 0)
-						glDeleteProgram(program_glyph_subpixel_part1);
+						if (program_glyph_subpixel_part1 != 0)
+							glDeleteProgram(program_glyph_subpixel_part1);
 
-					if (program_glyph_normal != 0)
-						glDeleteProgram(program_glyph_normal);
+						if (program_glyph_normal != 0)
+							glDeleteProgram(program_glyph_normal);
 
-					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
+					}
 				}
 			}
+
+			SDL_GL_DeleteContext(context);
 		}
 
-		SDL_GL_DeleteContext(context);
+		SDL_DestroyWindow(window);
 	}
 
 	return NULL;
@@ -568,6 +582,7 @@
 	glDeleteVertexArrays(1, &vertex_array_id);
 #endif
 	SDL_GL_DeleteContext(context);
+	SDL_DestroyWindow(window);
 }
 
 void Backend_DrawScreen(void)
--- a/src/Draw.cpp
+++ b/src/Draw.cpp
@@ -84,7 +84,7 @@
 	return TRUE;
 }
 
-SDL_Window* CreateWindow(const char *title, int width, int height)
+BOOL StartDirectDraw(const char *title, int width, int height, int lMagnification)
 {
 #ifndef NDEBUG
 	puts("Available SDL2 video drivers:");
@@ -95,11 +95,6 @@
 	printf("Selected SDL2 video driver: %s\n", SDL_GetCurrentVideoDriver());
 #endif
 
-	return Backend_CreateWindow(title, width, height);
-}
-
-BOOL StartDirectDraw(SDL_Window *window, int lMagnification)
-{
 	memset(surface_metadata, 0, sizeof(surface_metadata));
 
 	switch (lMagnification)
@@ -117,11 +112,10 @@
 		case 2:
 			magnification = 2;
 			fullscreen = TRUE;
-			SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
 			break;
 	}
 
-	framebuffer = Backend_Init(window);
+	framebuffer = Backend_Init(title, width, height, fullscreen);
 
 	if (framebuffer == NULL)
 		return FALSE;
--- a/src/Draw.h
+++ b/src/Draw.h
@@ -51,8 +51,7 @@
 } SurfaceID;
 
 BOOL Flip_SystemTask(void);
-SDL_Window* CreateWindow(const char *title, int width, int height);
-BOOL StartDirectDraw(SDL_Window *window, int lMagnification);
+BOOL StartDirectDraw(const char *title, int width, int height, int lMagnification);
 void EndDirectDraw(void);
 void ReleaseSurface(SurfaceID s);
 BOOL MakeSurface_Resource(const char *name, SurfaceID surf_no);
--- a/src/Main.cpp
+++ b/src/Main.cpp
@@ -193,7 +193,7 @@
 
 	RECT unused_rect = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
 
-#ifdef _WIN32
+#ifdef _WIN32	// On Windows, we use native icons instead (so we can give the taskbar and window separate icons, like the original EXE does)
 	SDL_SetHint(SDL_HINT_WINDOWS_INTRESOURCE_ICON, "0");
 	SDL_SetHint(SDL_HINT_WINDOWS_INTRESOURCE_ICON_SMALL, "1");
 #endif
@@ -200,8 +200,6 @@
 
 	SDL_InitSubSystem(SDL_INIT_VIDEO);
 
-	SDL_Window *window;
-
 	switch (conf.display_mode)
 	{
 		case 1:
@@ -218,28 +216,23 @@
 				windowHeight = WINDOW_HEIGHT * 2;
 			}
 
-			window = CreateWindow(lpWindowName, windowWidth, windowHeight);
-
-			if (window == NULL)
-				return 0;
-
 		#ifdef FIX_BUGS
 			if (conf.display_mode == 1)
 			{
-				if (!StartDirectDraw(window, 0))
+				if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 0))
 					return 0;
 			}
 			else
 			{
-				if (!StartDirectDraw(window, 1))
+				if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 1))
 					return 0;
 			}
 		#else
 			// Doesn't handle StartDirectDraw failing
 			if (conf.display_mode == 1)
-				StartDirectDraw(window, 0);
+				StartDirectDraw(lpWindowName, windowWidth, windowHeight, 0);
 			else
-				StartDirectDraw(window, 1);
+				StartDirectDraw(lpWindowName, windowWidth, windowHeight, 1);
 		#endif
 
 			break;
@@ -251,17 +244,12 @@
 			windowWidth = WINDOW_WIDTH * 2;
 			windowHeight = WINDOW_HEIGHT * 2;
 
-			window = CreateWindow(lpWindowName, windowWidth, windowHeight);
-
-			if (window == NULL)
-				return 0;
-
 		#ifdef FIX_BUGS
-			if (!StartDirectDraw(window, 2))
+			if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 2))
 				return 0;
 		#else
 			// Doesn't handle StartDirectDraw failing
-			StartDirectDraw(window, 2);
+			StartDirectDraw(lpWindowName, windowWidth, windowHeight, 2);
 		#endif
 
 			bFullscreen = TRUE;
@@ -274,21 +262,10 @@
 	SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
 #endif
 
-	// Set up the window icon and cursor
-	const unsigned char *resource_data;
+	// Set up the cursor
 	size_t resource_size;
-	SDL_RWops *rwops;
-
-#ifndef _WIN32	// On Windows, we use native icons instead (so we can give the taskbar and window separate icons, like the original EXE does)
-	resource_data = FindResource("ICON_MINI", "ICON", &resource_size);
-	rwops = SDL_RWFromConstMem(resource_data, resource_size);
-	SDL_Surface *icon_surface = SDL_LoadBMP_RW(rwops, 1);
-	SDL_SetWindowIcon(window, icon_surface);
-	SDL_FreeSurface(icon_surface);
-#endif
-
-	resource_data = FindResource("CURSOR_NORMAL", "CURSOR", &resource_size);
-	rwops = SDL_RWFromConstMem(resource_data, resource_size);
+	const unsigned char *resource_data = FindResource("CURSOR_NORMAL", "CURSOR", &resource_size);
+	SDL_RWops *rwops = SDL_RWFromConstMem(resource_data, resource_size);
 	SDL_Surface *cursor_surface = SDL_LoadBMP_RW(rwops, 1);
 	SDL_SetColorKey(cursor_surface, SDL_TRUE, SDL_MapRGB(cursor_surface->format, 0xFF, 0, 0xFF));
 	SDL_Cursor *cursor = SDL_CreateColorCursor(cursor_surface, 0, 0);
@@ -315,7 +292,6 @@
 	{
         SDL_FreeCursor(cursor);
         SDL_FreeSurface(cursor_surface);
-		SDL_DestroyWindow(window);
 		return 1;
 	}
 
@@ -343,7 +319,6 @@
 
 	SDL_FreeCursor(cursor);
 	SDL_FreeSurface(cursor_surface);
-	SDL_DestroyWindow(window);
 
 	return 1;
 }