]> git.mxchange.org Git - quix0rs-blobwars.git/blobdiff - src/CEngine.cpp
Updated PNG icon files.
[quix0rs-blobwars.git] / src / CEngine.cpp
index 37238ae89a50b5a69ad44d1b142fa41017909c54..b5c0b15e2bc0b0fb11d5399a066889a0bf10957e 100644 (file)
@@ -1,5 +1,5 @@
 /*
-Copyright (C) 2004 Parallel Realities
+Copyright (C) 2004-2011 Parallel Realities
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
@@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
 
 #include "headers.h"
+#include <errno.h>
 
 Engine::Engine()
 {
@@ -64,7 +65,7 @@ Engine::Engine()
        binaryBuffer = NULL;
        #ifdef FRAMEWORK_SDL
        char pakPath[PATH_MAX];
-       strncpy(pakPath, PAKFULLPATH, sizeof(pakPath));
+       strlcpy(pakPath, PAKFULLPATH, sizeof(pakPath));
        if (CFBundleGetMainBundle() != NULL) {
                CFURLRef pakURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR(PAKNAME), NULL, NULL);
                if (pakURL != NULL) {
@@ -96,12 +97,10 @@ void Engine::destroy()
        deleteWidgets();
 
        debug(("engine: free databuffer\n"));
-       if (dataBuffer != NULL)
-               delete[] dataBuffer;
+       delete[] dataBuffer;
 
        debug(("engine: free binarybuffer\n"));
-       if (binaryBuffer != NULL)
-               delete[] binaryBuffer;
+       delete[] binaryBuffer;
 
        debug(("Clearing Define List...\n"));
        defineList.clear();
@@ -145,7 +144,7 @@ void Engine::addKeyEvent()
 
        if (index == -1)
        {
-               for (int i = 0 ; i < 25 ; i++)
+               for (int i = 0 ; i < 24 ; i++)
                {
                        lastKeyEvents[i] = lastKeyEvents[i + 1];
                }
@@ -229,7 +228,7 @@ void Engine::getInput()
                                }
 
                                keyState[event.key.keysym.sym] = 1;
-                               strncpy(lastKeyPressed, SDL_GetKeyName(event.key.keysym.sym), sizeof lastKeyPressed);
+                               strlcpy(lastKeyPressed, SDL_GetKeyName(event.key.keysym.sym), sizeof lastKeyPressed);
                                addKeyEvent();
                                break;
 
@@ -274,12 +273,12 @@ void Engine::getInput()
        }
 }
 
-int Engine::getMouseX()
+int Engine::getMouseX() const
 {
        return mouseX;
 }
 
-int Engine::getMouseY()
+int Engine::getMouseY() const
 {
        return mouseY;
 }
@@ -314,7 +313,7 @@ void Engine::clearInput()
 
 void Engine::setUserHome(const char *path)
 {
-       strncpy(userHomeDirectory, path, sizeof userHomeDirectory);
+       strlcpy(userHomeDirectory, path, sizeof userHomeDirectory);
        debug(("User Home = %s\n", path));
 }
 
@@ -331,18 +330,16 @@ since SDL currently provides no means to load music directly from memory
 */
 bool Engine::unpack(const char *filename, int fileType)
 {
+       bool ret = true;
+
        if (fileType == PAK_DATA)
        {
-               if (dataBuffer != NULL)
-                       delete[] dataBuffer;
-
+               delete[] dataBuffer;
                dataBuffer = NULL;
        }
        else
        {
-               if (binaryBuffer != NULL)
-                       delete[] binaryBuffer;
-
+               delete[] binaryBuffer;
                binaryBuffer = NULL;
        }
 
@@ -371,7 +368,7 @@ bool Engine::unpack(const char *filename, int fileType)
                }
        }
 
