Restructure keyboard input handling

master
outfrost 2020-06-27 02:45:16 +02:00
parent 34c82e0470
commit 71828d0b25
17 changed files with 67 additions and 43 deletions

View File

@ -17,6 +17,7 @@ sources ::= main.c \
engine/asset.c \ engine/asset.c \
engine/engine.c \ engine/engine.c \
engine/geometry.c \ engine/geometry.c \
engine/input.c \
engine/logger.c \ engine/logger.c \
engine/performance.c \ engine/performance.c \
engine/render.c \ engine/render.c \

View File

@ -1,5 +1,5 @@
#ifndef ASSET_H_ #ifndef ENGINE_ASSET_H_
#define ASSET_H_ #define ENGINE_ASSET_H_
#include <stddef.h> #include <stddef.h>
#include <GL/gl.h> #include <GL/gl.h>
@ -40,4 +40,4 @@ struct Material {
const Solid* importSolid(const char* path); const Solid* importSolid(const char* path);
#endif // ASSET_H_ #endif // ENGINE_ASSET_H_

View File

@ -5,6 +5,7 @@
#include <GL/glxew.h> #include <GL/glxew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "input.h"
#include "logger.h" #include "logger.h"
#include "performance.h" #include "performance.h"
#include "render.h" #include "render.h"
@ -67,6 +68,7 @@ void init() {
resizeStage(window, width, height); resizeStage(window, width, height);
glfwSetFramebufferSizeCallback(window, resizeStage); glfwSetFramebufferSizeCallback(window, resizeStage);
glfwSetKeyCallback(window, onKeyboardEvent);
initRender(); initRender();
//initPerformanceMetering(); //initPerformanceMetering();
@ -97,10 +99,6 @@ void terminate() {
glfwTerminate(); glfwTerminate();
} }
void setKeyboardEventCallback(void (*keyboardEventCallback) (GLFWwindow*, int, int, int, int)) {
glfwSetKeyCallback(window, keyboardEventCallback);
}
static void onGlfwError(int error, const char* description) { static void onGlfwError(int error, const char* description) {
logError("GLFW error: %s", description); logError("GLFW error: %s", description);
} }

View File

@ -1,11 +1,8 @@
#ifndef ENGINE_H_ #ifndef ENGINE_ENGINE_H_
#define ENGINE_H_ #define ENGINE_ENGINE_H_
typedef struct GLFWwindow GLFWwindow;
void init(); void init();
void run(void (*updateFn) (float)); void run(void (*updateFn) (float));
void terminate(); void terminate();
void setKeyboardEventCallback(void (*) (GLFWwindow*, int, int, int, int));
#endif // ENGINE_H_ #endif // ENGINE_ENGINE_H_

View File

@ -1,5 +1,5 @@
#ifndef GEOMETRY_H_ #ifndef ENGINE_GEOMETRY_H_
#define GEOMETRY_H_ #define ENGINE_GEOMETRY_H_
#include <GL/gl.h> #include <GL/gl.h>
@ -36,4 +36,4 @@ Vector3D applyTransform(Transform transform, Vector3D vec);
Vector3D translationOf(Transform transform); Vector3D translationOf(Transform transform);
Vector3D normalized(Vector3D vec); Vector3D normalized(Vector3D vec);
#endif // GEOMETRY_H_ #endif // ENGINE_GEOMETRY_H_

19
src/engine/input.c Normal file
View File

@ -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;
}

9
src/engine/input.h Normal file
View File

@ -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

View File

