a-game

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

commit 0115aa89327c3640fc6ead0ce20a1eb3c9ecdafe
parent 27964f479ebb63f719496979e055778055bfa376
Author: amin <dev@aminmesbah.com>
Date:   Sat, 13 Apr 2019 19:17:20 +0000

Determine meter size by comparing aspect ratios

FossilOrigin-Name: e2e2a57a2842e595aabb431313b73b3716b7675ff81e6bc9a7bad4d8acd50790
Diffstat:
Msrc/game.c | 46++++++++++++++++++++++++++++------------------
Msrc/game.h | 1+
Msrc/glmth.c | 6+++++-
3 files changed, 34 insertions(+), 19 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -41,13 +41,8 @@ f32 randf(f32 min, f32 max) void game_init(struct GameState *game_state, v2u framebuffer) { - v2u screen = (v2u) { - framebuffer.width, - framebuffer.height, - }; - // init player - game_state->player.pos = (v2) { screen.width / 2.0f, screen.height / 2.0f }; + game_state->player.pos = (v2) { 10.0f, 10.0f }; // set up and load tiles { @@ -105,15 +100,28 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe framebuffer.height, }; + f32 pixels_per_meter = 0.0f; + + { + f32 screen_aspect_ratio = (f32)screen.width / (f32)screen.height; + if (screen_aspect_ratio < ROOM_ASPECT_RATIO) + { + pixels_per_meter = (f32)screen.width / (f32)ROOM_TILE_DIM_X; + } + else + { + pixels_per_meter = (f32)screen.height / (f32)ROOM_TILE_DIM_Y; + } + } + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - // In Knytt, the player is a 9 by 14 pixels and a tile is 24 by 24 pixels + // In Knytt, the player is 9 by 14 texels and a tile is 24 by 24 texels // TODO: Use world coordinates for everything other than rendering - f32 meter_size = 0.0f; // render tiles { glBindVertexArray(game_state->tiles.vao); @@ -134,12 +142,7 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }; - f32 tile_size = ((f32)screen.width) / (f32)ROOM_TILE_DIM_X; - if (tile_size * (f32)ROOM_TILE_DIM_Y > screen.height) - { - tile_size = ((f32)screen.height) / (f32)ROOM_TILE_DIM_Y; - } - meter_size = tile_size; + f32 tile_size = pixels_per_meter; v2 tilemap = (v2) { tile_size * (f32)ROOM_TILE_DIM_X, @@ -182,7 +185,7 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe // update player { - f32 movement_speed = 5.0f; + f32 movement_speed = 0.2f; struct GameInput input = game_state->input; if (input.key_up) @@ -210,13 +213,19 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe struct Entity player = game_state->player; glBindVertexArray(game_state->tiles.vao); m4 model = glmth_m4_init_id(); - model = glmth_translate(model, (v3) { player.pos.x, player.pos.y, 0.0f }); + + v2 player_render_pos = (v2) { + player.pos.x * pixels_per_meter, + player.pos.y * pixels_per_meter, + }; + + model = glmth_translate(model, (v3) { player_render_pos.x, player_render_pos.y, 0.0f }); // these dimensions are relative to a square 'meter', one tile v2 player_dim = (v2) { 0.375f, 0.583f }; v2 player_render_dim = (v2) { - player_dim.x * meter_size, - player_dim.y * meter_size, + player_dim.x * pixels_per_meter, + player_dim.y * pixels_per_meter, }; model = glmth_scale(model, (v3) { @@ -224,6 +233,7 @@ void game_update_and_render(struct GameState *game_state, f32 dt, v2u framebuffe player_render_dim.y, 1.0f }); + shader_setm4(&game_state->tiles.shader, "model", &model); v3 color = (v3) { 1.0f, 0.0f, 1.0f }; shader_setv3(&game_state->tiles.shader, "color", &color); diff --git a/src/game.h b/src/game.h @@ -13,6 +13,7 @@ #define ROOM_TILE_DIM_X 25 #define ROOM_TILE_DIM_Y 10 +#define ROOM_ASPECT_RATIO (f32)ROOM_TILE_DIM_X / (f32)ROOM_TILE_DIM_Y struct Tiles { diff --git a/src/glmth.c b/src/glmth.c @@ -91,6 +91,11 @@ v2u glmth_v2u_init(u32 x, u32 y) return v; } +void glmth_v2_print(v2 v) +{ + printf("( %f, %f )\n", v.x, v.y); +} + v3 glmth_v3_cross(v3 vec1, v3 vec2) { v3 r = { @@ -138,7 +143,6 @@ v3 glmth_v3_normalize(v3 v) return r; } - void glmth_v3_print(v3 v) { printf("( %f, %f, %f )\n", v.x, v.y, v.z);