]> git.mxchange.org Git - quix0rs-blobwars.git/blobdiff - src/init.cpp
Remove font size guessing hack.
[quix0rs-blobwars.git] / src / init.cpp
index 861af140aa8795a5151c1f972b698fbd644edef8..3c333cc10d8d3ed632c712b57ad9fd174e55e5fb 100644 (file)
@@ -1,5 +1,6 @@
 /*
-Copyright (C) 2004 Parallel Realities
+Copyright (C) 2004-2011 Parallel Realities
+Copyright (C) 2011-2015 Perpendicular Dimensions
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
@@ -20,6 +21,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #include "init.h"
 
+static bool displayLicense;
+
 void checkForLicense()
 {
        if (!engine.loadData(_("data/license")))
@@ -80,7 +83,7 @@ void showLicense()
                graphics.updateScreen();
                engine.getInput();
                config.populate();
-               if (engine.keyState[SDLK_SPACE])
+               if (engine.userAccepts())
                        break;
                SDL_Delay(16);
        }
@@ -144,7 +147,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 +160,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);
 
@@ -223,7 +232,13 @@ int initMedalService(void *data)
                return 0;
        }
        
-       fscanf(fp, "%s", privateKey);
+       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);
                
@@ -240,18 +255,21 @@ int initMedalService(void *data)
        return 1;
 }
 
+void initConfig()
+{
+       #if UNIX
+       setupUserHomeDirectory();
+       #endif
+
+       displayLicense = loadConfig();
+}
+
 /*
 Chugg chugg chugg.... brrr... chugg chugg chugg...brrrrrr... chugg ch..
 BRRRRRRRRRRRRRRRRRMMMMMMMMMMMMMMMMMMM!! Well, hopefully anyway! ;)
 */
 void initSystem()
 {
-       #if UNIX
-       setupUserHomeDirectory();
-       #endif
-
-       bool displayLicense = loadConfig();
-       
        long flags = SDL_INIT_VIDEO|SDL_INIT_JOYSTICK;
                        
        if (engine.useAudio)
@@ -266,29 +284,62 @@ void initSystem()
                exit(1);
        }
 
-       if (!engine.fullScreen)
+       graphics.screen = SDL_CreateRGBSurface(0, 640, 480, 32, 0xff0000, 0xff00, 0xff, 0xff000000);
+
+       if (graphics.screen == NULL)
+       {
+               printf("Couldn't set 640x480 video mode: %s\n", SDL_GetError());
+               exit(1);
+       }
+
+       // Increase the size of the window if we have large desktop resolutions
+       SDL_DisplayMode displayMode{0};
+       SDL_GetDesktopDisplayMode(0, &displayMode);
+       int w = graphics.screen->w;
+       int h = graphics.screen->h;
+       while (displayMode.w > w * 2 && displayMode.h > h * 2)
        {
-               graphics.screen = SDL_SetVideoMode(640, 480, 0, SDL_HWPALETTE);
+               w *= 2;
+               h *= 2;
        }
-       else
+
+       graphics.window = SDL_CreateWindow("Blobwars: Metal Blob Solid", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, SDL_WINDOW_RESIZABLE);
+
+       if (graphics.window == NULL)
        {
-               graphics.screen = SDL_SetVideoMode(640, 480, 0, SDL_HWPALETTE | SDL_FULLSCREEN);
+               printf("Couldn't create %dx%d window: %s\n", w, h, SDL_GetError());
+               exit(1);
        }
 
-       if (graphics.screen == NULL)
+       graphics.renderer = SDL_CreateRenderer(graphics.window, -1, SDL_RENDERER_PRESENTVSYNC);
+
+       if (graphics.renderer == NULL)
        {
-               printf("Couldn't set 640x480 video mode: %s\n", SDL_GetError());
+               printf("Couldn't create renderer: %s\n", SDL_GetError());
+               exit(1);
+       }
+
+       SDL_RenderSetLogicalSize(graphics.renderer, graphics.screen->w, graphics.screen->h);
+       graphics.texture = SDL_CreateTexture(graphics.renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, graphics.screen->w, graphics.screen->h);
+
+       if (graphics.texture == NULL)
+       {
+               printf("Could not create %dx%d texture: %s\n", graphics.screen->w, graphics.screen->h, SDL_GetError());
                exit(1);
        }
 
+       SDL_SetWindowFullscreen(graphics.window, engine.fullScreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
+
        // This (attempts to) set the gamma correction. We attempt to catch an error here
        // in case someone has done something really stupid in the config file(!!)
-    if (game.brightness != -1) {
-        Math::limitInt(&game.brightness, 1, 20);
-        float brightness = game.brightness;
-        brightness /= 10;
-        SDL_SetGamma(brightness, brightness, brightness);
-    }
+       if (game.brightness != -1) {
+               Math::limitInt(&game.brightness, 1, 20);
+               float brightness = game.brightness;
+               brightness /= 10;
+               uint16_t ramp[256];
+               SDL_CalculateGammaRamp(brightness, ramp);
+               SDL_SetWindowGammaRamp(graphics.window, ramp, ramp, ramp);
+       }
 
        if (TTF_Init() < 0)
        {
@@ -335,28 +386,18 @@ void initSystem()
        debug(("Loading Fonts...\n"));
 
        #if USEPAK
-                       
-               char tempPath[PATH_MAX];
-               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();
                }
        #endif
 
-       debug(("Trying to load correct font pixel sizes using a really half arsed routine!\n"));
-       debug(("If it crashes then you'll know why!\n"));
-                       
-       graphics.loadFont(0, "data/vera.ttf", 7);
-       graphics.loadFont(1, "data/vera.ttf", 9);
-       graphics.loadFont(2, "data/vera.ttf", 11);
-       graphics.loadFont(3, "data/vera.ttf", 13);
-       graphics.loadFont(4, "data/vera.ttf", 15);
-       
+       graphics.loadFont(0, "data/vera.ttf", 12);
+       graphics.loadFont(1, "data/vera.ttf", 15);
+       graphics.loadFont(2, "data/vera.ttf", 19);
+       graphics.loadFont(3, "data/vera.ttf", 23);
+       graphics.loadFont(4, "data/vera.ttf", 24);
+
        debug(("Font sizes all loaded!!\n"));
 
        audio.loadSound(SND_CHEAT, "sound/Lock And Load!!!");
@@ -373,12 +414,6 @@ void initSystem()
 
        SDL_Surface *device = graphics.loadImage("gfx/main/alienDevice.png");
 
-#ifndef SDL_FRAMEWORK
-       SDL_WM_SetIcon(device, NULL);
-#endif
-       SDL_WM_SetCaption("Blob Wars : Metal Blob Solid", "Blob Wars");
-       SDL_EnableKeyRepeat(350, 80);
-
        SDL_FreeSurface(device);
        
        if (strstr(engine.userHomeDirectory, "/root"))
@@ -401,7 +436,7 @@ void initSystem()
        }
        else
        {
-               SDL_Thread *thread = SDL_CreateThread(initMedalService, NULL);
+               SDL_Thread *thread = SDL_CreateThread(initMedalService, "MedalService", NULL);
        
                if (thread == NULL)
                {
@@ -465,7 +500,7 @@ void cleanup()
                SDL_JoystickEventState(SDL_DISABLE);
                for (int i = 0 ; i < SDL_NumJoysticks() ; i++)
                {
-                       debug(("Closing Joystick #%d - %s...\n", i, SDL_JoystickName(i)));
+                       debug(("Closing Joystick #%d\n", i));
                        SDL_JoystickClose(config.sdlJoystick);
                }
        }