]> git.mxchange.org Git - quix0rs-blobwars.git/blobdiff - src/init.cpp
Do not use DESTDIR in PREFIX, but only in the install target.
[quix0rs-blobwars.git] / src / init.cpp
index 66e09497829496cb02cf50bb91cf4996accc2efa..b12cba6398dfa2be5c7a38547e9f7e3c75ca9304 100644 (file)
@@ -1,5 +1,5 @@
 /*
-Copyright (C) 2004 Parallel Realities
+Copyright (C) 2004-2010 Parallel Realities
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
@@ -103,16 +103,16 @@ void setupUserHomeDirectory()
        debug(("User Home = %s\n", userHome));
        
        char dir[PATH_MAX];
-       strcpy(dir, "");
+       dir[0] = 0;
 
-       sprintf(dir, "%s/.parallelrealities", userHome);
+       snprintf(dir, sizeof dir, "%s/.parallelrealities", userHome);
        if ((mkdir(dir, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) != 0) && (errno != EEXIST))
        {
                printf("Couldn't create required directory '%s'", dir);
                exit(1);
        }
 
-       sprintf(dir, "%s/.parallelrealities/blobwars", userHome);
+       snprintf(dir, sizeof dir, "%s/.parallelrealities/blobwars", userHome);
        if ((mkdir(dir, S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) != 0) && (errno != EEXIST))
        {
                printf("Couldn't create required directory '%s'", dir);
@@ -120,7 +120,7 @@ void setupUserHomeDirectory()
        }
 
        char gameSavePath[PATH_MAX];
-       sprintf(gameSavePath, "%s/.parallelrealities/blobwars/", userHome);
+       snprintf(gameSavePath, sizeof gameSavePath, "%s/.parallelrealities/blobwars/", userHome);
        engine.setUserHome(gameSavePath);
 }
 #endif
@@ -133,7 +133,7 @@ bool loadConfig()
 
        char configPath[PATH_MAX];
 
-       sprintf(configPath, "%sconfig", engine.userHomeDirectory);
+       snprintf(configPath, sizeof configPath, "%sconfig", engine.userHomeDirectory);
 
        debug(("Loading Config from %s\n", configPath));
 
@@ -144,7 +144,10 @@ bool loadConfig()
                return true;
        }
 
-       fscanf(fp, "%f %d", &version, &release);
+       if (fscanf(fp, "%10f %10d", &version, &release) != 2)
+       {
+               rtn = true;
+       }
 
        debug(("Version = %.2f - Expected %.2f\n", version, VERSION));
        debug(("Release = %d - Expected %d\n", release, RELEASE));
@@ -154,7 +157,10 @@ bool loadConfig()
                rtn = true;
        }
 
-       fscanf(fp, "%d %d %d %d %d %d %d", &engine.fullScreen, &game.musicVol, &game.soundVol, &game.output, &game.brightness, &engine.extremeAvailable, &game.gore);
+       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)
+       {
+               rtn = true;
+       }
 
        fclose(fp);
 
@@ -177,7 +183,7 @@ void saveConfig()
 {
        char configPath[PATH_MAX];
 
-       sprintf(configPath, "%sconfig", engine.userHomeDirectory);
+       snprintf(configPath, sizeof configPath, "%sconfig", engine.userHomeDirectory);
 
        FILE *fp = fopen(configPath, "wb");
 
@@ -195,6 +201,57 @@ void saveConfig()
        debug(("Output Type = %d\n", game.output));
 }
 
+//
+// see if we can load the private keyState
+//
+int initMedalService(void *data)
+{
+       SDL_mutexP(medalServer.lock);
+       
+       char connectMessage[1024];
+       snprintf(connectMessage, sizeof connectMessage, "Contacting Medal Server - %s:%d", MEDAL_SERVER_HOST, MEDAL_SERVER_PORT);
+       
+       graphics.showMedalMessage(-1, connectMessage);
+       
+       char keyPath[PATH_MAX];
+       char privateKey[20];
+
+       snprintf(keyPath, sizeof keyPath, "%smedalKey", engine.userHomeDirectory);
+       
+       debug(("Loading private key from %s\n", keyPath));
+       
+       FILE *fp = fopen(keyPath, "rb");
+       
+       if (!fp)
+       {
+               graphics.showMedalMessage(-1, "No Medal Key found - Online functions disabled");
+               SDL_mutexV(medalServer.lock);
+               return 0;
+       }
+       
+       if (fscanf(fp, "%19s", privateKey) != 1)
+       {
+               graphics.showMedalMessage(-1, "Medal Key file corrupt - Online functions disabled");
+               SDL_mutexV(medalServer.lock);
+               fclose(fp);
+               return 0;
+       }
+       
+       fclose(fp);
+               
+       if (!medalServer.connect(privateKey))
+       {
+               graphics.showMedalMessage(-1, "Medal Server Connection Failed - Online functions disabled");
+               return 0;
+       }
+       
+       graphics.showMedalMessage(-1, "Connected to Medal Server");
+       
+       SDL_mutexV(medalServer.lock);
+       
+       return 1;
+}
+
 /*
 Chugg chugg chugg.... brrr... chugg chugg chugg...brrrrrr... chugg ch..
 BRRRRRRRRRRRRRRRRRMMMMMMMMMMMMMMMMMMM!! Well, hopefully anyway! ;)
@@ -255,7 +312,7 @@ void initSystem()
        {
                if (Mix_OpenAudio(22050, AUDIO_S16, engine.useAudio, 1024) < 0)
                {
-                       printf("Warning: Couldn't set 22050 Hz 16-bit audio - Reason: %s\n", Mix_GetError());
+                       printf("Warning: Couldn't set 44100 Hz 16-bit audio - Reason: %s\n", Mix_GetError());
                        printf("Sound and Music will be disabled\n");
                        engine.useAudio = 0;
                }
@@ -292,11 +349,9 @@ void initSystem()
        #if USEPAK
                        
                char tempPath[PATH_MAX];
-               sprintf(tempPath, "%sfont.ttf", engine.userHomeDirectory);      
+               snprintf(tempPath, sizeof tempPath, "%sfont.ttf", engine.userHomeDirectory);    
                remove(tempPath);
                
-               SDL_Delay(1000); // wait one second, just to be sure!
-               
                if (!engine.unpack("data/vera.ttf", PAK_FONT))
                {
                        engine.reportFontFailure();
@@ -315,8 +370,16 @@ void initSystem()
        debug(("Font sizes all loaded!!\n"));
 
        audio.loadSound(SND_CHEAT, "sound/Lock And Load!!!");
-       audio.loadSound(SND_HIGHLIGHT, "sound/menu.wav");
-       audio.loadSound(SND_SELECT, "sound/select.wav");
+       audio.loadSound(SND_HIGHLIGHT, "sound/menu");
+       audio.loadSound(SND_SELECT, "sound/select");
+       
+       graphics.medal[0] = graphics.loadImage("gfx/main/medal_bronze_1.png");
+       graphics.medal[1] = graphics.loadImage("gfx/main/award_star_silver_3.png");
+       graphics.medal[2] = graphics.loadImage("gfx/main/shield.png");
+       graphics.medal[3] = graphics.loadImage("gfx/main/ruby.png");
+
+       graphics.license[0] = graphics.loadImage("gfx/main/cc-by.png", true);
+       graphics.license[1] = graphics.loadImage("gfx/main/cc-by-sa.png", true);
 
        SDL_Surface *device = graphics.loadImage("gfx/main/alienDevice.png");
 
@@ -342,6 +405,23 @@ void initSystem()
                checkForLicense();
        }
        
+       if (SDLNet_Init() < 0)
+       {
+               printf("SDLNet_Init: %s\n", SDLNet_GetError());
+       }
+       else
+       {
+               SDL_Thread *thread = SDL_CreateThread(initMedalService, NULL);
+       
+               if (thread == NULL)
+               {
+                       printf("Unable to create thread: %s\n", SDL_GetError());
+                       printf("Calling medal server directly\n");
+                       initMedalService(NULL);
+                       return;
+               }
+       }
+       
        engine.saveConfig = true;
 
        debug(("Init Complete...\n"));
@@ -365,7 +445,7 @@ void cleanup()
        audio.destroy();
 
        debug(("Removing Music...\n"));
-       sprintf(tempPath, "%smusic.mod", engine.userHomeDirectory);
+       snprintf(tempPath, sizeof tempPath, "%smusic.mod", engine.userHomeDirectory);
        remove(tempPath);
 
        debug(("Freeing Game Info...\n"));
@@ -387,7 +467,7 @@ void cleanup()
        }
 
        debug(("Removing Font File...\n"));
-       sprintf(tempPath, "%sfont.ttf", engine.userHomeDirectory);
+       snprintf(tempPath, sizeof tempPath, "%sfont.ttf", engine.userHomeDirectory);
        remove(tempPath);
        
        if (SDL_NumJoysticks() > 0)
@@ -408,6 +488,9 @@ void cleanup()
 
        debug(("Closing TTF...\n"));
        TTF_Quit();
+       
+       debug(("Closing NET...\n"));
+       SDLNet_Quit();
 
        debug(("Closing SDL Sub System...\n"));
        SDL_Quit();