diff --git a/CMakeLists.txt b/CMakeLists.txt index 43773d2f..3026ff0e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,6 @@ option(FIX_BUGS "Fix various bugs in the game" OFF) 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 @@ else() 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") diff --git a/src/Backends/Platform/GLFW3.cpp b/src/Backends/Platform/GLFW3.cpp index 432fdaf6..1840707d 100644 --- a/src/Backends/Platform/GLFW3.cpp +++ b/src/Backends/Platform/GLFW3.cpp @@ -210,7 +210,7 @@ void WindowSizeCallback(GLFWwindow *window, int width, int height) (void)width; (void)height; - RenderBackend_HandleWindowResize(); + Backend_HandleWindowResize(); } void PlatformBackend_Init(void) diff --git a/src/Backends/Platform/SDL2.cpp b/src/Backends/Platform/SDL2.cpp index 82bd6211..4ded6c9a 100644 --- 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" diff --git a/src/Backends/Rendering.h b/src/Backends/Rendering.h index 6c8ecd55..29f3f2e1 100644 --- 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); diff --git a/src/Backends/Rendering/OpenGL3.cpp b/src/Backends/Rendering/OpenGL3.cpp index c0ed58dd..225a5ea1 100644 --- a/src/Backends/Rendering/OpenGL3.cpp +++ b/src/Backends/Rendering/OpenGL3.cpp @@ -513,108 +513,111 @@ static void GlyphBatch_DestroyTexture(SPRITEBATCH_U64 texture_id, void *udata) // 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)); - - // Set up blending (only used for font-rendering) - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - //glEnable(GL_DEBUG_OUTPUT); - //glDebugMessageCallback(MessageCallback, 0); - - 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 - - // Set up Vertex Buffer Objects - glGenBuffers(TOTAL_VBOS, vertex_buffer_ids); - - // 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); - - if (program_texture != 0 && program_texture_colour_key != 0 && program_colour_fill != 0 && program_glyph != 0) + if (WindowBackend_OpenGL_CreateWindow(window_title, screen_width, screen_height, fullscreen)) { - // Get shader uniforms - program_colour_fill_uniform_colour = glGetUniformLocation(program_colour_fill, "colour"); - program_glyph_uniform_colour = glGetUniformLocation(program_glyph, "colour"); + 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 framebuffer (used for surface-to-surface blitting) - glGenFramebuffers(1, &framebuffer_id); - glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_id); + // Set up blending (only used for font-rendering) + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + //glEnable(GL_DEBUG_OUTPUT); + //glDebugMessageCallback(MessageCallback, 0); + + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); - // 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); + // Set up Vertex Array Object + glGenVertexArrays(1, &vertex_array_id); + glBindVertexArray(vertex_array_id); #endif - framebuffer.width = screen_width; - framebuffer.height = screen_height; + // Set up Vertex Buffer Objects + glGenBuffers(TOTAL_VBOS, vertex_buffer_ids); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, framebuffer.texture_id, 0); - glViewport(0, 0, framebuffer.width, framebuffer.height); + // Set up the vertex attributes + glEnableVertexAttribArray(ATTRIBUTE_INPUT_VERTEX_COORDINATES); - // 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 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); - return &framebuffer; + 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 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; + + 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); + + return &framebuffer; + } + + if (program_glyph != 0) + glDeleteProgram(program_glyph); + + if (program_colour_fill != 0) + glDeleteProgram(program_colour_fill); + + if (program_texture_colour_key != 0) + glDeleteProgram(program_texture_colour_key); + + if (program_texture != 0) + glDeleteProgram(program_texture); + + glDeleteBuffers(TOTAL_VBOS, vertex_buffer_ids); + #ifndef USE_OPENGLES2 + glDeleteVertexArrays(1, &vertex_array_id); + #endif } - if (program_glyph != 0) - glDeleteProgram(program_glyph); - - if (program_colour_fill != 0) - glDeleteProgram(program_colour_fill); - - if (program_texture_colour_key != 0) - glDeleteProgram(program_texture_colour_key); - - if (program_texture != 0) - glDeleteProgram(program_texture); - - 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 @@ void RenderBackend_Deinit(void) #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,22 +693,21 @@ void RenderBackend_DrawScreen(void) 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); } // ==================== // 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 @@ Backend_Surface* RenderBackend_CreateSurface(unsigned int width, unsigned int he return surface; } -void RenderBackend_FreeSurface(Backend_Surface *surface) +void Backend_FreeSurface(Backend_Surface *surface) { if (surface == NULL) return; @@ -746,19 +750,19 @@ void RenderBackend_FreeSurface(Backend_Surface *surface) free(surface); } -BOOL RenderBackend_IsSurfaceLost(Backend_Surface *surface) +BOOL Backend_IsSurfaceLost(Backend_Surface *surface) { (void)surface; 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 @@ unsigned char* RenderBackend_LockSurface(Backend_Surface *surface, unsigned int 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 @@ void RenderBackend_UnlockSurface(Backend_Surface *surface, unsigned int width, u // 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 @@ void RenderBackend_Blit(Backend_Surface *source_surface, const RECT *rect, Backe 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 @@ void RenderBackend_ColourFill(Backend_Surface *surface, const RECT *rect, unsign // 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 @@ Backend_Glyph* RenderBackend_LoadGlyph(const unsigned char *pixels, unsigned int return NULL; } -void RenderBackend_UnloadGlyph(Backend_Glyph *glyph) +void Backend_UnloadGlyph(Backend_Glyph *glyph) { if (glyph == NULL) return; @@ -969,19 +973,19 @@ void RenderBackend_UnloadGlyph(Backend_Glyph *glyph) 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; 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 @@ void RenderBackend_FlushGlyphs(void) // 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 } diff --git a/src/Backends/Window.h b/src/Backends/Window.h index 6568e705..0f10e899 100644 --- 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); diff --git a/src/Backends/Window/GLFW3-OpenGL3.cpp b/src/Backends/Window/GLFW3-OpenGL3.cpp index b8a840cb..112b94eb 100644 --- a/src/Backends/Window/GLFW3-OpenGL3.cpp +++ b/src/Backends/Window/GLFW3-OpenGL3.cpp @@ -22,7 +22,7 @@ void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods 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 @@ Backend_Surface* Backend_Init(const char *window_title, int screen_width, int sc 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 @@ Backend_Surface* Backend_Init(const char *window_title, int screen_width, int sc glfwSetWindowFocusCallback(window, WindowFocusCallback); glfwSetWindowSizeCallback(window, WindowSizeCallback); - return RenderBackend_Init(screen_width, screen_height); + return TRUE; #ifndef USE_OPENGLES2 } else @@ -86,97 +94,15 @@ Backend_Surface* Backend_Init(const char *window_title, int screen_width, int sc 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(); } diff --git a/src/Backends/Window/SDL2-OpenGL3.cpp b/src/Backends/Window/SDL2-OpenGL3.cpp index 25a1de45..c5f5d039 100644 --- 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 @@ Backend_Surface* Backend_Init(const char *window_title, int screen_width, int sc if (GLAD_GL_VERSION_3_2) { #endif - return RenderBackend_Init(screen_width, screen_height); + return TRUE; #ifndef USE_OPENGLES2 } else @@ -100,98 +100,16 @@ Backend_Surface* Backend_Init(const char *window_title, int screen_width, int sc 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(); } diff --git a/src/Draw.cpp b/src/Draw.cpp index 4988b326..6fbc6497 100644 --- 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" diff --git a/src/Font.cpp b/src/Font.cpp index c244c5db..9d17cbed 100644 --- 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. diff --git a/src/Main.cpp b/src/Main.cpp index 27b8491b..5d6e0698 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -220,12 +220,22 @@ int main(int argc, char *argv[]) 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 @@ int main(int argc, char *argv[]) #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 @@ int main(int argc, char *argv[]) // 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; }