Cub3D
Loading...
Searching...
No Matches
init_cub_contents.c
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* init_cub_contents.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: hnagasak <hnagasak@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/09/12 09:49:52 by kamitsui #+# #+# */
9/* Updated: 2024/12/18 02:08:47 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "cub3d.h"
14
15static char *get_file_contents(int fd)
16{
17 char *file_contents;
18 char *line;
19 char *temp;
20
21 file_contents = NULL;
22 temp = NULL;
23 line = get_next_line(fd);
24 while (line != NULL)
25 {
26 if (file_contents == NULL)
27 file_contents = ft_strjoin("", line);
28 else
29 {
30 temp = ft_strjoin(file_contents, line);
31 free(file_contents);
32 file_contents = temp;
33 }
34 line = ft_free(line);
35 if (file_contents == NULL)
36 return (NULL);
37 line = get_next_line(fd);
38 }
39 return (file_contents);
40}
41
42static char *read_cubfile(char *filepath)
43{
44 int fd;
45 char *file_contents;
46
47 fd = open(filepath, O_RDONLY);
48 if (fd == -1)
49 {
50 ft_eprintf("%s%s\n", ERR_PROMPT, EMSG_OPEN_FAILED);
51 return (NULL);
52 }
53 file_contents = get_file_contents(fd);
54 if (file_contents == NULL)
55 {
56 ft_eprintf("%s%s\n", ERR_PROMPT, EMSG_READ_FAILED);
57 return (NULL);
58 }
59 close(fd);
60 return (file_contents);
61}
62
63static void init_frame(t_game *game)
64{
65 t_frame *frame;
66
67 frame = &game->frame;
68 frame->map = (t_map *)&game->map;
69 frame->img_3d = (t_img *)&game->img_3d;
70 frame->texture = (t_texture *)&game->texture;
71 frame->player = (t_player *)&game->player;
72 frame->flag = 0;
73}
74
75int init_cub_contents(t_game *game, char *filename)
76{
77 char *file_contents;
78 int status;
79 t_parse parse;
80
81 file_contents = read_cubfile(filename);
82 if (file_contents == NULL)
83 return (EXIT_FAILURE);
84 parse.flag = 0x00;
85 parse.game = game;
86 status = parse_cubfile(&parse, game, (const char *)file_contents);
87 init_frame(game);
88 free(file_contents);
89 if (status != EXIT_SUCCESS)
90 return (EXIT_FAILURE);
91 return (EXIT_SUCCESS);
92}
single still image for render_frame
Definition type_cub3d.h:247
main game
Definition type_cub3d.h:264
3D or 2D or Texture image
Definition type_cub3d.h:82
game map
Definition type_cub3d.h:237
keyboard event flag
Definition type_cub3d.h:226
texture
Definition type_cub3d.h:94