-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.c
More file actions
188 lines (176 loc) · 4.86 KB
/
timer.c
File metadata and controls
188 lines (176 loc) · 4.86 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "core.h"
/**
* @file time.c
* @brief Testing Program.
* @author C lahmar
* @version 1
* @date Apr 20, 2022
*
* Testing program for minimap
*
*/
/**
* @brief compteur qui calcule le temps du debut de joueur par le ms.
* @param tempsdebut debut de temps
* @return Nothing
*/
void Timer(int *tempsdebut)
{
if (SDL_GetTicks() - *tempsdebut >= 1000)
{
*tempsdebut = SDL_GetTicks();
}
}
/**
* @brief initialisation du temps .
* @param t temps du jeu
* @return Nothing
*/
void initGameTimer(GameTimer *t)
{
int test;
t->startTime = SDL_GetTicks();
t->minutes = 0;
t->seconds = 0;
test = initTimerDisplay(&t->display);
}
/**
* @brief initialisation du temps de l'enigme.
* @param t temps du jeu
* @return Nothing
*/
void initPuzzleTimer(GameTimer *t)
{
int test;
t->startTime = 16000;
t->minutes = 0;
t->seconds = 0;
test = initPuzzleDisplay(&t->display);
}
/**
* @brief initialisation du texte du temps avec un couleur blanc et une position dans le screen x=850 et y=20.
* @param T text du temps
* @return Nothing
*/
int initTimerDisplay(TextDisplay *T)
{
int testload;
T->textColor.r = 255;
T->textColor.g = 255;
T->textColor.b = 255;
strcpy(T->textSurface, "");
T->textPosition.x = 850;
T->textPosition.y = 20;
testload = loadTimerFont(T, "assets/angelina.TTF");
T->textSurface = NULL;
return testload;
}
/**
* @brief initialisation du texte du temps de l'enigme.
* @param T text du temps de l'enigme
* @return Nothing
*/
int initPuzzleDisplay(TextDisplay *T)
{
int testload;
T->textColor.r = 255;
T->textColor.g = 255;
T->textColor.b = 255;
strcpy(T->textSurface, "");
T->textPosition.x = 0;
T->textPosition.y = 0;
testload = loadTimerFont(T, "assets/angelina.TTF");
T->textSurface = NULL;
return testload;
}
/**
* @brief charger le font du text du temps.
* @param T text du temps
* @param path le nom du font
* @return Nothing
*/
int loadTimerFont(TextDisplay *T, char *path)
{
/* TTF already initialized in main.c */
T->font = TTF_OpenFont(path, 60);
if (T->font == NULL)
{
printf("Unable to load Font: %s\n", SDL_GetError());
return (-1);
}
return (0);
}
/**
* @brief update le temps du jeu.
* @param T text du temps
* @return Nothing
*/
void updateGameTime(GameTimer *T)
{
int ts;
Timer(&T->startTime);
ts = T->startTime / 1000;
T->minutes = ts / 60;
T->seconds = ts % 60;
/* Free previous surface to avoid memory leak */
if (T->display.textSurface != NULL)
{
SDL_FreeSurface(T->display.textSurface);
}
if (T->minutes < 10 && T->seconds < 10)
sprintf(T->display.textSurface, "*** time :0%d:0%d ***", T->minutes, T->seconds);
else if (T->minutes < 10 && T->seconds >= 10)
sprintf(T->display.textSurface, "*** time :0%d:%d ***", T->minutes, T->seconds);
else if (T->minutes >= 10 && T->seconds < 10)
sprintf(T->display.textSurface, "*** time :%d:0%d ***", T->minutes, T->seconds);
else
sprintf(T->display.textSurface, "*** time :%d:%d ***", T->minutes, T->seconds);
T->display.textSurface = TTF_RenderText_Solid(T->display.font, T->display.textSurface, T->display.textColor);
}
/**
* @brief update le temps du l'enigme.
* @param T text du temps
* @return Nothing
*/
void updatePuzzleTime(GameTimer *T)
{
int ts;
Timer(&T->startTime);
ts = T->startTime / 1000;
T->minutes = ts / 60;
T->seconds = ts % 60;
/* Free previous surface to avoid memory leak */
if (T->display.textSurface != NULL)
{
SDL_FreeSurface(T->display.textSurface);
}
if (T->minutes < 10 && T->seconds < 10)
sprintf(T->display.textSurface, "*** time :0%d:0%d ***", T->minutes, T->seconds);
else if (T->minutes < 10 && T->seconds >= 10)
sprintf(T->display.textSurface, "*** time :0%d:%d ***", T->minutes, T->seconds);
else if (T->minutes >= 10 && T->seconds < 10)
sprintf(T->display.textSurface, "*** time :%d:0%d ***", T->minutes, T->seconds);
else
sprintf(T->display.textSurface, "*** time :%d:%d ***", T->minutes, T->seconds);
T->display.textSurface = TTF_RenderText_Solid(T->display.font, T->display.textSurface, T->display.textColor);
}
/**
* @brief afficher le text du temps.
* @param T text du temps
* @param screen l'ecran
* @return Nothing
*/
void renderTimer(GameTimer T, SDL_Surface *screen)
{
SDL_BlitSurface(T.display.textSurface, NULL, screen, &(T.display.textPosition));
}
/**
* @brief liberer le text du temsp.
* @param T text du temps
* @return Nothing
*/
void freeTimerDisplay(TextDisplay T)
{
TTF_CloseFont(T.font);
TTF_Quit();
}