Browse Source

Import for Window

Kajetan Johannes Hammerle 11 months ago
parent
commit
6d535c9a6d
5 changed files with 113 additions and 89 deletions
  1. 1 2
      CMakeLists.txt
  2. 2 2
      include/core/Network.h
  3. 40 16
      include/core/Window.h
  4. 53 52
      src/Window.c
  5. 17 17
      test/Main.c

+ 1 - 2
CMakeLists.txt

@@ -56,7 +56,7 @@ target_compile_options(gaming_core PUBLIC
 )
 target_compile_definitions(gaming_core 
     PUBLIC CORE_LOG_LEVEL=${LOG_LEVEL}
-    PRIVATE ${DEFINITIONS}
+    PUBLIC ${DEFINITIONS}
 )
 target_link_libraries(gaming_core 
     PRIVATE m core glfw ${LINK_OPTIONS}
@@ -82,4 +82,3 @@ install(TARGETS gaming_core FILE_SET HEADERS)
 
 add_executable(test ${SRC_TESTS})
 target_link_libraries(test PRIVATE gaming_core)
-target_compile_definitions(test PRIVATE ${DEFINITIONS})

+ 2 - 2
include/core/Network.h

@@ -1,8 +1,8 @@
 #ifndef CORE_NETWORK_H
 #define CORE_NETWORK_H
 
-#include "core/Buffer.h"
-#include "core/Types.h"
+#include <core/Buffer.h>
+#include <core/Types.h>
 
 typedef enum {
     CORE_PACKET_RELIABLE,

+ 40 - 16
include/core/Window.h

@@ -13,23 +13,23 @@ typedef struct {
     const char* name;
 } CoreWindowOptions;
 
-bool coreWindowOpen(const CoreWindowOptions* options);
-void coreWindowClose(void);
-void coreWindowShow(void);
-void coreWindowTrapCursor(void);
-void coreWindowFreeCursor(void);
-bool coreWindowIsCursorTrapped(void);
-const CoreIntVector2* coreWindowGetSize(void);
-bool CoreWindowSizeChanged(void);
-bool coreWindowShouldClose(void);
+bool coreOpenWindow(const CoreWindowOptions* options);
+void coreCloseWindow(void);
+void coreShowWindow(void);
+void coreTrapWindowCursor(void);
+void coreFreeWindowCursor(void);
+bool coreIsWindowCursorTrapped(void);
+const CoreIntVector2* coreGetWindowSize(void);
+bool coreHasWindowSizeChanged(void);
+bool coreShouldWindowClose(void);
 
-void coreWindowRunHandler(CoreWindowRunHandler wr, void* data);
-void coreWindowTickHandler(CoreWindowTickHandler t, void* data);
-void coreWindowRenderHandler(CoreWindowRenderHandler r, void* data);
-void coreWindowNanosPerTick(i64 nanos);
-void coreWindowRun(void);
-float coreWindowTicksPerSecond(void);
-float coreWindowFramesPerSecond(void);
+void coreSetWindowRunHandler(CoreWindowRunHandler wr, void* data);
+void coreSetWindowTickHandler(CoreWindowTickHandler t, void* data);
+void coreSetWindowRenderHandler(CoreWindowRenderHandler r, void* data);
+void coreSetWindowNanosPerTick(i64 nanos);
+void coreRunWindow(void);
+float coreGetWindowTicksPerSecond(void);
+float coreGetWindowFramesPerSecond(void);
 
 // namespace Input {
 //     void setLimit(int limit);
@@ -69,4 +69,28 @@ int coreControlsDownTime(CoreButton b);
 bool coreControlsReleased(CoreButton b);
 const char* coreControlsName(CoreButton b);
 
+#ifdef IMPORT_CORE
+#define WindowRunHandler CoreWindowRunHandler
+#define WindowTickHandler CoreWindowTickHandler
+#define WindowRenderHandler CoreWindowRenderHandler
+#define WindowOptions CoreWindowOptions
+#define openWindow coreOpenWindow
+#define closeWindow coreCloseWindow
+#define showWindow coreShowWindow
+#define trapWindowCursor coreTrapWindowCursor
+#define freeWindowCursor coreFreeWindowCursor
+#define isWindowCursorTrapped coreIsWindowCursorTrapped
+#define getWindowSize coreGetWindowSize
+#define hasWindowSizeChanged coreHasWindowSizeChanged
+#define shouldWindowClose coreShouldWindowClose
+#define setWindowRunHandler coreSetWindowRunHandler
+#define setWindowTickHandler coreSetWindowTickHandler
+#define setWindowRenderHandler coreSetWindowRenderHandler
+#define setWindowNanosPerTick coreSetWindowNanosPerTick
+#define runWindow coreRunWindow
+#define getWindowTicksPerSecond coreGetWindowTicksPerSecond
+#define getWindowFramesPerSecond coreGetWindowFramesPerSecond
+#define Button CoreButton
+#endif
+
 #endif

+ 53 - 52
src/Window.c

@@ -1,3 +1,4 @@
+#define IMPORT_CORE
 #include "core/Window.h"
 
 #include <GLFW/glfw3.h>
@@ -7,7 +8,7 @@
 #include <stdio.h>
 
 static bool dummyWindowRunHandler(void*) {
-    return !coreWindowShouldClose();
+    return !shouldWindowClose();
 }
 
 static void dummyWindowTickHandler(void*) {
@@ -17,11 +18,11 @@ static void dummyWindowRenderHandler(void*, float) {
 }
 
 static GLFWwindow* window = nullptr;
-static CoreIntVector2 size = {0};
+static IntVector2 size = {0};
 static bool sizeChanged = false;
-static CoreWindowRunHandler runHandler = dummyWindowRunHandler;
-static CoreWindowTickHandler tickHandler = dummyWindowTickHandler;
-static CoreWindowRenderHandler renderHandler = dummyWindowRenderHandler;
+static WindowRunHandler runHandler = dummyWindowRunHandler;
+static WindowTickHandler tickHandler = dummyWindowTickHandler;
+static WindowRenderHandler renderHandler = dummyWindowRenderHandler;
 static void* runHandlerData = nullptr;
 static void* tickHandlerData = nullptr;
 static void* renderHandlerData = nullptr;
@@ -56,21 +57,21 @@ typedef struct {
 static ButtonData buttons[BUTTONS] = {{.name = "unknown"}};
 static size_t buttonIndex = 1;
 #define KEYS (GLFW_KEY_LAST + 1)
-static CoreButton keyToButton[KEYS] = {0};
+static Button keyToButton[KEYS] = {0};
 #define GAMEPAD_BUTTONS (GLFW_GAMEPAD_BUTTON_LAST + 1)
-static CoreButton gamepadToButton[GAMEPAD_BUTTONS] = {0};
+static Button gamepadToButton[GAMEPAD_BUTTONS] = {0};
 #define MOUSE_BUTTONS (GLFW_MOUSE_BUTTON_LAST + 1)
-static CoreButton mouseToButton[MOUSE_BUTTONS] = {0};
-static CoreVector2 lastMousePosition = {0};
-static CoreVector2 mousePosition = {0};
+static Button mouseToButton[MOUSE_BUTTONS] = {0};
+static Vector2 lastMousePosition = {0};
+static Vector2 mousePosition = {0};
 static int activeController = -1;
 static GLFWgamepadstate lastControllerState = {0};
 
-static void onButton(CoreButton* map, int n, int key, int action) {
+static void onButton(Button* map, int n, int key, int action) {
     if(key < 0 || key >= n) {
         return;
     }
-    CoreButton b = map[key];
+    Button b = map[key];
     if(b == 0 || b >= buttonIndex) {
         return;
     }
@@ -123,7 +124,7 @@ static void onKey(GLFWwindow*, int key, int scancode, int action, int mods) {
     }
     input.add(codepoint);
     for(int i = input.getLength() - 1; i > inputCursor; i--) {
-        Core::swap(input[i], input[i - 1]);
+        ::swap(input[i], input[i - 1]);
     }
     inputCursor++;
 }
@@ -150,7 +151,7 @@ static void onMouseMove(GLFWwindow*, double x, double y) {
     mousePosition.data[1] = (float)y;
 }
 
-bool coreWindowOpen(const CoreWindowOptions* o) {
+bool openWindow(const WindowOptions* o) {
     if(!glfwInit()) {
         CORE_LOG_ERROR("could not initialize GLFW");
         return true;
@@ -167,7 +168,7 @@ bool coreWindowOpen(const CoreWindowOptions* o) {
         glfwCreateWindow(o->size.data[0], o->size.data[1], o->name, m, nullptr);
     if(window == nullptr) {
         CORE_LOG_ERROR("could not create window");
-        coreWindowClose();
+        closeWindow();
         return true;
     }
     size = o->size;
@@ -179,7 +180,7 @@ bool coreWindowOpen(const CoreWindowOptions* o) {
     return false;
 }
 
-void coreWindowClose(void) {
+void closeWindow(void) {
     if(window != nullptr) {
         glfwDestroyWindow(window);
         window = nullptr;
@@ -187,55 +188,55 @@ void coreWindowClose(void) {
     glfwTerminate();
 }
 
-void coreWindowShow(void) {
+void showWindow(void) {
     glfwShowWindow(window);
 }
 
-void coreWindowTrapCursor(void) {
+void trapWindowCursor(void) {
     glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
 }
 
-void coreWindowFreeCursor(void) {
+void freeWindowCursor(void) {
     glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
 }
 
-bool coreWindowIsCursorTrapped(void) {
+bool isWindowCursorTrapped(void) {
     return glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED;
 }
 
-const CoreIntVector2* coreWindowGetSize(void) {
+const IntVector2* getWindowSize(void) {
     return &size;
 }
 
-bool CoreWindowSizeChanged(void) {
+bool hasWindowSizeChanged(void) {
     return sizeChanged;
 }
 
-bool coreWindowShouldClose(void) {
+bool shouldWindowClose(void) {
     return glfwWindowShouldClose(window);
 }
 
-void coreWindowRunHandler(CoreWindowRunHandler wr, void* data) {
+void setWindowRunHandler(WindowRunHandler wr, void* data) {
     runHandlerData = data;
     runHandler = wr == nullptr ? dummyWindowRunHandler : wr;
 }
 
-void coreWindowTickHandler(CoreWindowTickHandler t, void* data) {
+void setWindowTickHandler(WindowTickHandler t, void* data) {
     tickHandlerData = data;
     tickHandler = t == nullptr ? dummyWindowTickHandler : t;
 }
 
-void coreWindowRenderHandler(CoreWindowRenderHandler r, void* data) {
+void setWindowRenderHandler(WindowRenderHandler r, void* data) {
     renderHandlerData = data;
     renderHandler = r == nullptr ? dummyWindowRenderHandler : r;
 }
 
-void coreWindowNanosPerTick(i64 nanos) {
+void setWindowNanosPerTick(i64 nanos) {
     nanosPerTick = nanos <= 0 ? 1 : nanos;
 }
 
 static i64 updateClock(Clock* c) {
-    i64 nanos = coreGetNanos();
+    i64 nanos = getNanos();
     if(nanos < 0) {
         CORE_LOG_WARNING("Cannot get nanos, using default");
         nanos = 10000000 + c->last;
@@ -269,7 +270,7 @@ static void checkGamepad() {
         return;
     }
     for(int i = 0; i <= GLFW_GAMEPAD_BUTTON_LAST; i++) {
-        CoreButton b = gamepadToButton[i];
+        Button b = gamepadToButton[i];
         if(b == 0 || b >= buttonIndex) {
             return;
         }
@@ -318,10 +319,10 @@ static void tick() {
     lastMousePosition = mousePosition;
 }
 
-void coreWindowRun(void) {
+void runWindow(void) {
     glfwMakeContextCurrent(window);
-    tps.last = coreGetNanos();
-    fps.last = coreGetNanos();
+    tps.last = getNanos();
+    fps.last = getNanos();
     i64 lag = 0;
     while(runHandler(runHandlerData)) {
         lag += updateClock(&fps);
@@ -334,11 +335,11 @@ void coreWindowRun(void) {
     }
 }
 
-float coreWindowTicksPerSecond(void) {
+float getWindowTicksPerSecond(void) {
     return (1000000000.0f * CLOCK_SIZE) / (float)tps.sum;
 }
 
-float coreWindowFramesPerSecond(void) {
+float getWindowFramesPerSecond(void) {
     return (1000000000.0f * CLOCK_SIZE) / (float)fps.sum;
 }
 
@@ -420,49 +421,49 @@ const List<uint32>& Window::Input::getUnicode() {
     return input;
 }*/
 
-CoreButton coreControlsAdd(const char* name) {
+Button coreControlsAdd(const char* name) {
     if(buttonIndex >= BUTTONS) {
         return 0;
     }
-    CoreButton b = buttonIndex++;
+    Button b = buttonIndex++;
     snprintf(buttons[b].name, sizeof(buttons[b].name), "%s", name);
     return b;
 }
 
-void coreControlsBindKey(CoreButton b, int key) {
+void coreControlsBindKey(Button b, int key) {
     if(key >= 0 && key < KEYS) {
         keyToButton[key] = b;
     }
 }
 
-void coreControlsBindGamepad(CoreButton b, int gamepadButton) {
+void coreControlsBindGamepad(Button b, int gamepadButton) {
     if(gamepadButton >= 0 && gamepadButton < GAMEPAD_BUTTONS) {
         gamepadToButton[gamepadButton] = b;
     }
 }
 
-void coreControlsBindMouse(CoreButton b, int mouseButton) {
+void coreControlsBindMouse(Button b, int mouseButton) {
     if(mouseButton >= 0 && mouseButton < MOUSE_BUTTONS) {
         mouseToButton[mouseButton] = b;
     }
 }
 
-CoreVector2 coreControlsLastMousePosition(void) {
+Vector2 coreControlsLastMousePosition(void) {
     return lastMousePosition;
 }
 
-CoreVector2 coreControlsMousePosition(void) {
+Vector2 coreControlsMousePosition(void) {
     return mousePosition;
 }
 
-CoreVector2 coreControlsLeftGamepadAxis(void) {
-    return (CoreVector2){{lastControllerState.axes[GLFW_GAMEPAD_AXIS_LEFT_X],
-                          lastControllerState.axes[GLFW_GAMEPAD_AXIS_LEFT_Y]}};
+Vector2 coreControlsLeftGamepadAxis(void) {
+    return (Vector2){{lastControllerState.axes[GLFW_GAMEPAD_AXIS_LEFT_X],
+                      lastControllerState.axes[GLFW_GAMEPAD_AXIS_LEFT_Y]}};
 }
 
-CoreVector2 coreControlsRightGamepadAxis(void) {
-    return (CoreVector2){{lastControllerState.axes[GLFW_GAMEPAD_AXIS_RIGHT_X],
-                          lastControllerState.axes[GLFW_GAMEPAD_AXIS_RIGHT_Y]}};
+Vector2 coreControlsRightGamepadAxis(void) {
+    return (Vector2){{lastControllerState.axes[GLFW_GAMEPAD_AXIS_RIGHT_X],
+                      lastControllerState.axes[GLFW_GAMEPAD_AXIS_RIGHT_Y]}};
 }
 
 float coreControlsLeftGamepadTrigger(void) {
@@ -473,22 +474,22 @@ float coreControlsRightGamepadTrigger(void) {
     return lastControllerState.axes[GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER];
 }
 
-static const ButtonData* getButton(CoreButton id) {
+static const ButtonData* getButton(Button id) {
     return buttons + (id >= buttonIndex ? 0 : id);
 }
 
-bool coreControlsDown(CoreButton b) {
+bool coreControlsDown(Button b) {
     return getButton(b)->downTime > 0;
 }
 
-int coreControlsDownTime(CoreButton b) {
+int coreControlsDownTime(Button b) {
     return getButton(b)->downTime;
 }
 
-bool coreControlsReleased(CoreButton b) {
+bool coreControlsReleased(Button b) {
     return getButton(b)->released;
 }
 
-const char* coreControlsName(CoreButton b) {
+const char* coreControlsName(Button b) {
     return getButton(b)->name;
 }

+ 17 - 17
test/Main.c

@@ -1,3 +1,4 @@
+#define IMPORT_CORE
 #include <GLFW/glfw3.h>
 #include <core/Logger.h>
 #include <core/Utility.h>
@@ -8,18 +9,17 @@
 #include "core/Window.h"
 
 static int ticks = 200;
-static CoreButton closeButton = 0;
-static CoreButton testButton = 0;
+static Button closeButton = 0;
+static Button testButton = 0;
 
 static bool isRunning(void*) {
-    return !coreWindowShouldClose() && ticks > 0 &&
-           !coreControlsDown(closeButton);
+    return !shouldWindowClose() && ticks > 0 && !coreControlsDown(closeButton);
 }
 
 static void tick(void*) {
     ticks -= ticks > 0;
-    printf("TPS: %.3f\nFPS: %.3f\n", (double)coreWindowTicksPerSecond(),
-           (double)coreWindowFramesPerSecond());
+    printf("TPS: %.3f\nFPS: %.3f\n", (double)getWindowTicksPerSecond(),
+           (double)getWindowFramesPerSecond());
     printf("%12s | Down: %d | DownTime: %3d | Released: %d\n",
            coreControlsName(closeButton), coreControlsDown(closeButton),
            coreControlsDownTime(closeButton),
@@ -28,7 +28,7 @@ static void tick(void*) {
            coreControlsName(testButton), coreControlsDown(testButton),
            coreControlsDownTime(testButton), coreControlsReleased(testButton));
 
-    CoreVector2 mouse = coreControlsLastMousePosition();
+    Vector2 mouse = coreControlsLastMousePosition();
     printf("Mouse: %.2f %.2f\n", (double)mouse.data[0], (double)mouse.data[1]);
 }
 
@@ -36,8 +36,8 @@ static void render(void*, float) {
 }
 
 static void testWindow(void) {
-    CoreWindowOptions options = {{{800, 480}}, false, "Test"};
-    if(coreWindowOpen(&options)) {
+    WindowOptions options = {{{800, 480}}, false, "Test"};
+    if(openWindow(&options)) {
         return;
     }
 
@@ -46,13 +46,13 @@ static void testWindow(void) {
     testButton = coreControlsAdd("Test Button");
     coreControlsBindKey(testButton, GLFW_KEY_T);
 
-    coreWindowShow();
-    coreWindowRunHandler(isRunning, nullptr);
-    coreWindowTickHandler(tick, nullptr);
-    coreWindowRenderHandler(render, nullptr);
-    coreWindowNanosPerTick(50000000);
-    coreWindowRun();
-    coreWindowClose();
+    showWindow();
+    setWindowRunHandler(isRunning, nullptr);
+    setWindowTickHandler(tick, nullptr);
+    setWindowRenderHandler(render, nullptr);
+    setWindowNanosPerTick(50000000);
+    runWindow();
+    closeWindow();
 }
 
 int main(int argAmount, char** args) {
@@ -66,7 +66,7 @@ int main(int argAmount, char** args) {
     } else if(strcmp("window", args[2]) == 0) {
         testWindow();
     }
-    coreFinalizeTests();
+    finalizeTests();
     corePrintMemoryReport();
     return 0;
 }