Remove platform backend dependency on core engine

The backends need to have no dependency on the engine, otherwise
there'll be conflicts when we do stuff like include `window.h` in a
file that also happens to include "WindowsWrapper.h" somewhere.
This commit is contained in:
Clownacy 2020-09-08 03:46:19 +01:00
parent bdcb1f3a3e
commit 84d6b50bc2
5 changed files with 38 additions and 19 deletions

View file

@ -85,7 +85,7 @@ enum
BACKEND_KEYBOARD_TOTAL BACKEND_KEYBOARD_TOTAL
}; };
bool Backend_Init(void); bool Backend_Init(void (*drag_and_drop_callback)(const char *path), void (*window_focus_callback)(bool focus));
void Backend_Deinit(void); void Backend_Deinit(void);
void Backend_PostWindowCreation(void); void Backend_PostWindowCreation(void);
bool Backend_GetBasePath(std::string *string_buffer); bool Backend_GetBasePath(std::string *string_buffer);

View file

@ -14,8 +14,6 @@
#include "../Rendering.h" #include "../Rendering.h"
#include "../Shared/GLFW3.h" #include "../Shared/GLFW3.h"
#include "../../Attributes.h" #include "../../Attributes.h"
#include "../../Main.h"
#include "../../Profile.h"
#define DO_KEY(GLFW_KEY, BACKEND_KEY) \ #define DO_KEY(GLFW_KEY, BACKEND_KEY) \
case GLFW_KEY: \ case GLFW_KEY: \
@ -26,6 +24,9 @@ static bool keyboard_state[BACKEND_KEYBOARD_TOTAL];
static GLFWcursor* cursor; static GLFWcursor* cursor;
static void (*drag_and_drop_callback)(const char *path);
static void (*window_focus_callback)(bool focus);
static 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)window;
@ -126,10 +127,7 @@ static void WindowFocusCallback(GLFWwindow *window, int focused)
{ {
(void)window; (void)window;
if (focused) window_focus_callback(focused);
ActiveWindow();
else
InactiveWindow();
} }
static void WindowSizeCallback(GLFWwindow *window, int width, int height) static void WindowSizeCallback(GLFWwindow *window, int width, int height)
@ -144,7 +142,7 @@ static void DragAndDropCallback(GLFWwindow *window, int count, const char **path
(void)window; (void)window;
(void)count; (void)count;
LoadProfile(paths[0]); drag_and_drop_callback(paths[0]);
} }
static void ErrorCallback(int code, const char *description) static void ErrorCallback(int code, const char *description)
@ -152,8 +150,11 @@ static void ErrorCallback(int code, const char *description)
Backend_PrintError("GLFW error received (%d): %s", code, description); Backend_PrintError("GLFW error received (%d): %s", code, description);
} }
bool Backend_Init(void) bool Backend_Init(void (*drag_and_drop_callback_param)(const char *path), void (*window_focus_callback_param)(bool focus))
{ {
drag_and_drop_callback = drag_and_drop_callback_param;
window_focus_callback = window_focus_callback_param;
glfwSetErrorCallback(ErrorCallback); glfwSetErrorCallback(ErrorCallback);
if (glfwInit() == GL_TRUE) if (glfwInit() == GL_TRUE)

View file

@ -12,8 +12,6 @@
#include "../Rendering.h" #include "../Rendering.h"
#include "../Shared/SDL2.h" #include "../Shared/SDL2.h"
#include "../../Attributes.h" #include "../../Attributes.h"
#include "../../Main.h"
#include "../../Profile.h"
#define DO_KEY(SDL_KEY, BACKEND_KEY) \ #define DO_KEY(SDL_KEY, BACKEND_KEY) \
case SDL_KEY: \ case SDL_KEY: \
@ -26,8 +24,14 @@ static unsigned char *cursor_surface_pixels;
static SDL_Surface *cursor_surface; static SDL_Surface *cursor_surface;
static SDL_Cursor *cursor; static SDL_Cursor *cursor;
bool Backend_Init(void) static void (*drag_and_drop_callback)(const char *path);
static void (*window_focus_callback)(bool focus);
bool Backend_Init(void (*drag_and_drop_callback_param)(const char *path), void (*window_focus_callback_param)(bool focus))
{ {
drag_and_drop_callback = drag_and_drop_callback_param;
window_focus_callback = window_focus_callback_param;
if (SDL_Init(SDL_INIT_EVENTS) == 0) if (SDL_Init(SDL_INIT_EVENTS) == 0)
{ {
if (SDL_InitSubSystem(SDL_INIT_VIDEO) == 0) if (SDL_InitSubSystem(SDL_INIT_VIDEO) == 0)
@ -269,7 +273,7 @@ bool Backend_SystemTask(bool active)
break; break;
case SDL_DROPFILE: case SDL_DROPFILE:
LoadProfile(event.drop.file); drag_and_drop_callback(event.drop.file);
SDL_free(event.drop.file); SDL_free(event.drop.file);
break; break;
@ -277,11 +281,11 @@ bool Backend_SystemTask(bool active)
switch (event.window.event) switch (event.window.event)
{ {
case SDL_WINDOWEVENT_FOCUS_LOST: case SDL_WINDOWEVENT_FOCUS_LOST:
InactiveWindow(); window_focus_callback(false);
break; break;
case SDL_WINDOWEVENT_FOCUS_GAINED: case SDL_WINDOWEVENT_FOCUS_GAINED:
ActiveWindow(); window_focus_callback(true);
break; break;
case SDL_WINDOWEVENT_RESIZED: case SDL_WINDOWEVENT_RESIZED:

View file

@ -18,10 +18,14 @@
#include "KeyControl.h" #include "KeyControl.h"
#include "MyChar.h" #include "MyChar.h"
#include "Organya.h" #include "Organya.h"
#include "Profile.h"
#include "Resource.h" #include "Resource.h"
#include "Sound.h" #include "Sound.h"
#include "Triangle.h" #include "Triangle.h"
void InactiveWindow(void);
void ActiveWindow(void);
std::string gModulePath; std::string gModulePath;
std::string gDataPath; std::string gDataPath;
@ -42,6 +46,19 @@ static const char* const lpWindowName = "洞窟物語"; // "Cave Story"
static const char* const lpWindowName = "Cave Story ~ Doukutsu Monogatari"; static const char* const lpWindowName = "Cave Story ~ Doukutsu Monogatari";
#endif #endif
static void DragAndDropCallback(const char *path)
{
LoadProfile(path);
}
static void WindowFocusCallback(bool focus)
{
if (focus)
ActiveWindow();
else
InactiveWindow();
}
// Framerate stuff // Framerate stuff
static unsigned long CountFramePerSecound(void) static unsigned long CountFramePerSecound(void)
{ {
@ -86,7 +103,7 @@ int main(int argc, char *argv[])
int i; int i;
if (!Backend_Init()) if (!Backend_Init(DragAndDropCallback, WindowFocusCallback))
return EXIT_FAILURE; return EXIT_FAILURE;
// Get executable's path // Get executable's path

View file

@ -16,7 +16,4 @@ extern BOOL gbUseJoystick;
void PutFramePerSecound(void); void PutFramePerSecound(void);
void InactiveWindow(void);
void ActiveWindow(void);
BOOL SystemTask(void); BOOL SystemTask(void);