a-game

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

commit 11730170d0f580d0582f9ab7dd2012f4872f0cb0
parent abe6a9f4d478286d17781a6edea54a9932d0f3c7
Author: amin <dev@aminmesbah.com>
Date:   Sun, 31 Mar 2019 07:56:03 +0000

Move player with keyboard controls

FossilOrigin-Name: 4a6b1cd3456034c1f9dcf58032b000b80b9e47b3665eb99681f59ddcb7b0ec2c
Diffstat:
Msrc/game.c | 40+++++++++++++++++++++++++++++-----------
Msrc/game.h | 8+++++++-
Msrc/platform_linux.c | 1-
3 files changed, 36 insertions(+), 13 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -46,6 +46,9 @@ void game_init(struct GameState *game_state, v2u framebuffer) framebuffer.height, }; + // init player + game_state->player.pos = (v2) { screen.width / 2.0f, screen.height / 2.0f }; + // set up and load star vertex data { f32 max_speed = 0.3f; @@ -170,15 +173,6 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe 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) @@ -264,12 +258,36 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe glBindVertexArray(0); } + // update player + { + f32 movement_speed = 5.0f; + struct GameInput input = game_state->input; + + if (input.key_up) + { + game_state->player.pos.y -= movement_speed; + } + else if (input.key_down) + { + game_state->player.pos.y += movement_speed; + } + + if (input.key_left) + { + game_state->player.pos.x -= movement_speed; + } + else if (input.key_right) + { + game_state->player.pos.x += movement_speed; + } + } + // render player { + struct Entity player = game_state->player; glBindVertexArray(game_state->tiles.vao); - v2 player_pos = (v2) { screen.width / 2.0f, screen.height / 2.0f }; m4 model = glmth_m4_init_id(); - model = glmth_translate(model, (v3) { player_pos.x, player_pos.y, 0.0f }); + model = glmth_translate(model, (v3) { player.pos.x, player.pos.y, 0.0f }); model = glmth_scale(model, (v3) {100.0f, 100.0f, 1.0f}); shader_setm4(&game_state->star_shader, "model", &model); v3 color = (v3) { 1.0f, 0.0f, 1.0f }; diff --git a/src/game.h b/src/game.h @@ -30,8 +30,8 @@ struct Tiles enum InputKeyAction { - KEY_PRESS, KEY_RELEASE, + KEY_PRESS, KEY_REPEAT, }; @@ -43,6 +43,11 @@ struct GameInput enum InputKeyAction key_down; }; +struct Entity +{ + v2 pos; +}; + struct GameState { struct Tiles tiles; @@ -54,6 +59,7 @@ struct GameState struct Shader triangle_shader; struct Stars stars; struct GameInput input; + struct Entity player; }; #ifdef PLATFORM_HOTLOAD_GAME_CODE diff --git a/src/platform_linux.c b/src/platform_linux.c @@ -135,7 +135,6 @@ 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;