Convert cursors to RGBA

...instead of colour-keyed RGB. Less goofing-around for the user.
This commit is contained in:
Clownacy 2020-09-25 13:56:23 +01:00
parent 51e6cb5a6c
commit baf88b0d4b
8 changed files with 21 additions and 56 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 225 B

After

Width:  |  Height:  |  Size: 229 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 B

After

Width:  |  Height:  |  Size: 144 B

View file

@ -92,7 +92,7 @@ void Backend_PostWindowCreation(void);
bool Backend_GetBasePath(std::string *string_buffer);
void Backend_HideMouse(void);
void Backend_SetWindowIcon(const unsigned char *rgb_pixels, size_t width, size_t height);
void Backend_SetCursor(const unsigned char *rgb_pixels, size_t width, size_t height);
void Backend_SetCursor(const unsigned char *rgba_pixels, size_t width, size_t height);
void PlaybackBackend_EnableDragAndDrop(void);
bool Backend_SystemTask(bool active);
void Backend_GetKeyboardState(bool *keyboard_state);

View file

@ -229,45 +229,13 @@ void Backend_SetWindowIcon(const unsigned char *rgb_pixels, size_t width, size_t
}
}
void Backend_SetCursor(const unsigned char *rgb_pixels, size_t width, size_t height)
void Backend_SetCursor(const unsigned char *rgba_pixels, size_t width, size_t height)
{
// Convert to RGBA, since that's the only thing GLFW3 accepts
unsigned char *rgba_pixels = (unsigned char*)malloc(width * height * 4);
GLFWimage glfw_image = {(int)width, (int)height, rgba_pixels};
cursor = glfwCreateCursor(&glfw_image, 0, 0);
const unsigned char *rgb_pointer = rgb_pixels;
unsigned char *rgba_pointer = rgba_pixels;
if (rgba_pixels != NULL)
{
for (size_t y = 0; y < height; ++y)
{
for (size_t x = 0; x < width; ++x)
{
if (rgb_pointer[0] == 0xFF && rgb_pointer[1] == 0 && rgb_pointer[2] == 0xFF) // Colour-key
{
*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);
if (cursor != NULL)
glfwSetCursor(window, cursor);
free(rgba_pixels);
}
if (cursor != NULL)
glfwSetCursor(window, cursor);
}
void Backend_EnableDragAndDrop(void)

View file

@ -41,9 +41,9 @@ void Backend_SetWindowIcon(const unsigned char *rgb_pixels, size_t width, size_t
(void)height;
}
void Backend_SetCursor(const unsigned char *rgb_pixels, size_t width, size_t height)
void Backend_SetCursor(const unsigned char *rgba_pixels, size_t width, size_t height)
{
(void)rgb_pixels;
(void)rgba_pixels;
(void)width;
(void)height;
}

View file

@ -134,25 +134,22 @@ void Backend_SetWindowIcon(const unsigned char *rgb_pixels, size_t width, size_t
}
}
void Backend_SetCursor(const unsigned char *rgb_pixels, size_t width, size_t height)
void Backend_SetCursor(const unsigned char *rgba_pixels, size_t width, size_t height)
{
cursor_surface_pixels = (unsigned char*)malloc(width * height * 3);
cursor_surface_pixels = (unsigned char*)malloc(width * height * 4);
if (cursor_surface_pixels != NULL)
{
memcpy(cursor_surface_pixels, rgb_pixels, width * height * 3);
memcpy(cursor_surface_pixels, rgba_pixels, width * height * 4);
cursor_surface = SDL_CreateRGBSurfaceWithFormatFrom(cursor_surface_pixels, width, height, 0, width * 3, SDL_PIXELFORMAT_RGB24);
cursor_surface = SDL_CreateRGBSurfaceWithFormatFrom(cursor_surface_pixels, width, height, 0, width * 4, SDL_PIXELFORMAT_RGBA32);
if (cursor_surface != NULL)
{
if (SDL_SetColorKey(cursor_surface, SDL_TRUE, SDL_MapRGB(cursor_surface->format, 0xFF, 0, 0xFF)) == 0)
{
cursor = SDL_CreateColorCursor(cursor_surface, 0, 0);
cursor = SDL_CreateColorCursor(cursor_surface, 0, 0);
if (cursor != NULL)
SDL_SetCursor(cursor);
}
if (cursor != NULL)
SDL_SetCursor(cursor);
}
}
else

View file

@ -83,9 +83,9 @@ void Backend_SetWindowIcon(const unsigned char *rgb_pixels, size_t width, size_t
(void)height;
}
void Backend_SetCursor(const unsigned char *rgb_pixels, size_t width, size_t height)
void Backend_SetCursor(const unsigned char *rgba_pixels, size_t width, size_t height)
{
(void)rgb_pixels;
(void)rgba_pixels;
(void)width;
(void)height;
}

View file

@ -312,12 +312,12 @@ int main(int argc, char *argv[])
if (cursor_resource_data != NULL)
{
size_t cursor_width, cursor_height;
unsigned char *cursor_rgb_pixels = DecodeBitmap(cursor_resource_data, cursor_resource_size, &cursor_width, &cursor_height, 3);
unsigned char *cursor_rgba_pixels = DecodeBitmap(cursor_resource_data, cursor_resource_size, &cursor_width, &cursor_height, 4);
if (cursor_rgb_pixels != NULL)
if (cursor_rgba_pixels != NULL)
{
Backend_SetCursor(cursor_rgb_pixels, cursor_width, cursor_height);
FreeBitmap(cursor_rgb_pixels);
Backend_SetCursor(cursor_rgba_pixels, cursor_width, cursor_height);
FreeBitmap(cursor_rgba_pixels);
}
}