From ff70664604b15d63851fe2ba69ba82025f4f8a8a Mon Sep 17 00:00:00 2001 From: Clownacy Date: Wed, 1 Apr 2020 16:11:34 +0100 Subject: [PATCH] Cleanup and fixes --- src/Backends/Platform.h | 1 + src/Backends/Platform/GLFW3.cpp | 62 ++++++++++++++++++++++++--- src/Backends/Platform/SDL2.cpp | 27 +++++++++++- src/Backends/Rendering.h | 2 +- src/Backends/Rendering/OpenGL3.cpp | 5 ++- src/Backends/Rendering/SDLSurface.cpp | 26 ++++------- src/Backends/Rendering/SDLTexture.cpp | 22 ++-------- src/Backends/Rendering/Software.cpp | 25 +++-------- src/Backends/Window/GLFW3-OpenGL3.cpp | 48 +-------------------- src/Backends/Window/SDL2-OpenGL3.cpp | 34 +++++---------- 10 files changed, 115 insertions(+), 137 deletions(-) diff --git a/src/Backends/Platform.h b/src/Backends/Platform.h index ee661712..64a96719 100644 --- a/src/Backends/Platform.h +++ b/src/Backends/Platform.h @@ -6,6 +6,7 @@ extern BOOL bActive; void PlatformBackend_Init(void); void PlatformBackend_Deinit(void); +void PlatformBackend_PostWindowCreation(void); BOOL PlatformBackend_GetBasePath(char *string_buffer); BOOL PlatformBackend_SystemTask(void); void PlatformBackend_ShowMessageBox(const char *title, const char *message); diff --git a/src/Backends/Platform/GLFW3.cpp b/src/Backends/Platform/GLFW3.cpp index 1840707d..894f6b7e 100644 --- a/src/Backends/Platform/GLFW3.cpp +++ b/src/Backends/Platform/GLFW3.cpp @@ -1,6 +1,8 @@ #include "../Platform.h" #include +#include +#include #include #include @@ -9,16 +11,18 @@ #include "../../WindowsWrapper.h" +#include "../../Bitmap.h" #include "../../KeyControl.h" #include "../../Main.h" #include "../../Organya.h" #include "../../Profile.h" +#include "../../Resource.h" BOOL bActive = TRUE; extern GLFWwindow *window; -void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) +static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) { (void)window; (void)scancode; @@ -194,7 +198,7 @@ void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods } } -void WindowFocusCallback(GLFWwindow *window, int focused) +static void WindowFocusCallback(GLFWwindow *window, int focused) { (void)window; @@ -204,13 +208,11 @@ void WindowFocusCallback(GLFWwindow *window, int focused) InactiveWindow(); } -void WindowSizeCallback(GLFWwindow *window, int width, int height) +static void WindowSizeCallback(GLFWwindow *window, int width, int height) { (void)window; - (void)width; - (void)height; - Backend_HandleWindowResize(); + Backend_HandleWindowResize(width, height); } void PlatformBackend_Init(void) @@ -223,6 +225,54 @@ void PlatformBackend_Deinit(void) glfwTerminate(); } +void PlatformBackend_PostWindowCreation(void) +{ + // Hook callbacks + glfwSetKeyCallback(window, KeyCallback); + glfwSetWindowFocusCallback(window, WindowFocusCallback); + glfwSetWindowSizeCallback(window, WindowSizeCallback); + + // Set up window icon + + // TODO - GLFW_ICON +#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); + + unsigned int width, height; + unsigned char *rgb_pixels = DecodeBitmap(resource_data, resource_size, &width, &height); + + if (rgb_pixels != NULL) + { + unsigned char *rgba_pixels = (unsigned char*)malloc(width * height * 4); + + unsigned char *rgb_pointer = rgb_pixels; + unsigned char *rgba_pointer = rgba_pixels; + + if (rgba_pixels != NULL) + { + for (unsigned int y = 0; y < height; ++y) + { + for (unsigned int x = 0; x < width; ++x) + { + *rgba_pointer++ = *rgb_pointer++; + *rgba_pointer++ = *rgb_pointer++; + *rgba_pointer++ = *rgb_pointer++; + *rgba_pointer++ = 0xFF; + } + } + + GLFWimage glfw_image = {(int)width, (int)height, rgba_pixels}; + glfwSetWindowIcon(window, 1, &glfw_image); + + free(rgba_pixels); + } + + FreeBitmap(rgb_pixels); + } +#endif +} + BOOL PlatformBackend_GetBasePath(char *string_buffer) { return FALSE; diff --git a/src/Backends/Platform/SDL2.cpp b/src/Backends/Platform/SDL2.cpp index 4ded6c9a..aa60526e 100644 --- a/src/Backends/Platform/SDL2.cpp +++ b/src/Backends/Platform/SDL2.cpp @@ -10,6 +10,9 @@ #include "../../Main.h" #include "../../Organya.h" #include "../../Profile.h" +#include "../../Resource.h" + +extern SDL_Window *window; BOOL bActive = TRUE; @@ -23,6 +26,13 @@ void PlatformBackend_Init(void) #endif SDL_InitSubSystem(SDL_INIT_VIDEO); + + puts("Available SDL2 video drivers:"); + + for (int i = 0; i < SDL_GetNumVideoDrivers(); ++i) + puts(SDL_GetVideoDriver(i)); + + printf("Selected SDL2 video driver: %s\n", SDL_GetCurrentVideoDriver()); } void PlatformBackend_Deinit(void) @@ -30,6 +40,19 @@ void PlatformBackend_Deinit(void) SDL_Quit(); } +void PlatformBackend_PostWindowCreation(void) +{ + // Set up window icon +#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 +} + BOOL PlatformBackend_GetBasePath(char *string_buffer) { char *base_path = SDL_GetBasePath(); @@ -240,7 +263,7 @@ BOOL PlatformBackend_SystemTask(void) case SDL_WINDOWEVENT_RESIZED: case SDL_WINDOWEVENT_SIZE_CHANGED: - Backend_HandleWindowResize(); + Backend_HandleWindowResize(event.window.data1, event.window.data2); break; } @@ -262,7 +285,7 @@ BOOL PlatformBackend_SystemTask(void) void PlatformBackend_ShowMessageBox(const char *title, const char *message) { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title, message, NULL); + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title, message, window); } unsigned long PlatformBackend_GetTicks(void) diff --git a/src/Backends/Rendering.h b/src/Backends/Rendering.h index 29f3f2e1..8572fbbb 100644 --- a/src/Backends/Rendering.h +++ b/src/Backends/Rendering.h @@ -23,4 +23,4 @@ void Backend_PrepareToDrawGlyphs(Backend_Surface *destination_surface, const uns void Backend_DrawGlyph(Backend_Glyph *glyph, long x, long y); void Backend_FlushGlyphs(void); void Backend_HandleRenderTargetLoss(void); -void Backend_HandleWindowResize(void); +void Backend_HandleWindowResize(unsigned int width, unsigned int height); diff --git a/src/Backends/Rendering/OpenGL3.cpp b/src/Backends/Rendering/OpenGL3.cpp index 3ce68ad9..65b1cd87 100644 --- a/src/Backends/Rendering/OpenGL3.cpp +++ b/src/Backends/Rendering/OpenGL3.cpp @@ -1029,7 +1029,8 @@ void Backend_HandleRenderTargetLoss(void) // No problem for us } -void Backend_HandleWindowResize(void) +void Backend_HandleWindowResize(unsigned int width, unsigned int height) { - // No problem for us + actual_screen_width = width; + actual_screen_height = height; } diff --git a/src/Backends/Rendering/SDLSurface.cpp b/src/Backends/Rendering/SDLSurface.cpp index 8adfb4c8..4ffda7c2 100644 --- a/src/Backends/Rendering/SDLSurface.cpp +++ b/src/Backends/Rendering/SDLSurface.cpp @@ -1,14 +1,14 @@ #include "../Rendering.h" #include -#include #include +#include #include "SDL.h" #include "../../WindowsWrapper.h" -#include "../../Resource.h" +#include "../Platform.h" typedef struct Backend_Surface { @@ -44,26 +44,10 @@ static void RectToSDLRect(const RECT *rect, SDL_Rect *sdl_rect) Backend_Surface* Backend_Init(const char *window_title, int screen_width, int screen_height, BOOL fullscreen) { - puts("Available SDL2 video drivers:"); - - for (int i = 0; i < SDL_GetNumVideoDrivers(); ++i) - puts(SDL_GetVideoDriver(i)); - - printf("Selected SDL2 video driver: %s\n", SDL_GetCurrentVideoDriver()); - window = SDL_CreateWindow(window_title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, screen_width, screen_height, 0); 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 - if (fullscreen) SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN); @@ -72,9 +56,15 @@ Backend_Surface* Backend_Init(const char *window_title, int screen_width, int sc framebuffer.sdlsurface = SDL_CreateRGBSurfaceWithFormat(0, window_sdlsurface->w, window_sdlsurface->h, 0, SDL_PIXELFORMAT_RGB24); if (framebuffer.sdlsurface != NULL) + { + PlatformBackend_PostWindowCreation(); + return &framebuffer; + } else + { SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error (SDLSurface rendering backend)", "Could not create framebuffer surface", window); + } SDL_DestroyWindow(window); } diff --git a/src/Backends/Rendering/SDLTexture.cpp b/src/Backends/Rendering/SDLTexture.cpp index 57b32da5..b1f56e75 100644 --- a/src/Backends/Rendering/SDLTexture.cpp +++ b/src/Backends/Rendering/SDLTexture.cpp @@ -1,8 +1,8 @@ #include "../Rendering.h" #include -#include #include +#include #include "SDL.h" @@ -11,10 +11,10 @@ #include "../../WindowsWrapper.h" +#inclide "../Platform.h" #include "../../Draw.h" #include "../../Ending.h" #include "../../MapName.h" -#include "../../Resource.h" #include "../../TextScr.h" typedef struct Backend_Surface @@ -116,13 +116,6 @@ static void GlyphBatch_DestroyTexture(SPRITEBATCH_U64 texture_id, void *udata) Backend_Surface* Backend_Init(const char *window_title, int screen_width, int screen_height, BOOL fullscreen) { - puts("Available SDL2 video drivers:"); - - for (int i = 0; i < SDL_GetNumVideoDrivers(); ++i) - puts(SDL_GetVideoDriver(i)); - - printf("Selected SDL2 video driver: %s\n", SDL_GetCurrentVideoDriver()); - puts("Available SDL2 render drivers:"); for (int i = 0; i < SDL_GetNumRenderDrivers(); ++i) @@ -136,15 +129,6 @@ Backend_Surface* Backend_Init(const char *window_title, int screen_width, int sc 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 - if (fullscreen) SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN); @@ -180,6 +164,8 @@ Backend_Surface* Backend_Init(const char *window_title, int screen_width, int sc config.delete_texture_callback = GlyphBatch_DestroyTexture; spritebatch_init(&glyph_batcher, &config, NULL); + PlatformBackend_PostWindowCreation(); + return &framebuffer; } else diff --git a/src/Backends/Rendering/Software.cpp b/src/Backends/Rendering/Software.cpp index 8b6a3c08..e78c677d 100644 --- a/src/Backends/Rendering/Software.cpp +++ b/src/Backends/Rendering/Software.cpp @@ -1,7 +1,6 @@ #include "../Rendering.h" #include -#include #include #include @@ -9,7 +8,7 @@ #include "../../WindowsWrapper.h" -#include "../../Resource.h" +#include "../Platform.h" #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) @@ -39,26 +38,10 @@ static Backend_Surface *glyph_destination_surface; Backend_Surface* Backend_Init(const char *window_title, int screen_width, int screen_height, BOOL fullscreen) { - puts("Available SDL2 video drivers:"); - - for (int i = 0; i < SDL_GetNumVideoDrivers(); ++i) - puts(SDL_GetVideoDriver(i)); - - printf("Selected SDL2 video driver: %s\n", SDL_GetCurrentVideoDriver()); - window = SDL_CreateWindow(window_title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, screen_width, screen_height, 0); 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 - if (fullscreen) SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN); @@ -73,18 +56,20 @@ Backend_Surface* Backend_Init(const char *window_title, int screen_width, int sc framebuffer.height = framebuffer_sdlsurface->h; framebuffer.pitch = framebuffer_sdlsurface->pitch; + PlatformBackend_PostWindowCreation(); + return &framebuffer; } else { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error (software rendering backend)", "Could not create framebuffer surface", window); + PlatformBackend_ShowMessageBox("Fatal error (software rendering backend)", "Could not create framebuffer surface"); } SDL_DestroyWindow(window); } else { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error (software rendering backend)", "Could not create window", NULL); + PlatformBackend_ShowMessageBox("Fatal error (software rendering backend)", "Could not create window"); } return NULL; diff --git a/src/Backends/Window/GLFW3-OpenGL3.cpp b/src/Backends/Window/GLFW3-OpenGL3.cpp index 8831d3fe..89ae1996 100644 --- a/src/Backends/Window/GLFW3-OpenGL3.cpp +++ b/src/Backends/Window/GLFW3-OpenGL3.cpp @@ -13,16 +13,10 @@ #include "../../WindowsWrapper.h" #include "../Platform.h" -#include "../../Bitmap.h" -#include "../../Resource.h" // Horrible hacks GLFWwindow *window; -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); - BOOL WindowBackend_OpenGL_CreateWindow(const char *window_title, int *screen_width, int *screen_height, BOOL fullscreen) { #ifdef USE_OPENGLES2 @@ -56,44 +50,6 @@ BOOL WindowBackend_OpenGL_CreateWindow(const char *window_title, int *screen_wid 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); - - unsigned int width, height; - unsigned char *rgb_pixels = DecodeBitmap(resource_data, resource_size, &width, &height); - - if (rgb_pixels != NULL) - { - unsigned char *rgba_pixels = (unsigned char*)malloc(width * height * 4); - - unsigned char *rgb_pointer = rgb_pixels; - unsigned char *rgba_pointer = rgba_pixels; - - if (rgba_pixels != NULL) - { - for (unsigned int y = 0; y < height; ++y) - { - for (unsigned int x = 0; x < width; ++x) - { - *rgba_pointer++ = *rgb_pointer++; - *rgba_pointer++ = *rgb_pointer++; - *rgba_pointer++ = *rgb_pointer++; - *rgba_pointer++ = 0xFF; - } - } - - GLFWimage glfw_image = {(int)width, (int)height, rgba_pixels}; - glfwSetWindowIcon(window, 1, &glfw_image); - - free(rgba_pixels); - } - - FreeBitmap(rgb_pixels); - } - #endif - - glfwMakeContextCurrent(window); #ifndef USE_OPENGLES2 @@ -103,9 +59,7 @@ BOOL WindowBackend_OpenGL_CreateWindow(const char *window_title, int *screen_wid if (GLAD_GL_VERSION_3_2) { #endif - glfwSetKeyCallback(window, KeyCallback); - glfwSetWindowFocusCallback(window, WindowFocusCallback); - glfwSetWindowSizeCallback(window, WindowSizeCallback); + PlatformBackend_PostWindowCreation(); return TRUE; #ifndef USE_OPENGLES2 diff --git a/src/Backends/Window/SDL2-OpenGL3.cpp b/src/Backends/Window/SDL2-OpenGL3.cpp index 57c47ec9..b3377a98 100644 --- a/src/Backends/Window/SDL2-OpenGL3.cpp +++ b/src/Backends/Window/SDL2-OpenGL3.cpp @@ -11,20 +11,15 @@ #include "../../WindowsWrapper.h" +#include "../Platform.h" #include "../../Resource.h" -static SDL_Window *window; +SDL_Window *window; + static SDL_GLContext context; BOOL WindowBackend_OpenGL_CreateWindow(const char *window_title, int *screen_width, int *screen_height, BOOL fullscreen) { - puts("Available SDL2 video drivers:"); - - for (int i = 0; i < SDL_GetNumVideoDrivers(); ++i) - puts(SDL_GetVideoDriver(i)); - - printf("Selected SDL2 video driver: %s\n", SDL_GetCurrentVideoDriver()); - #ifdef USE_OPENGLES2 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0); @@ -37,19 +32,10 @@ BOOL WindowBackend_OpenGL_CreateWindow(const char *window_title, int *screen_wid SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); #endif - window = SDL_CreateWindow(window_title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, *screen_width, *screen_height, SDL_WINDOW_OPENGL); + window = SDL_CreateWindow(window_title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, *screen_width, *screen_height, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL); 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 - if (fullscreen) SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN); @@ -66,37 +52,39 @@ BOOL WindowBackend_OpenGL_CreateWindow(const char *window_title, int *screen_wid if (GLAD_GL_VERSION_3_2) { #endif + PlatformBackend_PostWindowCreation(); + return TRUE; #ifndef USE_OPENGLES2 } else { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error (OpenGL rendering backend)", "Your system does not support OpenGL 3.2", window); + PlatformBackend_ShowMessageBox("Fatal error (OpenGL rendering backend)", "Your system does not support OpenGL 3.2"); } } else { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error (OpenGL rendering backend)", "Could not load OpenGL functions", window); + PlatformBackend_ShowMessageBox("Fatal error (OpenGL rendering backend)", "Could not load OpenGL functions"); } #endif } else { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error (OpenGL rendering backend)", "SDL_GL_MakeCurrent failed", window); + PlatformBackend_ShowMessageBox("Fatal error (OpenGL rendering backend)", "SDL_GL_MakeCurrent failed"); } SDL_GL_DeleteContext(context); } else { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error (OpenGL rendering backend)", "Could not create OpenGL context", window); + PlatformBackend_ShowMessageBox("Fatal error (OpenGL rendering backend)", "Could not create OpenGL context"); } SDL_DestroyWindow(window); } else { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error (OpenGL rendering backend)", "Could not create window", NULL); + PlatformBackend_ShowMessageBox("Fatal error (OpenGL rendering backend)", "Could not create window"); } return FALSE;