-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHighscore.c
103 lines (90 loc) · 2.27 KB
/
Highscore.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
#include "PacSnake.h"
#include "Rendering.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <SDL2/SDL.h>
extern bool saved;
void addHS(struct highscore *newHs)
{
if (hs == NULL)
hs = newHs;
else {
if (hs->score < newHs->score)
{
newHs->next = hs;
hs = newHs;
} else {
struct highscore* current = hs;
while (current->next != NULL && current->next->score > newHs->score)
current = current->next;
newHs->next = current->next;
current->next = newHs;
}
}
}
void loadHS()
{
struct highscore *toDelete = hs;
while (toDelete != NULL)
{
hs = toDelete->next;
free(toDelete);
toDelete = hs;
}
char leaderBoard[4096];
FILE *fptr;
if ((fptr = fopen("history.db", "r")) == NULL){
return;
}
char name[256];
int score = 0;
while (fscanf(fptr, " %[0-9a-zA-zäöüÄÖÜ !?]|%d;", name, &score) != EOF)
{
struct highscore *newHS = malloc(sizeof(struct highscore));
strcpy(newHS->name, name);
newHS->score = score;
newHS->next = NULL;
addHS(newHS);
}
fclose(fptr);
}
void drawHS()
{
darkenBackground(true);
renderText("Game History", 0, fieldHeight, white, 2, width / 10);
struct highscore *current = hs;
int i = 0;
while(current != NULL && i < 10)
{
renderText(current->name, 0, (height / 4) + (i * (height / 16)), white, 3, width / 30);
char scoreString[10];
sprintf(scoreString, "%d", current->score);
renderText(scoreString, 0, (height / 4) + (i * (height / 16)), white, 4, width / 30);
current = current->next;
i++;
}
}
void writeHS(struct GameState *state)
{
loadHS();
struct highscore *newHs = malloc(sizeof(struct highscore));
newHs->score = state->maxScore;
strcpy(newHs->name, name);
newHs->next = NULL;
addHS(newHs);
FILE *f = fopen("history.db", "w");
if (f == NULL)
{
return;
}
struct highscore *current = hs;
while(current != NULL)
{
fprintf(f, "%s|%d;", current->name, current->score);
current = current->next;
}
fclose(f);
saved = true;
}