2 Copyright (C) 2004-2010 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 fscanf(fp, "%10f %10d", &version, &release);
149 debug(("Version = %.2f - Expected %.2f\n", version, VERSION));
150 debug(("Release = %d - Expected %d\n", release, RELEASE));
152 if ((version != VERSION) && (release != RELEASE))
157 fscanf(fp, "%10d %10d %10d %10d %10d %10d %10d", &engine.fullScreen, &game.musicVol, &game.soundVol, &game.output, &game.brightness, &engine.extremeAvailable, &game.gore);
161 debug(("Extreme Mode = %d\n", engine.extremeAvailable));
162 debug(("Output Type = %d\n", game.output));
164 // Override audio if there is no sound available
165 if ((engine.useAudio) && (game.output))
167 engine.useAudio = game.output;
170 config.loadKeyConfig();
171 config.loadJoystickConfig();
178 char configPath[PATH_MAX];
180 snprintf(configPath, sizeof configPath, "%sconfig", engine.userHomeDirectory);
182 FILE *fp = fopen(configPath, "wb");
186 printf("Error Saving Config to %s\n", configPath);
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);
195 debug(("Output Type = %d\n", game.output));
199 // see if we can load the private keyState
201 int initMedalService(void *data)
203 SDL_mutexP(medalServer.lock);
205 char connectMessage[1024];
206 snprintf(connectMessage, sizeof connectMessage, "Contacting Medal Server - %s:%d", MEDAL_SERVER_HOST, MEDAL_SERVER_PORT);
208 graphics.showMedalMessage(-1, connectMessage);
210 char keyPath[PATH_MAX];
213 snprintf(keyPath, sizeof keyPath, "%smedalKey", engine.userHomeDirectory);
215 debug(("Loading private key from %s\n", keyPath));
217 FILE *fp = fopen(keyPath, "rb");
221 graphics.showMedalMessage(-1, "No Medal Key found - Online functions disabled");
222 SDL_mutexV(medalServer.lock);
226 fscanf(fp, "%19s", privateKey);
230 if (!medalServer.connect(privateKey))
232 graphics.showMedalMessage(-1, "Medal Server Connection Failed - Online functions disabled");
236 graphics.showMedalMessage(-1, "Connected to Medal Server");
238 SDL_mutexV(medalServer.lock);
244 Chugg chugg chugg.... brrr... chugg chugg chugg...brrrrrr... chugg ch..
245 BRRRRRRRRRRRRRRRRRMMMMMMMMMMMMMMMMMMM!! Well, hopefully anyway! ;)
250 setupUserHomeDirectory();
253 bool displayLicense = loadConfig();
255 long flags = SDL_INIT_VIDEO|SDL_INIT_JOYSTICK;
259 flags |= SDL_INIT_AUDIO;
262 /* Initialize the SDL library */
263 if (SDL_Init(flags) < 0)
265 printf("Couldn't initialize SDL: %s\n", SDL_GetError());
269 if (!engine.fullScreen)
271 graphics.screen = SDL_SetVideoMode(640, 480, 0, SDL_HWPALETTE);
275 graphics.screen = SDL_SetVideoMode(640, 480, 0, SDL_HWPALETTE | SDL_FULLSCREEN);
278 if (graphics.screen == NULL)
280 printf("Couldn't set 640x480 video mode: %s\n", SDL_GetError());
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;
290 SDL_SetGamma(brightness, brightness, brightness);
295 printf("Couldn't initialize SDL TTF: %s\n", SDL_GetError());
301 if (Mix_OpenAudio(22050, AUDIO_S16, engine.useAudio, 1024) < 0)
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");
309 debug(("Found %d Joysticks...\n", SDL_NumJoysticks()));
311 if (SDL_NumJoysticks() > 0)
313 debug(("Opening Joystick - %s...\n", SDL_JoystickName(0)));
314 SDL_JoystickEventState(SDL_ENABLE);
315 config.sdlJoystick = SDL_JoystickOpen(0);
318 SDL_ShowCursor(SDL_DISABLE);
319 SDL_EventState(SDL_MOUSEMOTION, SDL_DISABLE);
321 graphics.registerEngine(&engine);
322 graphics.mapColors();
324 audio.registerEngine(&engine);
325 audio.setSoundVolume(game.soundVol);
326 audio.setMusicVolume(game.musicVol);
327 audio.output = game.output;
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"));
339 char tempPath[PATH_MAX];
340 snprintf(tempPath, sizeof tempPath, "%sfont.ttf", engine.userHomeDirectory);
343 SDL_Delay(1000); // wait one second, just to be sure!
345 if (!engine.unpack("data/vera.ttf", PAK_FONT))
347 engine.reportFontFailure();
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"));
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);
360 debug(("Font sizes all loaded!!\n"));
362 audio.loadSound(SND_CHEAT, "sound/Lock And Load!!!");
363 audio.loadSound(SND_HIGHLIGHT, "sound/menu");
364 audio.loadSound(SND_SELECT, "sound/select");
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");
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);
374 SDL_Surface *device = graphics.loadImage("gfx/main/alienDevice.png");
376 #ifndef SDL_FRAMEWORK
377 SDL_WM_SetIcon(device, NULL);
379 SDL_WM_SetCaption("Blob Wars : Metal Blob Solid", "Blob Wars");
380 SDL_EnableKeyRepeat(350, 80);
382 SDL_FreeSurface(device);
384 if (strstr(engine.userHomeDirectory, "/root"))
386 graphics.showRootWarning();
398 if (SDLNet_Init() < 0)
400 printf("SDLNet_Init: %s\n", SDLNet_GetError());
404 SDL_Thread *thread = SDL_CreateThread(initMedalService, NULL);
408 printf("Unable to create thread: %s\n", SDL_GetError());
409 printf("Calling medal server directly\n");
410 initMedalService(NULL);
415 engine.saveConfig = true;
417 debug(("Init Complete...\n"));
421 Removes [hopefully] all the resources that has been
422 loaded and created during the game. This is called by
427 char tempPath[PATH_MAX];
429 debug(("Cleaning Up...\n"));
431 debug(("Updating Replay Data\n"));
432 replayData.header.score = game.score;
434 debug(("Freeing Audio...\n"));
437 debug(("Removing Music...\n"));
438 snprintf(tempPath, sizeof tempPath, "%smusic.mod", engine.userHomeDirectory);
441 debug(("Freeing Game Info...\n"));
444 debug(("Freeing Map Data...\n"));
447 debug(("Freeing Engine Data...\n"));
450 debug(("Freeing Graphics...\n"));
453 debug(("Saving Config...\n"));
454 if (engine.saveConfig)
459 debug(("Removing Font File...\n"));
460 snprintf(tempPath, sizeof tempPath, "%sfont.ttf", engine.userHomeDirectory);
463 if (SDL_NumJoysticks() > 0)
465 SDL_JoystickEventState(SDL_DISABLE);
466 for (int i = 0 ; i < SDL_NumJoysticks() ; i++)
468 debug(("Closing Joystick #%d - %s...\n", i, SDL_JoystickName(i)));
469 SDL_JoystickClose(config.sdlJoystick);
473 debug(("Closing Audio...\n"));
479 debug(("Closing TTF...\n"));
482 debug(("Closing NET...\n"));
485 debug(("Closing SDL Sub System...\n"));
488 debug(("All Done.\n"));
490 if (replayData.replayMode == REPLAY_MODE::PLAYBACK)
492 printf("\n==== ACTUAL PLAYBACK DATA ====\n");
493 replayData.printReplayInformation();