ketm (0.0.6-22) 020_save_hiscores.diff

Summary

 hiscore.c |   42 ++++++++++++++++++++++++++++++++++++++++++
 main.c    |    4 ++++
 2 files changed, 46 insertions(+)

    
download this patch

Patch contents

Index: ketm-0.0.6/main.c
===================================================================
--- ketm-0.0.6.orig/main.c	2001-09-06 20:27:02.000000000 +0200
+++ ketm-0.0.6/main.c	2006-05-15 20:01:42.000000000 +0200
@@ -1,3 +1,4 @@
+#include <unistd.h>
 #include "main.h"
 
 extern SDL_Surface *screen;
@@ -14,6 +15,7 @@
 int main(int argc, char *argv[])
 {
 
+	chdir(DATA);
 	game_init(argc, argv);
 	while (state.mainstate!=ST_GAME_QUIT) {
 		keyboard_poll();
@@ -75,6 +77,8 @@
 		fps_newframe();
 	}
 
+
+	hsc_save(); // save hiscore
 	/* TODO: Free everything (memory, SDL_Surfaces, Joysticks...) */
 
 	fprintf(stdout,"Thank you for playing\n");
Index: ketm-0.0.6/hiscore.c
===================================================================
--- ketm-0.0.6.orig/hiscore.c	2006-05-15 20:01:32.000000000 +0200
+++ ketm-0.0.6/hiscore.c	2006-05-15 20:01:42.000000000 +0200
@@ -16,9 +16,51 @@
 extern double fps_factor;
 
 
+int hsc_load()
+{
+  FILE *fp;
+  int i;
+#ifndef HISCORE_FILE
+  return 0;
+#else
+  fp = fopen(HISCORE_FILE,"r");
+  if(fp==NULL)
+    return 0;
+
+  for(i=0;i<10;i++){ 
+      if(fscanf(fp,"%3s %d\n",hsc_table[i].name,&hsc_table[i].score)==EOF){
+	  return 0;
+	}
+    }
+  return -1;
+#endif
+}
+
+void hsc_save()
+{
+  FILE *fp;
+  int i;
+#ifndef HISCORE_FILE
+  return;
+#else
+  fp = fopen(HISCORE_FILE,"w");
+  if(fp==NULL)
+    {
+      printf("Couldn`t open hiscore file. Hiscores couldn't be saved!\n");
+      return;
+    }
+
+  for(i=0;i<10;i++) 
+    fprintf(fp,"%s %d\n",hsc_table[i].name,hsc_table[i].score);
+#endif
+}
+
 void hsc_init()
 {
 	int i;
+	if(hsc_load()!=0)
+	  return;
+	printf("Couldn't find hiscore file. Using default values\n");
 	for(i=0;i<10;i++) {
 		strcpy(hsc_table[i].name,"W.H");
 		hsc_table[i].score=(10-i)*1000;