-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhighscore.c
241 lines (203 loc) · 5.5 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* highscore.c
* Hedgewood
*
* Prozedurale Programmierung WS10/11 TUHH
*
* Created by Tobias Conradi
* Version 11.01.11
*
* Copyright 2011 Gruppe 34. All rights reserved.
*
* Description:
*
*
*/
#include "highscore.h"
// "privat" functions
/**
* checks if the player is in highscore
* @param data the dataStore
* @param the points the player achieved
* @return 1 if the player is in highscore
* @return 0 if the player is not in highscore
*/
int inHighscore(dataStore *data, int points);
/**
* Sorts the Highscore
* @param data the dataStore
* @return 0
*/
int sortHighscore(dataStore *data);
//implementation
int addHighscore(SDL_Surface *screen ,dataStore *data, int points)
{
if (inHighscore(data, points)) {
char *playerName = malloc(sizeof(char)*15);
inputPopUp(screen,data,"Highscore! Please enter your Name:", playerName, 15, "Ok", NULL);
strcpy(data->highscore[9].name,playerName);
data->highscore[9].points = points;
sortHighscore(data);
saveDataStore(data, 1, 0);
displayHighscore(screen,data);
return 1;
}
return 0;
}
int sortHighscore(dataStore *data)
{
highscoreElement *highscore=data->highscore;
highscoreElement temp;
int i,n,changed,steps=0;
for (i=0; i<10; i++) {
changed=0;
for (n=1; n<10-i; n++) {
if (highscore[n-1].points<highscore[n].points) {
memcpy(&temp,&highscore[n-1],sizeof(highscoreElement));
memcpy(&highscore[n-1],&highscore[n],sizeof(highscoreElement));
memcpy(&highscore[n],&temp,sizeof(highscoreElement));
changed++;
}
steps++;
}
if (changed==0) {
break;
}
}
return 0;
}
int inHighscore(dataStore *data, int points)
{
sortHighscore(data);
if (points>data->highscore[9].points)
return 1;
else
return 0;
}
int displayHighscore(SDL_Surface *screen, dataStore *data)
{
int done, mouseX, mouseY;
SDL_Event event;
SDL_Surface *message;
highscoreElement *highscore=data->highscore;
TTF_Font *aButtonFont = theFont(32);
SDL_Color textColor = { 255, 255, 255,0};
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x00, 0x00, 0x00 ));
if (!(message = TTF_RenderText_Blended( aButtonFont, "Highscore", textColor )))
printf("%s\n",TTF_GetError());
SDL_Rect offset;
offset.x = screen->clip_rect.w/2-message->w/2;
offset.y = 20;
//Blit
SDL_BlitSurface( message, NULL, screen, &offset );
SDL_FreeSurface(message);
TTF_CloseFont(aButtonFont);
SDL_Rect rankRect={screen->clip_rect.w/2-200,screen->clip_rect.h/2-230,0,0};
SDL_Rect nameRect={screen->clip_rect.w/2-150,screen->clip_rect.h/2-230,0,0};
SDL_Rect pointsRect={screen->clip_rect.w/2+250,screen->clip_rect.h/2-230,0,0};
int pointRight=pointsRect.x;
TTF_Font *midFont = theFont(28);
int i;
int lineOffset = 40;
char tmp[15];
for (i=0;i<10 ; i++) {
/*Rank*/
sprintf(tmp, "%d",i+1);
if (!(message = TTF_RenderText_Blended( midFont,tmp, textColor )))
printf("%s\n",TTF_GetError());
SDL_BlitSurface( message, NULL, screen, &rankRect);
SDL_FreeSurface(message);
rankRect.y+=lineOffset;
/*Name*/
if (!(message = TTF_RenderText_Blended( midFont,highscore[i].name, textColor )))
printf("%s\n",TTF_GetError());
SDL_BlitSurface( message, NULL, screen, &nameRect);
SDL_FreeSurface(message);
nameRect.y+=lineOffset;
/*Points*/
sprintf(tmp, "%d",highscore[i].points);
if (!(message = TTF_RenderText_Blended( midFont,tmp, textColor )))
printf("%s\n",TTF_GetError());
pointsRect.x=pointRight-message->w;
SDL_BlitSurface( message, NULL, screen, &pointsRect);
SDL_FreeSurface(message);
pointsRect.y+=lineOffset;
}
TTF_CloseFont(midFont);
myButton button;
button.rect.x = screen->clip_rect.w/2-BUTTONWIDTH/2;
button.rect.y = screen->clip_rect.h-50-BUTTONHEIGHT;
button.rect.w = BUTTONWIDTH;
button.rect.h = BUTTONHEIGHT;
button.name="Back";
drawButton(screen, &button);
SDL_Flip(screen);
unsigned int startTime, stopTime, diffTime;
unsigned int innerStartTime, innerStopTime;
done = 0;
while ( !done ) {
startTime = SDL_GetTicks();
/* Check for events */
while ( SDL_PollEvent(&event) ) {
innerStartTime = SDL_GetTicks();
switch (event.type) {
case SDL_MOUSEMOTION:
break;
case SDL_MOUSEBUTTONUP:
SDL_GetMouseState(&mouseX,&mouseY);
if (isButtonClicked(&button, mouseX, mouseY)) {
done = 1;
}
#if (DEBUG==1)
printf("Cusor-Position x: %d y: %d\n",mouseX,mouseY);
#endif
break;
case SDL_KEYDOWN:
/* Any keypress quits the app... */
switch( event.key.keysym.sym )
{
case SDLK_f:
break;
case SDLK_0:
printf ("Music off\n");
Mix_HaltMusic();
Mix_HaltChannel(-1);
data->soundEnabled=0;
break;
case SDLK_m:
printf ("Music on /Pause\n");
if( Mix_PlayingMusic() == 0 )
Mix_PlayMusic( data->ingamemusic, -1);
if( Mix_PausedMusic() == 1 )
Mix_ResumeMusic();
else Mix_PauseMusic();
break;
case SDLK_ESCAPE:
case SDLK_q:
done = 1;
break;
default:
break;
}
break;
case SDL_QUIT:
done = 1;
quitSDL(data);
break;
default:
break;
}
innerStopTime = SDL_GetTicks();
diffTime=(innerStopTime-innerStartTime);
//25 Frames per second (40 Milliseconds per frame)
if (MS_FRAMETIME>diffTime)
SDL_Delay(MS_FRAMETIME-diffTime);
}
stopTime = SDL_GetTicks();
diffTime = (stopTime-startTime);
//25 Frames per second (40 Milliseconds per frame)
if (MS_FRAMETIME>diffTime)
SDL_Delay(MS_FRAMETIME-diffTime);
}
return 0;
}