]> git.lizzy.rs Git - shadowclad.git/commitdiff
Restructure keyboard input handling
authoroutfrost <kotlet.bahn@gmail.com>
Sat, 27 Jun 2020 00:45:16 +0000 (02:45 +0200)
committeroutfrost <kotlet.bahn@gmail.com>
Sat, 27 Jun 2020 00:45:16 +0000 (02:45 +0200)
17 files changed:
Makefile
src/engine/asset.h
src/engine/engine.c
src/engine/engine.h
src/engine/geometry.h
src/engine/input.c [new file with mode: 0644]
src/engine/input.h [new file with mode: 0644]
src/engine/logger.h
src/engine/performance.h
src/engine/render.h
src/engine/scene.h
src/engine/string.h
src/engine/tga.h
src/engine/ui.h
src/game/game.c
src/game/input.c
src/game/input.h

index 61c5b433f5450eec9bbc2d43be203693f023250b..0922628921980f3fd5b96961bd065809a39385b2 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -17,6 +17,7 @@ sources ::= main.c \
             engine/asset.c \
             engine/engine.c \
             engine/geometry.c \
+            engine/input.c \
             engine/logger.c \
             engine/performance.c \
             engine/render.c \
index 21ecadbc9574225c9271883ea4a4d295b538ae46..dbe713dc8d73fecd0850ea9f24dbc5a05ff1db75 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef ASSET_H_
-#define ASSET_H_
+#ifndef ENGINE_ASSET_H_
+#define ENGINE_ASSET_H_
 
 #include <stddef.h>
 #include <GL/gl.h>
