]> git.jsancho.org Git - lugaru.git/blobdiff - Source/main.cpp
Fix unused-result warning for chdir
[lugaru.git] / Source / main.cpp
index 169809ac8d46771b83f16b68d6f16023c92d95ed..cacfabdc99b2a0632e8b759636a39b00d73151d9 100644 (file)
@@ -1,6 +1,6 @@
 /*
 Copyright (C) 2003, 2010 - Wolfire Games
-Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
+Copyright (C) 2010-2017 - Lugaru contributors (see AUTHORS file)
 
 This file is part of Lugaru.
 
@@ -22,8 +22,9 @@ along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
 
 #include "Audio/openal_wrapper.hpp"
 #include "Graphic/gamegl.hpp"
-#include "MacCompatibility.hpp"
+#include "Platform/Platform.hpp"
 #include "User/Settings.hpp"
+#include "Version.hpp"
 
 #include <fstream>
 #include <iostream>
@@ -31,14 +32,14 @@ along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
 #include <set>
 #include <stdio.h>
 #include <string.h>
-#include <zlib.h>
 
 using namespace Game;
 
 #ifdef WIN32
-#include "win-res/resource.hpp"
 #include <shellapi.h>
 #include <windows.h>
+#else
+#include <unistd.h>
 #endif
 
 extern float multiplier;
@@ -154,8 +155,9 @@ SDL_bool sdlEventProc(const SDL_Event& e)
             if ((e.key.keysym.scancode == SDL_SCANCODE_G) &&
                 (e.key.keysym.mod & KMOD_CTRL)) {
                 SDL_bool mode = SDL_TRUE;
-                if ((SDL_GetWindowFlags(sdlwindow) & SDL_WINDOW_FULLSCREEN) == 0)
+                if ((SDL_GetWindowFlags(sdlwindow) & SDL_WINDOW_FULLSCREEN) == 0) {
                     mode = (SDL_GetWindowGrab(sdlwindow) ? SDL_FALSE : SDL_TRUE);
+                }
                 SDL_SetWindowGrab(sdlwindow, mode);
                 SDL_SetRelativeMouseMode(mode);
             } else if ((e.key.keysym.scancode == SDL_SCANCODE_RETURN) && (e.key.keysym.mod & KMOD_ALT)) {
@@ -181,11 +183,12 @@ bool SetUp()
 
     DefaultSettings();
 
-    if (!SDL_WasInit(SDL_INIT_VIDEO))
+    if (!SDL_WasInit(SDL_INIT_VIDEO)) {
         if (SDL_Init(SDL_INIT_VIDEO) == -1) {
             fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
             return false;
         }
+    }
     if (!LoadSettings()) {
         fprintf(stderr, "Failed to load config, creating default\n");
         SaveSettings();
@@ -200,10 +203,12 @@ bool SetUp()
     for (int displayIdx = 0; displayIdx < SDL_GetNumVideoDisplays(); ++displayIdx) {
         for (int i = 0; i < SDL_GetNumDisplayModes(displayIdx); ++i) {
             SDL_DisplayMode mode;
-            if (SDL_GetDisplayMode(displayIdx, i, &mode) == -1)
+            if (SDL_GetDisplayMode(displayIdx, i, &mode) == -1) {
                 continue;
-            if ((mode.w < 640) || (mode.h < 480))
+            }
+            if ((mode.w < 640) || (mode.h < 480)) {
                 continue; // sane lower limit.
+            }
             pair<int, int> resolution(mode.w, mode.h);
             resolutions.insert(resolution);
         }
@@ -278,8 +283,9 @@ bool SetUp()
         return false;
     }
 
-    if (SDL_GL_SetSwapInterval(-1) == -1) // try swap_tear first.
+    if (SDL_GL_SetSwapInterval(-1) == -1) // try swap_tear first.
         SDL_GL_SetSwapInterval(1);
+    }
 
     SDL_ShowCursor(0);
     if (!commandLineOptions[NOMOUSEGRAB].last()->type()) {
@@ -318,14 +324,16 @@ static void DoMouse()
         deltav *= usermousesensitivity;
         mousecoordh += deltah;
         mousecoordv += deltav;
-        if (mousecoordh < 0)
+        if (mousecoordh < 0) {
             mousecoordh = 0;
-        else if (mousecoordh >= kContextWidth)
+        } else if (mousecoordh >= kContextWidth) {
             mousecoordh = kContextWidth - 1;
-        if (mousecoordv < 0)
+        }
+        if (mousecoordv < 0) {
             mousecoordv = 0;
-        else if (mousecoordv >= kContextHeight)
+        } else if (mousecoordv >= kContextHeight) {
             mousecoordv = kContextHeight - 1;
+        }
     }
 }
 
@@ -338,25 +346,30 @@ void DoFrameRate(int update)
     AbsoluteTime currTime = UpTime();
     double deltaTime = (float)AbsoluteDeltaToDuration(currTime, frametime);
 
-    if (0 > deltaTime) // if negative microseconds
+    if (0 > deltaTime) // if negative microseconds
         deltaTime /= -1000000.0;
-    else // else milliseconds
+    } else { // else milliseconds
         deltaTime /= 1000.0;
+    }
 
     multiplier = deltaTime;
-    if (multiplier < .001)
+    if (multiplier < .001) {
         multiplier = .001;
-    if (multiplier > 10)
+    }
+    if (multiplier > 10) {
         multiplier = 10;
-    if (update)
+    }
+    if (update) {
         frametime = currTime; // reset for next time interval
+    }
 
     deltaTime = (float)AbsoluteDeltaToDuration(currTime, time);
 
-    if (0 > deltaTime) // if negative microseconds
+    if (0 > deltaTime) // if negative microseconds
         deltaTime /= -1000000.0;
-    else // else milliseconds
+    } else { // else milliseconds
         deltaTime /= 1000.0;
+    }
     frames++;
     if (0.001 <= deltaTime) { // has update interval passed
         if (update) {
@@ -373,26 +386,32 @@ void DoUpdate()
     static float oldmult;
 
     DoFrameRate(1);
-    if (multiplier > .6)
+    if (multiplier > .6) {
         multiplier = .6;
+    }
 
     fps = 1 / multiplier;
 
     count = multiplier * sps;
-    if (count < 2)
+    if (count < 2) {
         count = 2;
+    }
 
     realmultiplier = multiplier;
     multiplier *= gamespeed;
-    if (difficulty == 1)
+    if (difficulty == 1) {
         multiplier *= .9;
-    if (difficulty == 0)
+    }
+    if (difficulty == 0) {
         multiplier *= .8;
+    }
 
-    if (loading == 4)
+    if (loading == 4) {
         multiplier *= .00001;
-    if (slomo && !mainmenu)
+    }
+    if (slomo && !mainmenu) {
         multiplier *= slomospeed;
+    }
     oldmult = multiplier;
     multiplier /= (float)count;
 
@@ -478,15 +497,16 @@ static char* findBinaryInPath(const char* bin, char* envr)
     do {
         size_t size;
         ptr = strchr(start, ':'); /* find next $PATH separator. */
