shithub: cstory

Download patch

ref: 84d6b50bc2db92dc695177445b2eb39e4b861a39
parent: bdcb1f3a3ec75a473d495b8e39afbcb6edbf821f
author: Clownacy <Clownacy@users.noreply.github.com>
date: Mon Sep 7 23:46:19 EDT 2020

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.

--- a/src/Backends/Misc.h
+++ b/src/Backends/Misc.h
@@ -85,7 +85,7 @@
 	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);
--- 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 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 @@
 {
 	(void)window;
 
-	if (focused)
-		ActiveWindow();
-	else
-		InactiveWindow();
+	window_focus_callback(focused);
 }
 
 static void WindowSizeCallback(GLFWwindow *window, int width, int height)
@@ -144,7 +142,7 @@
 	(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 @@
 	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)
--- 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 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 @@
 				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 @@
 				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:
--- 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 ~ 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 i;
 
-	if (!Backend_Init())
+	if (!Backend_Init(DragAndDropCallback, WindowFocusCallback))
 		return EXIT_FAILURE;
 
 	// Get executable's path
--- a/src/Main.h
+++ b/src/Main.h
@@ -16,7 +16,4 @@
 
 void PutFramePerSecound(void);
 
-void InactiveWindow(void);
-void ActiveWindow(void);
-
 BOOL SystemTask(void);