From: Dominik Kaiser Date: Sat, 11 May 2024 14:26:51 +0000 (+0200) Subject: Finish map input X-Git-Url: https://git.dkaiser.de/?a=commitdiff_plain;h=f199b13df1350aa864a9104616f0d9b82606b4ed;p=42%2Fso_long.git Finish map input --- diff --git a/include/so_long.h b/include/so_long.h index 2333368..47443aa 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -63,6 +63,8 @@ typedef struct s_tilemap t_ivector grid_size; t_ivector tile_size; char *tiles; + t_ivector player_start_tile; + t_ivector exit_tile; } t_tilemap; typedef struct s_game diff --git a/src/init.c b/src/init.c index 3c15791..5277b5c 100644 --- a/src/init.c +++ b/src/init.c @@ -6,7 +6,7 @@ /* By: dkaiser mlx = mlx_init(1920, 1080, "so_long", false); // TODO: make size and title dynamic game->input_direction = ZERO; init_hooks(game); - init_player(&game->player, 960, 540); - // TODO: make player spawn point dynamic - texture = mlx_load_png("textures/player.png"); - game->player.img = mlx_texture_to_image(game->mlx, texture); - mlx_resize_image(game->player.img, 48, 48); - mlx_image_to_window(game->mlx, game->player.img, game->player.position.x, - game->player.position.y); + init_player(game); return (0); } @@ -40,10 +33,19 @@ static void init_hooks(t_game *game) mlx_key_hook(game->mlx, on_key_input, game); } -static void init_player(t_player *player, int x, int y) +static void init_player(t_game *game) { - player->position.x = x; - player->position.y = y; + mlx_texture_t *texture; + t_player *player; + + player = &game->player; + texture = mlx_load_png("textures/player.png"); + player->position.x = game->map.player_start_tile.x * 48; + player->position.y = game->map.player_start_tile.y * 48; player->velocity.x = 0; player->velocity.y = 0; + player->img = mlx_texture_to_image(game->mlx, texture); + mlx_resize_image(player->img, 48, 48); + mlx_image_to_window(game->mlx, player->img, player->position.x, + player->position.y); } diff --git a/src/tilemap.c b/src/tilemap.c index 45d9fa5..ef830c7 100644 --- a/src/tilemap.c +++ b/src/tilemap.c @@ -6,11 +6,12 @@ /* By: dkaiser tiles[l * tilemap->grid_size.x + x] = line[x]; - else + if (line[pos.x] != '0' && line[pos.x] != '1' && line[pos.x] != 'C' + && line[pos.x] != 'P' && line[pos.x] != 'E') return (1); - line++; + if (line[pos.x] == PLAYER_START && !found_player_start) + { + tilemap->player_start_tile = pos; + found_player_start = 1; + } + else if (line[pos.x] == EXIT && !found_exit) + { + tilemap->exit_tile = pos; + found_exit = 1; + } + tilemap->tiles[l * tilemap->grid_size.x + pos.x] = line[pos.x]; + pos.x++; } return (0); }