Browse Source

Controls and tests, improved tasks

Kajetan Johannes Hammerle 11 months ago
parent
commit
3e3c7788d4
4 changed files with 96 additions and 107 deletions
  1. 14 16
      include/core/Window.h
  2. 44 56
      src/Window.c
  3. 8 27
      tasks
  4. 30 8
      test/Main.c

+ 14 - 16
include/core/Window.h

@@ -52,23 +52,21 @@ float coreWindowFramesPerSecond(void);
 
 typedef size_t CoreButton;
 
-void coreInitButton(void);
-void coreDestroyButton(void);
-CoreButton coreButtonAdd(const char* name);
-void coreButtonBindKey(CoreButton b, int key);
-void coreButtonBindGamepad(CoreButton b, int gamepadButton);
-void coreButtonBindMouse(CoreButton b, int mouseButton);
+CoreButton coreControlsAdd(const char* name);
+void coreControlsBindKey(CoreButton b, int key);
+void coreControlsBindGamepad(CoreButton b, int gamepadButton);
+void coreControlsBindMouse(CoreButton b, int mouseButton);
 
-CoreVector2 coreButtonLastMousePosition(void);
-CoreVector2 coreButtonMousePosition(void);
-CoreVector2 coreButtonLeftGamepadAxis(void);
-CoreVector2 coreButtonRightGamepadAxis(void);
-float coreButtonLeftGamepadTrigger(void);
-float coreButtonRightGamepadTrigger(void);
+CoreVector2 coreControlsLastMousePosition(void);
+CoreVector2 coreControlsMousePosition(void);
+CoreVector2 coreControlsLeftGamepadAxis(void);
+CoreVector2 coreControlsRightGamepadAxis(void);
+float coreControlsLeftGamepadTrigger(void);
+float coreControlsRightGamepadTrigger(void);
 
-bool coreButtonDown(CoreButton b);
-int coreButtonDownTime(CoreButton b);
-bool coreButtonReleased(CoreButton b);
-const char* coreButtonName(CoreButton b);
+bool coreControlsDown(CoreButton b);
+int coreControlsDownTime(CoreButton b);
+bool coreControlsReleased(CoreButton b);
+const char* coreControlsName(CoreButton b);
 
 #endif

+ 44 - 56
src/Window.c

@@ -17,7 +17,7 @@ static void dummyWindowRenderHandler(void*, float) {
 }
 
 static GLFWwindow* window = nullptr;
-static CoreIntVector2 size = {0, 0};
+static CoreIntVector2 size = {0};
 static bool sizeChanged = false;
 static CoreWindowRunHandler runHandler = dummyWindowRunHandler;
 static CoreWindowTickHandler tickHandler = dummyWindowTickHandler;
@@ -53,23 +53,25 @@ typedef struct {
     int controllerDown;
 } ButtonData;
 #define BUTTONS 100
-static ButtonData buttons[BUTTONS] = {0};
-static size_t buttonIndex = 0;
-static CoreHashMap keyToButtonId = {0};     // int -> CoreButton
-static CoreHashMap gamepadToButtonId = {0}; // int -> CoreButton
-static CoreHashMap mouseToButtonId = {0};   // int -> CoreButton
+static ButtonData buttons[BUTTONS] = {{.name = "unknown"}};
+static size_t buttonIndex = 1;
+#define KEYS (GLFW_KEY_LAST + 1)
+static CoreButton keyToButton[KEYS] = {0};
+#define GAMEPAD_BUTTONS (GLFW_GAMEPAD_BUTTON_LAST + 1)
+static CoreButton 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 int activeController = -1;
 static GLFWgamepadstate lastControllerState = {0};
 
