commit 8469482e9d3da2b0650be69300d1676052350809
parent e2a7c00e1ab3c69adbe59276363ec56f7d543bd1
Author: Amin Mesbah <dev@aminmesbah.com>
Date:   Sun, 30 Jun 2019 18:58:26 -0700
Fix windows platform layer
Diffstat:
1 file changed, 38 insertions(+), 60 deletions(-)
diff --git a/src/platform_windows.c b/src/platform_windows.c
@@ -11,6 +11,16 @@
 #include "platform.h"
 #include "platform_windows.h"
 
+// TODO: map this in a nicer way
+global_variable int platform_input_map[NUM_GAME_BUTTONS] = {
+    [BTN_LEFT] = GLFW_KEY_LEFT,
+    [BTN_RIGHT] = GLFW_KEY_RIGHT,
+    [BTN_UP] = GLFW_KEY_UP,
+    [BTN_DOWN] = GLFW_KEY_DOWN,
+    [BTN_JUMP] = GLFW_KEY_S,
+    [BTN_DEBUG_FLOAT] = GLFW_KEY_F11,
+};
+
 int main(void)
 {
     glfwSetErrorCallback(error_callback);
@@ -26,7 +36,7 @@ int main(void)
     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
     glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
 
-    GLFWwindow* window = glfwCreateWindow(PLATFORM_SCR_WIDTH, PLATFORM_SCR_HEIGHT, "A Game", NULL, NULL);
+    GLFWwindow *window = glfwCreateWindow(PLATFORM_SCR_WIDTH, PLATFORM_SCR_HEIGHT, "A Game", NULL, NULL);
     if (!window)
     {
         fprintf(stderr, "GLFW window creation failed\n");
@@ -112,81 +122,49 @@ int main(void)
     return 0;
 }
 
-internal void error_callback(int error, const char* description)
+internal void error_callback(int error, const char *description)
 {
     fprintf(stderr, "Error: %s\n", description);
 }
 
-internal void framebuffer_size_callback(GLFWwindow* window, int width, int height)
+internal void framebuffer_size_callback(GLFWwindow *window, int width, int height)
 {
     glViewport(0, 0, width, height);
 }
 
-internal void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
+internal void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
 {
-    struct GameInput *game_input = (struct GameInput*)glfwGetWindowUserPointer(window);
+    struct GameInput *game_input = (struct GameInput *)glfwGetWindowUserPointer(window);
 
-    enum InputKeyAction game_input_key_action = 0;
-    switch (action)
+    enum GameButton game_button = NULL_GAME_BUTTON;
+    // TODO: Determine the button in a nicer way
+    for (enum GameButton b = 0; b < NUM_GAME_BUTTONS; b++)
     {
-        case GLFW_PRESS:
-            game_input_key_action = KEY_PRESS;
-            break;
-        case GLFW_RELEASE:
-            game_input_key_action = KEY_RELEASE;
-            break;
-        case GLFW_REPEAT:
-            game_input_key_action = KEY_REPEAT;
-            break;
-        default:
-            exit(1);
-            break;
-    }
-
-    enum InputKeyAction* game_key = NULL;
-    switch (key)
-    {
-        case GLFW_KEY_LEFT:
-            game_key = &(game_input->key_left);
-            break;
-        case GLFW_KEY_RIGHT:
-            game_key = &(game_input->key_right);
-            break;
-        case GLFW_KEY_UP:
-            game_key = &(game_input->key_up);
-            break;
-        case GLFW_KEY_DOWN:
-            game_key = &(game_input->key_down);
-            break;
-        case GLFW_KEY_S:
-            game_key = &(game_input->key_jump);
-            break;
-        default:
-            break;
+        if (platform_input_map[b] == key)
+        {
+            game_button = b;
+        }
     }
-
-    if (game_key)
+    if (game_button != NULL_GAME_BUTTON)
     {
-        *game_key = game_input_key_action;
+        switch (action)
+        {
+            case GLFW_PRESS:
+                game_input->button_states |= (1 << game_button);
+                break;
+            case GLFW_RELEASE:
+                game_input->button_states &= ~(1 << game_button);
+                break;
+            case GLFW_REPEAT:
+                // Do nothing
+                break;
+            default:
+                exit(1);
+                break;
+        }
     }
 }
 
-//internal time_t file_get_modified_time(char *file_path)
-//{
-//    time_t mtime = 0;
-//    struct stat file_status = {0};
-//    if (stat(file_path, &file_status) == 0)
-//    {
-//        //printf("File: %s last modified: %s\n", file_path, ctime(&file_status.st_mtime));
-//        mtime = file_status.st_mtime;
-//    }
-//    else
-//    {
-//        fprintf(stderr, "ERROR: Failed to stat file: %s\n", file_path);
-//    }
-//    return mtime;
-//}
-
 internal PLATFORM_READ_ENTIRE_FILE(windows_read_entire_file)
 {
     FILE *handle = fopen(file_path, "rb");