-        if (ptr)
+        if (ptr) {
             *ptr = '\0';
-
+        }
         size = strlen(start) + strlen(bin) + 2;
         if (size > alloc_size) {
             char* x = (char*)realloc(exe, size);
             if (x == NULL) {
-                if (exe != NULL)
+                if (exe != NULL) {
                     free(exe);
+                }
                 return (NULL);
             } /* if */
 
@@ -496,8 +516,9 @@ static char* findBinaryInPath(const char* bin, char* envr)
 
         /* build full binary path... */
         strcpy(exe, start);
-        if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/'))
+        if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/')) {
             strcat(exe, "/");
+        }
         strcat(exe, bin);
 
         if (access(exe, X_OK) == 0) { /* Exists as executable? We're done. */
@@ -508,8 +529,9 @@ static char* findBinaryInPath(const char* bin, char* envr)
         start = ptr + 1; /* start points to beginning of next element. */
     } while (ptr != NULL);
 
-    if (exe != NULL)
+    if (exe != NULL) {
         free(exe);
+    }
 
     return (NULL); /* doesn't exist in path. */
 } /* findBinaryInPath */
@@ -522,17 +544,20 @@ char* calcBaseDir(const char* argv0)
 
     if (strchr(argv0, '/')) {
         retval = strdup(argv0);
-        if (retval)
+        if (retval) {
             *((char*)strrchr(retval, '/')) = '\0';
+        }
         return (retval);
     }
 
     envr = getenv("PATH");
-    if (!envr)
+    if (!envr) {
         return NULL;
+    }
     envr = strdup(envr);
-    if (!envr)
+    if (!envr) {
         return NULL;
+    }
     retval = findBinaryInPath(argv0, envr);
     free(envr);
     return (retval);
@@ -554,7 +579,10 @@ static inline void chdirToAppPath(const char* argv0)
                 *ptr = '\0';
         }
 #endif
-        chdir(dir);
+        errno = 0;
+        if (chdir(dir) != 0) {
+            printf("Error changing dir to '%s' (%s).\n", dir, strerror(errno));
+        }
         free(dir);
     }
 }
@@ -564,6 +592,7 @@ const option::Descriptor usage[] =
     {
       { UNKNOWN, 0, "", "", option::Arg::None, "USAGE: lugaru [options]\n\n"
                                                "Options:" },
+      { VERSION, 0, "v", "version", option::Arg::None, " -v, --version     Print version and exit." },
       { HELP, 0, "h", "help", option::Arg::None, " -h, --help        Print usage and exit." },
       { FULLSCREEN, 1, "f", "fullscreen", option::Arg::None, " -f, --fullscreen  Start the game in fullscreen mode." },
       { FULLSCREEN, 0, "w", "windowed", option::Arg::None, " -w, --windowed    Start the game in windowed mode (default)." },
@@ -596,6 +625,23 @@ int main(int argc, char** argv)
         return 1;
     }
 
+    // Always start by printing the version and info to the stdout
+    std::cout << "--------------------------------------------------------------------------\n"
+              << "Lugaru HD: The Rabbit's Foot, by Wolfire Games and the OSS Lugaru project.\n\n"
+              << "Licensed under the GPL 2.0+ and CC-BY-SA 3.0 and 4.0 licenses.\n"
+              << "More information, updates and bug reports at http://osslugaru.gitlab.io\n"
+              << std::endl;
+
+    std::cout << "Version " + VERSION_STRING + " -- " + VERSION_BUILD_TYPE + " build\n"
+              << "--------------------------------------------------------------------------\n"
+              << std::endl;
+
+    if (commandLineOptions[VERSION]) {
+        // That was enough, quit.
+        delete[] commandLineOptionsBuffer;
+        return 0;
+    }
+
     if (commandLineOptions[HELP]) {
         option::printUsage(std::cout, usage);
         delete[] commandLineOptionsBuffer;