Skip to content

Commit

Permalink
Read and load tilemap
Browse files Browse the repository at this point in the history
  • Loading branch information
claudemuller committed Nov 19, 2023
1 parent 5880f3a commit 3e89fe6
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 15 deletions.
77 changes: 68 additions & 9 deletions src/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "systems/render_collider_system.h"
#include "vector.h"
#include <SDL2/SDL_image.h>
#include <stdio.h>

const int FPS = 60;
const int MILLISECS_PER_FRAME = 1000 / FPS;
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
3 changes: 2 additions & 1 deletion src/engine.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef ENGINE_H_
#define ENGINE_H_

#include "entity.h"
#include "gfx.h"
#include "state_manager.h"
#include "asset_store.h"
Expand Down Expand Up @@ -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_
9 changes: 8 additions & 1 deletion src/entity.c
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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)
Expand Down
14 changes: 10 additions & 4 deletions src/entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,26 @@ 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(
entity_t *entity,
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
Expand Down
2 changes: 2 additions & 0 deletions src/systems/keyboard_control_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions src/systems/render_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
#include "../components/transform_component.h"
#include "render_system.h"
#include <SDL2/SDL_image.h>
#include <string.h>

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;
}
Expand Down

0 comments on commit 3e89fe6

Please sign in to comment.