a-game

2D platformer written from scratch.
git clone git://git.amin.space/a-game.git
Log | Files | Refs | README | LICENSE

commit abe6a9f4d478286d17781a6edea54a9932d0f3c7
parent 0f660ef29106284fb5cfb0d8c297211a43295d56
Author: amin <dev@aminmesbah.com>
Date:   Sun, 31 Mar 2019 07:30:10 +0000

Get keyboard input

FossilOrigin-Name: 8913bec691b5b8a49be8723a3595bdc33b1e7cd92dd71c194a60acd390eb9de5
Diffstat:
Msrc/game.c | 10++++++++++
Msrc/game.h | 16++++++++++++++++
Msrc/platform_linux.c | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 77 insertions(+), 0 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -169,6 +169,16 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe framebuffer.width, framebuffer.height, }; + + // test input + { + struct GameInput input = game_state->input; + printf("input.key_left: %d\n", input.key_left); + printf("input.key_right: %d\n", input.key_right); + printf("input.key_up: %d\n", input.key_up); + printf("input.key_down: %d\n", input.key_down); + } + // update stars { for (size_t i = 0; i < game_state->stars.num_stars; ++i) diff --git a/src/game.h b/src/game.h @@ -28,6 +28,21 @@ struct Tiles u32 count; }; +enum InputKeyAction +{ + KEY_PRESS, + KEY_RELEASE, + KEY_REPEAT, +}; + +struct GameInput +{ + enum InputKeyAction key_left; + enum InputKeyAction key_right; + enum InputKeyAction key_up; + enum InputKeyAction key_down; +}; + struct GameState { struct Tiles tiles; @@ -38,6 +53,7 @@ struct GameState struct Shader star_shader; struct Shader triangle_shader; struct Stars stars; + struct GameInput input; }; #ifdef PLATFORM_HOTLOAD_GAME_CODE diff --git a/src/platform_linux.c b/src/platform_linux.c @@ -11,6 +11,7 @@ void error_callback(int error, const char* description); void framebuffer_size_callback(GLFWwindow* window, int width, int height); +void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); #ifdef PLATFORM_HOTLOAD_GAME_CODE time_t file_get_modified_time(char *file_path); @@ -50,6 +51,7 @@ int main(void) } glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + glfwSetKeyCallback(window, key_callback); if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) { @@ -66,6 +68,7 @@ int main(void) #endif struct GameState game_state = {0}; + glfwSetWindowUserPointer(window, &game_state); game_init(&game_state, glmth_v2u_init(PLATFORM_SCR_WIDTH, PLATFORM_SCR_HEIGHT)); #ifdef PLATFORM_HOTLOAD_GAME_CODE @@ -130,6 +133,54 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height) } +void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) +{ + //printf("key: %s, action: %d\n", glfwGetKeyName(key, scancode), action); + struct GameState* game_state = (struct GameState *)glfwGetWindowUserPointer(window); + + enum InputKeyAction game_input_key_action = 0; + switch (action) + { + 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_state->input.key_left); + break; + case GLFW_KEY_RIGHT: + game_key = &(game_state->input.key_right); + break; + case GLFW_KEY_UP: + game_key = &(game_state->input.key_up); + break; + case GLFW_KEY_DOWN: + game_key = &(game_state->input.key_down); + break; + default: + break; + } + + if (game_key) + { + *game_key = game_input_key_action; + } +} + + time_t file_get_modified_time(char *file_path) { time_t mtime = 0;