root / proj / src / scoreboards.c @ 348
History | View | Annotate | Download (2.72 KB)
1 |
#include <lcom/lcf.h> |
---|---|
2 |
|
3 |
#include "scoreboards.h" |
4 |
|
5 |
#include "graph.h" |
6 |
#include "rectangle.h" |
7 |
#include "proj_func.h" |
8 |
#include "proj_macros.h" |
9 |
#include "rtc.h" |
10 |
#include "errors.h" |
11 |
|
12 |
#include <stdio.h> |
13 |
|
14 |
#define MAX_HIGHSCORES 3 /** @brief Number of scores saved */ |
15 |
|
16 |
struct score_info {
|
17 |
int day;
|
18 |
int month;
|
19 |
int year;
|
20 |
int hour;
|
21 |
int min;
|
22 |
int sec;
|
23 |
int score;
|
24 |
int time_played;
|
25 |
}; |
26 |
|
27 |
struct highscores {
|
28 |
score_info_t *highscores[MAX_HIGHSCORES]; |
29 |
|
30 |
const font_t *fnt;
|
31 |
rectangle_t *r[(MAX_HIGHSCORES+1)*3 + 1]; |
32 |
text_t *t[(MAX_HIGHSCORES+1)*3 + 1]; |
33 |
rectangle_t *frame; |
34 |
}; |
35 |
|
36 |
score_info_t* (score_ctor)(int score, int time_played){ |
37 |
score_info_t *ret = (score_info_t*)malloc(sizeof(score_info_t));
|
38 |
if (ret == NULL) return NULL; |
39 |
uint8_t score_date[4];
|
40 |
if (rtc_read_date(score_date)) return NULL; |
41 |
ret->sec = score_date[0];
|
42 |
ret->min = score_date[1];
|
43 |
ret->hour = score_date[2];
|
44 |
|
45 |
if (rtc_read_time(score_date)) return NULL; |
46 |
|
47 |
ret->day = score_date[1];
|
48 |
ret->month = score_date[2];
|
49 |
ret->year = score_date[3];
|
50 |
|
51 |
ret->score = score; |
52 |
ret->time_played = time_played; |
53 |
|
54 |
return ret;
|
55 |
} |
56 |
|
57 |
void (score_dtor)(score_info_t *p){
|
58 |
if (p != NULL) free(p); |
59 |
} |
60 |
|
61 |
highscores_t* (highscores_ctor)(const font_t *fnt, const char *path) { |
62 |
if(fnt == NULL || path == NULL) return NULL; |
63 |
|
64 |
FILE *f = fopen(path, "r");
|
65 |
if (f == NULL) return NULL; |
66 |
highscores_t *ret = (highscores_t*)malloc(sizeof(highscores_t));
|
67 |
if (ret == NULL) return NULL; |
68 |
char line[30]; |
69 |
for (int i = 0; i < MAX_HIGHSCORES; i++) { |
70 |
fgets(line, 30, f);
|
71 |
score_info_t *score = (score_info_t*)malloc(sizeof(score_info_t));
|
72 |
|
73 |
sscanf(line, "%d/%d/%d %d:%d:%d - %d - %d\n",
|
74 |
&(score->day), &(score->month), &(score->year), |
75 |
&(score->hour), &(score->month), &(score->sec), |
76 |
&(score->score), &(score->time_played)); |
77 |
ret->highscores[i] = score; |
78 |
} |
79 |
fclose(f); |
80 |
|
81 |
return ret;
|
82 |
} |
83 |
|
84 |
void (highscores_save)(const highscores_t *p, const char *path) { |
85 |
if(p == NULL || path == NULL) return; |
86 |
|
87 |
FILE *f = fopen(path, "w");
|
88 |
if (f == NULL) return; |
89 |
for (int i = 0; i < MAX_HIGHSCORES; i++) { |
90 |
score_info_t *score = p->highscores[i]; |
91 |
fprintf(f, "%d/%d/%d %d:%d:%d - %d - %d\n",
|
92 |
(score->day), (score->month), (score->year), |
93 |
(score->hour), (score->month), (score->sec), |
94 |
(score->score), (score->time_played)); |
95 |
} |
96 |
fclose(f); |
97 |
} |
98 |
|
99 |
void (highscores_dtor)(highscores_t *p) {
|
100 |
for (int i = 0; i < MAX_HIGHSCORES; i++) { |
101 |
score_dtor(p->highscores[i]); |
102 |
} |
103 |
} |