@ -1,5 +1,5 @@
#ifndef LOGGER_H_ #ifndef ENGINE_LOGGER_H_
#define LOGGER_H_ #define ENGINE_LOGGER_H_
typedef enum { typedef enum {
LOGLEVEL_ERROR, LOGLEVEL_ERROR,
@ -17,4 +17,4 @@ extern LogLevel logLevel;
void logMessage(LogLevel msgLevel, const char* func, const char* message, ...); void logMessage(LogLevel msgLevel, const char* func, const char* message, ...);
#endif // LOGGER_H_ #endif // ENGINE_LOGGER_H_

View File

@ -1,7 +1,7 @@
#ifndef PERFORMANCE_H_ #ifndef ENGINE_PERFORMANCE_H_
#define PERFORMANCE_H_ #define ENGINE_PERFORMANCE_H_
void initPerformanceMetering(); void initPerformanceMetering();
void frameRendered(); void frameRendered();
#endif // PERFORMANCE_H_ #endif // ENGINE_PERFORMANCE_H_

View File

@ -1,5 +1,5 @@
#ifndef RENDER_H_ #ifndef ENGINE_RENDER_H_
#define RENDER_H_ #define ENGINE_RENDER_H_
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
@ -11,4 +11,4 @@ extern const Scene* cameraAnchor;
void initRender(); void initRender();
void renderFrame(GLFWwindow* window); void renderFrame(GLFWwindow* window);
#endif // RENDER_H_ #endif // ENGINE_RENDER_H_

View File

@ -1,5 +1,5 @@
#ifndef SCENE_H_ #ifndef ENGINE_SCENE_H_
#define SCENE_H_ #define ENGINE_SCENE_H_
#include "asset.h" #include "asset.h"
@ -20,4 +20,4 @@ void insertChildScene(Scene* scene, Scene* newChild);
void reparentScene(Scene* scene, Scene* newParent); void reparentScene(Scene* scene, Scene* newParent);
Transform worldTransform(const Scene* scene); Transform worldTransform(const Scene* scene);
#endif // SCENE_H_ #endif // ENGINE_SCENE_H_

View File

@ -1,5 +1,5 @@
#ifndef STRING_H_ #ifndef ENGINE_STRING_H_
#define STRING_H_ #define ENGINE_STRING_H_
#include <assimp/types.h> #include <assimp/types.h>
@ -13,4 +13,4 @@ struct String {
String stringFromAiString(const struct aiString aistr); String stringFromAiString(const struct aiString aistr);
void dropString(String str); void dropString(String str);
#endif // STRING_H_ #endif // ENGINE_STRING_H_

View File

@ -1,5 +1,5 @@
#ifndef TGA_H_ #ifndef ENGINE_TGA_H_
#define TGA_H_ #define ENGINE_TGA_H_
#include <GL/gl.h> #include <GL/gl.h>
@ -29,4 +29,4 @@ typedef struct {
TgaImage* readTga(const char* path); TgaImage* readTga(const char* path);
#endif // TGA_H_ #endif // ENGINE_TGA_H_

View File

@ -1,5 +1,5 @@
#ifndef UI_H_ #ifndef ENGINE_UI_H_
#define UI_H_ #define ENGINE_UI_H_
#include <GL/gl.h> #include <GL/gl.h>
@ -7,4 +7,4 @@ typedef struct GLFWwindow GLFWwindow;
void resizeStage(GLFWwindow* window, int width, int height); void resizeStage(GLFWwindow* window, int width, int height);
#endif // UI_H_ #endif // ENGINE_UI_H_

View File

@ -1,6 +1,6 @@
#include "game.h" #include "game.h"
#include "engine/engine.h" #include "engine/input.h"
#include "input.h" #include "input.h"
#include "level.h" #include "level.h"
@ -11,7 +11,7 @@ void initGame() {
initPlayer(); initPlayer();
startLevel(); startLevel();
setKeyboardEventCallback(onKeyboardEvent); setKeyboardInputCallback(keyboardInput);
} }
void update(float delta) { void update(float delta) {

View File

@ -1,8 +1,10 @@
#include "input.h" #include "input.h"
#include <GLFW/glfw3.h>
#include "player.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) { switch (key) {
case GLFW_KEY_W: case GLFW_KEY_W:
if (action == GLFW_PRESS) { if (action == GLFW_PRESS) {

View File

@ -1,8 +1,6 @@
#ifndef INPUT_H_ #ifndef INPUT_H_
#define INPUT_H_ #define INPUT_H_
#include <GLFW/glfw3.h> void keyboardInput(int key, int scancode, int action, int mods);
void onKeyboardEvent(GLFWwindow* window, int key, int scancode, int action, int mods);
#endif // INPUT_H_ #endif // INPUT_H_