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 static bool displayLicense;
25 void checkForLicense()
27 if (!engine.loadData(_("data/license")))
29 graphics.showLicenseErrorAndExit();
34 Show the GNU Public License the first time the game is played. Waits 4 seconds
35 and then proceeds. THIS MUST NOT BE REMOVED!!!!!
39 SDL_FillRect(graphics.screen, NULL, graphics.black);
42 SDL_FillRect(graphics.screen, NULL, graphics.black);
43 SDL_Surface *pic = graphics.loadImage("gfx/main/licensePic.png");
44 graphics.blit(pic, 0, 0, graphics.screen, false);
52 char *token = strtok((char*)engine.dataBuffer, "\n");
56 sscanf(token, "%d %[^\n\r]", &y, line);
63 graphics.drawString(line, 320, y, true, graphics.screen);
65 token = strtok(NULL, "\n");
75 graphics.drawString(_("Press Space to Continue..."), 320, 440, true, graphics.screen);
82 graphics.updateScreen();
85 if (engine.keyState[SDL_SCANCODE_SPACE])
90 SDL_FillRect(graphics.screen, NULL, graphics.black);
95 void setupUserHomeDirectory()
97 char *userHome = getenv("HOME");
99 if ((!userHome) || (userHome == NULL))
101 printf("Couldn't determine user home directory! Exitting.\n");
105 debug(("User Home = %s\n", userHome));
110 snprintf(dir, sizeof dir, "%s/.parallelrealities", userHome);
111 if ((mkdir(dir, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) != 0) && (errno != EEXIST))
113 printf("Couldn't create required directory '%s'", dir);
117 snprintf(dir, sizeof dir, "%s/.parallelrealities/blobwars", userHome);
118 if ((mkdir(dir, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) != 0) && (errno != EEXIST))
120 printf("Couldn't create required directory '%s'", dir);
124 char gameSavePath[PATH_MAX];
125 snprintf(gameSavePath, sizeof gameSavePath, "%s/.parallelrealities/blobwars/", userHome);
126 engine.setUserHome(gameSavePath);
136 char configPath[PATH_MAX];
138 snprintf(configPath, sizeof configPath, "%sconfig", engine.userHomeDirectory);
140 debug(("Loading Config from %s\n", configPath));
142 FILE *fp = fopen(configPath, "rb");
149 if (fscanf(fp, "%10f %10d", &version, &release) != 2)
154 debug(("Version = %.2f - Expected %.2f\n", version, VERSION));
155 debug(("Release = %d - Expected %d\n", release, RELEASE));
157 if ((version != VERSION) && (release != RELEASE))
162 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)
169 debug(("Extreme Mode = %d\n", engine.extremeAvailable));
170 debug(("Output Type = %d\n", game.output));
172 // Override audio if there is no sound available
173 if ((engine.useAudio) && (game.output))
175 engine.useAudio = game.output;
178 config.loadKeyConfig();
179 config.loadJoystickConfig();
186 char configPath[PATH_MAX];
188 snprintf(configPath, sizeof configPath, "%sconfig", engine.userHomeDirectory);
190 FILE *fp = fopen(configPath, "wb");
194 printf("Error Saving Config to %s\n", configPath);
198 fprintf(fp, "%f %d\n", VERSION, RELEASE);
199 fprintf(fp, "%d %d %d %d %d %d %d\n", engine.fullScreen, game.musicVol, game.soundVol, game.output, game.brightness, engine.extremeAvailable, game.gore);
203 debug(("Output Type = %d\n", game.output));
207 // see if we can load the private keyState
209 int initMedalService(void *data)
211 SDL_mutexP(medalServer.lock);
213 char connectMessage[1024];
214 snprintf(connectMessage, sizeof connectMessage, "Contacting Medal Server - %s:%d", MEDAL_SERVER_HOST, MEDAL_SERVER_PORT);
216 graphics.showMedalMessage(-1, connectMessage);
218 char keyPath[PATH_MAX];
221 snprintf(keyPath, sizeof keyPath, "%smedalKey", engine.userHomeDirectory);
223 debug(("Loading private key from %s\n", keyPath));
225 FILE *fp = fopen(keyPath, "rb");
229 graphics.showMedalMessage(-1, "No Medal Key found - Online functions disabled");
230 SDL_mutexV(medalServer.lock);
234 if (fscanf(fp, "%19s", privateKey) != 1)
236 graphics.showMedalMessage(-1, "Medal Key file corrupt - Online functions disabled");
237 SDL_mutexV(medalServer.lock);
244 if (!medalServer.connect(privateKey))
246 graphics.showMedalMessage(-1, "Medal Server Connection Failed - Online functions disabled");
250 graphics.showMedalMessage(-1, "Connected to Medal Server");
252 SDL_mutexV(medalServer.lock);
260 setupUserHomeDirectory();
263 displayLicense = loadConfig();
267 Chugg chugg chugg.... brrr... chugg chugg chugg...brrrrrr... chugg ch..
268 BRRRRRRRRRRRRRRRRRMMMMMMMMMMMMMMMMMMM!! Well, hopefully anyway! ;)
272 long flags = SDL_INIT_VIDEO|SDL_INIT_JOYSTICK;
276 flags |= SDL_INIT_AUDIO;
279 /* Initialize the SDL library */
280 if (SDL_Init(flags) < 0)
282 printf("Couldn't initialize SDL: %s\n", SDL_GetError());
286 graphics.screen = SDL_CreateRGBSurface(0, 640, 480, 32, 0xff0000, 0xff00, 0xff, 0xff000000);
288 graphics.window = SDL_CreateWindow("Blobwars: Metal Blob Solid", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, graphics.screen->w, graphics.screen->h, 0);
289 graphics.renderer = SDL_CreateRenderer(graphics.window, -1, 0);
290 SDL_RenderSetLogicalSize(graphics.renderer, graphics.screen->w, graphics.screen->h);
291 graphics.texture = SDL_CreateTexture(graphics.renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, graphics.screen->w, graphics.screen->h);
293 if (graphics.window == NULL)
295 printf("Couldn't set 640x480 video mode: %s\n", SDL_GetError());
299 SDL_SetWindowFullscreen(graphics.window, engine.fullScreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
301 // This (attempts to) set the gamma correction. We attempt to catch an error here
302 // in case someone has done something really stupid in the config file(!!)
303 if (game.brightness != -1) {
304 Math::limitInt(&game.brightness, 1, 20);
305 float brightness = game.brightness;
308 SDL_CalculateGammaRamp(brightness, ramp);
309 SDL_SetWindowGammaRamp(graphics.window, ramp, ramp, ramp);
314 printf("Couldn't initialize SDL TTF: %s\n", SDL_GetError());
320 if (Mix_OpenAudio(22050, AUDIO_S16, engine.useAudio, 1024) < 0)
322 printf("Warning: Couldn't set 44100 Hz 16-bit audio - Reason: %s\n", Mix_GetError());
323 printf("Sound and Music will be disabled\n");
328 debug(("Found %d Joysticks...\n", SDL_NumJoysticks()));
330 if (SDL_NumJoysticks() > 0)
332 debug(("Opening Joystick - %s...\n", SDL_JoystickName(0)));
333 SDL_JoystickEventState(SDL_ENABLE);
334 config.sdlJoystick = SDL_JoystickOpen(0);
337 SDL_ShowCursor(SDL_DISABLE);
338 SDL_EventState(SDL_MOUSEMOTION, SDL_DISABLE);
340 graphics.registerEngine(&engine);
341 graphics.mapColors();
343 audio.registerEngine(&engine);
344 audio.setSoundVolume(game.soundVol);
345 audio.setMusicVolume(game.musicVol);
346 audio.output = game.output;
348 debug(("Sound Volume = %d\n", game.soundVol));
349 debug(("Music Volume = %d\n", game.musicVol));
350 debug(("Output Type = %d\n", game.output));
351 debug(("Brightness = %d\n", game.brightness));
352 debug(("tmp dir = %s\n", engine.userHomeDirectory));
353 debug(("Pack Dir = %s\n", PAKLOCATION));
354 debug(("Loading Fonts...\n"));
358 char tempPath[PATH_MAX];
359 snprintf(tempPath, sizeof tempPath, "%sfont.ttf", engine.userHomeDirectory);
362 if (!engine.unpack("data/vera.ttf", PAK_FONT))
364 engine.reportFontFailure();
368 debug(("Trying to load correct font pixel sizes using a really half arsed routine!\n"));
369 debug(("If it crashes then you'll know why!\n"));
371 graphics.loadFont(0, "data/vera.ttf", 7);
372 graphics.loadFont(1, "data/vera.ttf", 9);
373 graphics.loadFont(2, "data/vera.ttf", 11);
374 graphics.loadFont(3, "data/vera.ttf", 13);
375 graphics.loadFont(4, "data/vera.ttf", 15);
377 debug(("Font sizes all loaded!!\n"));
379 audio.loadSound(SND_CHEAT, "sound/Lock And Load!!!");
380 audio.loadSound(SND_HIGHLIGHT, "sound/menu");
381 audio.loadSound(SND_SELECT, "sound/select");
383 graphics.medal[0] = graphics.loadImage("gfx/main/medal_bronze_1.png");
384 graphics.medal[1] = graphics.loadImage("gfx/main/award_star_silver_3.png");
385 graphics.medal[2] = graphics.loadImage("gfx/main/shield.png");
386 graphics.medal[3] = graphics.loadImage("gfx/main/ruby.png");
388 graphics.license[0] = graphics.loadImage("gfx/main/cc-by.png", true);
389 graphics.license[1] = graphics.loadImage("gfx/main/cc-by-sa.png", true);
391 SDL_Surface *device = graphics.loadImage("gfx/main/alienDevice.png");
393 SDL_FreeSurface(device);
395 if (strstr(engine.userHomeDirectory, "/root"))
397 graphics.showRootWarning();
409 if (SDLNet_Init() < 0)
411 printf("SDLNet_Init: %s\n", SDLNet_GetError());
415 SDL_Thread *thread = SDL_CreateThread(initMedalService, "MedalService", NULL);
419 printf("Unable to create thread: %s\n", SDL_GetError());
420 printf("Calling medal server directly\n");
421 initMedalService(NULL);
426 engine.saveConfig = true;
428 debug(("Init Complete...\n"));
432 Removes [hopefully] all the resources that has been
433 loaded and created during the game. This is called by
438 char tempPath[PATH_MAX];
440 debug(("Cleaning Up...\n"));
442 debug(("Updating Replay Data\n"));
443 replayData.header.score = game.score;
445 debug(("Freeing Audio...\n"));
448 debug(("Removing Music...\n"));
449 snprintf(tempPath, sizeof tempPath, "%smusic.mod", engine.userHomeDirectory);
452 debug(("Freeing Game Info...\n"));
455 debug(("Freeing Map Data...\n"));
458 debug(("Freeing Engine Data...\n"));
461 debug(("Freeing Graphics...\n"));
464 debug(("Saving Config...\n"));
465 if (engine.saveConfig)
470 debug(("Removing Font File...\n"));
471 snprintf(tempPath, sizeof tempPath, "%sfont.ttf", engine.userHomeDirectory);
474 if (SDL_NumJoysticks() > 0)
476 SDL_JoystickEventState(SDL_DISABLE);
477 for (int i = 0 ; i < SDL_NumJoysticks() ; i++)
479 debug(("Closing Joystick #%d\n", i));
480 SDL_JoystickClose(config.sdlJoystick);
484 debug(("Closing Audio...\n"));
490 debug(("Closing TTF...\n"));
493 debug(("Closing NET...\n"));
496 debug(("Closing SDL Sub System...\n"));
499 debug(("All Done.\n"));
501 if (replayData.replayMode == REPLAY_MODE::PLAYBACK)
503 printf("\n==== ACTUAL PLAYBACK DATA ====\n");
504 replayData.printReplayInformation();