a-game

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

commit 4764785a956f5eb0469f2c98ce5267aa51e4e1a8
parent 7b5566daf81467b540d8d7b4280ce33400338ca7
Author: amin <dev@aminmesbah.com>
Date:   Thu,  9 May 2019 05:16:11 +0000

Calculate chunk index mask correctly

Also init rooms in a chunk to a distinctive value.

FossilOrigin-Name: e746b095e377d00f0cbc3be745a50df17a8ff366c238afd1ae72522c5ab55d22
Diffstat:
MMakefile | 2+-
Msrc/am_math.h | 6++++++
Msrc/game.c | 30++++++++++++++++++++++++++++--
Msrc/game.h | 2++
Asrc/intrinsics.h | 7+++++++
Msrc/world.c | 6+++++-
Msrc/world.h | 6++++--
7 files changed, 53 insertions(+), 6 deletions(-)

diff --git a/Makefile b/Makefile @@ -12,7 +12,7 @@ LIB_NAME = game.so DBGDIR = out/debug DBGEXE = $(DBGDIR)/$(EXE_FILE) -DBGCFLAGS = -g -O0 -Werror +DBGCFLAGS = -g -O0 RELDIR = out/release RELEXE = $(RELDIR)/$(EXE_FILE) diff --git a/src/am_math.h b/src/am_math.h @@ -576,6 +576,12 @@ internal inline bool math_v2_ge(v2 v, v2 compare) return is_ge; } +internal inline i32 math_log2(u32 x) +{ + i32 result = sizeof(u32) * CHAR_BIT - intr_clz(x) - 1; + return result; +} + #define math_is_pow_2(n) ((n) & ((n) - 1)) == 0 internal inline void math_v2_print(v2 v) diff --git a/src/game.c b/src/game.c @@ -61,8 +61,34 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) }, }; - world_room_set(game_state->world, collision_test_zone, &game_state->world_allocator); - world_room_set(game_state->world, knytt_hanging_fly_village, &game_state->world_allocator); + //world_room_set(game_state->world, collision_test_zone, &game_state->world_allocator); + //world_room_set(game_state->world, knytt_hanging_fly_village, &game_state->world_allocator); + + i32 s = 0; + i32 f = 100; + for (i32 x = s; x < f; x++) + { + for (i32 y = s; y < f; y++) + { + struct Room r = { + .index = {x, y}, + .tiles = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + }, + }; + world_room_set(game_state->world, r, &game_state->world_allocator); + } + } // set up and load tiles { diff --git a/src/game.h b/src/game.h @@ -1,4 +1,5 @@ #include <assert.h> +#include <limits.h> #include <math.h> #include <stdalign.h> #include <stdbool.h> @@ -9,6 +10,7 @@ #include <glad/glad.h> #include "types.h" +#include "intrinsics.h" #include "am_math.h" #include "shader.h" #include "memory.h" diff --git a/src/intrinsics.h b/src/intrinsics.h @@ -0,0 +1,7 @@ +// NOTE: __has_builtin is a clang-specific macro +#ifndef __has_builtin +#define __has_builtin(x) 0 +#endif + +static_assert(__has_builtin(__builtin_clz), "No builtin clz intrinsic"); +#define intr_clz(x) __builtin_clz((x)) diff --git a/src/world.c b/src/world.c @@ -108,7 +108,10 @@ internal struct WorldChunk* world_chunk_get_or_create(struct World *world, v2i c struct WorldChunk *prev_head_chunk = world->chunk_ht[hash_table_bucket]; struct WorldChunk *new_chunk = mem_st_alloc_struct(allocator, struct WorldChunk); new_chunk->index = chunk_i; - // TODO: Init rooms to 0? + for (size_t i = 0; i < CHUNK_NUM_ROOMS; i++) + { + new_chunk->rooms[i].index.y = ROOM_UNINITIALIZED_Y_VALUE; + } new_chunk->next = prev_head_chunk; world->chunk_ht[hash_table_bucket] = new_chunk; result = new_chunk; @@ -157,6 +160,7 @@ internal struct Room* world_room_set(struct World *world, struct Room room, stru if (chunk) { size_t room_i_in_chunk = world_room_get_index_within_chunk(room.index); + assert(chunk->rooms[room_i_in_chunk].index.y == ROOM_UNINITIALIZED_Y_VALUE); chunk->rooms[room_i_in_chunk] = room; result = &chunk->rooms[room_i_in_chunk]; } diff --git a/src/world.h b/src/world.h @@ -13,13 +13,15 @@ static_assert( ); // TODO: Tune chunk size -#define CHUNK_ROOMS_PER_SIDE 4 +#define CHUNK_ROOMS_PER_SIDE 2 static_assert( math_is_pow_2(CHUNK_ROOMS_PER_SIDE), "Must be a power of 2 for the chunk mask to work" ); #define CHUNK_NUM_ROOMS (CHUNK_ROOMS_PER_SIDE * CHUNK_ROOMS_PER_SIDE) -#define CHUNK_MASK_FOR_INTERNAL_ROOM_INDEX ((1 << CHUNK_ROOMS_PER_SIDE) - 1) +#define CHUNK_MASK_FOR_INTERNAL_ROOM_INDEX ((1 << math_log2(CHUNK_ROOMS_PER_SIDE)) - 1) + +#define ROOM_UNINITIALIZED_Y_VALUE INT32_MIN struct Room {