a-game

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

commit ce05fe56055d4841aa66d46b86684049d266f315
parent a5f2ac1ed1d4f53c019e186aa6fa02a8c021809b
Author: amin <dev@aminmesbah.com>
Date:   Thu, 25 Apr 2019 20:45:01 +0000

Compress some error prone comparisons

FossilOrigin-Name: 594e637b65bfc8345d2ef803235df2f74d64b44ccd78943dfc84c78218c29a3f
Diffstat:
Msrc/game.c | 26+++++++++++++++-----------
Msrc/glmth.h | 29+++++++++++++++++++++++++++++
2 files changed, 44 insertions(+), 11 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -89,8 +89,7 @@ internal struct WallCollision get_wall_collision(v2 entity_p_initial, v2 entity_ struct WallCollision result = {0}; result.collision_occurred = false; - assert(wall.min.x <= wall.max.x); - assert(wall.min.y <= wall.max.y); + assert(glmth_v2_le(wall.min, wall.max)); bool wall_is_horizontal = wall.min.y == wall.max.y; bool wall_is_vertical = wall.min.x == wall.max.x; @@ -122,10 +121,12 @@ internal struct WallCollision get_wall_collision(v2 entity_p_initial, v2 entity_ f32 segment_scale_factor = initial_d / player_delta.E[wall_normal_axis]; v2 collision_point = glmth_v2_a(entity_p_initial, glmth_v2f_m(player_delta, segment_scale_factor)); - if (segment_scale_factor >= 0.0f && segment_scale_factor <= 1.0f) + if (glmth_in_interval_open(segment_scale_factor, 0.0f, 1.0f)) { - if (collision_point.E[wall_axis] >= wall.min.E[wall_axis] - && collision_point.E[wall_axis] <= wall.max.E[wall_axis]) + if (glmth_in_interval_open( + collision_point.E[wall_axis], + wall.min.E[wall_axis], + wall.max.E[wall_axis])) { result.collision_occurred = true; result.distance_scale_factor = glmth_max(0.0f, segment_scale_factor); @@ -147,10 +148,13 @@ internal bool tile_is_solid(u32 tiles[][ROOM_TILE_DIM_X], v2 tile_pos) ROOM_TILE_DIM_Y - 1.0f - tile_pos.y, }; - assert(tile_index.x >= 0); - assert(tile_index.x < ROOM_TILE_DIM_X); - assert(tile_index.y >= 0); - assert(tile_index.y < ROOM_TILE_DIM_Y); + assert(glmth_v2_lt( + (v2) {tile_index.x, tile_index.y}, + (v2) {ROOM_TILE_DIM_X, ROOM_TILE_DIM_Y})); + + assert(glmth_v2_ge( + (v2) {tile_index.x, tile_index.y}, + (v2) {0})); u32 tile_value = tiles[tile_index.y][tile_index.x]; is_solid = tile_value > 0; @@ -165,8 +169,8 @@ internal bool wall_is_screen_edge(u32 tiles[][ROOM_TILE_DIM_X], segment wall) bool wall_is_vertical = wall.min.x == wall.max.x; assert(wall_is_vertical || wall_is_horizontal); - assert(wall.min.x >= 0.0f && wall.min.y >= 0.0f); - assert(wall.min.x <= ROOM_TILE_DIM_X && wall.max.y <= ROOM_TILE_DIM_Y); + assert(glmth_v2_ge(wall.min, (v2) {0})); + assert(glmth_v2_le(wall.max, (v2) {ROOM_TILE_DIM_X, ROOM_TILE_DIM_Y})); if (wall_is_vertical) { diff --git a/src/glmth.h b/src/glmth.h @@ -535,6 +535,35 @@ internal inline segment glmth_rect_get_edge(rect r, enum GlmthRectEdge e) return edge; } +internal inline bool glmth_in_interval_open(f32 n, f32 min, f32 max) +{ + assert(min <= max); + bool in_open_interval = false; + in_open_interval = (n >= min && n <= max); + return in_open_interval; +} + +internal inline bool glmth_v2_lt(v2 v, v2 compare) +{ + bool is_lt = false; + is_lt = (v.x < compare.x && v.y < compare.y); + return is_lt; +} + +internal inline bool glmth_v2_le(v2 v, v2 compare) +{ + bool is_le = false; + is_le = (v.x <= compare.x && v.y <= compare.y); + return is_le; +} + +internal inline bool glmth_v2_ge(v2 v, v2 compare) +{ + bool is_ge = false; + is_ge = (v.x >= compare.x && v.y >= compare.y); + return is_ge; +} + internal inline void glmth_v2_print(v2 v) { printf("( %f, %f )\n", v.x, v.y);