Improved layout of statistics displaying.
[supertux.git] / src / statistics.cpp
1 //
2 //  SuperTux -  A Jump'n Run
3 //  Copyright (C) 2004 Ricardo Cruz <rick2@aeiou.pt>
4 //
5 //  This program is free software; you can redistribute it and/or
6 //  modify it under the terms of the GNU General Public License
7 //  as published by the Free Software Foundation; either version 2
8 //  of the License, or (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 // 
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18 //  02111-1307, USA.
19
20 #include "utils/lispreader.h"
21 #include "utils/lispwriter.h"
22 #include "statistics.h"
23 #include "video/drawing_context.h"
24 #include "app/gettext.h"
25 #include "app/globals.h"
26 #include "resources.h"
27
28 Statistics global_stats;
29
30 std::string
31 stat_name_to_string(int stat_enum)
32 {
33   switch(stat_enum)
34     {
35     case SCORE_STAT:
36       return "score";
37     case BADGUYS_SQUISHED_STAT:
38       return "badguys-squished";
39     case SHOTS_STAT:
40       return "shots";
41     case TIME_NEEDED_STAT:
42       return "time-needed";
43     case JUMPS_STAT:
44       return "jumps";
45     }
46 }
47
48 int
49 my_min(int a, int b)
50 {
51 if(a == -1)
52   return b;
53 if(b == -1)
54   return a;
55 return std::min(a, b);
56 }
57
58 Statistics::Statistics()
59 {
60   timer.init(true);
61   display_stat = 1;
62
63   for(int i = 0; i < NUM_STATS; i++)
64     stats[i] = -1;
65 }
66
67 Statistics::~Statistics()
68 {
69 }
70
71 void
72 Statistics::parse(LispReader& reader)
73 {
74   for(int i = 0; i < NUM_STATS; i++)
75     reader.read_int(stat_name_to_string(i).c_str(), stats[i]);
76 }
77
78 void
79 Statistics::write(LispWriter& writer)
80 {
81   for(int i = 0; i < NUM_STATS; i++)
82     writer.write_int(stat_name_to_string(i), stats[i]);
83 }
84
85 #define TOTAL_DISPLAY_TIME 3400
86 #define FADING_TIME         600
87
88 #define WMAP_INFO_LEFT_X  555
89 #define WMAP_INFO_RIGHT_X 705
90
91 void
92 Statistics::draw_worldmap_info(DrawingContext& context)
93 {
94   if(stats[SCORE_STAT] == -1)  // not initialized yet
95     return;
96
97   if(!timer.check())
98     {
99     timer.start(TOTAL_DISPLAY_TIME);
100     display_stat++;
101     if(display_stat >= NUM_STATS)
102       display_stat = 1;
103     }
104
105   int alpha;
106   if(timer.get_gone() < FADING_TIME)
107     alpha = timer.get_gone() * 255 / FADING_TIME;
108   else if(timer.get_left() < FADING_TIME)
109     alpha = timer.get_left() * 255 / FADING_TIME;
110   else
111     alpha = 255;
112
113   char str[128];
114
115   context.draw_text(white_small_text, _("Level Statistics"),
116                     Vector((WMAP_INFO_LEFT_X + WMAP_INFO_RIGHT_X) / 2, 490),
117                     CENTER_ALLIGN, LAYER_GUI);
118
119   sprintf(str, _("Max score:"));
120   context.draw_text(white_small_text, str, Vector(WMAP_INFO_LEFT_X, 506), LEFT_ALLIGN, LAYER_GUI);
121
122   sprintf(str, "%d", stats[SCORE_STAT]);
123   context.draw_text(white_small_text, str, Vector(WMAP_INFO_RIGHT_X, 506), RIGHT_ALLIGN, LAYER_GUI);
124
125   // draw other small info
126
127   if(display_stat == BADGUYS_SQUISHED_STAT)
128     sprintf(str, _("Max fragging:"));
129   else if(display_stat == SHOTS_STAT)
130     sprintf(str, _("Min shots:"));
131   else if(display_stat == TIME_NEEDED_STAT)
132     sprintf(str, _("Min time needed:"));
133   else// if(display_stat == JUMPS_STAT)
134     sprintf(str, _("Min jumps:"));
135
136   context.draw_text(white_small_text, str, Vector(WMAP_INFO_LEFT_X, 522), LEFT_ALLIGN, LAYER_GUI, NONE_EFFECT, alpha);
137
138   if(display_stat == BADGUYS_SQUISHED_STAT)
139     sprintf(str, "%d", stats[BADGUYS_SQUISHED_STAT]);
140   else if(display_stat == SHOTS_STAT)
141     sprintf(str, "%d", stats[SHOTS_STAT]);
142   else if(display_stat == TIME_NEEDED_STAT)
143     sprintf(str, "%d", stats[TIME_NEEDED_STAT]);
144   else// if(display_stat == JUMPS_STAT)
145     sprintf(str, "%d", stats[JUMPS_STAT]);
146
147   context.draw_text(white_small_text, str, Vector(WMAP_INFO_RIGHT_X, 522), RIGHT_ALLIGN, LAYER_GUI, NONE_EFFECT, alpha);
148 }
149
150 void
151 Statistics::draw_message_info(DrawingContext& context, std::string title)
152 {
153   if(stats[SCORE_STAT] == -1)  // not initialized yet
154     return;
155
156   context.draw_text(gold_text, title, Vector(screen->w/2, 410), CENTER_ALLIGN, LAYER_GUI);
157
158   char str[128];
159
160   sprintf(str, _(    "Max score:       %d"), stats[SCORE_STAT]);
161   context.draw_text(white_text, str, Vector(screen->w/2, 450), CENTER_ALLIGN, LAYER_GUI);
162
163   for(int i = 1; i < NUM_STATS; i++)
164     {
165     if(i == BADGUYS_SQUISHED_STAT)
166       sprintf(str, _("Max fragging:    %d"), stats[BADGUYS_SQUISHED_STAT]);
167     else if(i == SHOTS_STAT)
168       sprintf(str, _("Min shots:       %d"), stats[SHOTS_STAT]);
169     else if(i == TIME_NEEDED_STAT)
170       sprintf(str, _("Min time needed: %d"), stats[TIME_NEEDED_STAT]);
171     else// if(i == JUMPS_STAT)
172       sprintf(str, _("Min jumps:       %d"), stats[JUMPS_STAT]);
173
174     context.draw_text(white_small_text, str, Vector(screen->w/2, 462 + i*18), CENTER_ALLIGN, LAYER_GUI);
175     }
176 }
177
178 void
179 Statistics::add_points(int stat, int points)
180 {
181   stats[stat] += points;
182 }
183
184 int
185 Statistics::get_points(int stat)
186 {
187   return stats[stat];
188 }
189
190 void
191 Statistics::set_points(int stat, int points)
192 {
193   stats[stat] = points;
194 }
195
196 void
197 Statistics::reset()
198 {
199   for(int i = 0; i < NUM_STATS; i++)
200     stats[i] = 0;
201 }
202
203 void
204 Statistics::merge(Statistics& stats_)
205 {
206   stats[SCORE_STAT] = std::max(stats[SCORE_STAT], stats_.stats[SCORE_STAT]);
207   if(stats[JUMPS_STAT] != -1)
208     stats[JUMPS_STAT] = my_min(stats[JUMPS_STAT], stats_.stats[JUMPS_STAT]);
209   stats[BADGUYS_SQUISHED_STAT] =
210     std::max(stats[BADGUYS_SQUISHED_STAT], stats_.stats[BADGUYS_SQUISHED_STAT]);
211   stats[SHOTS_STAT] = my_min(stats[SHOTS_STAT], stats_.stats[SHOTS_STAT]);
212   stats[TIME_NEEDED_STAT] =
213     my_min(stats[TIME_NEEDED_STAT], stats_.stats[TIME_NEEDED_STAT]);
214 }
215
216 void
217 Statistics::operator+=(const Statistics& stats_)
218 {
219   for(int i = 0; i < NUM_STATS; i++)
220     stats[i] += stats_.stats[i];
221 }