00001
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef _ALLEGRO5_
00014 #define _ALLEGRO5_
00015 #include <allegro5/allegro5.h>
00016 #endif // _ALLEGRO5_
00017
00018 #ifndef _ALLEGRO5FONT_
00019 #define _ALLEGRO5FONT_
00020 #include <allegro5/allegro_font.h>
00021 #endif // _ALLEGRO5FONT_
00022
00023 #ifndef _ALLEGRO5TTF_
00024 #define _ALLEGRO5TTF_
00025 #include <allegro5/allegro_ttf.h>
00026 #endif // _ALLEGRO5TTF_
00027
00028 #ifndef _ALLEGRO5IMAGE_
00029 #define _ALLEGRO5IMAGE_
00030 #include <allegro5/allegro_image.h>
00031 #endif // _ALLEGRO5IMAGE_
00032
00033 #ifndef _STDIO_
00034 #define _STDIO_
00035 #include <stdio.h>
00036 #endif // _STDIO_
00037
00038 #ifndef _COMMONS_
00039 #define _COMMONS_
00040 #include "../include/commons.h"
00041 #endif // _COMMONS_
00042
00043 #ifndef _BUTTONS_
00044 #define _BUTTONS_
00045 #include "../include/buttons.h"
00046 #endif // _BUTTONS_
00047
00048 #ifndef _RENDER_
00049 #define _RENDER_
00050 #include "../include/render.h"
00051 #endif // _RENDER_
00052
00053 #ifndef _HIGHSCORES_
00054 #define _HIGHSCORES_
00055 #include "../include/highscores.h"
00056 #endif // _HIGHSCORES_
00057
00058
00059
00060 static BUTTONPTR back = NULL, clear = NULL;
00061 static char* scorefile = NULL, *scores = NULL;
00062
00063
00064
00065 void highscores(void)
00066 {
00067 GDATAPTR game = get_game_data();
00068 if(back == NULL)
00069 {
00070 back = create_new_button("BACK",
00071 "DEFAULT",
00072 "BACK",
00073 game->b_font,
00074 BCOLOR(255, 255, 255, 255),
00075 BCOLOR(65, 105, 255, 191),
00076 -280,
00077 500,
00078 50,
00079 BRIGHT,
00080 BVISIBLE,
00081 true
00082 );
00083 }
00084 if(clear == NULL)
00085 {
00086 clear = create_new_button("CLEAR",
00087 "DEFAULT",
00088 "CLEAR",
00089 game->b_font,
00090 BCOLOR(255, 255, 255, 255),
00091 BCOLOR(65, 105, 255, 191),
00092 805,
00093 500,
00094 475,
00095 BLEFT,
00096 BVISIBLE,
00097 true
00098 );
00099 }
00100
00101 if(scorefile == NULL || scores == NULL)
00102 {
00103 load_file();
00104 }
00105 add_element_to_render_queue(NULL, 50, 0, 0, RCOLOR(0, 0, 0, 255), print_scores);
00106 }
00107
00108
00109
00110 void print_scores(ALLEGRO_BITMAP *image, float x, float y, int nflags, ALLEGRO_COLOR color)
00111 {
00112 GDATAPTR game = get_game_data();
00113 char *buf, *temp = (char*)malloc(sizeof(char)*strlen(scorefile));
00114 int i = 0;
00115 int xx = x, yy = y;
00116 al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, color);
00117 al_draw_textf(game->font, x, y+40, nflags, "Name");
00118 al_draw_textf(game->font, x+250, y+40, nflags, "Score");
00119 al_draw_textf(game->font, x+500, y+40, nflags, "Survival Time");
00120 strcpy(temp, scorefile);
00121 buf = strtok(temp, " ^");
00122 yy += 40;
00123 while(buf != NULL && i < 30)
00124 {
00125 if(i % 3 == 0)
00126 {
00127 yy += 40;
00128 xx = x;
00129 }
00130 al_draw_textf(game->font, xx, yy, nflags, "%s", buf);
00131 buf = strtok(NULL, " ^");
00132 xx += 250;
00133 i++;
00134 }
00135 free(temp);
00136 al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, RDEFAULT);
00137 }
00138
00139
00140
00141 void load_file(void)
00142 {
00143 FILE *file = NULL;
00144 if((file = fopen("highscores.database", "r")) == NULL)
00145 {
00146 create_blank_file();
00147 file = fopen("highscores.database", "r");
00148 }
00149 fseek(file , 0 , SEEK_END);
00150 long size = ftell(file);
00151 rewind(file);
00152 scorefile = (char*)malloc(size*sizeof(char));
00153 fread(scorefile, 1, size, file);
00154 fclose(file);
00155 file = NULL;
00156 if((file = fopen("scores.database", "r")) == NULL)
00157 {
00158 create_blank_file();
00159 file = fopen("scores.database", "r");
00160 }
00161 fseek(file , 0 , SEEK_END);
00162 size = ftell(file);
00163 rewind(file);
00164 scores = (char*)malloc(size*sizeof(char));
00165 fread(scores, 1, size, file);
00166 fclose(file);
00167 }
00168
00169
00170
00171 void clear_highscores(void)
00172 {
00173 remove("highscores.database");
00174 free(scorefile);
00175 scorefile = NULL;
00176 load_file();
00177 }
00178
00179
00180
00181 void add_score(int score, char *survival_time)
00182 {
00183 int *array = get_scores();
00184 int pos = get_score_pos(array, score);
00185
00186 if(pos <= 10)
00187 {
00188 char *p = strchr(scores, '^');
00189 char *q = scores;
00190 int i = 1;
00191 while(i <= 10)
00192 {
00193 if(i == pos)
00194 {
00195 int diff = (int)(p-q);
00196 break;
00197 }
00198 q = p;
00199 p = strchr(p+1, '^');
00200 i++;
00201 }
00202 }
00203 }
00204
00205
00206
00207 int get_score_pos(int *array, int score)
00208 {
00209 int i = 0;
00210 for(i = 0; i < 10; i++)
00211 {
00212 if(score > array[i])
00213 break;
00214 }
00215 return i+1;
00216 }
00217
00218
00219
00220 int* get_scores(void)
00221 {
00222 int *array = (int*)malloc(sizeof(int)*10);
00223 char *buf, *temp = (char*)malloc(sizeof(char)*strlen(scores));
00224 int i = 0;
00225 strcpy(temp, scores);
00226 buf = strtok(temp, "^");
00227 while(buf != NULL && i < 10)
00228 {
00229 array[i] = atoi(buf);
00230 buf = strtok(NULL, "^");
00231 i++;
00232 }
00233 free(temp);
00234 return array;
00235 }
00236
00237
00238
00239 void create_blank_file(void)
00240 {
00241 FILE *file = fopen("highscores.database", "w");
00242 for(int i = 0; i < 10; i++)
00243 {
00244 if(i == 9)
00245 fprintf(file, "%d.Player%d ---- ----^", i, i);
00246 else
00247 fprintf(file, "%d.Player%d ---- ----^", i, i);
00248 }
00249 fclose(file);
00250 file = fopen("scores.database", "w");
00251 for(int i = 0; i < 10; i++)
00252 {
00253 fprintf(file, "0^");
00254 }
00255 fclose(file);
00256 }
00257
00258
00259
00260 void highscores_cleanup(void)
00261 {
00262 remove_element_from_button_list(&back);
00263 back = NULL;
00264 remove_element_from_button_list(&clear);
00265 clear = NULL;
00266 free(scorefile);
00267 scorefile = NULL;
00268 free(scores);
00269 scores = NULL;
00270 }
00271
00272
00273
00274 void exit_highscores(GDATAPTR game)
00275 {
00276 BUTTONPTR currentptr, firstptr = get_first_element_from_button_list();
00277 currentptr = search_button_list_for_element(&firstptr, "BACK");
00278 currentptr->bdata.limit = -280;
00279 currentptr->bdata.aflags = BLEFT;
00280 currentptr = search_button_list_for_element(&firstptr, "CLEAR");
00281 currentptr->bdata.limit = 805;
00282 currentptr->bdata.aflags = BRIGHT;
00283 }
00284
00285
00286
00287
00288
00289