aboutsummaryrefslogtreecommitdiff
path: root/platform
diff options
context:
space:
mode:
authorNicholas Tay <nick@windblume.net>2022-05-10 00:40:10 +1000
committerNicholas Tay <nick@windblume.net>2022-05-10 00:47:38 +1000
commit6a1e6a7d6b6a3d987f2fccb3c06f4f5da071f504 (patch)
tree3899a31897c01323fb5d896b08412aa45e939885 /platform
parent5270088f730a7e30155a642dcd5c4e9a80055d7a (diff)
downloadclak-6a1e6a7d6b6a3d987f2fccb3c06f4f5da071f504.tar.gz
clak-6a1e6a7d6b6a3d987f2fccb3c06f4f5da071f504.tar.bz2
clak-6a1e6a7d6b6a3d987f2fccb3c06f4f5da071f504.zip
Dynamic load boards as DLL (windows only for now)
The .h files are pretty weird, should look at other C projects to see how they load plugins. This function pointer business with typedefs is kinda weird, not sure where they should live.
Diffstat (limited to '')
-rw-r--r--platform/platform.h5
-rw-r--r--platform/win32.c38
2 files changed, 43 insertions, 0 deletions
diff --git a/platform/platform.h b/platform/platform.h
index da1efa6..fd9b9bb 100644
--- a/platform/platform.h
+++ b/platform/platform.h
@@ -1,6 +1,11 @@
#ifndef CLAK_PLATFORM_H_
#define CLAK_PLATFORM_H_
+#include <stdbool.h>
+
+#include "../board/board.h"
+
+bool load_board(char *board_name, fn_board_on_down *on_down, fn_board_on_down *on_up);
bool sound_init(float volume);
void sound_play(unsigned char *buffer);
void keyboard_unhook(void);
diff --git a/platform/win32.c b/platform/win32.c
index 90cd37c..cf22718 100644
--- a/platform/win32.c
+++ b/platform/win32.c
@@ -2,6 +2,9 @@
#include <stdbool.h>
#include <windows.h>
+#include "platform.h"
+#include "../board/board.h"
+
/* Required callbacks */
extern void keyboard_on_down(void);
extern void keyboard_on_up(void);
@@ -77,4 +80,39 @@ void enter_idle(void)
exit(1);
}
}
+}
+
+bool load_board(char *board_name, fn_board_on_down *on_down, fn_board_on_down *on_up)
+{
+ char dll_name[100];
+ int r = snprintf(dll_name, 100, "./board/%s.dll", board_name);
+ if (r < 0 || r >= 100) {
+ printf("ERROR: Invalid board name.\n");
+ return false;
+ }
+
+ HMODULE board_module = LoadLibrary(dll_name);
+ if (board_module == NULL) {
+ printf("ERROR: Could not load board module.\n");
+ return false;
+ }
+ fn_board_init board_init = (fn_board_init) (void (*)(void)) GetProcAddress(board_module, "board_init");
+ if (board_init == NULL) {
+ printf("ERROR: No board down function could be loaded.\n");
+ return false;
+ }
+ board_init(&sound_play);
+
+ *on_down = (fn_board_on_down) GetProcAddress(board_module, "board_on_down");
+ if (on_down == NULL) {
+ printf("ERROR: No board down function could be loaded.\n");
+ return false;
+ }
+ *on_up = (fn_board_on_up) GetProcAddress(board_module, "board_on_up");
+ if (on_up == NULL) {
+ printf("ERROR: No board up function could be loaded.\n");
+ return false;
+ }
+
+ return true;
} \ No newline at end of file