try to fix opengl warnings
[supertux.git] / src / high_scores.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Adam Czachorowski <gislan@o2.pl>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20
21 /* Open the highscore file: */
22
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include "globals.h"
27 #include "high_scores.h"
28 #include "menu.h"
29 #include "screen/drawing_context.h"
30 #include "screen/screen.h"
31 #include "screen/texture.h"
32 #include "setup.h"
33 #include "lispreader.h"
34
35 #ifdef WIN32
36 const char * highscore_filename = "/st_highscore.dat";
37 #else
38 const char * highscore_filename = "/highscore";
39 #endif
40
41 int hs_score;
42 std::string hs_name; /* highscores global variables*/
43
44 /* Load data from high score file: */
45
46 void load_hs(void)
47 {
48   hs_score = 100;
49   hs_name  = "Grandma";
50
51   FILE * fi;
52   lisp_object_t* root_obj = 0;
53   fi = fopen(highscore_filename, "r");
54   if (fi == NULL)
55     {
56       perror(highscore_filename);
57       return;
58     }
59
60   lisp_stream_t stream;
61   lisp_stream_init_file (&stream, fi);
62   root_obj = lisp_read (&stream);
63
64   if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
65     {
66       printf("HighScore: Parse Error in file %s", highscore_filename);
67     }
68
69
70   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-highscore") == 0)
71     {
72       LispReader reader(lisp_cdr(root_obj));
73       reader.read_int("score",  &hs_score);
74       reader.read_string("name", &hs_name);
75     }
76  
77   fclose(fi);
78   lisp_free(root_obj);
79 }
80
81 void save_hs(int score)
82 {
83   char str[80];
84
85   Surface* bkgd;
86   SDL_Event event;
87
88   DrawingContext context;
89   bkgd = new Surface(datadir + "/images/highscore/highscore.png", IGNORE_ALPHA);
90
91   hs_score = score;
92
93   Menu::set_current(highscore_menu);
94
95   if(!highscore_menu->item[0].input)
96     highscore_menu->item[0].input = (char*) malloc(strlen(hs_name.c_str()) + 1);
97
98   strcpy(highscore_menu->item[0].input,hs_name.c_str());
99
100   /* ask for player's name */
101   while(Menu::current())
102     {
103       context.draw_surface(bkgd, Vector(0, 0), LAYER_BACKGROUND0);
104
105       context.draw_text_center(blue_text, "Congratulations", 
106           Vector(0, 130), LAYER_FOREGROUND1);
107       context.draw_text(blue_text, "Your score:", Vector(150, 180),
108           LAYER_FOREGROUND1);
109       sprintf(str, "%d", hs_score);
110       context.draw_text(yellow_nums, str, Vector(250, 170), LAYER_FOREGROUND1);
111
112       Menu::current()->draw(context);
113       Menu::current()->action();
114
115       context.do_drawing();
116
117       while(SDL_PollEvent(&event))
118         if(event.type == SDL_KEYDOWN)
119           Menu::current()->event(event);
120
121       switch (highscore_menu->check())
122         {
123         case 0:
124           if(highscore_menu->item[0].input != NULL)
125             hs_name = highscore_menu->item[0].input;
126           break;
127         }
128
129       SDL_Delay(25);
130     }
131
132
133   /* Save to file: */
134
135   FILE* fi;
136   std::string filename;
137
138   /* Save data file: */
139   filename = highscore_filename;
140
141   fcreatedir(filename.c_str());
142   if(fwriteable(filename.c_str()))
143     {
144       fi = fopen(filename.c_str(), "w");
145       if (fi == NULL)
146         {
147           perror(filename.c_str());
148         }
149
150       /* Write header: */
151       fprintf(fi,";SuperTux HighScores\n");
152       fprintf(fi,"(supertux-highscore\n");
153
154       /* Save title info: */
155       fprintf(fi,"  (name \"%s\")\n", hs_name.c_str());
156
157       /* Save the description: */
158       fprintf(fi,"  (score \"%i\")\n", hs_score);
159
160       fprintf( fi,")");
161       fclose(fi);
162     }
163 }