-       if ((fileType == PAK_MUSIC) || (fileType == PAK_FONT))
+       if ((fileType == PAK_MUSIC) || (fileType == PAK_FONT) || (fileType == PAK_TAGS))
        {
                char tempPath[PATH_MAX];
                
@@ -383,6 +380,12 @@ bool Engine::unpack(const char *filename, int fileType)
                        fp = fopen(tempPath, "wb");
                }
 
+               if (fileType == PAK_TAGS)
+               {
+                       snprintf(tempPath, sizeof tempPath, "%smusic.tags", userHomeDirectory);
+                       fp = fopen(tempPath, "wb");
+               }
+
                if (fileType == PAK_FONT)
                {
                        snprintf(tempPath, sizeof tempPath, "%sfont.ttf", userHomeDirectory);
@@ -391,25 +394,29 @@ bool Engine::unpack(const char *filename, int fileType)
 
                if (!fp)
                {
+                       printf("Fatal Error: could not open %s for writing: %s", tempPath, strerror(errno));
                        return false;
                }
 
-               fwrite(binaryBuffer, 1, pak.getUncompressedSize(), fp);
+               if (fwrite(binaryBuffer, 1, pak.getUncompressedSize(), fp) != pak.getUncompressedSize())
+               {
+                       printf("Fatal Error: could not write to %s: %s", tempPath, strerror(errno));
+                       ret = false;
+               }
                fclose(fp);
        }
 
-       debug(("unpack() : Loaded %s (%d)\n", filename, pak.getUncompressedSize()));
+       debug(("unpack() : Loaded %s (%d), ret: %d\n", filename, pak.getUncompressedSize(), (int)ret));
 
-       return true;
+       return ret;
 }
 
 bool Engine::loadData(const char *filename)
 {
-       if (dataBuffer != NULL)
-       {
-               delete[] dataBuffer;
-               dataBuffer = NULL;
-       }
+       bool ret = true;
+
+       delete[] dataBuffer;
+       dataBuffer = NULL;
        
        #if USEPAK
                return unpack(filename, PAK_DATA);
@@ -426,15 +433,18 @@ bool Engine::loadData(const char *filename)
 
        rewind(fp);
 
-       dataBuffer = new unsigned char[fSize];
+       dataBuffer = new unsigned char[fSize + 1];
 
-       fread(dataBuffer, 1, fSize, fp);
+       if (fread(dataBuffer, 1, fSize, fp) != (size_t)fSize)
+               ret = false;
+
+       dataBuffer[fSize] = 0;
 
        fclose(fp);
 
-       debug(("loadData() : Loaded %s (%d)\n", filename, fSize));
+       debug(("loadData() : Loaded %s (%d), ret: %d\n", filename, fSize, (int)ret));
 
-       return true;
+       return ret;
 }
 
 void Engine::reportFontFailure()
@@ -457,7 +467,7 @@ void Engine::setPlayerPosition(int x, int y, int limitLeft, int limitRight, int
        Math::limitInt(&playerPosY, limitUp, limitDown);
 }
 
-int Engine::getFrameLoop()
+int Engine::getFrameLoop() const
 {
        return frameLoop;
 }
@@ -474,7 +484,7 @@ void Engine::doTimeDifference()
        time2 = SDL_GetTicks();
 }
 
-float Engine::getTimeDifference()
+float Engine::getTimeDifference() const
 {
        return timeDifference;
 }
@@ -488,7 +498,7 @@ void Engine::setInfoMessage(const char *message, int priority, int type)
 {
        if (priority >= messagePriority)
        {
-               strncpy(this->message, message, sizeof this->message);
+               strlcpy(this->message, message, sizeof this->message);
                messageTime = 180;
                messagePriority = priority;
                messageType = type;
@@ -956,7 +966,7 @@ int Engine::getValueOfFlagTokens(const char *realLine)
        char line[1024];
        bool found;
        int value;
-       strncpy(line, realLine, sizeof line);
+       strlcpy(line, realLine, sizeof line);
 
        int flags = 0;