Browse Source

grab textures and colors

master
narnaud 3 years ago
parent
commit
a4668526cb
  1. 30
      README.md
  2. 13
      includes/cub3d.h
  3. 90
      sources/main.c

30
README.md

@ -3,13 +3,18 @@ CUBE3D
[Subject](https://cdn.intra.42.fr/pdf/pdf/48490/fr.subject.pdf)
## Resources :
- [Walls textures](https://opengameart.org/art-search-advanced?field_art_tags_tid=wall)
- [Convert to xpm](https://convertio.co/)
## Program life cycle :
- map parsing :
- extract textures,
- verify map format`
- import map `into array
- save player pos
- verify map validy
- [ ] read file,
- [ ] extract walls textures,
- [ ] extract sky and ceil colors
- [ ] import map into array,
- [ ] save player pos,
- [ ] handle errors,
- initialize env
- generate window
- display view
@ -25,21 +30,8 @@ CUBE3D
The map must be closed with the player inside the walls.
- calloc a 2D array as X size is the size of the longer
line and y is the numbers of map lines.
- set the array cells to 1 for floor, 2 for walls and 3 for player.
- set the array cells to 1 for floor, 2 for walls
- outside cells will be zeros.
- e.g :
0 0 0 2 2 2 2 2 2 2 2 2 2 2 2 0 0 0 0 0 2 2 2 2 0 0 0 0 0
0 0 0 2 1 1 1 1 1 1 1 1 1 1 2 0 0 0 2 2 2 1 1 2 0 0 0 0 0
2 2 2 2 2 1 1 1 1 1 1 1 1 1 2 2 2 2 2 1 1 1 1 2 2 2 0 0 0
0 0 0 0 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2
0 0 0 0 2 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 2
0 0 0 0 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
0 0 0 0 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
2 2 2 2 2 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 1 1 1 1 1 1 1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 2 2 2 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- read line by line until you encounter first wall then turn around
the map following the walls and never going on an already visited
wall until you come back to starting pos.

13
includes/cub3d.h

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/21 19:24:12 by narnaud #+# #+# */
/* Updated: 2022/05/23 10:44:57 by narnaud ### ########.fr */
/* Updated: 2022/05/23 13:05:28 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,7 +14,7 @@
# define CUB3D_H
# define BUFFER_SIZE 4096
# define UNIT 100
# define UNIT 200
# define WALL_HEIGHT 200
# define PLAYER_HEIGHT 170
# define SCREEN_DISTANCE 50
@ -46,6 +46,15 @@ typedef struct s_map
char **map;
} t_map;
typedef struct s_env
{
int step;
char *wallTexture[4];
int floorColor;
int ceilColor;
t_map *map;
} t_env;
char *get_next_line(int fd);
#endif

90
sources/main.c

@ -6,36 +6,96 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/21 19:22:50 by narnaud #+# #+# */
/* Updated: 2022/05/23 09:54:53 by narnaud ### ########.fr */
/* Updated: 2022/05/23 13:35:12 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/cub3d.h"
/*
void get_map_datas(t_env *env, char **elem);
{
return ;
}
*/
int rgb_to_int(char *rgb)
{
int ret;
int c;
char **colors;
ret = 0;
colors = ft_split(rgb, ',');
c = 0;
while (c < 3)
{
ret |= ft_atoi(colors[c]) << (8 * (2 - c));
c++;
}
ft_free_split(colors);
return (ret);
}
void register_data(t_env *env, char *line)
{
char **elem;
elem = ft_split(line, ' ');
if (!elem[0] || !elem[1])
return ;
if (!ft_strncmp(elem[0],"NO", 3))
env->wallTexture[0] = ft_strdup(elem[1]);
else if (!ft_strncmp(elem[0],"SO", 3))
env->wallTexture[1] = ft_strdup(elem[1]);
else if (!ft_strncmp(elem[0],"WE", 3))
env->wallTexture[2] = ft_strdup(elem[1]);
else if (!ft_strncmp(elem[0],"EA", 3))
env->wallTexture[3] = ft_strdup(elem[1]);
else if (!ft_strncmp(elem[0],"F", 2))
env->floorColor = rgb_to_int(elem[1]);
else if (!ft_strncmp(elem[0],"C", 2))
env->ceilColor = rgb_to_int(elem[1]);
//else if (env->step > 6)
// get_map_datas(env, elem);
else
env->step--;
env->step++;
ft_free_split(elem);
}
t_map parse_map(char *map_filename)
t_env *parse_envFile(char *filename)
{
static t_map ret;
int fd;
char *file_line;
char *line;
t_env *ret;
fd = open(map_filename, O_RDONLY);
file_line = get_next_line(fd);
while (file_line)
ret = malloc(sizeof(t_env));
fd = open(filename, O_RDONLY);
line = get_next_line(fd);
while (line)
{
ret.deep++;
printf("%s", file_line);
free(file_line);
file_line = get_next_line(fd);
register_data(ret, line);
printf("%s", line);
free(line);
line = get_next_line(fd);
}
if (ret->step < 6)
return (NULL);
printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
printf("%s\n", ret->wallTexture[0]);
printf("%s\n", ret->wallTexture[1]);
printf("%s\n", ret->wallTexture[2]);
printf("%s\n", ret->wallTexture[3]);
printf("%d\n", ret->floorColor);
printf("%d\n", ret->ceilColor);
return (ret);
}
int main(int argc, char **argv)
{
t_map map;
t_env *env;
(void)argc;
map = parse_map(argv[1]);
if (argc != 2)
return (EXIT_FAILURE);
env = parse_envFile(argv[1]);
return (EXIT_SUCCESS);
}

Loading…
Cancel
Save