5267f4b8ac1ec0382eaeec1651ae9c38cd842689
[supertux.git] / src / resources.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2003 Tobias Glaesser <tobi.web@gmx.de>
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  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "app/globals.h"
23 #include "special/sprite_manager.h"
24 #include "app/setup.h"
25 #include "gui/menu.h"
26 #include "gui/button.h"
27 #include "scene.h"
28 #include "resources.h"
29 #include "object/gameobjs.h"
30 #include "object/player.h"
31
32 Surface* img_waves[3]; 
33 Surface* img_water;
34 Surface* img_pole;
35 Surface* img_poletop;
36 Surface* img_flag[2];
37 Surface* img_cloud[2][4];
38 Surface* img_distro[4];
39
40 Menu* main_menu      = 0;
41 Menu* game_menu      = 0;
42 Menu* options_menu   = 0;
43 Menu* options_keys_menu     = 0;
44 Menu* options_joystick_menu = 0;
45 Menu* highscore_menu = 0;
46 Menu* load_game_menu = 0;
47 Menu* save_game_menu = 0;
48 Menu* contrib_menu   = 0;
49 Menu* contrib_subset_menu   = 0;
50
51 MusicRef herring_song;
52 MusicRef level_end_song;
53
54 SpriteManager* sprite_manager = 0;
55
56 char * soundfilenames[NUM_SOUNDS] = {
57                                        "/sounds/jump.wav",
58                                        "/sounds/bigjump.wav",
59                                        "/sounds/skid.wav",
60                                        "/sounds/coin.wav",
61                                        "/sounds/invincible.wav",
62                                        "/sounds/brick.wav",
63                                        "/sounds/hurt.wav",
64                                        "/sounds/squish.wav",
65                                        "/sounds/fall.wav",
66                                        "/sounds/ricochet.wav",
67                                        "/sounds/bump-upgrade.wav",
68                                        "/sounds/upgrade.wav",
69                                        "/sounds/grow.wav",
70                                        "/sounds/fire-flower.wav",
71                                        "/sounds/shoot.wav",
72                                        "/sounds/lifeup.wav",
73                                        "/sounds/stomp.wav",
74                                        "/sounds/kick.wav",
75                                        "/sounds/explosion.wav",
76                                        "/sounds/warp.wav",
77                                        "/sounds/fireworks.wav"
78                                     };
79
80
81 Font* gold_text;
82 Font* blue_text;
83 Font* gray_text;
84 Font* yellow_nums;
85 Font* white_text;
86 Font* white_small_text;
87 Font* white_big_text;                               
88                                     
89 /* Load graphics/sounds shared between all levels: */
90 void loadshared()
91 {
92
93   /* Load global images: */
94   gold_text = new Font(datadir + "/images/fonts/gold.png", Font::TEXT, 16,18);
95   blue_text = new Font(datadir + "/images/fonts/blue.png", Font::TEXT, 16,18,3);
96   white_text  = new Font(datadir + "/images/fonts/white.png",
97                          Font::TEXT, 16,18);
98   gray_text  = new Font(datadir + "/images/fonts/gray.png",
99                         Font::TEXT, 16,18);
100   white_small_text = new Font(datadir + "/images/fonts/white-small.png",
101                               Font::TEXT, 8,9, 1);
102   white_big_text   = new Font(datadir + "/images/fonts/white-big.png",
103                               Font::TEXT, 20,22, 3);
104   yellow_nums = new Font(datadir + "/images/fonts/numbers.png",
105                          Font::NUM, 32,32);
106
107   Menu::default_font = white_text;
108   Menu::active_font = blue_text;
109   Menu::deactive_font = gray_text;
110   Menu::label_font = white_big_text;
111   Menu::field_font = gold_text;
112   
113   Button::info_font = white_small_text;
114
115   int i;
116
117   sprite_manager = new SpriteManager(datadir + "/images/supertux.strf");
118
119   /* Tuxes: */
120   smalltux_star = sprite_manager->create("smalltux-star");
121   bigtux_star = sprite_manager->create("bigtux-star");
122   smalltux_gameover = sprite_manager->create("smalltux-gameover");
123
124   char img_name[1024];
125   for (int i = 0; i < GROWING_FRAMES; i++)
126     {
127       sprintf(img_name, "%s/images/shared/tux-grow-left-%i.png", datadir.c_str(), i+1);
128       growingtux_left[i] = new Surface(img_name, true);
129
130       sprintf(img_name, "%s/images/shared/tux-grow-right-%i.png", datadir.c_str(), i+1);
131       growingtux_right[i] = new Surface(img_name, true);
132     }
133
134   small_tux = new TuxBodyParts();
135   small_tux->head = 0;
136   small_tux->body = sprite_manager->create("small-tux-body");
137   small_tux->arms = sprite_manager->create("small-tux-arms");
138   small_tux->feet = 0;
139
140   big_tux = new TuxBodyParts();
141   big_tux->head = sprite_manager->create("big-tux-head");
142   big_tux->body = sprite_manager->create("big-tux-body");
143   big_tux->arms = sprite_manager->create("big-tux-arms");
144   big_tux->feet = sprite_manager->create("big-tux-feet");
145
146   fire_tux = new TuxBodyParts();
147   fire_tux->head = sprite_manager->create("big-fire-tux-head");
148   fire_tux->body = sprite_manager->create("big-tux-body");
149   fire_tux->arms = sprite_manager->create("big-tux-arms");
150   fire_tux->feet = sprite_manager->create("big-tux-feet");
151
152   ice_tux = new TuxBodyParts();
153   ice_tux->head = sprite_manager->create("big-tux-head");
154   ice_tux->body = sprite_manager->create("big-tux-body");
155   ice_tux->arms = sprite_manager->create("big-tux-arms");
156   ice_tux->feet = sprite_manager->create("big-tux-feet");
157
158   /* Water: */
159   img_water = new Surface(datadir + "/images/shared/water.png", false);
160
161   img_waves[0] = new Surface(datadir + "/images/shared/waves-0.png",
162                              true);
163
164   img_waves[1] = new Surface(datadir + "/images/shared/waves-1.png",
165                              true);
166
167
168   img_waves[2] = new Surface(datadir + "/images/shared/waves-2.png",
169                              true);
170
171
172   /* Pole: */
173
174   img_pole = new Surface(datadir + "/images/shared/pole.png", true);
175   img_poletop = new Surface(datadir + "/images/shared/poletop.png",
176                             true);
177
178
179   /* Flag: */
180
181   img_flag[0] = new Surface(datadir + "/images/shared/flag-0.png",
182                             true);
183   img_flag[1] = new Surface(datadir + "/images/shared/flag-1.png",
184                             true);
185
186
187   /* Cloud: */
188
189   img_cloud[0][0] = new Surface(datadir + "/images/shared/cloud-00.png",
190                                 true);
191
192   img_cloud[0][1] = new Surface(datadir + "/images/shared/cloud-01.png",
193                                 true);
194
195   img_cloud[0][2] = new Surface(datadir + "/images/shared/cloud-02.png",
196                                 true);
197
198   img_cloud[0][3] = new Surface(datadir + "/images/shared/cloud-03.png",
199                                 true);
200
201
202   img_cloud[1][0] = new Surface(datadir + "/images/shared/cloud-10.png",
203                                 true);
204
205   img_cloud[1][1] = new Surface(datadir + "/images/shared/cloud-11.png",
206                                 true);
207
208   img_cloud[1][2] = new Surface(datadir + "/images/shared/cloud-12.png",
209                                 true);
210
211   img_cloud[1][3] = new Surface(datadir + "/images/shared/cloud-13.png",
212                                 true);
213
214   /* Objects */
215   load_object_gfx();
216
217   /* Distros: */
218   img_distro[0] = new Surface(datadir + "/images/tilesets/coin1.png",
219                               true);
220
221   img_distro[1] = new Surface(datadir + "/images/tilesets/coin2.png",
222                               true);
223
224   img_distro[2] = new Surface(datadir + "/images/tilesets/coin3.png",
225                               true);
226
227   img_distro[3] = new Surface(datadir + "/images/tilesets/coin2.png",
228                               true);
229
230
231   /* Tux life: */
232
233   tux_life = new Surface(datadir + "/images/shared/tux-life.png",
234                          true);
235
236   /* Sound effects: */
237
238   /* if (use_sound) // this will introduce SERIOUS bugs here ! because "load_sound"
239                     // initialize sounds[i] with the correct pointer's value:
240                     // NULL or something else. And it will be dangerous to
241                     // play with not-initialized pointers.
242                     // This is also true with if (use_music)
243                     Send a mail to me: neoneurone@users.sf.net, if you have another opinion. :)
244   */
245   for (i = 0; i < NUM_SOUNDS; i++)
246     SoundManager::get
247       ()->add_sound(SoundManager::get
248                       ()->load_sound(datadir + soundfilenames[i]),i);
249
250   /* Herring song */
251   herring_song = SoundManager::get
252                    ()->load_music(datadir + "/music/salcon.mod");
253   level_end_song = SoundManager::get
254                      ()->load_music(datadir + "/music/leveldone.mod");
255 }
256
257 /* Free shared data: */
258 void unloadshared(void)
259 {
260   delete smalltux_star;
261   delete bigtux_star;
262   delete smalltux_gameover;
263
264   /* Free global images: */
265   delete gold_text;
266   delete white_text;
267   delete blue_text;
268   delete gray_text;
269   delete white_small_text;
270   delete white_big_text;
271   delete yellow_nums;
272   
273   free_object_gfx();
274
275   delete img_water;
276   for (int i = 0; i < 3; i++)
277     delete img_waves[i];
278
279   delete img_pole;
280   delete img_poletop;
281
282   for (int i = 0; i < 2; i++)
283     delete img_flag[i];
284
285   for (int i = 0; i < 4; i++) {
286     delete img_distro[i];
287     delete img_cloud[0][i];
288     delete img_cloud[1][i];
289   }
290
291   delete tux_life;
292
293   delete small_tux;
294   delete big_tux;
295   delete fire_tux;
296   delete ice_tux;
297
298   for (int i = 0; i < GROWING_FRAMES; i++) {
299     delete growingtux_left[i];
300     delete growingtux_right[i];
301   }
302
303   delete sprite_manager;
304   sprite_manager = 0;
305 }
306
307 std::string get_resource_filename(const std::string& resource)
308 {
309   std::string filepath = st_dir + resource;
310   if(access(filepath.c_str(), R_OK) == 0)
311     return filepath;
312   
313   filepath = datadir + resource;
314   if(access(filepath.c_str(), R_OK) == 0)
315     return filepath;
316
317   std::cerr << "Couldn't find resource: '" << resource  << "'." << std::endl;
318   return "";
319 }