-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.c
90 lines (81 loc) · 2.5 KB
/
setup.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* setup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sadamant <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/02/22 14:59:40 by sadamant #+# #+# */
/* Updated: 2018/03/13 13:29:03 by sadamant ### ########.fr */
/* */
/* ************************************************************************** */
#include "wolf3d.h"
static t_world *setup_world(char **argv)
{
t_world *world;
if (!(world = ft_memalloc(sizeof(t_world))))
exit_id(1);
world->wall_h = WALL_H;
world->map = parse_file(argv, world);
return (world);
}
static void position_player(t_player *p, char **map)
{
int i;
int j;
j = 0;
while (map[j])
{
i = 0;
while (map[j][i])
{
if (map[j][i] == 'P')
{
p->x = (i + 1) * TILE - TILE / 2;
p->y = (j + 1) * TILE - TILE / 2;
return ;
}
i++;
}
j++;
}
exit_error("Can't find player position. :(");
}
static t_player *setup_player(t_env *e)
{
t_player *p;
if (!(p = ft_memalloc(sizeof(t_player))))
exit_id(1);
position_player(p, e->world->map);
p->v = WALK_SPEED;
p->h = 75;
p->c = WALL_H * PLAYER_TO_PLANE;
p->fov = M_PI / 3;
p->cov = 120 * (M_PI / 180);
return (p);
}
t_env *setup_environment(int argc, char **argv)
{
t_env *e;
if (argc != 2)
{
ft_putendl_fd("Welcome to Wolf3D!", 2);
exit_error("usage: ./maps/mapfile");
}
e = ft_memalloc(sizeof(t_env));
e->world = setup_world(argv);
e->p = setup_player(e);
e->mlx = mlx_init();
e->wid = mlx_new_window(e->mlx, WINDOW_W, WINDOW_H, "wolf3d");
e->img = new_image(e, WINDOW_W, WINDOW_H);
e->key = ft_memalloc(sizeof(t_key));
e->t = ft_memalloc(sizeof(t_texture));
e->t->textures = load_textures(e);
if (!e->world || !e->p || !e->mlx || !e->wid || !e->img || !e->key || !e->t)
exit_id(1);
mlx_hook(e->wid, KEYPRESS_EVENT, 0, handle_keypress, e);
mlx_hook(e->wid, KEYRELEASE_EVENT, 0, handle_keyrelease, e);
mlx_hook(e->wid, REDX_EVENT, 0, quit_program, e);
mlx_loop_hook(e->mlx, refresh_screen, e);
return (e);
}