]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/init.cpp
Import of version 1.14 minus music and sounds.
[quix0rs-blobwars.git] / src / init.cpp
1 /*
2 Copyright (C) 2004 Parallel Realities
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the 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  02111-1307, USA.
18
19 */
20
21 #include "init.h"
22
23 void checkForLicense()
24 {
25         if (!engine.loadData(_("data/license")))
26         {
27                 graphics.showLicenseErrorAndExit();
28         }
29 }
30
31 /*
32 Show the GNU Public License the first time the game is played. Waits 4 seconds
33 and then proceeds. THIS MUST NOT BE REMOVED!!!!!
34 */
35 void showLicense()
36 {
37         SDL_FillRect(graphics.screen, NULL, graphics.black);
38         graphics.delay(1000);
39
40         SDL_FillRect(graphics.screen, NULL, graphics.black);
41         SDL_Surface *pic = graphics.loadImage("gfx/main/licensePic.png");
42         graphics.blit(pic, 0, 0, graphics.screen, false);
43         SDL_FreeSurface(pic);
44
45         checkForLicense();
46
47         char line[255];
48         int y = 0;
49
50         char *token = strtok((char*)engine.dataBuffer, "\n");
51
52         while (true)
53         {
54                 sscanf(token, "%d %[^\n\r]", &y, line);
55                 
56                 if (y == -1)
57                 {
58                         break;
59                 }
60
61                 graphics.drawString(line, 320, y, true, graphics.screen);
62
63                 token = strtok(NULL, "\n");
64
65                 if (token == NULL)
66                 {
67                         break;
68                 }
69         }
70
71         graphics.delay(4000);
72
73         graphics.drawString(_("Press Space to Continue..."), 320, 440, true, graphics.screen);
74
75         engine.flushInput();
76         engine.clearInput();
77
78         while (true)
79         {
80                 graphics.updateScreen();
81                 engine.getInput();
82                 config.populate();
83                 if (engine.keyState[SDLK_SPACE])
84                         break;
85                 SDL_Delay(16);
86         }
87         
88         SDL_FillRect(graphics.screen, NULL, graphics.black);
89         graphics.delay(1000);
90 }
91
92 #if UNIX
93 void setupUserHomeDirectory()
94 {
95         char *userHome = getenv("HOME");
96         
97         if ((!userHome) || (userHome == NULL))
98         {
99                 printf("Couldn't determine user home directory! Exitting.\n");
100                 exit(1);
101         }
102
103         debug(("User Home = %s\n", userHome));
104         
105         char dir[PATH_MAX];
106         strcpy(dir, "");
107
108         sprintf(dir, "%s/.parallelrealities", userHome);
109         if ((mkdir(dir, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) != 0) && (errno != EEXIST))
110         {
111                 printf("Couldn't create required directory '%s'", dir);
112                 exit(1);
113         }
114
115         sprintf(dir, "%s/.parallelrealities/blobwars", userHome);
116         if ((mkdir(dir, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) != 0) && (errno != EEXIST))
117         {
118                 printf("Couldn't create required directory '%s'", dir);
119                 exit(1);
120         }
121
122         char gameSavePath[PATH_MAX];
123         sprintf(gameSavePath, "%s/.parallelrealities/blobwars/", userHome);
124         engine.setUserHome(gameSavePath);
125 }
126 #endif
127
128 bool loadConfig()
129 {
130         float version = 0;
131         int release = 0;
132         bool rtn = false;
133
134         char configPath[PATH_MAX];
135
136         sprintf(configPath, "%sconfig", engine.userHomeDirectory);
137
138         debug(("Loading Config from %s\n", configPath));
139
140         FILE *fp = fopen(configPath, "rb");
141
142         if (!fp)
143         {
144                 return true;
145         }
146
147         fscanf(fp, "%f %d", &version, &release);
148
149         debug(("Version = %.2f - Expected %.2f\n", version, VERSION));
150         debug(("Release = %d - Expected %d\n", release, RELEASE));
151
152         if ((version != VERSION) && (release != RELEASE))
153         {
154                 rtn = true;
155         }
156
157         fscanf(fp, "%d %d %d %d %d %d %d", &engine.fullScreen, &game.musicVol, &game.soundVol, &game.output, &game.brightness, &engine.extremeAvailable, &game.gore);
158
159         fclose(fp);
160
161         debug(("Extreme Mode = %d\n", engine.extremeAvailable));
162         debug(("Output Type = %d\n", game.output));
163
164         // Override audio if there is no sound available
165         if ((engine.useAudio) && (game.output))
166         {
167                 engine.useAudio = game.output;
168         }
169                 
170         config.loadKeyConfig();
171         config.loadJoystickConfig();
172
173         return rtn;
174 }
175
176 void saveConfig()
177 {
178         char configPath[PATH_MAX];
179
180         sprintf(configPath, "%sconfig", engine.userHomeDirectory);
181
182         FILE *fp = fopen(configPath, "wb");
183
184         if (!fp)
185         {
186                 printf("Error Saving Config to %s\n", configPath);
187                 return;
188         }
189
190         fprintf(fp, "%f %d\n", VERSION, RELEASE);
191         fprintf(fp, "%d %d %d %d %d %d %d\n", engine.fullScreen, game.musicVol, game.soundVol, game.output, game.brightness, engine.extremeAvailable, game.gore);
192
193         fclose(fp);
194         
195         debug(("Output Type = %d\n", game.output));
196 }
197
198 /*
199 Chugg chugg chugg.... brrr... chugg chugg chugg...brrrrrr... chugg ch..
200 BRRRRRRRRRRRRRRRRRMMMMMMMMMMMMMMMMMMM!! Well, hopefully anyway! ;)
201 */
202 void initSystem()
203 {
204         #if UNIX
205         setupUserHomeDirectory();
206         #endif
207
208         bool displayLicense = loadConfig();
209         
210         long flags = SDL_INIT_VIDEO|SDL_INIT_JOYSTICK;
211                         
212         if (engine.useAudio)
213         {
214                 flags |= SDL_INIT_AUDIO;
215         }
216
217         /* Initialize the SDL library */
218         if (SDL_Init(flags) < 0)
219         {
220                 printf("Couldn't initialize SDL: %s\n", SDL_GetError());
221                 exit(1);
222         }
223
224         if (!engine.fullScreen)
225         {
226                 graphics.screen = SDL_SetVideoMode(640, 480, 0, SDL_HWPALETTE);
227         }
228         else
229         {
230                 graphics.screen = SDL_SetVideoMode(640, 480, 0, SDL_HWPALETTE | SDL_FULLSCREEN);
231         }
232
233         if (graphics.screen == NULL)
234         {
235                 printf("Couldn't set 640x480 video mode: %s\n", SDL_GetError());
236                 exit(1);
237         }
238
239         // This (attempts to) set the gamma correction. We attempt to catch an error here
240         // in case someone has done something really stupid in the config file(!!)
241     if (game.brightness != -1) {
242         Math::limitInt(&game.brightness, 1, 20);
243         float brightness = game.brightness;
244         brightness /= 10;
245         SDL_SetGamma(brightness, brightness, brightness);
246     }
247
248         if (TTF_Init() < 0)
249         {
250                 printf("Couldn't initialize SDL TTF: %s\n", SDL_GetError());
251                 exit(1);
252         }
253
254         if (engine.useAudio)
255         {
256                 if (Mix_OpenAudio(22050, AUDIO_S16, engine.useAudio, 1024) < 0)
257                 {
258                         printf("Warning: Couldn't set 22050 Hz 16-bit audio - Reason: %s\n", Mix_GetError());
259                         printf("Sound and Music will be disabled\n");
260                         engine.useAudio = 0;
261                 }
262         }
263
264         debug(("Found %d Joysticks...\n", SDL_NumJoysticks()));
265
266         if (SDL_NumJoysticks() > 0)
267         {
268                 debug(("Opening Joystick - %s...\n", SDL_JoystickName(0)));
269                 SDL_JoystickEventState(SDL_ENABLE);
270                 config.sdlJoystick = SDL_JoystickOpen(0);
271         }
272
273         SDL_ShowCursor(SDL_DISABLE);
274         SDL_EventState(SDL_MOUSEMOTION, SDL_DISABLE);
275
276         graphics.registerEngine(&engine);
277         graphics.mapColors();
278
279         audio.registerEngine(&engine);
280         audio.setSoundVolume(game.soundVol);
281         audio.setMusicVolume(game.musicVol);
282         audio.output = game.output;
283
284         debug(("Sound Volume = %d\n", game.soundVol));
285         debug(("Music Volume = %d\n", game.musicVol));
286         debug(("Output Type = %d\n", game.output));
287         debug(("Brightness = %d\n", game.brightness));
288         debug(("tmp dir = %s\n", engine.userHomeDirectory));
289         debug(("Pack Dir = %s\n", PAKLOCATION));
290         debug(("Loading Fonts...\n"));
291
292         #if USEPAK
293                         
294                 char tempPath[PATH_MAX];
295                 sprintf(tempPath, "%sfont.ttf", engine.userHomeDirectory);      
296                 remove(tempPath);
297                 
298                 SDL_Delay(1000); // wait one second, just to be sure!
299                 
300                 if (!engine.unpack("data/vera.ttf", PAK_FONT))
301                 {
302                         engine.reportFontFailure();
303                 }
304         #endif
305
306         debug(("Trying to load correct font pixel sizes using a really half arsed routine!\n"));
307         debug(("If it crashes then you'll know why!\n"));
308                         
309         graphics.loadFont(0, "data/vera.ttf", 7);
310         graphics.loadFont(1, "data/vera.ttf", 9);
311         graphics.loadFont(2, "data/vera.ttf", 11);
312         graphics.loadFont(3, "data/vera.ttf", 13);
313         graphics.loadFont(4, "data/vera.ttf", 15);
314         
315         debug(("Font sizes all loaded!!\n"));
316
317         audio.loadSound(SND_CHEAT, "sound/Lock And Load!!!");
318         audio.loadSound(SND_HIGHLIGHT, "sound/menu.wav");
319         audio.loadSound(SND_SELECT, "sound/select.wav");
320
321         SDL_Surface *device = graphics.loadImage("gfx/main/alienDevice.png");
322
323 #ifndef SDL_FRAMEWORK
324         SDL_WM_SetIcon(device, NULL);
325 #endif
326         SDL_WM_SetCaption("Blob Wars : Metal Blob Solid", "Blob Wars");
327         SDL_EnableKeyRepeat(350, 80);
328
329         SDL_FreeSurface(device);
330         
331         if (strstr(engine.userHomeDirectory, "/root"))
332         {
333                 graphics.showRootWarning();
334         }
335
336         if (displayLicense)
337         {
338                 showLicense();
339         }
340         else
341         {
342                 checkForLicense();
343         }
344         
345         engine.saveConfig = true;
346
347         debug(("Init Complete...\n"));
348 }
349
350 /*
351 Removes [hopefully] all the resources that has been
352 loaded and created during the game. This is called by
353 atexit();
354 */
355 void cleanup()
356 {
357         char tempPath[PATH_MAX];
358         
359         debug(("Cleaning Up...\n"));
360         
361         debug(("Updating Replay Data\n"));
362         replayData.header.score = game.score;
363
364         debug(("Freeing Audio...\n"));
365         audio.destroy();
366
367         debug(("Removing Music...\n"));
368         sprintf(tempPath, "%smusic.mod", engine.userHomeDirectory);
369         remove(tempPath);
370
371         debug(("Freeing Game Info...\n"));
372         game.destroy();
373
374         debug(("Freeing Map Data...\n"));
375         map.destroy();
376
377         debug(("Freeing Engine Data...\n"));
378         engine.destroy();
379
380         debug(("Freeing Graphics...\n"));
381         graphics.destroy();
382
383         debug(("Saving Config...\n"));
384         if (engine.saveConfig)
385         {
386                 saveConfig();
387         }
388
389         debug(("Removing Font File...\n"));
390         sprintf(tempPath, "%sfont.ttf", engine.userHomeDirectory);
391         remove(tempPath);
392         
393         if (SDL_NumJoysticks() > 0)
394         {
395                 SDL_JoystickEventState(SDL_DISABLE);
396                 for (int i = 0 ; i < SDL_NumJoysticks() ; i++)
397                 {
398                         debug(("Closing Joystick #%d - %s...\n", i, SDL_JoystickName(i)));
399                         SDL_JoystickClose(config.sdlJoystick);
400                 }
401         }
402
403         debug(("Closing Audio...\n"));
404         if (engine.useAudio)
405         {
406                 Mix_CloseAudio();
407         }
408
409         debug(("Closing TTF...\n"));
410         TTF_Quit();
411
412         debug(("Closing SDL Sub System...\n"));
413         SDL_Quit();
414
415         debug(("All Done.\n"));
416         
417         if (replayData.replayMode == REPLAY_MODE::PLAYBACK)
418         {
419                 printf("\n==== ACTUAL PLAYBACK DATA ====\n");
420                 replayData.printReplayInformation();
421         }
422 }
423