diff --git a/inc/map.h b/inc/map.h index 7b4b8d9..a4be9c9 100644 --- a/inc/map.h +++ b/inc/map.h @@ -3,10 +3,10 @@ /* :::::::: */ /* map.h :+: :+: */ /* +:+ */ -/* By: qmennen +#+ */ +/* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/17 19:19:19 by qmennen #+# #+# */ -/* Updated: 2025/05/14 12:42:54 by whaffman ######## odam.nl */ +/* Updated: 2025/05/25 13:39:13 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -15,16 +15,17 @@ # include "cub3d.h" -int map_create(t_game **game, const char *mapfile); -void map_free(t_map *map); -void print_map(t_map *map); -t_tile get_tile(t_map *map, int x, int y); -int enclosed_map(t_map *map); -void grid_free(t_tile **grid, int height); -t_tile **copy_map(t_tile **grid, int width, int height); -int parse_args(const char *mapfile, t_game *game); -t_tile **create_grid(int width, int height); -int find_player_or_empty(t_map *map, int *x, int *y); -int floodfill(t_map *map, int x, int y); +int map_create(t_game **game, const char *mapfile); +void map_free(t_map *map); +void print_map(t_map *map); +t_tile get_tile(t_map *map, int x, int y); +int enclosed_map(t_map *map); +void grid_free(t_tile **grid, int height); +t_tile **copy_map(t_tile **grid, int width, int height); +int parse_args(const char *mapfile, t_game *game); +t_tile **create_grid(int width, int height); +int find_player_or_empty(t_map *map, int *x, int *y); +int floodfill(t_map *map, int x, int y); +t_sprite make_sprite(mlx_texture_t *texture, double x, double y, int collectible); #endif diff --git a/inc/types.h b/inc/types.h index 324c033..a431cd1 100644 --- a/inc/types.h +++ b/inc/types.h @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */ -/* Updated: 2025/05/23 15:17:29 by whaffman ######## odam.nl */ +/* Updated: 2025/05/25 13:37:39 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -58,6 +58,7 @@ typedef struct s_player float battery; } t_player; + typedef struct s_sprite { int n_frames; @@ -70,20 +71,24 @@ typedef struct s_sprite t_vec2 pos; } t_sprite; +typedef struct s_sprite_lib +{ + mlx_texture_t *texture; + int collectible; +} t_sprite_lib; + typedef struct s_map { unsigned int width; unsigned int height; t_tile **grid; - char *north_texture; - char *south_texture; - char *west_texture; - char *east_texture; mlx_texture_t *texture_floor; mlx_texture_t *texture_ceiling; mlx_texture_t *textures[4]; t_sprite *sprites; + t_sprite_lib *sprite_lib; unsigned int n_sprites; + unsigned int n_sprites_max; unsigned int floor_color; unsigned int ceiling_color; } t_map; diff --git a/src/map/grid_free.c b/src/map/grid_free.c index 3d790a4..da78d2a 100644 --- a/src/map/grid_free.c +++ b/src/map/grid_free.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/23 12:20:38 by whaffman #+# #+# */ -/* Updated: 2025/05/07 11:21:14 by whaffman ######## odam.nl */ +/* Updated: 2025/05/25 18:57:09 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/map/map_create.c b/src/map/map_create.c index 1724649..a148990 100644 --- a/src/map/map_create.c +++ b/src/map/map_create.c @@ -3,10 +3,10 @@ /* :::::::: */ /* map_create.c :+: :+: */ /* +:+ */ -/* By: qmennen +#+ */ +/* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/23 12:21:13 by whaffman #+# #+# */ -/* Updated: 2025/05/22 18:45:53 by whaffman ######## odam.nl */ +/* Updated: 2025/05/25 13:58:34 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -20,7 +20,15 @@ static int map_allocate(t_game **game) perror("Error allocating memory for (*game)->map"); return (FAILURE); } - ft_memset((*game)->map->textures, 0, sizeof((*game)->map->textures)); + ft_memset((*game)->map, 0, sizeof((*game)->map)); + (*game)->map->sprite_lib = malloc(sizeof(t_sprite_lib) * 26); + if (!(*game)->map->sprite_lib) + { + perror("Error allocating memory for (*game)->map->sprite_lib"); + free((*game)->map); + return (FAILURE); + } + ft_memset((*game)->map->sprite_lib, 0, sizeof(t_sprite_lib) * 26); return (SUCCESS); } diff --git a/src/map/map_free.c b/src/map/map_free.c index 8bcca15..95e52ad 100644 --- a/src/map/map_free.c +++ b/src/map/map_free.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/23 12:22:28 by whaffman #+# #+# */ -/* Updated: 2025/05/24 14:27:07 by whaffman ######## odam.nl */ +/* Updated: 2025/05/25 18:56:27 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -25,11 +25,14 @@ void map_free(t_map *map) mlx_delete_texture(map->texture_floor); if(map->texture_ceiling) mlx_delete_texture(map->texture_ceiling); - while (map->n_sprites > 0) + i = 0; + while (i < 26) { - map->n_sprites--; - mlx_delete_texture(map->sprites[map->n_sprites].texture); + if (map->sprite_lib[i].texture) + mlx_delete_texture(map->sprite_lib[i].texture); + i++; } + free(map->sprite_lib); free(map->sprites); i = 4; while (i > 0) diff --git a/src/parser/parse_config_line.c b/src/parser/parse_config_line.c index 481f6df..9949d20 100644 --- a/src/parser/parse_config_line.c +++ b/src/parser/parse_config_line.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/22 13:10:06 by whaffman #+# #+# */ -/* Updated: 2025/05/23 17:29:05 by whaffman ######## odam.nl */ +/* Updated: 2025/05/25 11:13:53 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -100,6 +100,28 @@ int handle_fc_texture(char *token, t_map *map) int handle_sprite(char *token, t_map *map) { + t_sprite_lib sprite; + char symbol; + char *texture_path; + + if (token[1] == 'c') + sprite.collectible = 1; + else + sprite.collectible = 0; + symbol = *ft_strtok(NULL, " "); + if (symbol < 'a' || symbol > 'z') + return (ft_putstr_fd("Error: Invalid sprite symbol\n", 2), FAILURE); + texture_path = ft_strtok(NULL, " "); + if (texture_path == NULL) + return (ft_putstr_fd("Error: Missing texture path\n", 2), FAILURE); + if (ft_strtok(NULL, " ") != NULL) + return (ft_putstr_fd("Error: Extra tokens after path\n", 2), FAILURE); + sprite.texture = load_texture(texture_path); + if (sprite.texture == NULL) + return (FAILURE); + if (map->sprite_lib[symbol - 'a'].texture != NULL) + return (ft_putstr_fd("Error: Sprite already defined for this symbol\n", 2), FAILURE); + map->sprite_lib[symbol - 'a'] = sprite; return (SUCCESS); } diff --git a/src/parser/parse_map.c b/src/parser/parse_map.c index e13c20c..1472648 100644 --- a/src/parser/parse_map.c +++ b/src/parser/parse_map.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* parse_map.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/04/22 13:12:04 by whaffman #+# #+# */ -/* Updated: 2025/05/22 14:01:24 by qmennen ### ########.fr */ +/* :::::::: */ +/* parse_map.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/04/22 13:12:04 by whaffman #+# #+# */ +/* Updated: 2025/05/25 18:54:20 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -74,26 +74,92 @@ int parse_map_line(char **lines, t_game *game, int y) return (SUCCESS); } -int parse_map(char **lines, t_game *game) +int count_sprites(char *line) +{ + int count; + int i; + + count = 0; + i = 0; + while (line[i]) + { + if (line[i] >= 'a' && line[i] <= 'z') + count++; + i++; + } + return (count); +} +int prepare_map(char **lines, t_game *game) { int y; t_map *map; - y = 0; - while (lines[y]) - y++; map = game->map; + y = 0; + map->n_sprites_max = 0; + map->n_sprites = 0; + while (lines[y]) + { + map->n_sprites_max += count_sprites(lines[y]); + y++; + } map->height = y; map->width = map_width(lines); map->grid = malloc(sizeof(t_tile *) * (map->height + 1)); if (!map->grid) return (FAILURE); ft_memset(map->grid, 0, sizeof(t_tile *) * (map->height + 1)); + printf(" n_sprite_max %d\n", map->n_sprites_max); + map->sprites = malloc(sizeof(t_sprite) * (map->n_sprites_max + 1)); + if (!map->sprites) + return (FAILURE); + ft_memset(map->sprites, 0, sizeof(t_sprite) * (map->n_sprites_max+ 1)); + return (SUCCESS); +} + +int parse_map_line_sprites(char *line, t_game *game, int y) +{ + int x; + t_map *map; + t_sprite_lib *sprite_lib; + + map = game->map; + sprite_lib = map->sprite_lib; + x = 0; + while (line[x]) + { + if (line[x] >= 'a' && line[x] <= 'z') + { + if (sprite_lib[line[x] - 'a'].texture == NULL) + { + ft_putstr_fd("Error: Undefined sprite symbol\n", 2); + return (FAILURE); + } + map->sprites[map->n_sprites] = make_sprite( + sprite_lib[line[x] - 'a'].texture, + (double)x + 0.5f, (double)y + 0.5f, + sprite_lib[line[x] - 'a'].collectible); + map->n_sprites++; + } + x++; + } + return (SUCCESS); +} +int parse_map(char **lines, t_game *game) +{ + int y; + t_map *map; + + map = game->map; + if (!prepare_map(lines, game)) + return (FAILURE); y = 0; while (y < map->height) { if (!parse_map_line(lines, game, y)) return (FAILURE); + if (!parse_map_line_sprites(lines[y], game, y)) + return (FAILURE); y++; } return (SUCCESS); diff --git a/src/parser/parse_tile.c b/src/parser/parse_tile.c index 2969c65..4e8112e 100644 --- a/src/parser/parse_tile.c +++ b/src/parser/parse_tile.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/22 13:12:31 by whaffman #+# #+# */ -/* Updated: 2025/05/07 11:48:30 by whaffman ######## odam.nl */ +/* Updated: 2025/05/25 11:49:44 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -22,6 +22,8 @@ int parse_tile(char c) return (TILE_VOID); else if (c == 'N' || c == 'S' || c == 'E' || c == 'W') return (TILE_PLAYER); + else if (c >= 'a' && c <= 'z') + return (TILE_EMPTY); else return (-2); } diff --git a/src/parser/print_config.c b/src/parser/print_config.c index 28e6548..75c1924 100644 --- a/src/parser/print_config.c +++ b/src/parser/print_config.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/22 13:11:18 by whaffman #+# #+# */ -/* Updated: 2025/05/14 20:00:32 by whaffman ######## odam.nl */ +/* Updated: 2025/05/25 10:53:31 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,10 +17,10 @@ void print_config(t_map *map) printf("Map:\n"); printf("Width: %d, Height: %d\n", map->width, map->height); printf("Textures:\n"); - printf("NO: %s\n", map->north_texture); - printf("SO: %s\n", map->south_texture); - printf("WE: %s\n", map->west_texture); - printf("EA: %s\n", map->east_texture); + // printf("NO: %s\n", map->north_texture); + // printf("SO: %s\n", map->south_texture); + // printf("WE: %s\n", map->west_texture); + // printf("EA: %s\n", map->east_texture); printf("Floor color: %u\n", map->floor_color); printf("Ceiling color: %u\n", map->ceiling_color); printf("Grid:\n"); diff --git a/src/util/initialize.c b/src/util/initialize.c index 3f8cff5..a059230 100644 --- a/src/util/initialize.c +++ b/src/util/initialize.c @@ -3,34 +3,29 @@ /* :::::::: */ /* initialize.c :+: :+: */ /* +:+ */ -/* By: qmennen +#+ */ +/* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/22 17:08:26 by qmennen #+# #+# */ -/* Updated: 2025/05/23 17:28:36 by whaffman ######## odam.nl */ +/* Updated: 2025/05/25 13:41:32 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "cub3d.h" -static t_sprite make_sprite(char *path, double x, double y, int collectible) +t_sprite make_sprite(mlx_texture_t *texture, double x, double y, int collectible) { t_sprite sprite; ft_memset(&sprite, 0, sizeof(t_sprite)); - sprite.texture = mlx_load_png(path); - if (!sprite.texture) - { - fprintf(stderr, "Error loading texture: %s\n", path); - exit(EXIT_FAILURE); - } + sprite.texture = texture; if (sprite.texture->width % 64 != 0) { - fprintf(stderr, "Error: Texture %s width not a multiple of 64\n", path); + ft_putstr_fd("Error: Texture width is not a multiple of 64\n", 2); exit(EXIT_FAILURE); } if (sprite.texture->height != 64) { - fprintf(stderr, "Error: Texture %s height is not 64\n", path); + ft_putstr_fd("Error: Texture height must be 64\n", 2); exit(EXIT_FAILURE); } sprite.n_frames = sprite.texture->width / 64; @@ -43,23 +38,23 @@ static t_sprite make_sprite(char *path, double x, double y, int collectible) static int init_temp(t_game **game) { - t_sprite *sprites; + // t_sprite *sprites; - (*game)->map->sprites = malloc(sizeof(t_sprite) * 10); - if (!(*game)->map->sprites) - return (FAILURE); - sprites = (*game)->map->sprites; - sprites[0] = make_sprite("./assets/battery.png", 3.5, 3.5, 1); - sprites[1] = make_sprite("./assets/plant.png", 2.5, 3.5, 0); - sprites[2] = make_sprite("./assets/plant.png", 1.5, 3.5, 0); - sprites[3] = make_sprite("./assets/broken_mirror.png", 3.5, 4.5, 0); - sprites[4] = make_sprite("./assets/lamp.png", 2.5, 5.5, 0); - sprites[5] = make_sprite("./assets/battery.png", 10.5, 6.5, 1); - sprites[6] = make_sprite("./assets/plant.png", 14.5, 3.5, 0); - sprites[7] = make_sprite("./assets/battery.png", 2.5, 7.5, 1); - sprites[8] = make_sprite("./assets/broken_mirror.png", 42.5, 4.5, 0); - sprites[9] = make_sprite("./assets/lamp.png", 9.5, 10.5, 0); - (*game)->map->n_sprites = 10; + // (*game)->map->sprites = malloc(sizeof(t_sprite) * 10); + // if (!(*game)->map->sprites) + // return (FAILURE); + // sprites = (*game)->map->sprites; + // sprites[0] = make_sprite("./assets/battery.png", 3.5, 3.5, 1); + // sprites[1] = make_sprite("./assets/plant.png", 2.5, 3.5, 0); + // sprites[2] = make_sprite("./assets/plant.png", 1.5, 3.5, 0); + // sprites[3] = make_sprite("./assets/broken_mirror.png", 3.5, 4.5, 0); + // sprites[4] = make_sprite("./assets/lamp.png", 2.5, 5.5, 0); + // sprites[5] = make_sprite("./assets/battery.png", 10.5, 6.5, 1); + // sprites[6] = make_sprite("./assets/plant.png", 14.5, 3.5, 0); + // sprites[7] = make_sprite("./assets/battery.png", 2.5, 7.5, 1); + // sprites[8] = make_sprite("./assets/broken_mirror.png", 42.5, 4.5, 0); + // sprites[9] = make_sprite("./assets/lamp.png", 9.5, 10.5, 0); + // (*game)->map->n_sprites = 10; (*game)->screen->hud = mlx_texture_to_image((*game)->screen->mlx, mlx_load_png("./assets/overlay2.png")); return (SUCCESS); diff --git a/test.cub b/test.cub index 2b8730f..95e2da0 100644 --- a/test.cub +++ b/test.cub @@ -15,21 +15,26 @@ C 100,100,200 FT ./assets/floor.png CT ./assets/ceiling64x64.png +-s a ./assets/lamp.png +-c b ./assets/battery.png +-s p ./assets/plant.png +-s m ./assets/broken_mirror.png + 1111111 111 1111 111111 1111111111 111111 1000001110111001 100001 1000000001 10001 1001000000000001 100001 100000110111100011 -1000001110000001110011111111 110000001000000001 -1000001 10001000000010000001 100000001000000001 -1000001110001000000000000001111100000001000000001 -1000000000001111111111100000000000111111000000001 +10b0001110000001110011111111 110000001000000001 +1000001 1000100a000010000001 100000001000000001 +100m00111000100000b00000000111110p00000100p00m001 +10b0000000001111111111100000000000111111000000001 1000000W00001 1000011111111 1000001001 -1000011111001 111111100001 1111111111110001 -100001 1001 1000000000011111111000010111 10001 +10p0011111001 111111100001 1111111111110001 +100001 1001 1p00000000011111111000010111 10b01 111111 1001 1000001000000000001000000101 100001 - 1001 1000001111111111111000010001 1000001 - 1001 1000000000000000000001110001 100001 - 1001 1000000000000000000011 11111 100001 + 1001 10000p1111111111111000010001 10000m1 + 1001 100000000000000000000111b001 100001 + 1001 1b00000000000p00000011 11111 100001 10011111111100111111111111111111001110001 - 1000000000000000000000000000000000000001 - 11111100111111101100111100011101011111111 + 1000000000000000000000b000000000000000001 + 111111b0111111101100111100011101011111111 1111 1111111 11111 11111 \ No newline at end of file