2 Copyright (C) 2004-2011 Parallel Realities
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.
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.
13 See the GNU General Public License for more details.
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.
23 void checkForLicense()
25 if (!engine.loadData(_("data/license")))
27 graphics.showLicenseErrorAndExit();
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!!!!!
37 SDL_FillRect(graphics.screen, NULL, graphics.black);
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);
50 char *token = strtok((char*)engine.dataBuffer, "\n");
54 sscanf(token, "%d %[^\n\r]", &y, line);
61 graphics.drawString(line, 320, y, true, graphics.screen);
63 token = strtok(NULL, "\n");
73 graphics.drawString(_("Press Space to Continue..."), 320, 440, true, graphics.screen);
80 graphics.updateScreen();
83 if (engine.keyState[SDLK_SPACE])
88 SDL_FillRect(graphics.screen, NULL, graphics.black);
93 void setupUserHomeDirectory()
95 char *userHome = getenv("HOME");
97 if ((!userHome) || (userHome == NULL))
99 printf("Couldn't determine user home directory! Exitting.\n");
103 debug(("User Home = %s\n", userHome));
108 snprintf(dir, sizeof dir, "%s/.parallelrealities", userHome);
109 if ((mkdir(dir, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) != 0) && (errno != EEXIST))
111 printf("Couldn't create required directory '%s'", dir);
115 snprintf(dir, sizeof dir, "%s/.parallelrealities/blobwars", userHome);
116 if ((mkdir(dir, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) != 0) && (errno != EEXIST))
118 printf("Couldn't create required directory '%s'", dir);
122 char gameSavePath[PATH_MAX];
123 snprintf(gameSavePath, sizeof gameSavePath, "%s/.parallelrealities/blobwars/", userHome);
124 engine.setUserHome(gameSavePath);
134 char configPath[PATH_MAX];
136 snprintf(configPath, sizeof configPath, "%sconfig", engine.userHomeDirectory);
138 debug(("Loading Config from %s\n", configPath));
140 FILE *fp = fopen(configPath, "rb");
147 if (fscanf(fp, "%10f %10d", &version, &release) != 2)
152 debug(("Version = %.2f - Expected %.2f\n", version, VERSION));
153 debug(("Release = %d - Expected %d\n", release, RELEASE));
155 if ((version != VERSION) && (release != RELEASE))
160 if (fscanf(fp, "%10d %10d %10d %10d %10d %10d %10d", &engine.fullScreen, &game.musicVol, &game.soundVol, &game.output, &game.brightness, &engine.extremeAvailable, &game.gore) != 7)
167 debug(("Extreme Mode = %d\n", engine.extremeAvailable));
168 debug(("Output Type = %d\n", game.output));
170 // Override audio if there is no sound available
171 if ((engine.useAudio) && (game.output))
173 engine.useAudio = game.output;
176 config.loadKeyConfig();
177 config.loadJoystickConfig();
184 char configPath[PATH_MAX];
186 snprintf(configPath, sizeof configPath, "%sconfig", engine.userHomeDirectory);
188 FILE *fp = fopen(configPath, "wb");
192 printf("Error Saving Config to %s\n", configPath);
196 fprintf(fp, "%f %d\n", VERSION, RELEASE);
197 fprintf(fp, "%d %d %d %d %d %d %d\n", engine.fullScreen, game.musicVol, game.soundVol, game.output, game.brightness, engine.extremeAvailable, game.gore);
201 debug(("Output Type = %d\n", game.output));
205 // see if we can load the private keyState
207 int initMedalService(void *data)
209 SDL_mutexP(medalServer.lock);
211 char connectMessage[1024];
212 snprintf(connectMessage, sizeof connectMessage, "Contacting Medal Server - %s:%d", MEDAL_SERVER_HOST, MEDAL_SERVER_PORT);
214 graphics.showMedalMessage(-1, connectMessage);
216 char keyPath[PATH_MAX];
219 snprintf(keyPath, sizeof keyPath, "%smedalKey", engine.userHomeDirectory);
221 debug(("Loading private key from %s\n", keyPath));
223 FILE *fp = fopen(keyPath, "rb");
227 graphics.showMedalMessage(-1, "No Medal Key found - Online functions disabled");
228 SDL_mutexV(medalServer.lock);
232 if (fscanf(fp, "%19s", privateKey) != 1)
234 graphics.showMedalMessage(-1, "Medal Key file corrupt - Online functions disabled");
235 SDL_mutexV(medalServer.lock);
242 if (!medalServer.connect(privateKey))
244 graphics.showMedalMessage(-1, "Medal Server Connection Failed - Online functions disabled");
248 graphics.showMedalMessage(-1, "Connected to Medal Server");
250 SDL_mutexV(medalServer.lock);
256 Chugg chugg chugg.... brrr... chugg chugg chugg...brrrrrr... chugg ch..
257 BRRRRRRRRRRRRRRRRRMMMMMMMMMMMMMMMMMMM!! Well, hopefully anyway! ;)
262 setupUserHomeDirectory();
265 bool displayLicense = loadConfig();
267 long flags = SDL_INIT_VIDEO|SDL_INIT_JOYSTICK;
271 flags |= SDL_INIT_AUDIO;
274 /* Initialize the SDL library */
275 if (SDL_Init(flags) < 0)
277 printf("Couldn't initialize SDL: %s\n", SDL_GetError());
281 if (!engine.fullScreen)
283 graphics.screen = SDL_SetVideoMode(640, 480, 0, SDL_DOUBLEBUF | SDL_HWPALETTE);
287 graphics.screen = SDL_SetVideoMode(640, 480, 0, SDL_DOUBLEBUF | SDL_HWPALETTE | SDL_FULLSCREEN);
290 if (graphics.screen == NULL)
292 printf("Couldn't set 640x480 video mode: %s\n", SDL_GetError());
296 // This (attempts to) set the gamma correction. We attempt to catch an error here
297 // in case someone has done something really stupid in the config file(!!)
298 if (game.brightness != -1) {
299 Math::limitInt(&game.brightness, 1, 20);
300 float brightness = game.brightness;
302 SDL_SetGamma(brightness, brightness, brightness);
307 printf("Couldn't initialize SDL TTF: %s\n", SDL_GetError());
313 if (Mix_OpenAudio(22050, AUDIO_S16, engine.useAudio, 1024) < 0)
315 printf("Warning: Couldn't set 44100 Hz 16-bit audio - Reason: %s\n", Mix_GetError());
316 printf("Sound and Music will be disabled\n");
321 debug(("Found %d Joysticks...\n", SDL_NumJoysticks()));
323 if (SDL_NumJoysticks() > 0)
325 debug(("Opening Joystick - %s...\n", SDL_JoystickName(0)));
326 SDL_JoystickEventState(SDL_ENABLE);
327 config.sdlJoystick = SDL_JoystickOpen(0);
330 SDL_ShowCursor(SDL_DISABLE);
331 SDL_EventState(SDL_MOUSEMOTION, SDL_DISABLE);
333 graphics.registerEngine(&engine);
334 graphics.mapColors();
336 audio.registerEngine(&engine);
337 audio.setSoundVolume(game.soundVol);
338 audio.setMusicVolume(game.musicVol);
339 audio.output = game.output;
341 debug(("Sound Volume = %d\n", game.soundVol));
342 debug(("Music Volume = %d\n", game.musicVol));
343 debug(("Output Type = %d\n", game.output));
344 debug(("Brightness = %d\n", game.brightness));
345 debug(("tmp dir = %s\n", engine.userHomeDirectory));
346 debug(("Pack Dir = %s\n", PAKLOCATION));
347 debug(("Loading Fonts...\n"));
351 char tempPath[PATH_MAX];
352 snprintf(tempPath, sizeof tempPath, "%sfont.ttf", engine.userHomeDirectory);
355 if (!engine.unpack("data/vera.ttf", PAK_FONT))
357 engine.reportFontFailure();
361 debug(("Trying to load correct font pixel sizes using a really half arsed routine!\n"));
362 debug(("If it crashes then you'll know why!\n"));
364 graphics.loadFont(0, "data/vera.ttf", 7);
365 graphics.loadFont(1, "data/vera.ttf", 9);
366 graphics.loadFont(2, "data/vera.ttf", 11);
367 graphics.loadFont(3, "data/vera.ttf", 13);
368 graphics.loadFont(4, "data/vera.ttf", 15);
370 debug(("Font sizes all loaded!!\n"));
372 audio.loadSound(SND_CHEAT, "sound/Lock And Load!!!");
373 audio.loadSound(SND_HIGHLIGHT, "sound/menu");
374 audio.loadSound(SND_SELECT, "sound/select");
376 graphics.medal[0] = graphics.loadImage("gfx/main/medal_bronze_1.png");
377 graphics.medal[1] = graphics.loadImage("gfx/main/award_star_silver_3.png");
378 graphics.medal[2] = graphics.loadImage("gfx/main/shield.png");
379 graphics.medal[3] = graphics.loadImage("gfx/main/ruby.png");
381 graphics.license[0] = graphics.loadImage("gfx/main/cc-by.png", true);
382 graphics.license[1] = graphics.loadImage("gfx/main/cc-by-sa.png", true);
384 SDL_Surface *device = graphics.loadImage("gfx/main/alienDevice.png");
386 #ifndef FRAMEWORK_SDL
387 SDL_WM_SetIcon(device, NULL);
389 SDL_WM_SetCaption("Blob Wars : Metal Blob Solid", "Blob Wars");
390 SDL_EnableKeyRepeat(350, 80);
392 SDL_FreeSurface(device);
394 if (strstr(engine.userHomeDirectory, "/root"))
396 graphics.showRootWarning();
408 if (SDLNet_Init() < 0)
410 printf("SDLNet_Init: %s\n", SDLNet_GetError());
414 SDL_Thread *thread = SDL_CreateThread(initMedalService, NULL);
418 printf("Unable to create thread: %s\n", SDL_GetError());
419 printf("Calling medal server directly\n");
420 initMedalService(NULL);
425 engine.saveConfig = true;
427 debug(("Init Complete...\n"));
431 Removes [hopefully] all the resources that has been
432 loaded and created during the game. This is called by
437 char tempPath[PATH_MAX];
439 debug(("Cleaning Up...\n"));
441 debug(("Updating Replay Data\n"));
442 replayData.header.score = game.score;
444 debug(("Freeing Audio...\n"));
447 debug(("Removing Music...\n"));
448 snprintf(tempPath, sizeof tempPath, "%smusic.mod", engine.userHomeDirectory);
451 debug(("Freeing Game Info...\n"));
454 debug(("Freeing Map Data...\n"));
457 debug(("Freeing Engine Data...\n"));
460 debug(("Freeing Graphics...\n"));
463 debug(("Saving Config...\n"));
464 if (engine.saveConfig)
469 debug(("Removing Font File...\n"));
470 snprintf(tempPath, sizeof tempPath, "%sfont.ttf", engine.userHomeDirectory);
473 if (SDL_NumJoysticks() > 0)
475 SDL_JoystickEventState(SDL_DISABLE);
476 for (int i = 0 ; i < SDL_NumJoysticks() ; i++)
478 debug(("Closing Joystick #%d - %s...\n", i, SDL_JoystickName(i)));
479 SDL_JoystickClose(config.sdlJoystick);
483 debug(("Closing Audio...\n"));
489 debug(("Closing TTF...\n"));
492 debug(("Closing NET...\n"));
495 debug(("Closing SDL Sub System...\n"));
498 debug(("All Done.\n"));
500 if (replayData.replayMode == REPLAY_MODE::PLAYBACK)
502 printf("\n==== ACTUAL PLAYBACK DATA ====\n");
503 replayData.printReplayInformation();