@@ -40,4 +40,4 @@ struct Material {
 
 const Solid* importSolid(const char* path);
 
-#endif // ASSET_H_
+#endif // ENGINE_ASSET_H_
index 81abb235a9b95cdb78bf323b88860759f1f87af3..17155334fc8862a8b4ac1d052c3618b7ef2c6da1 100644 (file)
@@ -5,6 +5,7 @@
 #include <GL/glxew.h>
 #include <GLFW/glfw3.h>
 
+#include "input.h"
 #include "logger.h"
 #include "performance.h"
 #include "render.h"
@@ -67,6 +68,7 @@ void init() {
        resizeStage(window, width, height);
 
        glfwSetFramebufferSizeCallback(window, resizeStage);
+       glfwSetKeyCallback(window, onKeyboardEvent);
 
        initRender();
        //initPerformanceMetering();
@@ -97,10 +99,6 @@ void terminate() {
        glfwTerminate();
 }
 
-void setKeyboardEventCallback(void (*keyboardEventCallback) (GLFWwindow*, int, int, int, int)) {
-       glfwSetKeyCallback(window, keyboardEventCallback);
-}
-
 static void onGlfwError(int error, const char* description) {
        logError("GLFW error: %s", description);
 }
index da5f96314e612e42819ef77b4e86a37be3c55759..f1ff1026b0a22743e7bd2c42d4b9480e97cac58d 100644 (file)
@@ -1,11 +1,8 @@
-#ifndef ENGINE_H_
-#define ENGINE_H_
-
-typedef struct GLFWwindow GLFWwindow;
+#ifndef ENGINE_ENGINE_H_
+#define ENGINE_ENGINE_H_
 
 void init();
 void run(void (*updateFn) (float));
 void terminate();
-void setKeyboardEventCallback(void (*) (GLFWwindow*, int, int, int, int));
 
-#endif // ENGINE_H_
+#endif // ENGINE_ENGINE_H_
index 40ee29fc627aa2eb6ab1854ff2908649c8e0c84a..83731b27a34132ed47f46b794996900ab4b6bbf0 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef GEOMETRY_H_
-#define GEOMETRY_H_
+#ifndef ENGINE_GEOMETRY_H_
+#define ENGINE_GEOMETRY_H_
 
 #include <GL/gl.h>
 
@@ -36,4 +36,4 @@ Vector3D applyTransform(Transform transform, Vector3D vec);
 Vector3D translationOf(Transform transform);
 Vector3D normalized(Vector3D vec);
 
-#endif // GEOMETRY_H_
+#endif // ENGINE_GEOMETRY_H_
diff --git a/src/engine/input.c b/src/engine/input.c
new file mode 100644 (file)
index 0000000..01d3c41
--- /dev/null
@@ -0,0 +1,19 @@
+#include "input.h"
+
+#include <stdbool.h>
+
+static void (*keyboardInputCallback) (int, int, int, int);
+
+
+
+void onKeyboardEvent(GLFWwindow* window, int key, int scancode, int action, int mods) {
+       bool handled = false;
+
+       if (!handled && keyboardInputCallback) {
+               keyboardInputCallback(key, scancode, action, mods);
+       }
+}
+
+void setKeyboardInputCallback(void (*callback) (int, int, int, int)) {
+       keyboardInputCallback = callback;
+}
diff --git a/src/engine/input.h b/src/engine/input.h
new file mode 100644 (file)
index 0000000..0a8880b
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef ENGINE_INPUT_H
+#define ENGINE_INPUT_H
+
+#include <GLFW/glfw3.h>
+
+void onKeyboardEvent(GLFWwindow* window, int key, int scancode, int action, int mods);
+void setKeyboardInputCallback(void (*) (int, int, int, int));
+
+#endif // ENGINE_INPUT_H
index d491f8abbe378e2f628b441f8ffd4fd015fced70..4529738e77a15dd5e9bd50a3bc557e2a04824058 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef LOGGER_H_
-#define LOGGER_H_
+#ifndef ENGINE_LOGGER_H_
+#define ENGINE_LOGGER_H_
 
 typedef enum {
        LOGLEVEL_ERROR,
@@ -17,4 +17,4 @@ extern LogLevel logLevel;
 
 void logMessage(LogLevel msgLevel, const char* func, const char* message, ...);
 
-#endif // LOGGER_H_
+#endif // ENGINE_LOGGER_H_
index fa47fe2a9d5b53d250d9edad1b89770669318f1f..5f8df7294ce72b0093506b943c35d5845d87fe66 100644 (file)
@@ -1,7 +1,7 @@
-#ifndef PERFORMANCE_H_
-#define PERFORMANCE_H_
+#ifndef ENGINE_PERFORMANCE_H_
+#define ENGINE_PERFORMANCE_H_
 
 void initPerformanceMetering();
 void frameRendered();
 
-#endif // PERFORMANCE_H_
+#endif // ENGINE_PERFORMANCE_H_
index 4b6132017fa6f60baef28c9fd57f13537cd928b4..f3be0a2fd4fc7cb2c10973543896395b41e64dd2 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef RENDER_H_
-#define RENDER_H_
+#ifndef ENGINE_RENDER_H_
+#define ENGINE_RENDER_H_
 
 #include <GLFW/glfw3.h>
 
@@ -11,4 +11,4 @@ extern const Scene* cameraAnchor;
 void initRender();
 void renderFrame(GLFWwindow* window);
 
-#endif // RENDER_H_
+#endif // ENGINE_RENDER_H_
index f603c147ab0462616b3a9be19d3612c2e3e4931a..895e0352433a48b328ed67482983778758b1ae0f 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef SCENE_H_
-#define SCENE_H_
+#ifndef ENGINE_SCENE_H_
+#define ENGINE_SCENE_H_
 
 #include "asset.h"
 
@@ -20,4 +20,4 @@ void insertChildScene(Scene* scene, Scene* newChild);
 void reparentScene(Scene* scene, Scene* newParent);
 Transform worldTransform(const Scene* scene);
 
-#endif // SCENE_H_
+#endif // ENGINE_SCENE_H_
index 73e600bb5fa6b78ed848abcd75bce0145575f561..edd6508e0d952196bde7aa9890fd5d676dd06211 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef STRING_H_
-#define STRING_H_
+#ifndef ENGINE_STRING_H_
+#define ENGINE_STRING_H_
 
 #include <assimp/types.h>
 
@@ -13,4 +13,4 @@ struct String {
 String stringFromAiString(const struct aiString aistr);
 void dropString(String str);
 
-#endif // STRING_H_
+#endif // ENGINE_STRING_H_
index d044362a24b03dc7851e9ccb4f3b76a9de8c464b..0936fa81aa7211baabf75dbb00cbaf6650522bec 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef TGA_H_
-#define TGA_H_
+#ifndef ENGINE_TGA_H_
+#define ENGINE_TGA_H_
 
 #include <GL/gl.h>
 
@@ -29,4 +29,4 @@ typedef struct {
 
 TgaImage* readTga(const char* path);
 
-#endif // TGA_H_
+#endif // ENGINE_TGA_H_
index 7ab1784102c6a5f7f9bc1f611175a13169725304..7e33b4f1a23df5f0a0ac004fcdac123d79d40c8b 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef UI_H_
-#define UI_H_
+#ifndef ENGINE_UI_H_
+#define ENGINE_UI_H_
 
 #include <GL/gl.h>
 
@@ -7,4 +7,4 @@ typedef struct GLFWwindow GLFWwindow;
 
 void resizeStage(GLFWwindow* window, int width, int height);
 
-#endif // UI_H_
+#endif // ENGINE_UI_H_
index a1a1da2b4c39b0f50a63a840e516c4228280e445..25b1c30d8550183dd4b6cbfddd3d169fe659d1a4 100644 (file)
@@ -1,6 +1,6 @@
 #include "game.h"
 
-#include "engine/engine.h"
+#include "engine/input.h"
 
 #include "input.h"
 #include "level.h"
@@ -11,7 +11,7 @@ void initGame() {
        initPlayer();
        startLevel();
 
-       setKeyboardEventCallback(onKeyboardEvent);
+       setKeyboardInputCallback(keyboardInput);
 }
 
 void update(float delta) {
index bf4ab03ebab671bb0ef44f52d443f84263a946a7..f52a27e79742ec4bff65c73279399096e4dc4b5f 100644 (file)
@@ -1,8 +1,10 @@
 #include "input.h"
 
+#include <GLFW/glfw3.h>
+
 #include "player.h"
 
-void onKeyboardEvent(GLFWwindow* window, int key, int scancode, int action, int mods) {
+void keyboardInput(int key, int scancode, int action, int mods) {
        switch (key) {
                case GLFW_KEY_W:
                        if (action == GLFW_PRESS) {
index 0f430b74e915c9d1ef5f12545413bdceb93e8aac..a354574b9b72c47c7bfbbf0e2a48a6e3dc286c70 100644 (file)
@@ -1,8 +1,6 @@
 #ifndef INPUT_H_
 #define INPUT_H_
 
-#include <GLFW/glfw3.h>
-
-void onKeyboardEvent(GLFWwindow* window, int key, int scancode, int action, int mods);
+void keyboardInput(int key, int scancode, int action, int mods);
 
 #endif // INPUT_H_