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