Make some backend code safer

This commit is contained in:
Clownacy 2020-04-06 20:05:15 +01:00
parent 6456649e11
commit 5395b00a95
2 changed files with 30 additions and 7 deletions

View file

@ -245,7 +245,9 @@ void Backend_SetCursor(const unsigned char *rgb_pixels, unsigned int width, unsi
GLFWimage glfw_image = {(int)width, (int)height, rgba_pixels};
cursor = glfwCreateCursor(&glfw_image, 0, 0);
glfwSetCursor(window, cursor);
if (cursor != NULL)
glfwSetCursor(window, cursor);
free(rgba_pixels);
}

View file

@ -2,6 +2,7 @@
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SDL.h"
@ -25,6 +26,7 @@ BOOL bActive = TRUE;
static BOOL keyboard_state[BACKEND_KEYBOARD_TOTAL];
static unsigned char *cursor_surface_pixels;
static SDL_Surface *cursor_surface;
static SDL_Cursor *cursor;
@ -50,6 +52,8 @@ void Backend_Deinit(void)
if (cursor_surface != NULL)
SDL_FreeSurface(cursor_surface);
free(cursor_surface_pixels);
SDL_Quit();
}
@ -78,16 +82,33 @@ void Backend_HideMouse(void)
void Backend_SetWindowIcon(const unsigned char *rgb_pixels, unsigned int width, unsigned int height)
{
SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormatFrom((void*)rgb_pixels, width, height, 0, width * 3, SDL_PIXELFORMAT_RGB24);
SDL_SetWindowIcon(window, surface);
SDL_FreeSurface(surface);
if (surface != NULL)
{
SDL_SetWindowIcon(window, surface);
SDL_FreeSurface(surface);
}
}
void Backend_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);
cursor_surface_pixels = (unsigned char*)malloc(width * height * 3);
if (cursor_surface_pixels != NULL)
{
cursor_surface = SDL_CreateRGBSurfaceWithFormatFrom(cursor_surface_pixels, width, height, 0, width * 3, SDL_PIXELFORMAT_RGB24);
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);
if (cursor != NULL)
SDL_SetCursor(cursor);
}
}
}
}
void PlaybackBackend_EnableDragAndDrop(void)