]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/init.cpp
Use field width limits with fscanf().
[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         dir[0] = 0;
107
108         snprintf(dir, sizeof 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         snprintf(dir, sizeof 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         snprintf(gameSavePath, sizeof 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         snprintf(configPath, sizeof 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, "%10f %10d", &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, "%10d %10d %10d %10d %10d %10d %10d", &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         snprintf(configPath, sizeof 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 // see if we can load the private keyState
200 //
201 int initMedalService(void *data)
202 {
203         SDL_mutexP(medalServer.lock);
204         
205         char connectMessage[1024];
206         snprintf(connectMessage, sizeof connectMessage, "Contacting Medal Server - %s:%d", MEDAL_SERVER_HOST, MEDAL_SERVER_PORT);
207         
208         graphics.showMedalMessage(-1, connectMessage);
209         
210         char keyPath[PATH_MAX];
211         char privateKey[20];
212
213         snprintf(keyPath, sizeof keyPath, "%smedalKey", engine.userHomeDirectory);
214         
215         debug(("Loading private key from %s\n", keyPath));
216         
217         FILE *fp = fopen(keyPath, "rb");
218         
219         if (!fp)
220         {
221                 graphics.showMedalMessage(-1, "No Medal Key found - Online functions disabled");
222                 SDL_mutexV(medalServer.lock);
223                 return 0;
224         }
225         
226         fscanf(fp, "%19s", privateKey);
227         
228         fclose(fp);
229                 
230         if (!medalServer.connect(privateKey))
231         {
232                 graphics.showMedalMessage(-1, "Medal Server Connection Failed - Online functions disabled");
233                 return 0;
234         }
235         
236         graphics.showMedalMessage(-1, "Connected to Medal Server");
237         
238         SDL_mutexV(medalServer.lock);
239         
240         return 1;
241 }
242
243 /*
244 Chugg chugg chugg.... brrr... chugg chugg chugg...brrrrrr... chugg ch..
245 BRRRRRRRRRRRRRRRRRMMMMMMMMMMMMMMMMMMM!! Well, hopefully anyway! ;)
246 */
247 void initSystem()
248 {
249         #if UNIX
250         setupUserHomeDirectory();
251         #endif
252
253         bool displayLicense = loadConfig();
254         
255         long flags = SDL_INIT_VIDEO|SDL_INIT_JOYSTICK;
256                         
257         if (engine.useAudio)
258         {
259                 flags |= SDL_INIT_AUDIO;
260         }
261
262         /* Initialize the SDL library */
263         if (SDL_Init(flags) < 0)
264         {
265                 printf("Couldn't initialize SDL: %s\n", SDL_GetError());
266                 exit(1);
267         }
268
269         if (!engine.fullScreen)
270         {
271                 graphics.screen = SDL_SetVideoMode(640, 480, 0, SDL_HWPALETTE);
272         }
273         else
274         {
275                 graphics.screen = SDL_SetVideoMode(640, 480, 0, SDL_HWPALETTE | SDL_FULLSCREEN);
276         }
277
278         if (graphics.screen == NULL)
279         {
280                 printf("Couldn't set 640x480 video mode: %s\n", SDL_GetError());
281                 exit(1);
282         }
283
284         // This (attempts to) set the gamma correction. We attempt to catch an error here
285         // in case someone has done something really stupid in the config file(!!)
286     if (game.brightness != -1) {
287         Math::limitInt(&game.brightness, 1, 20);
288         float brightness = game.brightness;
289         brightness /= 10;
290         SDL_SetGamma(brightness, brightness, brightness);
291     }
292
293         if (TTF_Init() < 0)
294         {
295                 printf("Couldn't initialize SDL TTF: %s\n", SDL_GetError());
296                 exit(1);
297         }
298
299         if (engine.useAudio)
300         {
301                 if (Mix_OpenAudio(22050, AUDIO_S16, engine.useAudio, 1024) < 0)
302                 {
303                         printf("Warning: Couldn't set 44100 Hz 16-bit audio - Reason: %s\n", Mix_GetError());
304                         printf("Sound and Music will be disabled\n");
305                         engine.useAudio = 0;
306                 }
307         }
308
309         debug(("Found %d Joysticks...\n", SDL_NumJoysticks()));
310
311         if (SDL_NumJoysticks() > 0)
312         {
313                 debug(("Opening Joystick - %s...\n", SDL_JoystickName(0)));
314                 SDL_JoystickEventState(SDL_ENABLE);
315                 config.sdlJoystick = SDL_JoystickOpen(0);
316         }
317
318         SDL_ShowCursor(SDL_DISABLE);
319         SDL_EventState(SDL_MOUSEMOTION, SDL_DISABLE);
320
321         graphics.registerEngine(&engine);
322         graphics.mapColors();
323
324         audio.registerEngine(&engine);
325         audio.setSoundVolume(game.soundVol);
326         audio.setMusicVolume(game.musicVol);
327         audio.output = game.output;
328
329         debug(("Sound Volume = %d\n", game.soundVol));
330         debug(("Music Volume = %d\n", game.musicVol));
331         debug(("Output Type = %d\n", game.output));
332         debug(("Brightness = %d\n", game.brightness));
333         debug(("tmp dir = %s\n", engine.userHomeDirectory));
334         debug(("Pack Dir = %s\n", PAKLOCATION));
335         debug(("Loading Fonts...\n"));
336
337         #if USEPAK
338                         
339                 char tempPath[PATH_MAX];
340                 snprintf(tempPath, sizeof tempPath, "%sfont.ttf", engine.userHomeDirectory);    
341                 remove(tempPath);
342                 
343                 SDL_Delay(1000); // wait one second, just to be sure!
344                 
345                 if (!engine.unpack("data/vera.ttf", PAK_FONT))
346                 {
347                         engine.reportFontFailure();
348                 }
349         #endif
350
351         debug(("Trying to load correct font pixel sizes using a really half arsed routine!\n"));
352         debug(("If it crashes then you'll know why!\n"));
353                         
354         graphics.loadFont(0, "data/vera.ttf", 7);
355         graphics.loadFont(1, "data/vera.ttf", 9);
356         graphics.loadFont(2, "data/vera.ttf", 11);
357         graphics.loadFont(3, "data/vera.ttf", 13);
358         graphics.loadFont(4, "data/vera.ttf", 15);
359         
360         debug(("Font sizes all loaded!!\n"));
361
362         audio.loadSound(SND_CHEAT, "sound/Lock And Load!!!");
363         audio.loadSound(SND_HIGHLIGHT, "sound/menu");
364         audio.loadSound(SND_SELECT, "sound/select");
365         
366         graphics.medal[0] = graphics.loadImage("gfx/main/medal_bronze_1.png");
367         graphics.medal[1] = graphics.loadImage("gfx/main/award_star_silver_3.png");
368         graphics.medal[2] = graphics.loadImage("gfx/main/shield.png");
369         graphics.medal[3] = graphics.loadImage("gfx/main/ruby.png");
370
371         graphics.license[0] = graphics.loadImage("gfx/main/cc-by.png", true);
372         graphics.license[1] = graphics.loadImage("gfx/main/cc-by-sa.png", true);
373
374         SDL_Surface *device = graphics.loadImage("gfx/main/alienDevice.png");
375
376 #ifndef SDL_FRAMEWORK
377         SDL_WM_SetIcon(device, NULL);
378 #endif
379         SDL_WM_SetCaption("Blob Wars : Metal Blob Solid", "Blob Wars");
380         SDL_EnableKeyRepeat(350, 80);
381
382         SDL_FreeSurface(device);
383         
384         if (strstr(engine.userHomeDirectory, "/root"))
385         {
386                 graphics.showRootWarning();
387         }
388
389         if (displayLicense)
390         {
391                 showLicense();
392         }
393         else
394         {
395                 checkForLicense();
396         }
397         
398         if (SDLNet_Init() < 0)
399         {
400                 printf("SDLNet_Init: %s\n", SDLNet_GetError());
401         }
402         else
403         {
404                 SDL_Thread *thread = SDL_CreateThread(initMedalService, NULL);
405         
406                 if (thread == NULL)
407                 {
408                         printf("Unable to create thread: %s\n", SDL_GetError());
409                         printf("Calling medal server directly\n");
410                         initMedalService(NULL);
411                         return;
412                 }
413         }
414         
415         engine.saveConfig = true;
416
417         debug(("Init Complete...\n"));
418 }
419
420 /*
421 Removes [hopefully] all the resources that has been
422 loaded and created during the game. This is called by
423 atexit();
424 */
425 void cleanup()
426 {
427         char tempPath[PATH_MAX];
428         
429         debug(("Cleaning Up...\n"));
430         
431         debug(("Updating Replay Data\n"));
432         replayData.header.score = game.score;
433
434         debug(("Freeing Audio...\n"));
435         audio.destroy();
436
437         debug(("Removing Music...\n"));
438         snprintf(tempPath, sizeof tempPath, "%smusic.mod", engine.userHomeDirectory);
439         remove(tempPath);
440
441         debug(("Freeing Game Info...\n"));
442         game.destroy();
443
444         debug(("Freeing Map Data...\n"));
445         map.destroy();
446
447         debug(("Freeing Engine Data...\n"));
448         engine.destroy();
449
450         debug(("Freeing Graphics...\n"));
451         graphics.destroy();
452
453         debug(("Saving Config...\n"));
454         if (engine.saveConfig)
455         {
456                 saveConfig();
457         }
458
459         debug(("Removing Font File...\n"));
460         snprintf(tempPath, sizeof tempPath, "%sfont.ttf", engine.userHomeDirectory);
461         remove(tempPath);
462         
463         if (SDL_NumJoysticks() > 0)
464         {
465                 SDL_JoystickEventState(SDL_DISABLE);
466                 for (int i = 0 ; i < SDL_NumJoysticks() ; i++)
467                 {
468                         debug(("Closing Joystick #%d - %s...\n", i, SDL_JoystickName(i)));
469                         SDL_JoystickClose(config.sdlJoystick);
470                 }
471         }
472
473         debug(("Closing Audio...\n"));
474         if (engine.useAudio)
475         {
476                 Mix_CloseAudio();
477         }
478
479         debug(("Closing TTF...\n"));
480         TTF_Quit();
481         
482         debug(("Closing NET...\n"));
483         SDLNet_Quit();
484
485         debug(("Closing SDL Sub System...\n"));
486         SDL_Quit();
487
488         debug(("All Done.\n"));
489         
490         if (replayData.replayMode == REPLAY_MODE::PLAYBACK)
491         {
492                 printf("\n==== ACTUAL PLAYBACK DATA ====\n");
493                 replayData.printReplayInformation();
494         }
495 }
496