2 Copyright (C) 2004-2011 Parallel Realities
3 Copyright (C) 2011-2015 Perpendicular Dimensions
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 static bool displayLicense;
26 void checkForLicense()
28 if (!engine.loadData(_("data/license")))
30 graphics.showLicenseErrorAndExit();
35 Show the GNU Public License the first time the game is played. Waits 4 seconds
36 and then proceeds. THIS MUST NOT BE REMOVED!!!!!
40 SDL_FillRect(graphics.screen, NULL, graphics.black);
43 SDL_FillRect(graphics.screen, NULL, graphics.black);
44 SDL_Surface *pic = graphics.loadImage("gfx/main/licensePic.png");
45 graphics.blit(pic, 0, 0, graphics.screen, false);
53 char *token = strtok((char*)engine.dataBuffer, "\n");
57 sscanf(token, "%d %[^\n\r]", &y, line);
64 graphics.drawString(line, 320, y, true, graphics.screen);
66 token = strtok(NULL, "\n");
76 graphics.drawString(_("Press Space to Continue..."), 320, 440, true, graphics.screen);
83 graphics.updateScreen();
86 if (engine.userAccepts())
91 SDL_FillRect(graphics.screen, NULL, graphics.black);
96 void setupUserHomeDirectory()
98 char *userHome = getenv("HOME");
100 if ((!userHome) || (userHome == NULL))
102 printf("Couldn't determine user home directory! Exitting.\n");
106 debug(("User Home = %s\n", userHome));
111 snprintf(dir, sizeof dir, "%s/.parallelrealities", userHome);
112 if ((mkdir(dir, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) != 0) && (errno != EEXIST))
114 printf("Couldn't create required directory '%s'", dir);
118 snprintf(dir, sizeof dir, "%s/.parallelrealities/blobwars", userHome);
119 if ((mkdir(dir, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) != 0) && (errno != EEXIST))
121 printf("Couldn't create required directory '%s'", dir);
125 char gameSavePath[PATH_MAX];
126 snprintf(gameSavePath, sizeof gameSavePath, "%s/.parallelrealities/blobwars/", userHome);
127 engine.setUserHome(gameSavePath);
137 char configPath[PATH_MAX];
139 snprintf(configPath, sizeof configPath, "%sconfig", engine.userHomeDirectory);
141 debug(("Loading Config from %s\n", configPath));
143 FILE *fp = fopen(configPath, "rb");
150 if (fscanf(fp, "%10f %10d", &version, &release) != 2)
155 debug(("Version = %.2f - Expected %.2f\n", version, VERSION));
156 debug(("Release = %d - Expected %d\n", release, RELEASE));
158 if ((version != VERSION) && (release != RELEASE))
163 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)
170 debug(("Extreme Mode = %d\n", engine.extremeAvailable));
171 debug(("Output Type = %d\n", game.output));
173 // Override audio if there is no sound available
174 if ((engine.useAudio) && (game.output))
176 engine.useAudio = game.output;
179 config.loadKeyConfig();
180 config.loadJoystickConfig();
187 char configPath[PATH_MAX];
189 snprintf(configPath, sizeof configPath, "%sconfig", engine.userHomeDirectory);
191 FILE *fp = fopen(configPath, "wb");
195 printf("Error Saving Config to %s\n", configPath);
199 fprintf(fp, "%f %d\n", VERSION, RELEASE);
200 fprintf(fp, "%d %d %d %d %d %d %d\n", engine.fullScreen, game.musicVol, game.soundVol, game.output, game.brightness, engine.extremeAvailable, game.gore);
204 debug(("Output Type = %d\n", game.output));
208 // see if we can load the private keyState
210 int initMedalService(void *data)
212 SDL_mutexP(medalServer.lock);
214 char connectMessage[1024];
215 snprintf(connectMessage, sizeof connectMessage, "Contacting Medal Server - %s:%d", MEDAL_SERVER_HOST, MEDAL_SERVER_PORT);
217 graphics.showMedalMessage(-1, connectMessage);
219 char keyPath[PATH_MAX];
222 snprintf(keyPath, sizeof keyPath, "%smedalKey", engine.userHomeDirectory);
224 debug(("Loading private key from %s\n", keyPath));
226 FILE *fp = fopen(keyPath, "rb");
230 graphics.showMedalMessage(-1, "No Medal Key found - Online functions disabled");
231 SDL_mutexV(medalServer.lock);
235 if (fscanf(fp, "%19s", privateKey) != 1)
237 graphics.showMedalMessage(-1, "Medal Key file corrupt - Online functions disabled");
238 SDL_mutexV(medalServer.lock);
245 if (!medalServer.connect(privateKey))
247 graphics.showMedalMessage(-1, "Medal Server Connection Failed - Online functions disabled");
251 graphics.showMedalMessage(-1, "Connected to Medal Server");
253 SDL_mutexV(medalServer.lock);
261 setupUserHomeDirectory();
264 displayLicense = loadConfig();
268 Chugg chugg chugg.... brrr... chugg chugg chugg...brrrrrr... chugg ch..
269 BRRRRRRRRRRRRRRRRRMMMMMMMMMMMMMMMMMMM!! Well, hopefully anyway! ;)
273 long flags = SDL_INIT_VIDEO|SDL_INIT_JOYSTICK;
277 flags |= SDL_INIT_AUDIO;
280 /* Initialize the SDL library */
281 if (SDL_Init(flags) < 0)
283 printf("Couldn't initialize SDL: %s\n", SDL_GetError());
287 graphics.screen = SDL_CreateRGBSurface(0, 640, 480, 32, 0xff0000, 0xff00, 0xff, 0xff000000);
289 graphics.window = SDL_CreateWindow("Blobwars: Metal Blob Solid", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, graphics.screen->w, graphics.screen->h, 0);
290 graphics.renderer = SDL_CreateRenderer(graphics.window, -1, 0);
291 SDL_RenderSetLogicalSize(graphics.renderer, graphics.screen->w, graphics.screen->h);
292 graphics.texture = SDL_CreateTexture(graphics.renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, graphics.screen->w, graphics.screen->h);
294 if (graphics.window == NULL)
296 printf("Couldn't set 640x480 video mode: %s\n", SDL_GetError());
300 SDL_SetWindowFullscreen(graphics.window, engine.fullScreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
302 // This (attempts to) set the gamma correction. We attempt to catch an error here
303 // in case someone has done something really stupid in the config file(!!)
304 if (game.brightness != -1) {
305 Math::limitInt(&game.brightness, 1, 20);
306 float brightness = game.brightness;
309 SDL_CalculateGammaRamp(brightness, ramp);
310 SDL_SetWindowGammaRamp(graphics.window, ramp, ramp, ramp);
315 printf("Couldn't initialize SDL TTF: %s\n", SDL_GetError());
321 if (Mix_OpenAudio(22050, AUDIO_S16, engine.useAudio, 1024) < 0)
323 printf("Warning: Couldn't set 44100 Hz 16-bit audio - Reason: %s\n", Mix_GetError());
324 printf("Sound and Music will be disabled\n");
329 debug(("Found %d Joysticks...\n", SDL_NumJoysticks()));
331 if (SDL_NumJoysticks() > 0)
333 debug(("Opening Joystick - %s...\n", SDL_JoystickName(0)));
334 SDL_JoystickEventState(SDL_ENABLE);
335 config.sdlJoystick = SDL_JoystickOpen(0);
338 SDL_ShowCursor(SDL_DISABLE);
339 SDL_EventState(SDL_MOUSEMOTION, SDL_DISABLE);
341 graphics.registerEngine(&engine);
342 graphics.mapColors();
344 audio.registerEngine(&engine);
345 audio.setSoundVolume(game.soundVol);
346 audio.setMusicVolume(game.musicVol);
347 audio.output = game.output;
349 debug(("Sound Volume = %d\n", game.soundVol));
350 debug(("Music Volume = %d\n", game.musicVol));
351 debug(("Output Type = %d\n", game.output));
352 debug(("Brightness = %d\n", game.brightness));
353 debug(("tmp dir = %s\n", engine.userHomeDirectory));
354 debug(("Pack Dir = %s\n", PAKLOCATION));
355 debug(("Loading Fonts...\n"));
359 char tempPath[PATH_MAX];
360 snprintf(tempPath, sizeof tempPath, "%sfont.ttf", engine.userHomeDirectory);
363 if (!engine.unpack("data/vera.ttf", PAK_FONT))
365 engine.reportFontFailure();
369 debug(("Trying to load correct font pixel sizes using a really half arsed routine!\n"));
370 debug(("If it crashes then you'll know why!\n"));
372 graphics.loadFont(0, "data/vera.ttf", 7);
373 graphics.loadFont(1, "data/vera.ttf", 9);
374 graphics.loadFont(2, "data/vera.ttf", 11);
375 graphics.loadFont(3, "data/vera.ttf", 13);
376 graphics.loadFont(4, "data/vera.ttf", 15);
378 debug(("Font sizes all loaded!!\n"));
380 audio.loadSound(SND_CHEAT, "sound/Lock And Load!!!");
381 audio.loadSound(SND_HIGHLIGHT, "sound/menu");
382 audio.loadSound(SND_SELECT, "sound/select");
384 graphics.medal[0] = graphics.loadImage("gfx/main/medal_bronze_1.png");
385 graphics.medal[1] = graphics.loadImage("gfx/main/award_star_silver_3.png");
386 graphics.medal[2] = graphics.loadImage("gfx/main/shield.png");
387 graphics.medal[3] = graphics.loadImage("gfx/main/ruby.png");
389 graphics.license[0] = graphics.loadImage("gfx/main/cc-by.png", true);
390 graphics.license[1] = graphics.loadImage("gfx/main/cc-by-sa.png", true);
392 SDL_Surface *device = graphics.loadImage("gfx/main/alienDevice.png");
394 SDL_FreeSurface(device);
396 if (strstr(engine.userHomeDirectory, "/root"))
398 graphics.showRootWarning();
410 if (SDLNet_Init() < 0)
412 printf("SDLNet_Init: %s\n", SDLNet_GetError());
416 SDL_Thread *thread = SDL_CreateThread(initMedalService, "MedalService", NULL);
420 printf("Unable to create thread: %s\n", SDL_GetError());
421 printf("Calling medal server directly\n");
422 initMedalService(NULL);
427 engine.saveConfig = true;
429 debug(("Init Complete...\n"));
433 Removes [hopefully] all the resources that has been
434 loaded and created during the game. This is called by
439 char tempPath[PATH_MAX];
441 debug(("Cleaning Up...\n"));
443 debug(("Updating Replay Data\n"));
444 replayData.header.score = game.score;
446 debug(("Freeing Audio...\n"));
449 debug(("Removing Music...\n"));
450 snprintf(tempPath, sizeof tempPath, "%smusic.mod", engine.userHomeDirectory);
453 debug(("Freeing Game Info...\n"));
456 debug(("Freeing Map Data...\n"));
459 debug(("Freeing Engine Data...\n"));
462 debug(("Freeing Graphics...\n"));
465 debug(("Saving Config...\n"));
466 if (engine.saveConfig)
471 debug(("Removing Font File...\n"));
472 snprintf(tempPath, sizeof tempPath, "%sfont.ttf", engine.userHomeDirectory);
475 if (SDL_NumJoysticks() > 0)
477 SDL_JoystickEventState(SDL_DISABLE);
478 for (int i = 0 ; i < SDL_NumJoysticks() ; i++)
480 debug(("Closing Joystick #%d\n", i));
481 SDL_JoystickClose(config.sdlJoystick);
485 debug(("Closing Audio...\n"));
491 debug(("Closing TTF...\n"));
494 debug(("Closing NET...\n"));
497 debug(("Closing SDL Sub System...\n"));
500 debug(("All Done.\n"));
502 if (replayData.replayMode == REPLAY_MODE::PLAYBACK)
504 printf("\n==== ACTUAL PLAYBACK DATA ====\n");
505 replayData.printReplayInformation();