tb1/tb1_linux/hiscore.c

116 lines
3.3 KiB
C
Raw Normal View History

2000-09-16 23:23:00 +00:00
#include <stdio.h>
#include "svmwgraph/svmwgraph.h"
#include "tb1_state.h"
2000-09-17 05:47:00 +00:00
#include "graphic_tools.h"
#include "tblib.h"
2000-09-16 23:23:00 +00:00
#include <unistd.h> /* For usleep */
2000-09-17 05:47:00 +00:00
#include <string.h> /* For strncpy */
/* Yes these routines are simplistic, but they seemed amazingly */
/* clever when I came up with them 6 years ago. */
2000-09-16 23:23:00 +00:00
2000-09-17 05:47:00 +00:00
int showhiscore(tb1_state *game_state,int showchart,int return_highest)
2000-09-16 23:23:00 +00:00
{
FILE *hilist;
char tempstr[10];
int i;
char names[10][25];
int scores[10];
2000-09-17 05:47:00 +00:00
hilist=fopen(tb1_data_file("hiscore.tb1",game_state->path_to_data),"r");
2000-09-16 23:23:00 +00:00
if (hilist==NULL) {
printf("Error! can't open high score file!\n");
return 0;
}
2000-10-28 02:09:00 +00:00
for(i=0;i<10;i++) {
fgets(names[i],11,hilist);
/* There has to be a cleaner way of getting rid of that '\n' */
names[i][strlen(names[i])-1]='\0';
}
2000-09-16 23:23:00 +00:00
for (i=0;i<10;i++) fscanf(hilist,"%i",&scores[i]);
fclose(hilist);
2000-09-17 05:47:00 +00:00
if (!showchart) {
if (return_highest) return scores[0];
else return scores[9];
}
2000-09-16 23:23:00 +00:00
if (showchart) {
vmwDrawBox(45,40,240,120,7,game_state->virtual_1);
coolbox(45,40,285,160,1,game_state->virtual_1);
for(i=0;i<10;i++)
vmwTextXY(names[i],51,46+(i*10),9,7,1,game_state->graph_state->default_font,game_state->virtual_1);
for(i=0;i<10;i++){
sprintf(tempstr,"%i",scores[i]);
vmwTextXY(tempstr,181,46+(i*10),9,7,1,game_state->graph_state->default_font,game_state->virtual_1);
}
2000-09-17 05:47:00 +00:00
vmwBlitMemToDisplay(game_state->graph_state,game_state->virtual_1);
2000-09-16 23:23:00 +00:00
while(vmwGetInput()==0) usleep(100);
}
return 0;
}
2000-09-17 05:47:00 +00:00
typedef struct {
char name[12];
int score;
} vmwHighScore;
void write_hs_list(tb1_state *game_state,int score,char *hiname,int wipe)
2000-09-16 23:23:00 +00:00
{
2000-09-17 05:47:00 +00:00
int i,place,in_place;
2000-09-16 23:23:00 +00:00
FILE *hilist;
2000-10-28 02:09:00 +00:00
vmwHighScore old_list[12];
vmwHighScore new_list[12];
2000-09-16 23:23:00 +00:00
2000-09-17 05:47:00 +00:00
hilist=fopen(tb1_data_file("hiscore.tb1",game_state->path_to_data),"r");
2000-10-28 02:09:00 +00:00
for(i=0;i<10;i++) {
fgets(old_list[i].name,12,hilist);
old_list[i].name[strlen(old_list[i].name)-1]='\0';
}
2000-09-17 05:47:00 +00:00
for (i=0;i<10;i++) fscanf(hilist,"%i",&old_list[i].score);
2000-09-16 23:23:00 +00:00
fclose(hilist);
place=0;
2000-09-17 05:47:00 +00:00
i=0;
in_place=0;
while (i<10) {
if ((score>old_list[place].score) && (in_place==0)) {
strncpy(new_list[i].name,hiname,10);
new_list[i].score=score;
i++;
in_place=1;
}
if (i<10) {
strncpy(new_list[i].name,old_list[place].name,10);
new_list[i].score=old_list[place].score;
}
place++;
i++;
2000-09-16 23:23:00 +00:00
}
2000-09-17 05:47:00 +00:00
// if (!read_only_mode) {
if ((hilist=fopen(tb1_data_file("hiscore.tb1",
game_state->path_to_data),"w"))==NULL) {
printf("Error opening high score file!\n");
return;
}
if (wipe) {
2004-09-29 04:52:00 +00:00
fprintf(hilist,"Vince\nChipper\nHairold\nLeonard\n"
"Susie\nJim\nPete\nBrigid\nLizann\nMarie\n"
2000-09-17 05:47:00 +00:00
"5000\n4500\n4000\n3500\n3000\n"
"2500\n2000\n1500\n1000\n500\n");
}
else {
for(i=0;i<10;i++) fprintf(hilist,"%s\n",new_list[i].name);
for(i=0;i<10;i++) fprintf(hilist,"%i\n",new_list[i].score);
}
fclose(hilist);
// }
2000-09-16 23:23:00 +00:00
}