Cleanup and fixes

This commit is contained in:
Clownacy 2020-04-01 16:11:34 +01:00
parent 7bd6ff8617
commit ff70664604
10 changed files with 115 additions and 137 deletions

View file

@ -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);

View file

@ -1,6 +1,8 @@
#include "../Platform.h"
#include <chrono>
#include <stddef.h>
#include <stdlib.h>
#include <thread>
#include <GLFW/glfw3.h>
@ -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;

View file

@ -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)

View file

@ -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);

View file

@ -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;
}

View file

@ -1,14 +1,14 @@
#include "../Rendering.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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);
}

View file

@ -1,8 +1,8 @@
#include "../Rendering.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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

View file

@ -1,7 +1,6 @@
#include "../Rendering.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -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;

View file

@ -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

View file

@ -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;