SFML и C++ Уроки \ Разработка игр › Форумы › Логика игр › ввод переменных из файла › Ответ в теме: ввод переменных из файла
Ну на скорую руку могу только предоставить менеджер локализации написаный много времени назад, но новая студия вроде как спокойно “съела” код, для твоих целей он в принципе подойдёт
Но если тебе нужна только загрузка с файла тебе понадобится только этот кусок кода где localeBuffer будет полностью вмещать содержимое файла. И да, не забывай о том что потребуется приведение к типам из строки к числу – для этого есть std::stoi, std::stof, std::stod, std::stol, std::stoul и т.д. Перевод перевод делай желательно sf::String::toWideString() ибо мы работаем с юникодом при использовании строки sfml
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
sf::String curLocale = "ru_RU"; std::string curDir = ""; sf::String defaultLang = "en_UK"; std::ifstream curLocaleFile; std::ifstream defaultLocaleFile; sf::String localeBuffer; if (localeBuffer.isEmpty()) { curLocaleFile.open(std::string(curDir + curLocale + ".lang"), std::ios_base::in); char S; int count = 0; if (curLocaleFile.is_open()) { while (!curLocaleFile.eof())//count simbols { curLocaleFile.get(S); count++; } curLocaleFile.close(); curLocaleFile.open(std::string(curDir + curLocale + ".lang")); //open the file again int i = 0; //iter while (!curLocaleFile.eof()) //while not end of file { char buf; curLocaleFile.get(buf); localeBuffer += buf; //add simbol i++; //move iter next } curLocaleFile.close(); } else { //LOG("LocalisationManager::getLocalisation: Failed to open file(" + curDir + curLocale + ".lang" + ")", Logger::Info); } } |
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
//LocalisationManager.h #pragma once //#include "SFGE\LabelButton.h" //#include <SFGE\SFGE.h> #include <fstream> #include <SFML\System\String.hpp> //#include <Windows.h> //#include <atlwin.h> #define lm sfge::LocalisationManager::instance() namespace sfge { /// \brief A localisation manager that give licalised names and descriptions in selected locale. /// /// In order not to let calling the class get verbose, a MACRO is defined.\n /// @code #define lm sfge::LocalisationManager::instance() @endcode /// This makes calling the localisation manager elsewere as this: /// @code /// #include <SFGE/LocalisationManager.h> /// lm->getLocalisation("block.dirt.name"); /// @endcode /// And resulted string ben a localised string, or return input if localisation not exist class LocalisationManager { public: static LocalisationManager* instance(); ~LocalisationManager() { } /// \brief Set localisation (and filename) as main source of licalised names /// @param sf::String - locale in style en_UK, ru_RU, etc. void setLocale(sf::String locale) { curLocale = locale; } /// \brief Set lang folder /// \@param string Dir root void setDir(std::string root) { curDir = root; } /// \brief Get localisation from .lang file /// @param sf::String Unlocalised Name /// @return sf::String result sf::String getLocalisation(sf::String unlocalisedName); protected: LocalisationManager() { //curLocale = "ru_RU"; } private: static LocalisationManager* lcr; sf::String curLocale = "ru_RU"; std::string curDir = ""; sf::String defaultLang = "en_UK"; std::ifstream curLocaleFile; std::ifstream defaultLocaleFile; /// \brief Locale buffer is containing all chars from file /// and we no more need to load file in to memory again, and again, and again.... sf::String localeBuffer; };//end LocalisationManager }// end namespace sfge |
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
//LocalisationManager.cpp //#include "Block.h" //#include <SFGE\Block.h> //#include "gamestate.h" #include "LocalisationManager.h" sfge::LocalisationManager* sfge::LocalisationManager::lcr = 0; namespace sfge { LocalisationManager* LocalisationManager::instance() { //setlocale(LC_CTYPE, "rus");//We cant get <Windows.h> any more if (lcr == NULL) lcr = new LocalisationManager; return lcr; } sf::String LocalisationManager::getLocalisation(sf::String unlocalisedName) { sf::String temp; sf::String result; if (localeBuffer.isEmpty()) { curLocaleFile.open(std::string(curDir + curLocale + ".lang"), std::ios_base::in); char S; int count = 0; if (curLocaleFile.is_open()) { while (!curLocaleFile.eof())//count simbols { curLocaleFile.get(S); count++; } curLocaleFile.close(); curLocaleFile.open(std::string(curDir + curLocale + ".lang")); //open the file again int i = 0; //iter while (!curLocaleFile.eof()) //while not end of file { char buf; curLocaleFile.get(buf); localeBuffer += buf; //add simbol i++; //move iter next } curLocaleFile.close(); } else { //LOG("LocalisationManager::getLocalisation: Failed to open file(" + curDir + curLocale + ".lang" + ")", Logger::Info); } } sf::String c1; auto c1it = localeBuffer.find("."); c1it = localeBuffer.find(".", c1it + 1); c1 = localeBuffer.substring(c1it + 1, 4); if (c1 == "name")//Here we determinate -> we search name or description { if (unlocalisedName == "block..name" || unlocalisedName == "item..name" || unlocalisedName == "entity..name") { //LOG("LocalisationManager::getLocalisation: Failed. Unlocalised name is missing", Logger::Error); return "missingNo.name"; } int checkCode = 0; //if (localeBuffer.find(unlocalisedName)) //{ auto it = localeBuffer.find(unlocalisedName + "="); it += std::string(unlocalisedName + "=").size(); if (localeBuffer.find("\n", it) != sf::String::InvalidPos)//We found \n or get end of string? checkCode = 1; else checkCode = -1; auto itE = localeBuffer.find("\n", it); if (checkCode == 1)//If we found \n we use it position while (it < itE) { //std::cout << std::to_string(it) << " " << std::to_string(itE) << std::endl; result += localeBuffer[it]; it++; } else if (checkCode == -1)//Else use end of string position; while (it < localeBuffer.getSize() - 1) { //std::cout << std::to_string(it) << " " << std::to_string(localeBuffer.getSize()) << std::endl; result += localeBuffer[it]; it++; } //LOG("LocalisationManager::getLocalisation:(" + unlocalisedName + ") = " + result, Logger::Info); return result; } else if (c1 == "desc") { auto c2c = unlocalisedName.substring(0, unlocalisedName.find(".")); if (c2c == "" || c2c == ".") { //LOG("LocalisationManager::getLocalisation: Failed. Unlocalised name is missing", Logger::Error); return "missingNo.desc"; } int checkCode = 0; auto it = localeBuffer.find(unlocalisedName + "="); it += std::string(unlocalisedName + "=").size(); //if (localeBuffer.find("#enddesc", it) != sf::String::InvalidPos)//We found \n or get end of string? // checkCode = 1; //else checkCode = -1; auto itE = localeBuffer.find("#enddesc", it); if (itE != sf::String::InvalidPos) while (it < itE) { //std::cout << std::to_string(it) << " " << std::to_string(itE) << std::endl; result += localeBuffer[it]; it++; } else { //LOG("LocalisationManager::getLocalisation: Failed(" + unlocalisedName + "): #enddesc not found", Logger::Error); return "missingNo.desc.notEnded"; } } //} //else //{ //LOG("LocalisationManager::getLocalisation: Failed(" + unlocalisedName + ")", Logger::Warning); //LOG("LocalisationManager::getLocalisation: localeBuffer(\n" + localeBuffer + ")", Logger::Warning); return unlocalisedName; //} } } |
C++
1 2 3 4 |
//ru_Ru.lang - example block.dirt.name=Земля block.cobble.name=Булыжник block.grass.name=Трава |