-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredraw.c
More file actions
91 lines (82 loc) · 2.83 KB
/
Copy pathredraw.c
File metadata and controls
91 lines (82 loc) · 2.83 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* redraw.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: phelebra <xhelp00@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/10 16:50:14 by jbartosi #+# #+# */
/* Updated: 2023/10/16 16:05:02 by phelebra ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
void init_redraw(t_box *box)
{
mlx_destroy_image(box->mlx, box->image.img);
box->image.bits_pp = 0;
box->image.line_len = 0;
box->image.endian = 0;
box->image.img = mlx_new_image(box->mlx, SCREENWIDTH, SCREENHEIGHT);
box->image.addr = (unsigned char *)mlx_get_data_addr(box->image.img,
&box->image.bits_pp, &box->image.line_len, &box->image.endian);
}
void handle_game_state(t_box *box)
{
double frame;
if (box->player.hit)
fill_screen_red(box);
mlx_put_image_to_window(box->mlx, box->win, box->image.img, 0, 0);
if (box->won || box->lost)
{
frame = ((((box->time.tv_sec - box->fin_time.tv_sec)
+ ((box->time.tv_usec - box->fin_time.tv_usec)
/ 1000000.0)) * 10) * 16) / 10;
box->player.frame = frame;
if (box->lost)
mlx_put_image_to_window(box->mlx, box->win, box->textures[GRIM].img,
360, 95);
else if (box->win)
mlx_put_image_to_window(box->mlx, box->win, box->textures[WIN].img,
320, 40);
if (box->player.frame > 100)
exit_hook(box);
}
}
void display_and_free_stat(t_box *box, int value, int x, int y)
{
char *nbr;
nbr = ft_itoa(value);
mlx_string_put(box->mlx, box->win, x, y, 0x00FFFFFF, nbr);
free(nbr);
}
void update_hud(t_box *box)
{
char *nbr;
nbr = ft_itoa(1.0 / box->info.frame_time);
mlx_string_put(box->mlx, box->win, 20, 20, 0x00FFFFFF, nbr);
free(nbr);
if (box->hud)
{
display_and_free_stat(box, box->player.speed, 65, 203);
display_and_free_stat(box, box->player.range, 65, 245);
display_and_free_stat(box, box->player.fire_rate, 65, 287);
display_and_free_stat(box, box->player.shot_speed, 65, 329);
display_and_free_stat(box, box->player.dmg, 65, 371);
}
nbr = ft_itoa(box->player.n_key);
mlx_string_put(box->mlx, box->win, 50, 70, 0x00FFFFFF, nbr);
free(nbr);
}
void redraw(t_box *box)
{
init_redraw(box);
cast_floor(box);
cast_wall(box);
cast_obj(box);
cal_move(box);
cal_sprite_move(box);
draw_minimap(box);
draw_hud(box);
handle_game_state(box);
update_hud(box);
}