From add0627f03a435f2cb331ba38f71306bce25723d Mon Sep 17 00:00:00 2001 From: Clownacy Date: Wed, 1 Apr 2020 21:36:46 +0100 Subject: [PATCH] Restore custom cursor support --- src/Backends/Platform.h | 1 + src/Backends/Platform/GLFW3.cpp | 44 ++++++++++++++++++++++++++++++++- src/Backends/Platform/SDL2.cpp | 17 +++++++++++++ src/Main.cpp | 42 ++++++++++++++----------------- 4 files changed, 79 insertions(+), 25 deletions(-) diff --git a/src/Backends/Platform.h b/src/Backends/Platform.h index b9f198e5..edb53c54 100644 --- a/src/Backends/Platform.h +++ b/src/Backends/Platform.h @@ -10,6 +10,7 @@ void PlatformBackend_PostWindowCreation(void); BOOL PlatformBackend_GetBasePath(char *string_buffer); void PlatformBackend_HideMouse(void); void PlatformBackend_SetWindowIcon(const unsigned char *rgb_pixels, unsigned int width, unsigned int height); +void PlatformBackend_SetCursor(const unsigned char *rgb_pixels, unsigned int width, unsigned int height); BOOL PlatformBackend_SystemTask(void); void PlatformBackend_ShowMessageBox(const char *title, const char *message); unsigned long PlatformBackend_GetTicks(void); diff --git a/src/Backends/Platform/GLFW3.cpp b/src/Backends/Platform/GLFW3.cpp index e0903c26..bcc418c5 100644 --- a/src/Backends/Platform/GLFW3.cpp +++ b/src/Backends/Platform/GLFW3.cpp @@ -18,9 +18,11 @@ #include "../../Profile.h" #include "../../Resource.h" +GLFWwindow *window; + BOOL bActive = TRUE; -GLFWwindow *window; +static GLFWcursor* cursor; static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) { @@ -222,6 +224,9 @@ void PlatformBackend_Init(void) void PlatformBackend_Deinit(void) { + if (cursor != NULL) + glfwDestroyCursor(cursor); + glfwTerminate(); } @@ -270,6 +275,43 @@ void PlatformBackend_SetWindowIcon(const unsigned char *rgb_pixels, unsigned int } } +void PlatformBackend_SetCursor(const unsigned char *rgb_pixels, unsigned int width, unsigned int height) +{ + unsigned char *rgba_pixels = (unsigned char*)malloc(width * height * 4); + + const 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) + { + if (rgb_pointer[0] == 0xFF && rgb_pointer[1] == 0 && rgb_pointer[2] == 0xFF) + { + *rgba_pointer++ = *rgb_pointer++; + *rgba_pointer++ = *rgb_pointer++; + *rgba_pointer++ = *rgb_pointer++; + *rgba_pointer++ = 0; + } + else + { + *rgba_pointer++ = *rgb_pointer++; + *rgba_pointer++ = *rgb_pointer++; + *rgba_pointer++ = *rgb_pointer++; + *rgba_pointer++ = 0xFF; + } + } + } + + GLFWimage glfw_image = {(int)width, (int)height, rgba_pixels}; + cursor = glfwCreateCursor(&glfw_image, 0, 0); + glfwSetCursor(window, cursor); + + free(rgba_pixels); + } +} BOOL PlatformBackend_SystemTask(void) { diff --git a/src/Backends/Platform/SDL2.cpp b/src/Backends/Platform/SDL2.cpp index 77a64cde..ef10f97f 100644 --- a/src/Backends/Platform/SDL2.cpp +++ b/src/Backends/Platform/SDL2.cpp @@ -16,6 +16,9 @@ SDL_Window *window; BOOL bActive = TRUE; +static SDL_Surface *cursor_surface; +static SDL_Cursor *cursor; + void PlatformBackend_Init(void) { SDL_Init(SDL_INIT_EVENTS); @@ -37,6 +40,12 @@ void PlatformBackend_Init(void) void PlatformBackend_Deinit(void) { + if (cursor != NULL) + SDL_FreeCursor(cursor); + + if (cursor_surface != NULL) + SDL_FreeSurface(cursor_surface); + SDL_Quit(); } @@ -68,6 +77,14 @@ void PlatformBackend_SetWindowIcon(const unsigned char *rgb_pixels, unsigned int SDL_FreeSurface(surface); } +void PlatformBackend_SetCursor(const unsigned char *rgb_pixels, unsigned int width, unsigned int height) +{ + cursor_surface = SDL_CreateRGBSurfaceWithFormatFrom((void*)rgb_pixels, width, height, 0, width * 3, SDL_PIXELFORMAT_RGB24); + SDL_SetColorKey(cursor_surface, SDL_TRUE, SDL_MapRGB(cursor_surface->format, 0xFF, 0, 0xFF)); + cursor = SDL_CreateColorCursor(cursor_surface, 0, 0); + SDL_SetCursor(cursor); +} + BOOL PlatformBackend_SystemTask(void) { while (SDL_PollEvent(NULL) || !bActive) diff --git a/src/Main.cpp b/src/Main.cpp index 67c9615f..634b1cf8 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -223,8 +223,6 @@ int main(int argc, char *argv[]) { if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 0)) { - //SDL_FreeCursor(cursor); - //SDL_FreeSurface(cursor_surface); PlatformBackend_Deinit(); return EXIT_FAILURE; } @@ -233,8 +231,6 @@ int main(int argc, char *argv[]) { if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 1)) { - //SDL_FreeCursor(cursor); - //SDL_FreeSurface(cursor_surface); PlatformBackend_Deinit(); return EXIT_FAILURE; } @@ -285,25 +281,29 @@ int main(int argc, char *argv[]) size_t window_icon_resource_size; const unsigned char *window_icon_resource_data = FindResource("ICON_MINI", "ICON", &window_icon_resource_size); - unsigned int width, height; - unsigned char *rgb_pixels = DecodeBitmap(window_icon_resource_data, window_icon_resource_size, &width, &height); + unsigned int window_icon_width, window_icon_height; + unsigned char *window_icon_rgb_pixels = DecodeBitmap(window_icon_resource_data, window_icon_resource_size, &window_icon_width, &window_icon_height); - if (rgb_pixels != NULL) + if (window_icon_rgb_pixels != NULL) { - PlatformBackend_SetWindowIcon(rgb_pixels, width, height); - FreeBitmap(rgb_pixels); + PlatformBackend_SetWindowIcon(window_icon_rgb_pixels, window_icon_width, window_icon_height); + FreeBitmap(window_icon_rgb_pixels); } #endif -/* + // Set up the cursor - size_t 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); - SDL_SetCursor(cursor); -*/ + size_t cursor_resource_size; + const unsigned char *cursor_resource_data = FindResource("CURSOR_NORMAL", "CURSOR", &cursor_resource_size); + + unsigned int cursor_width, cursor_height; + unsigned char *cursor_rgb_pixels = DecodeBitmap(cursor_resource_data, cursor_resource_size, &cursor_width, &cursor_height); + + if (cursor_rgb_pixels != NULL) + { + PlatformBackend_SetCursor(cursor_rgb_pixels, cursor_width, cursor_height); + FreeBitmap(cursor_rgb_pixels); + } + if (IsKeyFile("fps")) bFps = TRUE; @@ -323,8 +323,6 @@ int main(int argc, char *argv[]) // Draw to screen if (!Flip_SystemTask()) { - //SDL_FreeCursor(cursor); - //SDL_FreeSurface(cursor_surface); PlatformBackend_Deinit(); return EXIT_SUCCESS; } @@ -350,10 +348,6 @@ int main(int argc, char *argv[]) EndTextObject(); EndDirectSound(); EndDirectDraw(); -/* - SDL_FreeCursor(cursor); - SDL_FreeSurface(cursor_surface); -*/ PlatformBackend_Deinit();