shithub: cstory

Download patch

ref: add0627f03a435f2cb331ba38f71306bce25723d
parent: f23117bbdc25c0ce7431eccedd2e39aee4ca6659
author: Clownacy <Clownacy@users.noreply.github.com>
date: Wed Apr 1 17:36:46 EDT 2020

Restore custom cursor support

--- a/src/Backends/Platform.h
+++ b/src/Backends/Platform.h
@@ -10,6 +10,7 @@
 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);
--- 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_Deinit(void)
 {
+	if (cursor != NULL)
+		glfwDestroyCursor(cursor);
+
 	glfwTerminate();
 }
 
@@ -270,6 +275,43 @@
 	}
 }
 
+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)
 {
--- a/src/Backends/Platform/SDL2.cpp
+++ b/src/Backends/Platform/SDL2.cpp
@@ -16,6 +16,9 @@
 
 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_Deinit(void)
 {
+	if (cursor != NULL)
+		SDL_FreeCursor(cursor);
+
+	if (cursor_surface != NULL)
+		SDL_FreeSurface(cursor_surface);
+
 	SDL_Quit();
 }
 
@@ -66,6 +75,14 @@
 	SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormatFrom((void*)rgb_pixels, width, height, 0, width * 3, SDL_PIXELFORMAT_RGB24);
 	SDL_SetWindowIcon(window, surface);
 	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)
--- a/src/Main.cpp
+++ b/src/Main.cpp
@@ -223,8 +223,6 @@
 			{
 				if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 0))
 				{
-					//SDL_FreeCursor(cursor);
-					//SDL_FreeSurface(cursor_surface);
 					PlatformBackend_Deinit();
 					return EXIT_FAILURE;
 				}
@@ -233,8 +231,6 @@
 			{
 				if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 1))
 				{
-					//SDL_FreeCursor(cursor);
-					//SDL_FreeSurface(cursor_surface);
 					PlatformBackend_Deinit();
 					return EXIT_FAILURE;
 				}
@@ -285,25 +281,29 @@
 	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 @@
 	// Draw to screen
 	if (!Flip_SystemTask())
 	{
-		//SDL_FreeCursor(cursor);
-		//SDL_FreeSurface(cursor_surface);
 		PlatformBackend_Deinit();
 		return EXIT_SUCCESS;
 	}
@@ -350,10 +348,6 @@
 	EndTextObject();
 	EndDirectSound();
 	EndDirectDraw();
-/*
-	SDL_FreeCursor(cursor);
-	SDL_FreeSurface(cursor_surface);
-*/
 
 	PlatformBackend_Deinit();