-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhigh_scores_state.c
112 lines (100 loc) · 2.9 KB
/
high_scores_state.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
110
111
112
#include <SDL2/SDL.h>
#include "globals.h"
#include "game.h"
#include "high_scores_state.h"
#include "high_score.h"
#include "utils.h"
#include "fsm.h"
SDL_Texture *highScoresTitle;
SDL_Texture *highScoreTextures[MAX_HIGH_SCORES_NUMBER][2];
void _format_time(const int time, char timeFormatted[8]);
void high_scores_state_init(s_Game *game) {
int times[MAX_HIGH_SCORES_NUMBER];
int turns[MAX_HIGH_SCORES_NUMBER];
int nbRows, i;
SDL_Color white = {255, 255, 255, 255};
utils_createTextTexture(
game->renderer,
game->highScoreTitleFont,
"High Scores",
white,
&highScoresTitle
);
high_score_list(times, turns, &nbRows);
for (i = 0; i < MAX_HIGH_SCORES_NUMBER; ++i) {
char timeLabel[10], turnsLabel[10];
if (i < nbRows) {
_format_time(times[i], timeLabel);
snprintf(turnsLabel, 10, "%d turns", turns[i]);
}
else {
snprintf(timeLabel, 2, "-");
snprintf(turnsLabel, 9, "Turns: -");
}
utils_createTextTexture(
game->renderer,
game->highScoreFont,
timeLabel,
white,
&highScoreTextures[i][0]
);
utils_createTextTexture(
game->renderer,
game->highScoreFont,
turnsLabel,
white,
&highScoreTextures[i][1]
);
}
}
void _format_time(const int time, char timeFormatted[10]) {
unsigned int utime = (unsigned) time;
unsigned int min = (utime / 60000) % 60,
sec = (utime / 1000) % 60,
msec = utime % 1000;
snprintf(timeFormatted, 10, "%02d:%02d.%03d", min, sec, msec);
}
void high_scores_state_clean() {
int i;
SDL_DestroyTexture(highScoresTitle);
for (i = 0; i < MAX_HIGH_SCORES_NUMBER; ++i) {
SDL_DestroyTexture(highScoreTextures[i][0]);
SDL_DestroyTexture(highScoreTextures[i][1]);
}
}
void high_scores_state_render(s_Game* game) {
int i, textWidth, textHeight,
marginTitleX, marginTitleY,
marginTimeX, marginTurnsX, marginY;
if (IS_GCW) {
marginTitleX = 20;
marginTitleY = 10;
marginTimeX = 20;
marginTurnsX = 160;
marginY = 50;
}
else {
marginTitleX = 50;
marginTitleY = 30;
marginTimeX = 50;
marginTurnsX = 200;
marginY = 80;
}
SDL_QueryTexture(highScoresTitle, NULL, NULL, &textWidth, &textHeight);
SDL_Rect renderQuad = {marginTitleX, marginTitleY, textWidth, textHeight};
SDL_RenderCopy(game->renderer, highScoresTitle, NULL, &renderQuad);
for (i = 0; i < MAX_HIGH_SCORES_NUMBER; ++i) {
SDL_QueryTexture(highScoreTextures[i][0], NULL, NULL, &textWidth, &textHeight);
SDL_Rect renderQuadTime = {marginTimeX, marginY + 33 * i, textWidth, textHeight};
SDL_RenderCopy(game->renderer, highScoreTextures[i][0], NULL, &renderQuadTime);
SDL_QueryTexture(highScoreTextures[i][1], NULL, NULL, &textWidth, &textHeight);
SDL_Rect renderQuadTurns = {marginTurnsX, marginY + 33 * i, textWidth, textHeight};
SDL_RenderCopy(game->renderer, highScoreTextures[i][1], NULL, &renderQuadTurns);
}
}
void high_scores_state_handleEvent(s_Game* game, int key) {
// exit if ESCAPE is pressed
if (key == SDLK_ESCAPE) {
fsm_setState(game, mainmenu);
}
}