-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
109 lines (101 loc) · 3.09 KB
/
main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nschumac <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/18 18:28:32 by nschumac #+# #+# */
/* Updated: 2021/10/08 16:47:08 by nschumac ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub.h"
#include "validate/validate.h"
#include "render/render.h"
#include "sprite/sprite.h"
int cross_hook(int key, void *param)
{
exit(0);
(void)param;
(void)key;
return (1);
}
static void ft_docf(t_mlx *mlx)
{
int x;
int y;
unsigned int color;
y = 0;
while (y < mlx->win.height)
{
x = 0;
while (x < mlx->win.width)
{
if (y <= mlx->win.height / 2)
color = ft_tcolour2uint(mlx->file.clr[ROOF]);
else
color = ft_tcolour2uint(mlx->file.clr[FLOOR]);
ft_putpixelimg(mlx->imgs[0], x, y, color);
x++;
}
y++;
}
}
static void ft_move(t_mlx *mlx)
{
if (mlx->file.map.arr[(int)(mlx->player.x_coord + mlx->player.x_move)
/ mlx->cube_size][abs((int)(mlx->player.y_coord + mlx->player.y_move)
/ mlx->cube_size)] == '1')
return ;
mlx->player.x_coord += mlx->player.x_move;
mlx->player.y_coord += mlx->player.y_move;
}
int render_next_frame(t_mlx *mlx)
{
if (mlx->menu == GAME_SCREEN)
{
update_map(&mlx->file.map, mlx);
mlx_put_image_to_window(mlx->mlx_ptr, mlx->win.win_ptr,
mlx->imgs[0]->img_ptr, 0, 0);
mlx_put_image_to_window(mlx->mlx_ptr, mlx->win.win_ptr,
mlx->map->img_ptr, 0, 0);
if (mlx->fcount % 4 == 0)
{
mlx->sprites[SP_COIN]->current_frame++;
if (mlx->sprites[SP_COIN]->current_frame
>= mlx->sprites[SP_COIN]->frame_count)
mlx->sprites[SP_COIN]->current_frame = 0;
ft_setframe(mlx->sprites[SP_COIN]);
}
ft_docf(mlx);
if (mlx->player.move)
ft_move(mlx);
ft_rendergame(mlx);
mlx->fcount++;
return (0);
}
mlx_put_image_to_window(mlx->mlx_ptr, mlx->win.win_ptr,
mlx->imgs[0]->img_ptr, 0, 0);
return (ft_mainscreen(mlx));
}
int main(int argc, char *argv[])
{
t_mlx mlx;
if (reader(&mlx, argc, argv))
return (1);
if (init_mlx_stuff(&mlx))
return (1);
ft_docf(&mlx);
ft_rendergame(&mlx);
mlx_put_image_to_window(mlx.mlx_ptr, mlx.win.win_ptr,
mlx.imgs[0]->img_ptr, 0, 0);
ft_mainscreen(&mlx);
mlx_hook(mlx.win.win_ptr, 6, (1L << 6), mouse_hook, &mlx);
mlx_hook(mlx.win.win_ptr, 2, (1L << 0), key_press_hook, &mlx);
mlx_hook(mlx.win.win_ptr, 17, (1L << 0), cross_hook, &mlx);
mlx_key_hook(mlx.win.win_ptr, key_hook, &mlx);
mlx_loop_hook(mlx.mlx_ptr, render_next_frame, &mlx);
mlx_mouse_hide();
mlx_loop(mlx.mlx_ptr);
return (0);
}