Work around a NULL deref in SDL_SetGammaRamp on MinGW
[blobwars-mingw.git] / src / init.cpp
blobdff77e1aa588739a54e695dde3a7fcee6055a7c8
1 /*
2 Copyright (C) 2004 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.
21 #include "init.h"
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!!!!!
35 void showLicense()
37 SDL_FillRect(graphics.screen, NULL, graphics.black);
38 graphics.delay(1000);
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);
45 checkForLicense();
47 char line[255];
48 int y = 0;
50 char *token = strtok((char*)engine.dataBuffer, "\n");
52 while (true)
54 sscanf(token, "%d %[^\n\r]", &y, line);
56 if (y == -1)
58 break;
61 graphics.drawString(line, 320, y, true, graphics.screen);
63 token = strtok(NULL, "\n");
65 if (token == NULL)
67 break;
71 graphics.delay(4000);
73 graphics.drawString(_("Press Space to Continue..."), 320, 440, true, graphics.screen);
75 engine.flushInput();
76 engine.clearInput();
78 while (true)
80 graphics.updateScreen();
81 engine.getInput();
82 config.populate();
83 if (engine.keyState[SDLK_SPACE])
84 break;
85 SDL_Delay(16);
88 SDL_FillRect(graphics.screen, NULL, graphics.black);
89 graphics.delay(1000);
93 This bit is just for Linux and Unix users. It attempts to get the user's
94 home directory, then creates the .parallelrealities and .parallelrealities/blobwars
95 directories so that saves and temporary data files can be written there. Good, eh? :)
97 #if UNIX
98 void setupUserHomeDirectory()
100 char *userHome;
102 char *name = getlogin();
104 passwd *pass;
106 if (name != NULL)
108 pass = getpwnam(name);
110 else
112 pass = getpwuid(geteuid());
115 if (pass == NULL)
117 printf("Couldn't determine the user home directory. Exitting.\n");
118 exit(1);
121 userHome = pass->pw_dir;
123 debug(("User Home = %s\n", userHome));
125 char dir[PATH_MAX];
126 strcpy(dir, "");
128 sprintf(dir, "%s/.parallelrealities", userHome);
129 if ((mkdir(dir, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) != 0) && (errno != EEXIST))
131 printf("Couldn't create required directory '%s'", dir);
132 exit(1);
135 sprintf(dir, "%s/.parallelrealities/blobwars", userHome);
136 if ((mkdir(dir, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) != 0) && (errno != EEXIST))
138 printf("Couldn't create required directory '%s'", dir);
139 exit(1);
142 char gameSavePath[PATH_MAX];
143 sprintf(gameSavePath, "%s/.parallelrealities/blobwars/", userHome);
144 engine.setUserHome(gameSavePath);
147 #endif
149 bool loadConfig()
151 float version = 0;
152 int release = 0;
153 bool rtn = false;
155 char configPath[PATH_MAX];
157 sprintf(configPath, "%sconfig", engine.userHomeDirectory);
159 debug(("Loading Config from %s\n", configPath));
161 FILE *fp = fopen(configPath, "rb");
163 if (!fp)
165 return true;
168 fscanf(fp, "%f %d", &version, &release);
170 debug(("Version = %.2f - Expected %.2f\n", version, VERSION));
171 debug(("Release = %d - Expected %d\n", release, RELEASE));
173 if ((version != VERSION) && (release != RELEASE))
175 rtn = true;
178 fscanf(fp, "%d %d %d %d %d %d %d", &engine.fullScreen, &game.musicVol, &game.soundVol, &game.output, &game.brightness, &engine.extremeAvailable, &game.gore);
180 fclose(fp);
182 debug(("Extreme Mode = %d\n", engine.extremeAvailable));
183 debug(("Output Type = %d\n", game.output));
185 // Override audio if there is no sound available
186 if ((engine.useAudio) && (game.output))
188 engine.useAudio = game.output;
191 config.loadKeyConfig();
192 config.loadJoystickConfig();
194 return rtn;
197 void saveConfig()
199 char configPath[PATH_MAX];
201 sprintf(configPath, "%sconfig", engine.userHomeDirectory);
203 FILE *fp = fopen(configPath, "wb");
205 if (!fp)
207 printf("Error Saving Config to %s\n", configPath);
208 return;
211 fprintf(fp, "%f %d\n", VERSION, RELEASE);
212 fprintf(fp, "%d %d %d %d %d %d %d\n", engine.fullScreen, game.musicVol, game.soundVol, game.output, game.brightness, engine.extremeAvailable, game.gore);
214 fclose(fp);
216 debug(("Output Type = %d\n", game.output));
220 Chugg chugg chugg.... brrr... chugg chugg chugg...brrrrrr... chugg ch..
221 BRRRRRRRRRRRRRRRRRMMMMMMMMMMMMMMMMMMM!! Well, hopefully anyway! ;)
223 void initSystem()
225 #if UNIX
226 setupUserHomeDirectory();
227 #endif
229 bool displayLicense = loadConfig();
231 long flags = SDL_INIT_VIDEO|SDL_INIT_JOYSTICK;
233 if (engine.useAudio)
235 flags |= SDL_INIT_AUDIO;
238 /* Initialize the SDL library */
239 if (SDL_Init(flags) < 0)
241 printf("Couldn't initialize SDL: %s\n", SDL_GetError());
242 exit(1);
245 if (!engine.fullScreen)
247 graphics.screen = SDL_SetVideoMode(640, 480, 0, SDL_HWPALETTE);
249 else
251 graphics.screen = SDL_SetVideoMode(640, 480, 0, SDL_HWPALETTE | SDL_FULLSCREEN);
254 if (graphics.screen == NULL)
256 printf("Couldn't set 640x480 video mode: %s\n", SDL_GetError());
257 exit(1);
260 // This (attempts to) set the gamma correction. We attempt to catch an error here
261 // in case someone has done something really stupid in the config file(!!)
262 if (game.brightness != -1) {
263 Math::limitInt(&game.brightness, 1, 20);
264 float brightness = game.brightness;
265 brightness /= 10;
266 #if SDL_SETGAMMARAMP_CHECKS_FOR_A_NULL_PALETTE
267 SDL_SetGamma(brightness, brightness, brightness);
268 #endif
271 if (TTF_Init() < 0)
273 printf("Couldn't initialize SDL TTF: %s\n", SDL_GetError());
274 exit(1);
277 if (engine.useAudio)
279 if (Mix_OpenAudio(22050, AUDIO_S16, engine.useAudio, 1024) < 0)
281 printf("Warning: Couldn't set 22050 Hz 16-bit audio - Reason: %s\n", Mix_GetError());
282 printf("Sound and Music will be disabled\n");
283 engine.useAudio = 0;
287 debug(("Found %d Joysticks...\n", SDL_NumJoysticks()));
289 if (SDL_NumJoysticks() > 0)
291 debug(("Opening Joystick - %s...\n", SDL_JoystickName(0)));
292 SDL_JoystickEventState(SDL_ENABLE);
293 config.sdlJoystick = SDL_JoystickOpen(0);
296 SDL_ShowCursor(SDL_DISABLE);
297 SDL_EventState(SDL_MOUSEMOTION, SDL_DISABLE);
299 graphics.registerEngine(&engine);
300 graphics.mapColors();
302 audio.registerEngine(&engine);
303 audio.setSoundVolume(game.soundVol);
304 audio.setMusicVolume(game.musicVol);
305 audio.output = game.output;
307 debug(("Sound Volume = %d\n", game.soundVol));
308 debug(("Music Volume = %d\n", game.musicVol));
309 debug(("Output Type = %d\n", game.output));
310 debug(("Brightness = %d\n", game.brightness));
311 debug(("tmp dir = %s\n", engine.userHomeDirectory));
312 debug(("Pack Dir = %s\n", PAKLOCATION));
313 debug(("Loading Fonts...\n"));
315 #if USEPAK
317 char tempPath[PATH_MAX];
318 sprintf(tempPath, "%sfont.ttf", engine.userHomeDirectory);
319 remove(tempPath);
321 SDL_Delay(1000); // wait one second, just to be sure!
323 if (!engine.unpack("data/vera.ttf", PAK_FONT))
325 engine.reportFontFailure();
327 #endif
329 debug(("Trying to load correct font pixel sizes using a really half arsed routine!\n"));
330 debug(("If it crashes then you'll know why!\n"));
332 graphics.loadFont(0, "data/vera.ttf", 7);
333 graphics.loadFont(1, "data/vera.ttf", 9);
334 graphics.loadFont(2, "data/vera.ttf", 11);
335 graphics.loadFont(3, "data/vera.ttf", 13);
336 graphics.loadFont(4, "data/vera.ttf", 15);
338 debug(("Font sizes all loaded!!\n"));
340 audio.loadSound(SND_CHEAT, "sound/Lock And Load!!!");
341 audio.loadSound(SND_HIGHLIGHT, "sound/menu.wav");
342 audio.loadSound(SND_SELECT, "sound/select.wav");
344 SDL_Surface *device = graphics.loadImage("gfx/main/alienDevice.png");
346 #ifndef SDL_FRAMEWORK
347 SDL_WM_SetIcon(device, NULL);
348 #endif
349 SDL_WM_SetCaption("Blob Wars : Metal Blob Solid", "Blob Wars");
350 SDL_EnableKeyRepeat(350, 80);
352 SDL_FreeSurface(device);
354 if (strstr(engine.userHomeDirectory, "/root"))
356 graphics.showRootWarning();
359 if (displayLicense)
361 showLicense();
363 else
365 checkForLicense();
368 engine.saveConfig = true;
370 debug(("Init Complete...\n"));
374 Removes [hopefully] all the resources that has been
375 loaded and created during the game. This is called by
376 atexit();
378 void cleanup()
380 char tempPath[PATH_MAX];
382 debug(("Cleaning Up...\n"));
384 debug(("Updating Replay Data"));
385 replayData.header.score = game.score;
387 debug(("Freeing Audio...\n"));
388 audio.destroy();
390 debug(("Removing Music...\n"));
391 sprintf(tempPath, "%smusic.mod", engine.userHomeDirectory);
392 remove(tempPath);
394 debug(("Freeing Game Info...\n"));
395 game.destroy();
397 debug(("Freeing Map Data...\n"));
398 map.destroy();
400 debug(("Freeing Engine Data...\n"));
401 engine.destroy();
403 debug(("Freeing Graphics...\n"));
404 graphics.destroy();
406 debug(("Saving Config...\n"));
407 if (engine.saveConfig)
409 saveConfig();
412 debug(("Removing Font File...\n"));
413 sprintf(tempPath, "%sfont.ttf", engine.userHomeDirectory);
414 remove(tempPath);
416 if (SDL_NumJoysticks() > 0)
418 SDL_JoystickEventState(SDL_DISABLE);
419 for (int i = 0 ; i < SDL_NumJoysticks() ; i++)
421 debug(("Closing Joystick #%d - %s...\n", i, SDL_JoystickName(i)));
422 SDL_JoystickClose(config.sdlJoystick);
426 debug(("Closing Audio...\n"));
427 if (engine.useAudio)
429 Mix_CloseAudio();
432 debug(("Closing TTF...\n"));
433 TTF_Quit();
435 debug(("Closing SDL Sub System...\n"));
436 SDL_Quit();
438 debug(("All Done.\n"));
440 if (replayData.replayMode == REPLAY_MODE::PLAYBACK)
442 printf("\n==== ACTUAL PLAYBACK DATA ====\n");
443 replayData.printReplayInformation();