Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "highscores.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int aantal;
- Score_Entry* scores;
- int veranderd=0;
- void load_highscores()
- {
- FILE* file=fopen(HIGHSCORE_FILE,"r");
- if(file!=NULL){
- fseek(file,0,SEEK_END);
- aantal=ftell(file)/sizeof(Score_Entry);
- fseek(file,0,SEEK_SET);
- if(aantal<MAX_HIGHSCORE_ENTRIES){
- scores=(Score_Entry*)calloc(aantal+1,sizeof(Score_Entry));
- fread(scores,sizeof(Score_Entry),aantal+1,file);
- }
- else if(aantal==5){
- scores=(Score_Entry*)calloc(aantal,sizeof(Score_Entry));
- fread(scores,sizeof(Score_Entry),aantal,file);
- }
- fclose(file);
- }
- else{
- aantal=0;
- scores=(Score_Entry*)calloc(1,sizeof(Score_Entry));
- }
- }
- void check_highscore_entry(int score){
- if(aantal<MAX_HIGHSCORE_ENTRIES){
- aantal++;
- veranderd=1;
- }
- else if(score>scores[MAX_HIGHSCORE_ENTRIES-1].score){
- veranderd=1;
- }
- if(veranderd){
- int i=1;
- const time_t tijd=time(NULL);
- strftime(scores[aantal-1].datum,20,"%d/%m/%Y",localtime(&tijd));
- printf("Naam?\n");
- scanf("%s",scores[aantal-1].naam);
- scores[aantal-1].score=score;
- while(scores[aantal-i].score>scores[aantal-(i+1)].score&&(i+1<=aantal)){
- Score_Entry temp=scores[aantal-(i+1)];
- scores[aantal-(i+1)]=scores[aantal-i];
- scores[aantal-i]=temp;
- i++;
- }
- }
- }
- void display_highscores()
- {
- int i;
- for(i=0;i<aantal;i++){
- printf("%d, %s %s\n",scores[i].score,scores[i].naam,scores[i].datum);
- }
- }
- void save_highscores()
- {
- if(veranderd){
- FILE* file=fopen(HIGHSCORE_FILE,"w");
- fwrite(scores,sizeof(Score_Entry),aantal,file);
- veranderd=0;
- fclose(file);
- free(scores);
- }
- }
Add Comment
Please, Sign In to add comment