From: Dominik Kaiser Date: Mon, 10 Jun 2024 14:39:55 +0000 (+0200) Subject: Cleanup X-Git-Url: https://git.dkaiser.de/?a=commitdiff_plain;h=ff7d60ad044e98e1e2e14170b5804d141856a949;p=42%2Fso_long.git Cleanup --- diff --git a/include/so_long.h b/include/so_long.h index 1090358..ea43bc7 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,7 +6,7 @@ /* By: dkaiser grid_size.x * map->grid_size.y); - if (!tiles) - return (1); // TODO: Error - ft_strlcpy(tiles, map->tiles, map->grid_size.x * map->grid_size.y + 1); - floodfill(tiles, map->grid_size, map->player_start_tile); - return (check_tiles(tiles, map->grid_size.x * map->grid_size.y)); + tiles = malloc(map->grid_size.x * map->grid_size.y); + if (!tiles) + { + ft_putstr_fd("Allocation error\n", 1); + return (1); + } + ft_strlcpy(tiles, map->tiles, map->grid_size.x * map->grid_size.y + 1); + floodfill(tiles, map->grid_size, map->player_start_tile); + return (check_tiles(tiles, map->grid_size.x * map->grid_size.y)); } -static void floodfill(char *tiles, t_ivector size, t_ivector pos) +static void floodfill(char *tiles, t_ivector size, t_ivector pos) { - if (tiles[pos.y * size.x + pos.x] == WALL || tiles[pos.y * size.x + pos.x] == 'X') - return ; - tiles[pos.y * size.x + pos.x] = 'X'; - floodfill(tiles, size, (t_ivector){pos.x - 1, pos.y}); - floodfill(tiles, size, (t_ivector){pos.x + 1, pos.y}); - floodfill(tiles, size, (t_ivector){pos.x, pos.y - 1}); - floodfill(tiles, size, (t_ivector){pos.x, pos.y + 1}); + if (tiles[pos.y * size.x + pos.x] == WALL) + return ; + if (tiles[pos.y * size.x + pos.x] == 'X') + return ; + tiles[pos.y * size.x + pos.x] = 'X'; + floodfill(tiles, size, (t_ivector){pos.x - 1, pos.y}); + floodfill(tiles, size, (t_ivector){pos.x + 1, pos.y}); + floodfill(tiles, size, (t_ivector){pos.x, pos.y - 1}); + floodfill(tiles, size, (t_ivector){pos.x, pos.y + 1}); } -static int check_tiles(char *tiles, int size) +static int check_tiles(char *tiles, int size) { - int i; + int i; - i = 0; - while (i < size) - { - if (tiles[i] != WALL && tiles[i] != 'X') - return (1); - i++; - } - return (0); + i = 0; + while (i < size) + { + if (tiles[i] != WALL && tiles[i] != 'X') + return (1); + i++; + } + return (0); } diff --git a/src/collision.c b/src/collision.c index 115b70a..c416184 100644 --- a/src/collision.c +++ b/src/collision.c @@ -6,7 +6,7 @@ /* By: dkaiser size; c.position.x = actor->position.x + (actor->velocity.x * delta_time); c.position.y = actor->position.y; if ((check_map_collision(c, map, WALL) & (RIGHT | LEFT)) == 0) + { actor->position.x = c.position.x; + is_moving += (actor->direction.x != 0); + } else actor->velocity.x = 0; c.position.x = actor->position.x; c.position.y = actor->position.y + (actor->velocity.y * delta_time); if ((check_map_collision(c, map, WALL) & (UP | DOWN)) == 0) + { actor->position.y = c.position.y; + is_moving += (actor->direction.y != 0); + } else actor->velocity.y = 0; + return (is_moving != 0); } int is_on_floor(t_collider collider, t_tilemap *map) diff --git a/src/draw.c b/src/draw.c index 5284d74..2c525dc 100644 --- a/src/draw.c +++ b/src/draw.c @@ -6,7 +6,7 @@ /* By: dkaiser player.img->instances[0].x = game->player.position.x; game->player.img->instances[0].y = game->player.position.y; - return (0); } -void draw_exit(t_game *game) +void draw_map(t_game *game) { - mlx_texture_t *exit_texture; - mlx_image_t *exit_image; - t_vector pos; - - exit_texture = mlx_load_png("textures/exit.png"); - exit_image = mlx_texture_to_image(game->mlx, exit_texture); - mlx_resize_image(exit_image, game->map.tile_size.x, game->map.tile_size.y); - pos = grid_to_screen_pos(game->map.exit_tile, game->map.tile_size); - mlx_image_to_window(game->mlx, exit_image, pos.x, pos.y); + draw_walls_and_collectibles(game); + draw_exit(game); } -int draw_walls(t_game *game) +static void draw_walls_and_collectibles(t_game *game) { - int x; - int y; - mlx_texture_t *wall_texture; - mlx_image_t *wall_image; - mlx_texture_t *collectible_texture; + int x; + int y; + mlx_image_t *wall_image; - wall_texture = mlx_load_png("textures/wall.png"); - wall_image = mlx_texture_to_image(game->mlx, wall_texture); - collectible_texture = mlx_load_png("textures/collectible.png"); - game->map.collectible_img = mlx_texture_to_image(game->mlx, collectible_texture); - mlx_resize_image(wall_image, 48, 48); - mlx_resize_image(game->map.collectible_img, 48, 48); - game->map.tile_size.x = 48; - game->map.tile_size.y = 48; + setup_images(game, &wall_image); x = 0; while (x < game->map.grid_size.x) { @@ -66,5 +53,31 @@ int draw_walls(t_game *game) } x++; } - return (0); +} + +static void setup_images(t_game *game, mlx_image_t **wall_image) +{ + mlx_texture_t *wall_texture; + mlx_texture_t *collectible_texture; + + wall_texture = mlx_load_png("textures/wall.png"); + *wall_image = mlx_texture_to_image(game->mlx, wall_texture); + collectible_texture = mlx_load_png("textures/collectible.png"); + game->map.collectible_img = mlx_texture_to_image(game->mlx, + collectible_texture); + mlx_resize_image(*wall_image, 48, 48); + mlx_resize_image(game->map.collectible_img, 48, 48); +} + +static void draw_exit(t_game *game) +{ + mlx_texture_t *exit_texture; + mlx_image_t *exit_image; + t_vector pos; + + exit_texture = mlx_load_png("textures/exit.png"); + exit_image = mlx_texture_to_image(game->mlx, exit_texture); + mlx_resize_image(exit_image, game->map.tile_size.x, game->map.tile_size.y); + pos = grid_to_screen_pos(game->map.exit_tile, game->map.tile_size); + mlx_image_to_window(game->mlx, exit_image, pos.x, pos.y); } diff --git a/src/init.c b/src/init.c index 524e2ab..c4600b0 100644 --- a/src/init.c +++ b/src/init.c @@ -6,7 +6,7 @@ /* By: dkaiser input_direction = ZERO; game->map.tile_size = (t_ivector){48, 48}; - game->mlx = mlx_init(game->map.grid_size.x * game->map.tile_size.x, game->map.grid_size.y * game->map.tile_size.y, "so_long", false); + game->mlx = mlx_init(game->map.grid_size.x * game->map.tile_size.x, + game->map.grid_size.y * game->map.tile_size.y, "so_long", false); init_hooks(game); init_player(game); return (0); @@ -43,6 +44,8 @@ static void init_player(t_game *game) game->map.tile_size); player->velocity = (t_vector){0, 0}; player->size = (t_ivector){44, 44}; + player->steps = 0; + ft_printf("Steps: %d\n", game->player.steps); player->img = mlx_texture_to_image(game->mlx, texture); mlx_resize_image(player->img, player->size.x, player->size.y); mlx_image_to_window(game->mlx, player->img, player->position.x, diff --git a/src/input.c b/src/input.c index a6cb8d4..0bf54ec 100644 --- a/src/input.c +++ b/src/input.c @@ -6,37 +6,49 @@ /* By: dkaiser input_direction |= UP; - else if (event.key == MLX_KEY_A || event.key == MLX_KEY_LEFT) - game->input_direction |= LEFT; - else if (event.key == MLX_KEY_S || event.key == MLX_KEY_DOWN) - game->input_direction |= DOWN; - else if (event.key == MLX_KEY_D || event.key == MLX_KEY_RIGHT) - game->input_direction |= RIGHT; - } + if (event.key == MLX_KEY_ESCAPE && event.action == MLX_PRESS) + mlx_close_window(game->mlx); + else if (event.action == MLX_PRESS) + on_key_press(game, event); else if (event.action == MLX_RELEASE) - { - if (event.key == MLX_KEY_W || event.key == MLX_KEY_UP) - game->input_direction &= ~UP; - else if (event.key == MLX_KEY_A || event.key == MLX_KEY_LEFT) - game->input_direction &= ~LEFT; - else if (event.key == MLX_KEY_S || event.key == MLX_KEY_DOWN) - game->input_direction &= ~DOWN; - else if (event.key == MLX_KEY_D || event.key == MLX_KEY_RIGHT) - game->input_direction &= ~RIGHT; - } + on_key_release(game, event); +} + +static void on_key_press(t_game *game, mlx_key_data_t event) +{ + if (event.key == MLX_KEY_W || event.key == MLX_KEY_UP) + game->input_direction |= UP; + else if (event.key == MLX_KEY_A || event.key == MLX_KEY_LEFT) + game->input_direction |= LEFT; + else if (event.key == MLX_KEY_S || event.key == MLX_KEY_DOWN) + game->input_direction |= DOWN; + else if (event.key == MLX_KEY_D || event.key == MLX_KEY_RIGHT) + game->input_direction |= RIGHT; +} + +static void on_key_release(t_game *game, mlx_key_data_t event) +{ + if (event.key == MLX_KEY_W || event.key == MLX_KEY_UP) + game->input_direction &= ~UP; + else if (event.key == MLX_KEY_A || event.key == MLX_KEY_LEFT) + game->input_direction &= ~LEFT; + else if (event.key == MLX_KEY_S || event.key == MLX_KEY_DOWN) + game->input_direction &= ~DOWN; + else if (event.key == MLX_KEY_D || event.key == MLX_KEY_RIGHT) + game->input_direction &= ~RIGHT; } diff --git a/src/loop.c b/src/loop.c index c4f95b3..ec7f1b3 100644 --- a/src/loop.c +++ b/src/loop.c @@ -6,7 +6,7 @@ /* By: dkaiser player.steps) + ft_printf("\e[1A\e[KSteps: %d\n", game->player.steps); draw(game); } diff --git a/src/main.c b/src/main.c index 75b58d2..10e68bc 100644 --- a/src/main.c +++ b/src/main.c @@ -6,24 +6,46 @@ /* By: dkaiser ")); if (load_map_from_file(&game.map, argv[1])) - return (1); + return (error("Not a valid map.")); if (check_map(&game.map)) - return (1); + { + free(game.map.tiles); + return (error("Not a valid map.")); + } init(&game); - draw_walls(&game); - draw_exit(&game); + draw_map(&game); mlx_loop(game.mlx); + cleanup(&game); +} + +static void cleanup(t_game *game) +{ + if (game->map.tiles) + free(game->map.tiles); + if (game->mlx) + mlx_terminate(game->mlx); +} + +static int error(char *msg) +{ + ft_putendl_fd("Error", 2); + ft_putendl_fd(msg, 2); + return (1); } diff --git a/src/player_process.c b/src/player_process.c index 4d78779..5c6c6fb 100644 --- a/src/player_process.c +++ b/src/player_process.c @@ -6,17 +6,18 @@ /* By: dkaiser velocity.x = player->direction.x * PLAYER_MOVE_SPEED; player->velocity.y = player->direction.y * PLAYER_MOVE_SPEED; handle_collectible_collision(player, &game->map); - handle_exit_collision(player, &game->map); - move_and_slide(player, &game->map, game->mlx->delta_time); + handle_exit_collision(game); + if (move_and_slide(player, &game->map, game->mlx->delta_time)) + player->steps++; } static void handle_collectible_collision(t_actor *player, t_tilemap *map) @@ -61,11 +63,10 @@ static void handle_collectible_collision(t_actor *player, t_tilemap *map) static void collect_collectible(t_ivector pos, t_tilemap *map) { - size_t i; - t_vector collectible_pos; - t_ivector collectible_tile; + size_t i; + t_vector collectible_pos; + t_ivector collectible_tile; - ft_printf("Collected a collectile.\n"); set_tile(map, pos.x, pos.y, EMPTY); i = 0; while (i < map->collectible_img->count) @@ -82,11 +83,15 @@ static void collect_collectible(t_ivector pos, t_tilemap *map) } } -static void handle_exit_collision(t_actor *player, t_tilemap *map) +static void handle_exit_collision(t_game *game) { t_collider player_collider; - size_t i; + size_t i; + t_actor *player; + t_tilemap *map; + player = &game->player; + map = &game->map; player_collider = (t_collider){player->position, player->size}; if (check_map_collision(player_collider, map, EXIT)) { @@ -97,7 +102,7 @@ static void handle_exit_collision(t_actor *player, t_tilemap *map) return ; i++; } - exit(0); + mlx_close_window(game->mlx); } }