diff --git a/src/Backends/Misc.h b/src/Backends/Misc.h index 1377d88d..a4cfd293 100644 --- a/src/Backends/Misc.h +++ b/src/Backends/Misc.h @@ -85,7 +85,7 @@ enum 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_PostWindowCreation(void); bool Backend_GetBasePath(std::string *string_buffer); diff --git a/src/Backends/Platform/GLFW3.cpp b/src/Backends/Platform/GLFW3.cpp index 74fb9ea1..df7b234b 100644 --- a/src/Backends/Platform/GLFW3.cpp +++ b/src/Backends/Platform/GLFW3.cpp @@ -14,8 +14,6 @@ #include "../Rendering.h" #include "../Shared/GLFW3.h" #include "../../Attributes.h" -#include "../../Main.h" -#include "../../Profile.h" #define DO_KEY(GLFW_KEY, BACKEND_KEY) \ case GLFW_KEY: \ @@ -26,6 +24,9 @@ static bool keyboard_state[BACKEND_KEYBOARD_TOTAL]; 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) { (void)window; @@ -126,10 +127,7 @@ static void WindowFocusCallback(GLFWwindow *window, int focused) { (void)window; - if (focused) - ActiveWindow(); - else - InactiveWindow(); + window_focus_callback(focused); } 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)count; - LoadProfile(paths[0]); + drag_and_drop_callback(paths[0]); } 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); } -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); if (glfwInit() == GL_TRUE) diff --git a/src/Backends/Platform/SDL2.cpp b/src/Backends/Platform/SDL2.cpp index 1301d899..777fa0ff 100644 --- a/src/Backends/Platform/SDL2.cpp +++ b/src/Backends/Platform/SDL2.cpp @@ -12,8 +12,6 @@ #include "../Rendering.h" #include "../Shared/SDL2.h" #include "../../Attributes.h" -#include "../../Main.h" -#include "../../Profile.h" #define DO_KEY(SDL_KEY, BACKEND_KEY) \ case SDL_KEY: \ @@ -26,8 +24,14 @@ static unsigned char *cursor_surface_pixels; static SDL_Surface *cursor_surface; 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_InitSubSystem(SDL_INIT_VIDEO) == 0) @@ -269,7 +273,7 @@ bool Backend_SystemTask(bool active) break; case SDL_DROPFILE: - LoadProfile(event.drop.file); + drag_and_drop_callback(event.drop.file); SDL_free(event.drop.file); break; @@ -277,11 +281,11 @@ bool Backend_SystemTask(bool active) switch (event.window.event) { case SDL_WINDOWEVENT_FOCUS_LOST: - InactiveWindow(); + window_focus_callback(false); break; case SDL_WINDOWEVENT_FOCUS_GAINED: - ActiveWindow(); + window_focus_callback(true); break; case SDL_WINDOWEVENT_RESIZED: diff --git a/src/Main.cpp b/src/Main.cpp index 090e48a4..161580ae 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -18,10 +18,14 @@ #include "KeyControl.h" #include "MyChar.h" #include "Organya.h" +#include "Profile.h" #include "Resource.h" #include "Sound.h" #include "Triangle.h" +void InactiveWindow(void); +void ActiveWindow(void); + std::string gModulePath; std::string gDataPath; @@ -42,6 +46,19 @@ static const char* const lpWindowName = "洞窟物語"; // "Cave Story" static const char* const lpWindowName = "Cave Story ~ Doukutsu Monogatari"; #endif +static void DragAndDropCallback(const char *path) +{ + LoadProfile(path); +} + +static void WindowFocusCallback(bool focus) +{ + if (focus) + ActiveWindow(); + else + InactiveWindow(); +} + // Framerate stuff static unsigned long CountFramePerSecound(void) { @@ -86,7 +103,7 @@ int main(int argc, char *argv[]) int i; - if (!Backend_Init()) + if (!Backend_Init(DragAndDropCallback, WindowFocusCallback)) return EXIT_FAILURE; // Get executable's path diff --git a/src/Main.h b/src/Main.h index 95753c27..b54972a6 100644 --- a/src/Main.h +++ b/src/Main.h @@ -16,7 +16,4 @@ extern BOOL gbUseJoystick; void PutFramePerSecound(void); -void InactiveWindow(void); -void ActiveWindow(void); - BOOL SystemTask(void);