a-game

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

commit 0ce7c308dc7fe8d998d05e8495100b3d4ed9a9b5
parent af6ac03f1b4677f5e225fa5e614f7fa8022ac7b6
Author: amin <dev@aminmesbah.com>
Date:   Tue, 16 Jul 2019 02:34:01 +0000

Map room tiles to different tileset tiles

FossilOrigin-Name: 19c1af1ef9741819092b4305aa2a9d8d7e460a71a09f623f7c5f291bc9cc7013
Diffstat:
Mshader/main_f.glsl | 3+--
Msrc/game.c | 48++++++++++++++++++++++++++----------------------
Msrc/render.c | 1+
Msrc/render.h | 1+
Msrc/shader.c | 3+++
Msrc/shader.h | 1+
Msrc/world.c | 8++++----
Msrc/world.h | 2+-
8 files changed, 38 insertions(+), 29 deletions(-)

diff --git a/shader/main_f.glsl b/shader/main_f.glsl @@ -13,11 +13,10 @@ out vec4 frag_color; uniform float tex_interp; uniform sampler2DArray tileset; +uniform float tile_id; void main() { - // TODO: make this an input - float tile_id = 16.0f; vec4 tile_texel = texture(tileset, vec3(tex_coord, tile_id)); frag_color = mix(vertex_color, tile_texel, tex_interp); } diff --git a/src/game.c b/src/game.c @@ -107,34 +107,33 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) struct Room knytt_hanging_fly_village = { .index = {0, 0}, .tiles = { - 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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, 3, 3, 15, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 15, 3, 3, 16, 0, + 0, 25, 0, 0, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 25, 0, + 0, 25, 0, 14, 28, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 26, 16, 0, 25, 0, + 0, 25, 0, 26, 28, 0, 0, 0, 0, 0, 13, 0, 0, 0, 13, 0, 0, 0, 0, 0, 26, 28, 0, 25, 0, + 0, 25, 0, 26, 28, 0, 0, 0, 0, 0, 37, 0, 0, 0, 37, 0, 0, 0, 0, 0, 26, 28, 0, 25, 0, + 0, 25, 0, 26, 28, 0, 0, 1, 0, 0, 0, 2, 3, 4, 0, 0, 0, 1, 0, 0, 26, 28, 0, 25, 0, + 0, 25, 0, 26, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 28, 0, 25, 0, + 0, 37, 0, 38, 39, 3, 3, 3, 3, 3, 3, 4, 0, 2, 3, 3, 3, 3, 3, 3, 39, 40, 0, 37, 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, }, }; @@ -598,10 +597,14 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga v3 color; bool solid = world_tile_in_room_is_solid(current_room->tiles, tile_pos); f32 tex_interp = 0.0f; + u8 tile_id = 0; if (solid) { color = (v3) {0.4f, 0.4f, 0.4f}; tex_interp = 1.0f; + tile_id = get_tile_value(current_room->tiles, tile_pos); + assert(tile_id > 0); + tile_id -= 1; } else { @@ -615,6 +618,7 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga .color = color, .model = model, .tex_interp = tex_interp, + .tile_id = tile_id, .layer = RENDER_LAYER_TILES, }); } diff --git a/src/render.c b/src/render.c @@ -165,6 +165,7 @@ internal void renderer_jobs_draw(struct RendererState *renderer) shader_setv3(&renderer->shader, SHADER_UNIFORM_COLOR, &j.color); shader_setm4(&renderer->shader, SHADER_UNIFORM_MODEL, &j.model); shader_setf(&renderer->shader, SHADER_UNIFORM_TEX_INTERP, j.tex_interp); + shader_setf(&renderer->shader, SHADER_UNIFORM_TILE_ID, j.tile_id); if (j.ebo == renderer->quad_ebo) { glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); diff --git a/src/render.h b/src/render.h @@ -16,6 +16,7 @@ struct RenderJob v3 color; m4 model; f32 tex_interp; + u8 tile_id; enum RenderLayer layer; }; diff --git a/src/shader.c b/src/shader.c @@ -171,6 +171,9 @@ char *shader_uniform_get_name(enum ShaderUniform u) case SHADER_UNIFORM_TEX_INTERP: name = "tex_interp"; break; + case SHADER_UNIFORM_TILE_ID: + name = "tile_id"; + break; case NUM_SHADER_UNIFORMS: // fallthrough default: diff --git a/src/shader.h b/src/shader.h @@ -5,6 +5,7 @@ enum ShaderUniform SHADER_UNIFORM_PROJECTION, SHADER_UNIFORM_COLOR, SHADER_UNIFORM_TEX_INTERP, + SHADER_UNIFORM_TILE_ID, NUM_SHADER_UNIFORMS, }; diff --git a/src/world.c b/src/world.c @@ -224,7 +224,7 @@ internal struct Room *world_room_set(struct World *world, struct Room room, stru return result; } -internal u32 get_tile_value(u32 *tiles, v2 tile_pos) +internal u8 get_tile_value(u8 *tiles, v2 tile_pos) { assert(tiles); @@ -244,14 +244,14 @@ internal u32 get_tile_value(u32 *tiles, v2 tile_pos) u32 tile_i_linearized = (ROOM_TILE_DIM_X * tile_i_2d.y) + tile_i_2d.x; assert(tile_i_linearized < ROOM_TILE_COUNT); - u32 tile_value = tiles[tile_i_linearized]; + u8 tile_value = tiles[tile_i_linearized]; return tile_value; } -internal bool world_tile_in_room_is_solid(u32 *tiles, v2 tile_pos) +internal bool world_tile_in_room_is_solid(u8 *tiles, v2 tile_pos) { bool is_solid = false; - u32 tile_value = get_tile_value(tiles, tile_pos); + u8 tile_value = get_tile_value(tiles, tile_pos); is_solid = tile_value > 0; return is_solid; } diff --git a/src/world.h b/src/world.h @@ -26,7 +26,7 @@ static_assert( struct Room { v2i index; - u32 tiles[ROOM_TILE_COUNT]; + u8 tiles[ROOM_TILE_COUNT]; }; struct WorldChunk