-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation_info.txt
115 lines (100 loc) · 5.82 KB
/
animation_info.txt
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
110
111
112
113
114
115
ANIMATIONS:
1o: Arranjar a sequencia de XPM's a colocar no projeto.
O nome dos ficheiros devera ser do tipo: (comecando do 1)
- gun1.xpm
- gun2.xpm
- gun(...).xpm
- gun16.xpm
2o: Para fazer o load das animacoes e necessario:
- Criar uma variavel na struct t_animation com o nome gun_animation(por exemplo)
- na funcao load_textures_to_mlx():
- load_gun(game);
- Na funcao load_gun(t_game *game):
- game->gun_animation.frameNum = Numero total de ficheiros xpm (neste caso 16);
- game->gun_animation.frames = load_n_images(game, "gun", 16);
-->colocar a string com apenas o nome chave dos ficheiros xpm
neste caso, gun1.xpm, etc... ficaria apenas "gun"
- game->gun_animation.current_frame = game->gun_animation.frames; // current_frame to print
- game->gun_animation.trigger = 0; // 1 for animation active
- game->gun_animation.frameCount = frameNum; // which frame it is on. 16 means 0, 15 means 1, etc...
3o: A funcao update trata de executar a animacao:
update --> update_gun(game):
// game->now_time and game->old_time are initialized before the loop starts
void update_gun(t_game *game)
{
static t_anim_list *current_frame = NULL; // init current_frame to no frame
int time_now;
int time_old;
if (current_frame == NULL) // first initialization
current_frame = game->player_animation.frames;
if (game->player_animation.trigger == 1) // if animation is triggered in event_hook
{
gettimeofday(&game->now_time, NULL); // get the current time
time_now = game->now_time.tv_sec * 1000 + game->now_time.tv_usec / 1000; // convert to milliseconds
time_old = game->old_time.tv_sec * 1000 + game->old_time.tv_usec / 1000; // convert to milliseconds
if (time_now - time_old > 60) // if 60 milliseconds have passed (best suited for the gun animation)
{
game->old_time = game->now_time; // update old_time
printf("time: %d\n", time_now - time_old);
if (game->player_animation.frameCount > 0) // if frameCount is not 0, print current frame
{
game->player_animation.current_frame = current_frame; // current_frame is the frame to print
current_frame = current_frame->next;
game->player_animation.frameCount--; // iterate to next frame
}
if (game->player_animation.frameCount == 0) // if no frames left
{
game->player_animation.trigger = 0; // shutdown animation
game->player_animation.frameCount = game->player_animation.frameNum; // reset frameCount
current_frame = NULL; // reset current_frame
game->player_animation.current_frame = game->player_animation.frames; // reset current_frame to first frame
}
}
}
}
void update_gun(t_game *game)
{
static t_anim_list *current_frame = NULL;
if (current_frame == NULL)
current_frame = game->player_animation.frames;
if (game->player_animation.trigger == 1)
{
if (game->player_animation.frameCount > 0)
{
game->player_animation.current_frame = current_frame;
current_frame = current_frame->next;
game->player_animation.frameCount--;
}
if (game->player_animation.frameCount == 0)
{
game->player_animation.trigger = 0;
game->player_animation.frameCount = game->player_animation.frameNum;
current_frame = NULL;
game->player_animation.current_frame = game->player_animation.frames;
}
}
}
5o: A funcao draw_gun() trata de desenhar a animacao segundo a current_frame:
void draw_weapon(t_game *game)
{
int i;
int j;
int spot_x;
int spot_y;
int color;
spot_x = WINDOWSIZE_X / 2 - game->player_animation.current_frame->img.width / 2;
spot_y = WINDOWSIZE_Y - game->player_animation.current_frame->img.height;
i = 0;
while (i < game->player_animation.current_frame->img.height)
{
j = 0;
while (j < game->player_animation.current_frame->img.width)
{
color = mypixelget(&game->player_animation.current_frame->img, j, i);
if ((color & 0x00FFFFFF) != 0)
mypixelput(&game->imgbuffer, spot_x + j, spot_y + i, color);
j++;
}
i++;
}
}