-static void onButton(CoreHashMap* map, int key, int action) {
-    CoreButton* bp = coreHashMapSearch(map, int, key, CoreButton);
-    if(bp == nullptr) {
+static void onButton(CoreButton* map, int n, int key, int action) {
+    if(key < 0 || key >= n) {
         return;
     }
-    CoreButton b = *bp;
-    if(b >= buttonIndex) {
+    CoreButton b = map[key];
+    if(b == 0 || b >= buttonIndex) {
         return;
     }
     if(action == GLFW_RELEASE) {
@@ -112,7 +114,7 @@ static void onKey(GLFWwindow*, int key, int scancode, int action, int mods) {
     // if(inputActive) {
     //     handleInputKey(key, action);
     // }
-    onButton(&keyToButtonId, key, action);
+    onButton(keyToButton, KEYS, key, action);
 }
 
 /*static void addUnicode(uint32 codepoint) {
@@ -140,7 +142,7 @@ static void onResize(GLFWwindow*, int width, int height) {
 
 static void onMouse(GLFWwindow*, int button, int action, int mods) {
     (void)mods;
-    onButton(&mouseToButtonId, button, action);
+    onButton(mouseToButton, MOUSE_BUTTONS, button, action);
 }
 
 static void onMouseMove(GLFWwindow*, double x, double y) {
@@ -174,7 +176,6 @@ bool coreWindowOpen(const CoreWindowOptions* o) {
     glfwSetFramebufferSizeCallback(window, onResize);
     glfwSetMouseButtonCallback(window, onMouse);
     glfwSetCursorPosCallback(window, onMouseMove);
-
     return false;
 }
 
@@ -268,13 +269,8 @@ static void checkGamepad() {
         return;
     }
     for(int i = 0; i <= GLFW_GAMEPAD_BUTTON_LAST; i++) {
-        CoreButton* bp =
-            coreHashMapSearch(&gamepadToButtonId, int, i, CoreButton);
-        if(bp == nullptr) {
-            return;
-        }
-        CoreButton b = *bp;
-        if(b >= buttonIndex) {
+        CoreButton b = gamepadToButton[i];
+        if(b == 0 || b >= buttonIndex) {
             return;
         }
         if(!lastControllerState.buttons[i] && state.buttons[i]) {
@@ -424,21 +420,7 @@ const List<uint32>& Window::Input::getUnicode() {
     return input;
 }*/
 
-void coreInitButton(void) {
-    coreButtonAdd("unknown");
-    coreInitHashMap(&keyToButtonId, sizeof(int), sizeof(CoreButton));
-    coreInitHashMap(&gamepadToButtonId, sizeof(int), sizeof(CoreButton));
-    coreInitHashMap(&mouseToButtonId, sizeof(int), sizeof(CoreButton));
-}
-
-void coreDestroyButton(void) {
-    buttonIndex = 0;
-    coreDestroyHashMap(&keyToButtonId);
-    coreDestroyHashMap(&gamepadToButtonId);
-    coreDestroyHashMap(&mouseToButtonId);
-}
-
-CoreButton coreButtonAdd(const char* name) {
+CoreButton coreControlsAdd(const char* name) {
     if(buttonIndex >= BUTTONS) {
         return 0;
     }
@@ -447,41 +429,47 @@ CoreButton coreButtonAdd(const char* name) {
     return b;
 }
 
-void coreButtonBindKey(CoreButton b, int key) {
-    coreHashMapPut(&keyToButtonId, int, key, CoreButton, b);
+void coreControlsBindKey(CoreButton b, int key) {
+    if(key >= 0 && key < KEYS) {
+        keyToButton[key] = b;
+    }
 }
 
-void coreButtonBindGamepad(CoreButton b, int gamepadButton) {
-    coreHashMapPut(&gamepadToButtonId, int, gamepadButton, CoreButton, b);
+void coreControlsBindGamepad(CoreButton b, int gamepadButton) {
+    if(gamepadButton >= 0 && gamepadButton < GAMEPAD_BUTTONS) {
+        gamepadToButton[gamepadButton] = b;
+    }
 }
 
-void coreButtonBindMouse(CoreButton b, int mouseButton) {
-    coreHashMapPut(&mouseToButtonId, int, mouseButton, CoreButton, b);
+void coreControlsBindMouse(CoreButton b, int mouseButton) {
+    if(mouseButton >= 0 && mouseButton < MOUSE_BUTTONS) {
+        mouseToButton[mouseButton] = b;
+    }
 }
 
-CoreVector2 coreButtonLastMousePosition(void) {
+CoreVector2 coreControlsLastMousePosition(void) {
     return lastMousePosition;
 }
 
-CoreVector2 coreButtonMousePosition(void) {
+CoreVector2 coreControlsMousePosition(void) {
     return mousePosition;
 }
 
-CoreVector2 coreButtonLeftGamepadAxis(void) {
-    return (CoreVector2){lastControllerState.axes[GLFW_GAMEPAD_AXIS_LEFT_X],
-                         lastControllerState.axes[GLFW_GAMEPAD_AXIS_LEFT_Y]};
+CoreVector2 coreControlsLeftGamepadAxis(void) {
+    return (CoreVector2){{lastControllerState.axes[GLFW_GAMEPAD_AXIS_LEFT_X],
+                          lastControllerState.axes[GLFW_GAMEPAD_AXIS_LEFT_Y]}};
 }
 
-CoreVector2 coreButtonRightGamepadAxis(void) {
-    return (CoreVector2){lastControllerState.axes[GLFW_GAMEPAD_AXIS_RIGHT_X],
-                         lastControllerState.axes[GLFW_GAMEPAD_AXIS_RIGHT_Y]};
+CoreVector2 coreControlsRightGamepadAxis(void) {
+    return (CoreVector2){{lastControllerState.axes[GLFW_GAMEPAD_AXIS_RIGHT_X],
+                          lastControllerState.axes[GLFW_GAMEPAD_AXIS_RIGHT_Y]}};
 }
 
-float coreButtonLeftGamepadTrigger(void) {
+float coreControlsLeftGamepadTrigger(void) {
     return lastControllerState.axes[GLFW_GAMEPAD_AXIS_LEFT_TRIGGER];
 }
 
-float coreButtonRightGamepadTrigger(void) {
+float coreControlsRightGamepadTrigger(void) {
     return lastControllerState.axes[GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER];
 }
 
@@ -489,18 +477,18 @@ static const ButtonData* getButton(CoreButton id) {
     return buttons + (id >= buttonIndex ? 0 : id);
 }
 
-bool coreButtonDown(CoreButton b) {
+bool coreControlsDown(CoreButton b) {
     return getButton(b)->downTime > 0;
 }
 
-int coreButtonDownTime(CoreButton b) {
+int coreControlsDownTime(CoreButton b) {
     return getButton(b)->downTime;
 }
 
-bool coreButtonReleased(CoreButton b) {
+bool coreControlsReleased(CoreButton b) {
     return getButton(b)->released;
 }
 
-const char* coreButtonName(CoreButton b) {
+const char* coreControlsName(CoreButton b) {
     return getButton(b)->name;
 }

+ 8 - 27
tasks

@@ -16,8 +16,6 @@ printHelpExit() {
     echo "$0 test <type>     | run the tests"
     echo "$0 valgrind <type> | run the tests with valgrind"
     echo "$0 coverage        | generate code coverage"
-    echo "$0 performance     | run the performance tests"
-    echo "$0 stats           | run the performance tests with stats"
     echo "$0 time            | check build time"
     exit 0
 }
@@ -36,11 +34,10 @@ test_debug=false
 test_release=false
 
 valgrind=""
-performance=false
 time=false
 install=false
 coverage=false
-stats=false
+testArgs="test"
 
 export CMAKE_EXPORT_COMPILE_COMMANDS=true
 
@@ -75,13 +72,17 @@ elif [ "$task" = "test" ]; then
     elif [ "$type" = "release" ]; then
         build_release=true
         test_release=true
+    elif [ "$type" = "window" ]; then
+        build_debug=true
+        test_debug=true
+        testArgs="window"
     elif [ "$type" = "all" ]; then
         build_debug=true
         test_debug=true
         build_release=true
         test_release=true
     else
-        echo "Valid test types are: debug, release, all"
+        echo "Valid test types are: debug, release, window, all"
         printHelpExit
     fi
 elif [ "$task" = "valgrind" ]; then
@@ -102,13 +103,6 @@ elif [ "$task" = "valgrind" ]; then
         printHelpExit
     fi
     valgrind="valgrind"
-elif [ "$task" = "performance" ]; then
-    build_profile=true
-    performance=true
-elif [ "$task" = "stats" ]; then
-    build_profile=true
-    performance=true
-    stats=true
 elif [ "$task" = "time" ]; then
     build_release=true
     time=true
@@ -141,7 +135,7 @@ if $install; then
 fi
 
 function runTests() {
-    LLVM_PROFILE_FILE="default.profraw" $valgrind ./test ../test/resources $valgrind || true
+    LLVM_PROFILE_FILE="default.profraw" $valgrind ./test ../test/resources $testArgs || true
 }
 
 if $test_debug; then
@@ -154,19 +148,6 @@ if $test_release; then
     runTests
     cd ..
 fi
-if $performance; then
-    cd build_profile
-    if $stats; then
-        user=$(whoami)
-        sudo perf record ./performance
-        sudo chown $user:$user perf.data
-        sudo chown $user:$user default.profraw
-        perf report
-    else
-        ./performance
-    fi
-    cd ..
-fi
 if $time; then
     lines=$(cat build_release/.ninja_log | grep "^[0-9]")
 
@@ -196,6 +177,6 @@ if $coverage; then
     else
         files=$(find build_debug -name *.profraw)
         llvm-profdata-16 merge -sparse $files -o build_debug/default.profdata
-        llvm-cov-16 show ./build_debug/test -instr-profile=build_debug/default.profdata --ignore-filename-regex="test/" -line-coverage-lt=100
+        llvm-cov-16 show ./build_debug/test -instr-profile=build_debug/default.profdata --ignore-filename-regex="(test/)|(Window.c)" -line-coverage-lt=100
     fi
 fi 

+ 30 - 8
test/Main.c

@@ -2,30 +2,50 @@
 #include <core/Logger.h>
 #include <core/Utility.h>
 #include <stdio.h>
+#include <string.h>
 
 #include "Tests.h"
 #include "core/Window.h"
 
-static int ticks = 40;
+static int ticks = 200;
+static CoreButton closeButton = 0;
+static CoreButton testButton = 0;
 
 static bool isRunning(void*) {
-    return !coreWindowShouldClose() && ticks > 0;
+    return !coreWindowShouldClose() && ticks > 0 &&
+           !coreControlsDown(closeButton);
 }
 
 static void tick(void*) {
     ticks -= ticks > 0;
     printf("TPS: %.3f\nFPS: %.3f\n", (double)coreWindowTicksPerSecond(),
            (double)coreWindowFramesPerSecond());
+    printf("%12s | Down: %d | DownTime: %3d | Released: %d\n",
+           coreControlsName(closeButton), coreControlsDown(closeButton),
+           coreControlsDownTime(closeButton),
+           coreControlsReleased(closeButton));
+    printf("%12s | Down: %d | DownTime: %3d | Released: %d\n",
+           coreControlsName(testButton), coreControlsDown(testButton),
+           coreControlsDownTime(testButton), coreControlsReleased(testButton));
+
+    CoreVector2 mouse = coreControlsLastMousePosition();
+    printf("Mouse: %.2f %.2f\n", (double)mouse.data[0], (double)mouse.data[1]);
 }
 
 static void render(void*, float) {
 }
 
 static void testWindow(void) {
-    CoreWindowOptions options = {{800, 480}, false, "Test"};
+    CoreWindowOptions options = {{{800, 480}}, false, "Test"};
     if(coreWindowOpen(&options)) {
         return;
     }
+
+    closeButton = coreControlsAdd("Close Button");
+    coreControlsBindKey(closeButton, GLFW_KEY_Q);
+    testButton = coreControlsAdd("Test Button");
+    coreControlsBindKey(testButton, GLFW_KEY_T);
+
     coreWindowShow();
     coreWindowRunHandler(isRunning, nullptr);
     coreWindowTickHandler(tick, nullptr);
@@ -37,13 +57,15 @@ static void testWindow(void) {
 
 int main(int argAmount, char** args) {
     (void)args;
-    if(argAmount < 2) {
-        CORE_LOG_ERROR("missing path to images");
+    if(argAmount < 3) {
+        CORE_LOG_ERROR("missing path to images and/or mode");
         return 0;
+    } else if(strcmp("test", args[2]) == 0) {
+        coreTestImageReader(args[1]);
+        coreTestNetwork();
+    } else if(strcmp("window", args[2]) == 0) {
+        testWindow();
     }
-    // coreTestImageReader(args[1]);
-    // coreTestNetwork();
-    testWindow();
     coreFinalizeTests();
     corePrintMemoryReport();
     return 0;