diff options
-rw-r--r-- | src/game.cpp | 2 | ||||
-rw-r--r-- | src/gui/help.cpp | 24 | ||||
-rw-r--r-- | src/gui/updatewindow.cpp | 21 | ||||
-rw-r--r-- | src/resources/resourcemanager.cpp | 29 | ||||
-rw-r--r-- | src/resources/resourcemanager.h | 7 |
5 files changed, 47 insertions, 36 deletions
diff --git a/src/game.cpp b/src/game.cpp index f6d569b1..6013e4d5 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -270,7 +270,7 @@ void do_input() { // TODO: one different value of tolerance is needed for each direction // This probably means the need for a tuning utility/window - int tolerance = config.getValue("joytolerance", 10); + int tolerance = (int)config.getValue("joytolerance", 10); SDL_JoystickUpdate(); if (SDL_JoystickGetAxis(joypad, 0) > tolerance) { diff --git a/src/gui/help.cpp b/src/gui/help.cpp index cff36da8..3ff8f0f4 100644 --- a/src/gui/help.cpp +++ b/src/gui/help.cpp @@ -87,27 +87,11 @@ void HelpWindow::loadHelp(const std::string &helpFile) void HelpWindow::loadFile(const std::string &file) { ResourceManager *resman = ResourceManager::getInstance(); - const std::string filePath = "help/" + file + ".txt"; - int contentsLength; - char *fileContents = (char*)resman->loadFile(filePath, contentsLength); + std::vector<std::string> lines = + resman->loadTextFile("help/" + file + ".txt"); - if (!fileContents) + for (unsigned int i = 0; i < lines.size(); ++i) { - logger->log("Couldn't load help file: %s", filePath.c_str()); - return; + browserBox->addRow(lines[i]); } - - // Reallocate and include terminating 0 character - fileContents = (char*)realloc(fileContents, contentsLength + 1); - fileContents[contentsLength] = '\0'; - - // Tokenize and add each line separately - char *line = strtok(fileContents, "\n"); - while (line != NULL) - { - browserBox->addRow(line); - line = strtok(NULL, "\n"); - } - - free(fileContents); } diff --git a/src/gui/updatewindow.cpp b/src/gui/updatewindow.cpp index 75f94b4b..473cc171 100644 --- a/src/gui/updatewindow.cpp +++ b/src/gui/updatewindow.cpp @@ -127,25 +127,16 @@ void UpdaterWindow::action(const std::string& eventId) void UpdaterWindow::loadNews() { - browserBox->clearRows(); ResourceManager *resman = ResourceManager::getInstance(); - int contentsLength; - std::ifstream newsFile(TMW_DATADIR "data/news.txt"); - if (!newsFile.is_open()) - { - logger->log("Couldn't load news.txt"); - browserBox->addRow("Error"); - return; - } + std::vector<std::string> lines = resman->loadTextFile("news.txt"); - // Tokenize and add each line separately - std::string line(""); - while (!newsFile.eof()) + browserBox->clearRows(); + + for (unsigned int i = 0; i < lines.size(); ++i) { - getline(newsFile, line); - browserBox->addRow(line); + browserBox->addRow(lines[i]); } - newsFile.close(); + scrollArea->setVerticalScrollAmount(0); setVisible(true); } diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp index 50d0f4b8..6ed933e4 100644 --- a/src/resources/resourcemanager.cpp +++ b/src/resources/resourcemanager.cpp @@ -281,3 +281,32 @@ ResourceManager::loadFile(const std::string &fileName, int &fileSize) return buffer; } + +std::vector<std::string> +ResourceManager::loadTextFile(const std::string &fileName) +{ + int contentsLength; + char *fileContents = (char*)loadFile(fileName, contentsLength); + std::vector<std::string> lines; + + if (!fileContents) + { + logger->log("Couldn't load text file: %s", fileName.c_str()); + return lines; + } + + // Reallocate and include terminating 0 character + fileContents = (char*)realloc(fileContents, contentsLength + 1); + fileContents[contentsLength] = '\0'; + + // Tokenize and add each line separately + char *line = strtok(fileContents, "\n"); + while (line != NULL) + { + lines.push_back(line); + line = strtok(NULL, "\n"); + } + + free(fileContents); + return lines; +} diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h index e3ad1e94..93c2dd85 100644 --- a/src/resources/resourcemanager.h +++ b/src/resources/resourcemanager.h @@ -26,6 +26,7 @@ #include <map> #include <string> +#include <vector> #include "resource.h" #include "image.h" #include "music.h" @@ -114,6 +115,12 @@ class ResourceManager loadFile(const std::string &fileName, int &fileSize); /** + * Retrieves the contents of a text file. + */ + std::vector<std::string> + loadTextFile(const std::string &fileName); + + /** * Returns an instance of the class, creating one if it does not * already exist. */ |