-Started to move stuff from library back to main game
[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 "sprite/sprite_manager.h"
24 #include "app/setup.h"
25 #include "gui/menu.h"
26 #include "gui/button.h"
27 #include "resources.h"
28 #include "tile_manager.h"
29 #include "object/gameobjs.h"
30 #include "object/player.h"
31
32 MusicRef herring_song;
33 MusicRef level_end_song;
34 MusicRef credits_song;
35
36 SpriteManager* sprite_manager = 0;
37 TileManager* tile_manager = 0;
38 SoundManager* sound_manager = 0;
39
40 Font* gold_text;
41 Font* blue_text;
42 Font* gray_text;
43 Font* yellow_nums;
44 Font* white_text;
45 Font* white_small_text;
46 Font* white_big_text;                               
47                                     
48 /* Load graphics/sounds shared between all levels: */
49 void load_shared()
50 {
51   /* Load GUI/menu images: */
52   checkbox = new Surface(datadir + "/images/status/checkbox.png", true);
53   checkbox_checked = new Surface(datadir + "/images/status/checkbox-checked.png", true);
54   back = new Surface(datadir + "/images/status/back.png", true);
55   arrow_left = new Surface(datadir + "/images/icons/left.png", true);
56   arrow_right = new Surface(datadir + "/images/icons/right.png", true);
57
58   /* Load the mouse-cursor */
59   mouse_cursor = new MouseCursor( datadir + "/images/status/mousecursor.png",1);
60   MouseCursor::set_current(mouse_cursor);
61
62   /* Load global images: */
63   gold_text = new Font(datadir + "/images/fonts/gold.png", Font::TEXT, 16,18);
64   blue_text = new Font(datadir + "/images/fonts/blue.png", Font::TEXT, 16,18,3);
65   white_text  = new Font(datadir + "/images/fonts/white.png",
66                          Font::TEXT, 16,18);
67   gray_text  = new Font(datadir + "/images/fonts/gray.png",
68                         Font::TEXT, 16,18);
69   white_small_text = new Font(datadir + "/images/fonts/white-small.png",
70                               Font::TEXT, 8,9, 1);
71   white_big_text   = new Font(datadir + "/images/fonts/white-big.png",
72                               Font::TEXT, 20,22, 3);
73   yellow_nums = new Font(datadir + "/images/fonts/numbers.png",
74                          Font::NUM, 32,32);
75
76   Menu::default_font = white_text;
77   Menu::active_font = blue_text;
78   Menu::deactive_font = gray_text;
79   Menu::label_font = white_big_text;
80   Menu::field_font = gold_text;
81   
82   Button::info_font = white_small_text;
83
84   int i;
85
86   sprite_manager = new SpriteManager(
87       get_resource_filename("/images/supertux.strf"));
88   tile_manager = new TileManager("/images/tilesets/supertux.stgt");
89
90   /* Tuxes: */
91   char img_name[1024];
92   for (int i = 0; i < GROWING_FRAMES; i++)
93     {
94       sprintf(img_name, "%s/images/shared/tux-grow-left-%i.png", datadir.c_str(), i+1);
95       growingtux_left[i] = new Surface(img_name, true);
96
97       sprintf(img_name, "%s/images/shared/tux-grow-right-%i.png", datadir.c_str(), i+1);
98       growingtux_right[i] = new Surface(img_name, true);
99     }
100
101   small_tux = new TuxBodyParts();
102   small_tux->head = 0;
103   small_tux->body = sprite_manager->create("small-tux-body");
104   small_tux->arms = sprite_manager->create("small-tux-arms");
105   small_tux->feet = 0;
106
107   big_tux = new TuxBodyParts();
108   big_tux->head = sprite_manager->create("big-tux-head");
109   big_tux->body = sprite_manager->create("big-tux-body");
110   big_tux->arms = sprite_manager->create("big-tux-arms");
111   big_tux->feet = sprite_manager->create("big-tux-feet");
112
113   fire_tux = new TuxBodyParts();
114   fire_tux->head = sprite_manager->create("big-fire-tux-head");
115   fire_tux->body = sprite_manager->create("big-tux-body");
116   fire_tux->arms = sprite_manager->create("big-tux-arms");
117   fire_tux->feet = sprite_manager->create("big-tux-feet");
118
119   ice_tux = new TuxBodyParts();
120   ice_tux->head = sprite_manager->create("big-tux-head");
121   ice_tux->body = sprite_manager->create("big-tux-body");
122   ice_tux->arms = sprite_manager->create("big-tux-arms");
123   ice_tux->feet = sprite_manager->create("big-tux-feet");
124
125   /* Objects */
126   load_object_gfx();
127
128   /* Tux life: */
129   tux_life = new Surface(datadir + "/images/shared/tux-life.png",
130                          true);
131
132   /* Sound effects: */
133   sound_manager->preload_sound("jump");
134   sound_manager->preload_sound("bigjump");
135   sound_manager->preload_sound("skid");
136   sound_manager->preload_sound("coin");
137   sound_manager->preload_sound("invincible");
138   sound_manager->preload_sound("brick");
139   sound_manager->preload_sound("hurt");
140   sound_manager->preload_sound("squish");
141   sound_manager->preload_sound("fall");
142   sound_manager->preload_sound("ricochet");
143   sound_manager->preload_sound("bump-upgrade");
144   sound_manager->preload_sound("upgrade");
145   sound_manager->preload_sound("grow");
146   sound_manager->preload_sound("fire-flower");
147   sound_manager->preload_sound("shoot");
148   sound_manager->preload_sound("lifeup");
149   sound_manager->preload_sound("stomp");
150   sound_manager->preload_sound("kick");
151   sound_manager->preload_sound("explosion");
152   sound_manager->preload_sound("warp");
153   sound_manager->preload_sound("fireworks");
154
155   /* Herring song */
156   herring_song = sound_manager->load_music(datadir + "/music/salcon.mod");
157   level_end_song = sound_manager->load_music(datadir + "/music/leveldone.mod");
158 }
159
160 /* Free shared data: */
161 void unload_shared()
162 {
163   /* Free global images: */
164   delete gold_text;
165   delete white_text;
166   delete blue_text;
167   delete gray_text;
168   delete white_small_text;
169   delete white_big_text;
170   delete yellow_nums;
171   
172   free_object_gfx();
173
174   delete tux_life;
175
176   delete small_tux;
177   delete big_tux;
178   delete fire_tux;
179   delete ice_tux;
180
181   for (int i = 0; i < GROWING_FRAMES; i++) {
182     delete growingtux_left[i];
183     delete growingtux_right[i];
184   }
185
186   delete sprite_manager;
187   sprite_manager = 0;
188   delete tile_manager;
189   tile_manager = 0;
190
191   /* Free GUI/menu images: */
192   delete checkbox;
193   delete checkbox_checked;
194   delete back;
195   delete arrow_left;
196   delete arrow_right;
197
198   /* Free mouse-cursor */
199   delete mouse_cursor;
200 }
201
202 std::string get_resource_filename(const std::string& resource)
203 {
204   std::string filepath = user_dir + resource;
205   if(FileSystem::faccessible(filepath))
206     return filepath;
207   
208   filepath = datadir + resource;
209   if(FileSystem::faccessible(filepath))
210     return filepath;
211
212   std::cerr << "Couldn't find resource: '" << resource  << "'." << std::endl;
213   return "";
214 }