]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/init.cpp
1.17 Medal Support
[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 // 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         sprintf(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         sprintf(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, "%s", 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                 sprintf(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.wav");
364         audio.loadSound(SND_SELECT, "sound/select.wav");
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         SDL_Surface *device = graphics.loadImage("gfx/main/alienDevice.png");
372
373 #ifndef SDL_FRAMEWORK
374         SDL_WM_SetIcon(device, NULL);
375 #endif
376         SDL_WM_SetCaption("Blob Wars : Metal Blob Solid", "Blob Wars");
377         SDL_EnableKeyRepeat(350, 80);
378
379         SDL_FreeSurface(device);
380         
381         if (strstr(engine.userHomeDirectory, "/root"))
382         {
383                 graphics.showRootWarning();
384         }
385
386         if (displayLicense)
387         {
388                 showLicense();
389         }
390         else
391         {
392                 checkForLicense();
393         }
394         
395         if (SDLNet_Init() < 0)
396         {
397                 printf("SDLNet_Init: %s\n", SDLNet_GetError());
398         }
399         else
400         {
401                 SDL_Thread *thread = SDL_CreateThread(initMedalService, NULL);
402         
403                 if (thread == NULL)
404                 {
405                         printf("Unable to create thread: %s\n", SDL_GetError());
406                         printf("Calling medal server directly\n");
407                         initMedalService(NULL);
408                         return;
409                 }
410         }
411         
412         engine.saveConfig = true;
413
414         debug(("Init Complete...\n"));
415 }
416
417 /*
418 Removes [hopefully] all the resources that has been
419 loaded and created during the game. This is called by
420 atexit();
421 */
422 void cleanup()
423 {
424         char tempPath[PATH_MAX];
425         
426         debug(("Cleaning Up...\n"));
427         
428         debug(("Updating Replay Data\n"));
429         replayData.header.score = game.score;
430
431         debug(("Freeing Audio...\n"));
432         audio.destroy();
433
434         debug(("Removing Music...\n"));
435         sprintf(tempPath, "%smusic.mod", engine.userHomeDirectory);
436         remove(tempPath);
437
438         debug(("Freeing Game Info...\n"));
439         game.destroy();
440
441         debug(("Freeing Map Data...\n"));
442         map.destroy();
443
444         debug(("Freeing Engine Data...\n"));
445         engine.destroy();
446
447         debug(("Freeing Graphics...\n"));
448         graphics.destroy();
449
450         debug(("Saving Config...\n"));
451         if (engine.saveConfig)
452         {
453                 saveConfig();
454         }
455
456         debug(("Removing Font File...\n"));
457         sprintf(tempPath, "%sfont.ttf", engine.userHomeDirectory);
458         remove(tempPath);
459         
460         if (SDL_NumJoysticks() > 0)
461         {
462                 SDL_JoystickEventState(SDL_DISABLE);
463                 for (int i = 0 ; i < SDL_NumJoysticks() ; i++)
464                 {
465                         debug(("Closing Joystick #%d - %s...\n", i, SDL_JoystickName(i)));
466                         SDL_JoystickClose(config.sdlJoystick);
467                 }
468         }
469
470         debug(("Closing Audio...\n"));
471         if (engine.useAudio)
472         {
473                 Mix_CloseAudio();
474         }
475
476         debug(("Closing TTF...\n"));
477         TTF_Quit();
478         
479         debug(("Closing NET...\n"));
480         SDLNet_Quit();
481
482         debug(("Closing SDL Sub System...\n"));
483         SDL_Quit();
484
485         debug(("All Done.\n"));
486         
487         if (replayData.replayMode == REPLAY_MODE::PLAYBACK)
488         {
489                 printf("\n==== ACTUAL PLAYBACK DATA ====\n");
490                 replayData.printReplayInformation();
491         }
492 }
493