Made code using fireworks sound effect.
[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 <cstring>
24 #include <cstdlib>
25
26 #include "app/globals.h"
27 #include "high_scores.h"
28 #include "gui/menu.h"
29 #include "video/drawing_context.h"
30 #include "video/screen.h"
31 #include "video/surface.h"
32 #include "app/setup.h"
33 #include "utils/lispreader.h"
34 #include "resources.h"
35
36 using namespace SuperTux;
37
38 #ifdef WIN32
39 const char * highscore_filename = "/st_highscore.dat";
40 #else
41 const char * highscore_filename = "/highscore";
42 #endif
43
44 int hs_score;
45 std::string hs_name; /* highscores global variables*/
46
47 /* Load data from high score file: */
48
49 void load_hs(void)
50 {
51   hs_score = 100;
52   hs_name  = "Grandma";
53
54   FILE * fi;
55   lisp_object_t* root_obj = 0;
56   fi = fopen(highscore_filename, "r");
57   if (fi == NULL)
58     {
59       perror(highscore_filename);
60       return;
61     }
62
63   lisp_stream_t stream;
64   lisp_stream_init_file (&stream, fi);
65   root_obj = lisp_read (&stream);
66
67   if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
68     {
69       printf("HighScore: Parse Error in file %s", highscore_filename);
70     }
71
72
73   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-highscore") == 0)
74     {
75       LispReader reader(lisp_cdr(root_obj));
76       reader.read_int("score",  hs_score);
77       reader.read_string("name", hs_name);
78     }
79  
80   fclose(fi);
81   lisp_free(root_obj);
82 }
83
84 void save_hs(int score)
85 {
86   char str[80];
87
88   Surface* bkgd;
89   SDL_Event event;
90
91   DrawingContext context;
92   bkgd = new Surface(datadir + "/images/highscore/highscore.png", false);
93
94   hs_score = score;
95
96   Menu::set_current(highscore_menu);
97
98   highscore_menu->item[0].input = hs_name;
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           hs_name = highscore_menu->item[0].input;
125           break;
126         }
127
128       SDL_Delay(25);
129     }
130
131
132   /* Save to file: */
133
134   FILE* fi;
135   std::string filename;
136
137   /* Save data file: */
138   filename = highscore_filename;
139
140   FileSystem::fcreatedir(filename.c_str());
141   if(FileSystem::fwriteable(filename.c_str()))
142     {
143       fi = fopen(filename.c_str(), "w");
144       if (fi == NULL)
145         {
146           perror(filename.c_str());
147         }
148
149       /* Write header: */
150       fprintf(fi,";SuperTux HighScores\n");
151       fprintf(fi,"(supertux-highscore\n");
152
153       /* Save title info: */
154       fprintf(fi,"  (name \"%s\")\n", hs_name.c_str());
155
156       /* Save the description: */
157       fprintf(fi,"  (score \"%i\")\n", hs_score);
158
159       fprintf( fi,")");
160       fclose(fi);
161     }
162 }