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
|
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "preferences.h"
#include "HiScores.h"
static hiscore HS[kHiScoresNumber];
static bool loaded = false;
void initHiScores(const char * const defaultNames[kHiScoresNumber])
{
char HSID[8];
for (int i=0; i<kHiScoresNumber; i++)
{
sprintf(HSID,"HSN%2d",i);
GetStrPreference(HSID,HS[i].name,defaultNames[kHiScoresNumber-i-1],kHiScoreNameLenght+1);
sprintf(HSID,"HSS%2d",i);
HS[i].score = GetIntPreference(HSID,(kHiScoresNumber-i) * 10000);
}
loaded = true;
}
hiscore * getHiScores(void)
{
if (loaded!=true)
{
fprintf(stderr,"getHiscores() called before init, app may crash...");
return NULL;
}
else return HS;
}
int setHiScore(int score, const char * name)
{
int retour = -1;
if (loaded!=true)
{
fprintf(stderr,"setHiscores() called before init, app may crash...");
return retour;
}
hiscore * tmp = (hiscore*)malloc(sizeof(hiscore));
hiscore * tmp2= (hiscore*)malloc(sizeof(hiscore));
if ((tmp==NULL) || (tmp2 == NULL))
{
fprintf(stderr,"Malloc failed, I won't save your score...");
return retour;
}
strncpy(tmp->name,name,kHiScoreNameLenght);
tmp->name[kHiScoreNameLenght]=0;
tmp->score = score;
char HSID[8];
for (int i=0; i<kHiScoresNumber; i++)
{
if (tmp->score >= HS[i].score)
{
if (retour == -1) retour = i;
sprintf(HSID,"HSN%2d",i);
SetStrPreference(HSID,tmp->name);
sprintf(HSID,"HSS%2d",i);
SetIntPreference(HSID,tmp->score);
memcpy(tmp2,&(HS[i]),sizeof(hiscore));
memcpy(&(HS[i]),tmp,sizeof(hiscore));
memcpy(tmp,tmp2,sizeof(hiscore));
}
}
free(tmp);
free(tmp2);
return retour;
}
|