From 3e89fe6f35d39368f802255f6c92c03897000f5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claude=20M=C3=BCller?= Date: Sun, 19 Nov 2023 14:51:36 +0100 Subject: [PATCH] Read and load tilemap --- src/engine.c | 77 +++++++++++++++++++++++---- src/engine.h | 3 +- src/entity.c | 9 +++- src/entity.h | 14 +++-- src/systems/keyboard_control_system.c | 2 + src/systems/render_system.c | 2 + 6 files changed, 92 insertions(+), 15 deletions(-) diff --git a/src/engine.c b/src/engine.c index 8c971f8..8e34ddc 100644 --- a/src/engine.c +++ b/src/engine.c @@ -8,6 +8,7 @@ #include "systems/render_collider_system.h" #include "vector.h" #include +#include const int FPS = 60; const int MILLISECS_PER_FRAME = 1000 / FPS; @@ -52,20 +53,22 @@ bool engine_init(engine_t *engine, struct engine_options *options) void engine_setup(engine_t *engine) { + init_render_collider_system(); + init_keyboard_control_system(); + asset_store_add_texture(engine->graphics.renderer, "tilemap", "./assets/tilemaps/jungle.png"); asset_store_add_texture(engine->graphics.renderer, "tank", "./assets/tank.png"); - load_tilemap_data("./assets/tilemaps/jungle.map"); + entity_t *entities = NULL; - init_render_collider_system(); - init_keyboard_control_system(); + load_tilemap_data("./assets/tilemaps/jungle.map", &entities); - entity_t *entities = NULL; + printf("len:%d\n", array_length(&entities)); entity_t player = { - .id = 1, + .id = "player", }; - add_component_transform(&player, 10, 10, (vec2_t) { 1, 1 }); + add_component_transform(&player, 10, 10, (vec2_t) { 1, 1 }, 0); add_component_boxcollider( &player, 32, @@ -89,9 +92,9 @@ void engine_setup(engine_t *engine) array_push(entities, player); entity_t entity2 = { - .id = 2, + .id = "enemy1", }; - add_component_transform(&entity2, 100, 100, (vec2_t) { 1, 1 }); + add_component_transform(&entity2, 100, 100, (vec2_t) { 1, 1 }, 0); add_component_boxcollider( &entity2, 30, @@ -195,6 +198,62 @@ int engine_clean(engine_t *engine) return 0; } -void load_tilemap_data(const char *filename) +bool load_tilemap_data(const char *filename, entity_t **entities) { + FILE *fp = fopen(filename, "r"); + if (fp == NULL) { + SDL_LogError(1, "Error opening tilemap\n"); + return false; + } + + char *line = NULL; + size_t len = 0; + ssize_t read; + int x = 0, y = 0; + int tile_width = 32; + + while ((read = getline(&line, &len, fp)) != -1) { + char *token = strtok(line, ","); + while (token != NULL) { + char tile_id[50] = { 0 }; + sprintf(tile_id, "tile%d_%d", x, y); + + entity_t tile = { + .id = tile_id, + }; + add_component_transform( + &tile, + x * tile_width, + y * tile_width, + (vec2_t) { 1, 1 }, + 0 + ); + + int tile_num = atoi(token); + int tile_map_x = tile_num % 10; + int tile_map_y = tile_num / 10; + add_component_sprite( + &tile, + "tilemap", + tile_width, + tile_width, + tile_map_x * tile_width, + tile_map_y * tile_width, + 1, + false, + SDL_FLIP_NONE + ); + array_push(*entities, tile); + + token = strtok(NULL, ","); + x++; + } + x = 0; + y++; + } + + fclose(fp); + free(line); + + return true; } diff --git a/src/engine.h b/src/engine.h index 585e3bd..c6bfb0c 100644 --- a/src/engine.h +++ b/src/engine.h @@ -1,6 +1,7 @@ #ifndef ENGINE_H_ #define ENGINE_H_ +#include "entity.h" #include "gfx.h" #include "state_manager.h" #include "asset_store.h" @@ -30,6 +31,6 @@ void engine_process_input(engine_t *engine); void engine_update(engine_t *engine); void engine_render(engine_t *engine); int engine_clean(engine_t *engine); -void load_tilemap_data(const char *filename); +bool load_tilemap_data(const char *filename, entity_t **entities); #endif // ENGINE_H_ diff --git a/src/entity.c b/src/entity.c index 7e65611..e2e341f 100644 --- a/src/entity.c +++ b/src/entity.c @@ -1,7 +1,13 @@ #include "components/keyboard_control_component.h" #include "entity.h" -void add_component_transform(entity_t *entity, float x, float y, vec2_t scale) +void add_component_transform( + entity_t *entity, + const float x, + const float y, + const vec2_t scale, + const float rotation +) { entity->components.transform = malloc(sizeof(component_transform_t)); if (entity->components.transform) { @@ -12,6 +18,7 @@ void add_component_transform(entity_t *entity, float x, float y, vec2_t scale) entity->components.transform->position.y = y; entity->components.transform->scale.x = scale.x; entity->components.transform->scale.y = scale.y; + entity->components.transform->rotation = rotation; } void add_component_rigidbody(entity_t *entity, vec2_t vel) diff --git a/src/entity.h b/src/entity.h index f45a835..c41366a 100644 --- a/src/entity.h +++ b/src/entity.h @@ -18,11 +18,17 @@ typedef struct { } components_t; typedef struct { - unsigned int id; + const char *id; components_t components; } entity_t; -void add_component_transform(entity_t *entity, float x, float y, vec2_t scale); +void add_component_transform( + entity_t *entity, + const float x, + const float y, + const vec2_t scale, + const float rotation +); void add_component_rigidbody(entity_t *entity, vec2_t vel); void add_component_boxcollider(entity_t *entity, int width, int height, vec2_t offset, SDL_Color colour); void add_component_sprite( @@ -30,8 +36,8 @@ void add_component_sprite( const char *asset_id, const int width, const int height, - const int x, - const int y, + const int src_x, + const int src_y, const int z_index, const bool is_fixed, SDL_RendererFlip is_flipped diff --git a/src/systems/keyboard_control_system.c b/src/systems/keyboard_control_system.c index 4f589a8..106474d 100644 --- a/src/systems/keyboard_control_system.c +++ b/src/systems/keyboard_control_system.c @@ -61,4 +61,6 @@ int update_keyboard_control_system(entity_t *entities, const double dt) rigidbody->velocity.x += move_vec.x * PLAYER_SPEED * dt; rigidbody->velocity.y += move_vec.y * PLAYER_SPEED * dt; } + + return 0; } diff --git a/src/systems/render_system.c b/src/systems/render_system.c index 0295c1c..b490650 100644 --- a/src/systems/render_system.c +++ b/src/systems/render_system.c @@ -2,12 +2,14 @@ #include "../components/transform_component.h" #include "render_system.h" #include +#include int update_render_system(SDL_Renderer *renderer, entity_t *entities) { size_t entities_len = array_length(entities); for (size_t i = 0; i < entities_len; i++) { entity_t entity = entities[i]; + if (!entity.components.transform) { return 1; }