From 8a32dc9e4e1011b10f009e999d7d008aa2711d8a Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=B4me=20Chilliet?= Date: Sun, 11 Dec 2016 14:58:43 +0700 Subject: [PATCH 1/1] Removed all modifications of Account active from outside Account --- Source/Account.cpp | 31 ++++++++++++++++++------------- Source/Account.h | 8 ++++---- Source/GameInitDispose.cpp | 2 +- Source/Menu.cpp | 19 +++++++++++-------- 4 files changed, 34 insertions(+), 26 deletions(-) diff --git a/Source/Account.cpp b/Source/Account.cpp index d132c21..ba91890 100644 --- a/Source/Account.cpp +++ b/Source/Account.cpp @@ -49,35 +49,40 @@ void Account::setCurrentCampaign(const string& name) currentCampaign = name; } -Account* Account::add(const string& name) +void Account::add(const string& name) { accounts.push_back(new Account(name)); - return accounts.back(); + active = accounts.back(); } Account* Account::get(int i) { - if ((i >= 0) && (i < int(accounts.size()))) { return accounts[i]; } else return NULL; } -void Account::destroy(int i) +void Account::setActive(int i) { - accounts.erase(accounts.begin() + i); + active = get(i); } -Account* Account::destroy(Account* a) + +void Account::destroyActive() { for (unsigned i = 0; i < accounts.size(); i++) { - if (accounts[i] == a) { + if (accounts[i] == active) { accounts.erase(accounts.begin() + i); - return NULL; + active = nullptr; + return; } } - printf("Unexpected error : User %s not found\n", a->getName().c_str()); - return accounts.front(); + cerr << "Unexpected error : User " << active->getName() << " not found" << endl; + if (accounts.empty()) { + active = nullptr; + } else { + active = accounts.front(); + } } int Account::getDifficulty() @@ -111,7 +116,7 @@ void Account::winLevel(int level, float score, float time) progress = level + 1; } -Account* Account::loadFile(string filename) +void Account::loadFile(string filename) { FILE *tfile; int numaccounts; @@ -186,10 +191,10 @@ Account* Account::loadFile(string filename) } fclose(tfile); - return get(iactive); + active = get(iactive); } else { perror(("Couldn't load users from " + filename).c_str()); - return NULL; + active = nullptr; } } diff --git a/Source/Account.h b/Source/Account.h index f68c52b..8e43502 100644 --- a/Source/Account.h +++ b/Source/Account.h @@ -43,11 +43,11 @@ struct CampaignProgress { class Account { public: - static void destroy(int i); - static Account* destroy(Account* a); - static Account* add(const std::string& name); + static void destroyActive(); + static void setActive(int i); + static void add(const std::string& name); static Account* get(int i); - static Account* loadFile(std::string filename); + static void loadFile(std::string filename); static void saveFile(std::string filename); static int indice(Account* a); diff --git a/Source/GameInitDispose.cpp b/Source/GameInitDispose.cpp index e08ff8e..12515eb 100644 --- a/Source/GameInitDispose.cpp +++ b/Source/GameInitDispose.cpp @@ -458,7 +458,7 @@ void Game::InitGame() numchallengelevels = 14; - Account::active = Account::loadFile(Folders::getUserDataPath()+"/users"); + Account::loadFile(Folders::getUserDataPath()+"/users"); whichjointstartarray[0] = righthip; whichjointendarray[0] = rightfoot; diff --git a/Source/Menu.cpp b/Source/Menu.cpp index d3c9b0d..6a3c6fe 100644 --- a/Source/Menu.cpp +++ b/Source/Menu.cpp @@ -414,9 +414,10 @@ void Menu::Load() for (int i = 0; i < numlevels; i++) { XYZ midpoint = campaignlevels[i].getCenter(); float itemsize = campaignlevels[i].getWidth(); - const bool active = i >= Account::active->getCampaignChoicesMade(); - if (!active) + const bool active = (i >= Account::active->getCampaignChoicesMade()); + if (!active) { itemsize /= 2; + } if (i >= 1) { XYZ start = campaignlevels[i - 1].getCenter(); @@ -445,8 +446,9 @@ void Menu::Load() addLabel(0, "No More Users", 10, 400); addLabel(-2, "", 20, 400); addButton(Account::getNbAccounts() + 1, "Back", 10, 10); - for (int i = 0; i < Account::getNbAccounts(); i++) + for (int i = 0; i < Account::getNbAccounts(); i++) { addButton(i + 1, Account::get(i)->getName(), 10, 340 - 20 * (i + 1)); + } break; case 8: addButton(0, "Easier", 10, 400); @@ -771,7 +773,7 @@ void Menu::Tick() fireSound(); if (selected == 1) { flash(); - Account::active = Account::destroy(Account::active); + Account::destroyActive(); mainmenu = 7; } else if (selected == 2) { flash(); @@ -785,13 +787,14 @@ void Menu::Tick() } else if (selected < Account::getNbAccounts() + 1) { flash(); mainmenu = 5; - Account::active = Account::get(selected - 1); + Account::setActive(selected - 1); } else if (selected == Account::getNbAccounts() + 1) { flash(); - if (Account::active) + if (Account::active) { mainmenu = 5; - else + } else { mainmenu = 1; + } displaytext[0].clear(); displayselected = 0; entername = 0; @@ -872,7 +875,7 @@ void Menu::Tick() inputText(displaytext[0], &displayselected); if (!waiting) { // the input as finished if (!displaytext[0].empty()) { // with enter - Account::active = Account::add(string(displaytext[0])); + Account::add(string(displaytext[0])); mainmenu = 8; -- 2.39.2