]> git.jsancho.org Git - lugaru.git/commitdiff
Replaced all uses of Account::active outside of Account by call to active() method
authorCôme Chilliet <come@chilliet.eu>
Sun, 11 Dec 2016 08:03:38 +0000 (15:03 +0700)
committerCôme Chilliet <come@chilliet.eu>
Sun, 11 Dec 2016 08:03:38 +0000 (15:03 +0700)
Source/Account.cpp
Source/Account.h
Source/Campaign.cpp
Source/GameDraw.cpp
Source/GameInitDispose.cpp
Source/GameTick.cpp
Source/Menu.cpp

index ba91890d0de7d5173c2faa355e17354d1675ee62..bae2abda7bfcb45c29312e64da0c1ce6388f09ad 100644 (file)
@@ -30,7 +30,7 @@ using namespace std;
 extern bool devtools;
 
 vector<Account*> Account::accounts = vector<Account*>();
-Account* Account::active = nullptr;
+Account* Account::_active = nullptr;
 
 Account::Account(const string& name) : name(name), campaignProgress()
 {
@@ -52,7 +52,7 @@ void Account::setCurrentCampaign(const string& name)
 void Account::add(const string& name)
 {
     accounts.push_back(new Account(name));
-    active = accounts.back();
+    _active = accounts.back();
 }
 
 Account* Account::get(int i)
@@ -65,23 +65,23 @@ Account* Account::get(int i)
 
 void Account::setActive(int i)
 {
-    active = get(i);
+    _active = get(i);
 }
 
 void Account::destroyActive()
 {
     for (unsigned i = 0; i < accounts.size(); i++) {
-        if (accounts[i] == active) {
+        if (accounts[i] == _active) {
             accounts.erase(accounts.begin() + i);
-            active = nullptr;
+            _active = nullptr;
             return;
         }
     }
-    cerr << "Unexpected error : User " << active->getName() << " not found" << endl;
+    cerr << "Unexpected error : User " << _active->getName() << " not found" << endl;
     if (accounts.empty()) {
-        active = nullptr;
+        _active = nullptr;
     } else {
-        active = accounts.front();
+        _active = accounts.front();
     }
 }
 
@@ -191,10 +191,10 @@ void Account::loadFile(string filename)
         }
 
         fclose(tfile);
-        active = get(iactive);
+        _active = get(iactive);
     } else {
         perror(("Couldn't load users from " + filename).c_str());
-        active = nullptr;
+        _active = nullptr;
     }
 }
 
@@ -206,7 +206,7 @@ void Account::saveFile(string filename)
     tfile = fopen(filename.c_str(), "wb" );
     if (tfile) {
         fpackf(tfile, "Bi", getNbAccounts());
-        fpackf(tfile, "Bi", indice(Account::active));
+        fpackf(tfile, "Bi", indice(Account::_active));
 
         for (int i = 0; i < getNbAccounts(); i++) {
             Account* a = Account::get(i);
index 8e435025e3d6f04b1bd88c7a5ab5d2771c953915..f9bc5e1654940c614acfa9cda7b40309dee66534 100644 (file)
@@ -51,7 +51,9 @@ public:
     static void saveFile(std::string filename);
     static int indice(Account* a);
 
-    static Account* active;
+    static bool hasActive() { return (_active != nullptr); }
+    static Account& active() { return *_active; }
+    static Account* _active;
 
     void endGame();
     void winCampaignLevel(int choice, float score, float time);
index b02dd7bf58a66542ee193a5c16cc14fbfd7dfb76..fd7519888f9ffe7622a0dda4b6095a3158351375 100644 (file)
@@ -55,16 +55,17 @@ std::vector<std::string> ListCampaigns()
 
 void LoadCampaign()
 {
-    if (!Account::active)
+    if (!Account::hasActive()) {
         return;
-    std::ifstream ipstream(Folders::getResourcePath("Campaigns/" + Account::active->getCurrentCampaign() + ".txt"));
+    }
+    std::ifstream ipstream(Folders::getResourcePath("Campaigns/" + Account::active().getCurrentCampaign() + ".txt"));
     if (!ipstream.good()) {
-        if (Account::active->getCurrentCampaign() == "main") {
+        if (Account::active().getCurrentCampaign() == "main") {
             cerr << "Could not found main campaign!" << endl;
             return;
         }
-        cerr << "Could not found campaign \"" << Account::active->getCurrentCampaign() << "\", falling back to main." << endl;
-        Account::active->setCurrentCampaign("main");
+        cerr << "Could not found campaign \"" << Account::active().getCurrentCampaign() << "\", falling back to main." << endl;
+        Account::active().setCurrentCampaign("main");
         return LoadCampaign();
     }
     ipstream.ignore(256, ':');
@@ -78,16 +79,16 @@ void LoadCampaign()
     }
     ipstream.close();
 
-    std::ifstream test(Folders::getResourcePath("Textures/" + Account::active->getCurrentCampaign() + "/World.png"));
+    std::ifstream test(Folders::getResourcePath("Textures/" + Account::active().getCurrentCampaign() + "/World.png"));
     if (test.good()) {
-        Mainmenuitems[7].load("Textures/" + Account::active->getCurrentCampaign() + "/World.png", 0);
+        Mainmenuitems[7].load("Textures/" + Account::active().getCurrentCampaign() + "/World.png", 0);
     } else {
         Mainmenuitems[7].load("Textures/World.png", 0);
     }
 
-    if (Account::active->getCampaignChoicesMade() == 0) {
-        Account::active->setCampaignScore(0);
-        Account::active->resetFasttime();
+    if (Account::active().getCampaignChoicesMade() == 0) {
+        Account::active().setCampaignScore(0);
+        Account::active().resetFasttime();
     }
 }
 
index 76318a4775c4536f1ebba824e8825c73572e1f93..a346f23f2b519a1c353cefad412bc58f5f783e41 100644 (file)
@@ -1051,9 +1051,9 @@ int Game::DrawGLScene(StereoSide side)
             if (!tutoriallevel && !winfreeze && !Dialog::inDialog() && !mainmenu) {
                 if (campaign) {
                     if (scoreadded)
-                        sprintf (string, "Score: %d", (int)Account::active->getCampaignScore());
+                        sprintf (string, "Score: %d", (int)Account::active().getCampaignScore());
                     else
-                        sprintf (string, "Score: %d", (int)Account::active->getCampaignScore() + (int)bonustotal);
+                        sprintf (string, "Score: %d", (int)Account::active().getCampaignScore() + (int)bonustotal);
                 }
                 if (!campaign)
                     sprintf (string, "Score: %d", (int)bonustotal);
index 12515eb7cab8442f660cb0e8cb497a1c0d4ca9b3..d1f7b9d5450ed553140db8d78f67667b3802f3c2 100644 (file)
@@ -79,7 +79,7 @@ void Dispose()
     LOGFUNC;
 
     if (Game::endgame == 2) {
-        Account::active->endGame();
+        Account::active().endGame();
         Game::endgame = 0;
     }
 
index fab80e9e5ac4c29b7c07fc9422e0e27236ae2d80..1b865f13c9b2b63a77d8478b0d6a21bccf986a42 100644 (file)
@@ -692,8 +692,9 @@ void Game::Loadlevel(const std::string& name)
     damagedealt = 0;
     damagetaken = 0;
 
-    if (Account::active)
-        difficulty = Account::active->getDifficulty();
+    if (Account::hasActive()) {
+        difficulty = Account::active().getDifficulty();
+    }
 
     Hotspot::hotspots.clear();
     Hotspot::current = -1;
@@ -6217,11 +6218,11 @@ void Game::TickOnceAfter()
             if (changedelay > 0 && !Person::players[0]->dead && !won) {
                 //high scores, awards, win
                 if (campaign) {
-                    Account::active->winCampaignLevel(whichchoice, bonustotal, leveltime);
+                    Account::active().winCampaignLevel(whichchoice, bonustotal, leveltime);
                     scoreadded = 1;
                 } else {
                     wonleveltime = leveltime;
-                    Account::active->winLevel(whichlevel, bonustotal - startbonustotal, leveltime);
+                    Account::active().winLevel(whichlevel, bonustotal - startbonustotal, leveltime);
                 }
                 won = 1;
             }
@@ -6264,7 +6265,7 @@ void Game::TickOnceAfter()
 
                     fireSound(firestartsound);
 
-                    Loadlevel(campaignlevels[Account::active->getCampaignChoicesMade()].mapname.c_str());
+                    Loadlevel(campaignlevels[Account::active().getCampaignChoicesMade()].mapname.c_str());
 
                     fireSound();
 
index 6a3c6fed0d21ac88e12516db2465ef8eb93136de..14f54aa49169e316f40e5f6bdc0d2da9fde92e2c 100644 (file)
@@ -397,24 +397,24 @@ void Menu::Load()
         break;
     case 5: {
         LoadCampaign();
-        addLabel(-1, Account::active->getName(), 5, 400);
+        addLabel(-1, Account::active().getName(), 5, 400);
         addButton(1, "Tutorial", 5, 300);
         addButton(2, "Challenge", 5, 240);
         addButton(3, "Delete User", 400, 10);
         addButton(4, "Main Menu", 5, 10);
         addButton(5, "Change User", 5, 180);
-        addButton(6, "Campaign : " + Account::active->getCurrentCampaign(), 200, 420);
+        addButton(6, "Campaign : " + Account::active().getCurrentCampaign(), 200, 420);
 
         //show campaign map
         //with (2,-5) offset from old code
         addImage(-1, Mainmenuitems[7], 150 + 2, 60 - 5, 400, 400);
         //show levels
-        int numlevels = Account::active->getCampaignChoicesMade();
+        int numlevels = Account::active().getCampaignChoicesMade();
         numlevels += numlevels > 0 ? campaignlevels[numlevels - 1].nextlevel.size() : 1;
         for (int i = 0; i < numlevels; i++) {
             XYZ midpoint = campaignlevels[i].getCenter();
             float itemsize = campaignlevels[i].getWidth();
-            const bool active = (i >= Account::active->getCampaignChoicesMade());
+            const bool active = (i >= Account::active().getCampaignChoicesMade());
             if (!active) {
                 itemsize /= 2;
             }
@@ -462,18 +462,18 @@ void Menu::Load()
             if (name.size() < 17) {
                 name.append((17 - name.size()), ' ');
             }
-            name += to_string(int(Account::active->getHighScore(i)));
+            name += to_string(int(Account::active().getHighScore(i)));
             if (name.size() < 32) {
                 name.append((32 - name.size()), ' ');
             }
-            int fasttime = (int)round(Account::active->getFastTime(i));
+            int fasttime = (int)round(Account::active().getFastTime(i));
             name += to_string(int((fasttime - fasttime % 60) / 60));
             name += ":";
             if (fasttime % 60 < 10)
                 name += "0";
             name += to_string(fasttime % 60);
 
-            addButton(i, name, 10, 400 - i * 25, i > Account::active->getProgress() ? 0.5 : 1, 0, 0);
+            addButton(i, name, 10, 400 - i * 25, i > Account::active().getProgress() ? 0.5 : 1, 0, 0);
         }
 
         addButton(-1, "             High Score      Best Time", 10, 440);
@@ -484,8 +484,8 @@ void Menu::Load()
         addLabel(1, "You have avenged your family and", 140, 300);
         addLabel(2, "restored peace to the island of Lugaru.", 110, 270);
         addButton(3, "Back", 10, 10);
-        addLabel(4, string("Your score:         ") + to_string((int)Account::active->getCampaignScore()), 190, 200);
-        addLabel(5, string("Highest score:      ") + to_string((int)Account::active->getCampaignHighScore()), 190, 180);
+        addLabel(4, string("Your score:         ") + to_string((int)Account::active().getCampaignScore()), 190, 200);
+        addLabel(5, string("Highest score:      ") + to_string((int)Account::active().getCampaignHighScore()), 190, 180);
     }
     break;
     case 18:
@@ -537,7 +537,7 @@ void Menu::Tick()
 
     // some specific case where we do something even if the left mouse button is not pressed.
     if ((mainmenu == 5) && (endgame == 2)) {
-        Account::active->endGame();
+        Account::active().endGame();
         endgame = 0;
     }
     if (mainmenu == 10)
@@ -563,7 +563,7 @@ void Menu::Tick()
                 } else { //new game
                     fireSound(firestartsound);
                     flash();
-                    mainmenu = (Account::active ? 5 : 7);
+                    mainmenu = (Account::hasActive() ? 5 : 7);
                     selected = -1;
                 }
                 break;
@@ -704,7 +704,7 @@ void Menu::Tick()
         case 5:
             fireSound();
             flash();
-            if ((selected - NB_CAMPAIGN_MENU_ITEM >= Account::active->getCampaignChoicesMade())) {
+            if ((selected - NB_CAMPAIGN_MENU_ITEM >= Account::active().getCampaignChoicesMade())) {
                 startbonustotal = 0;
 
                 loading = 2;
@@ -714,8 +714,8 @@ void Menu::Tick()
                     TickOnceAfter();
                 else
                     LoadStuff();
-                whichchoice = selected - NB_CAMPAIGN_MENU_ITEM - Account::active->getCampaignChoicesMade();
-                actuallevel = (Account::active->getCampaignChoicesMade() > 0 ? campaignlevels[Account::active->getCampaignChoicesMade() - 1].nextlevel[whichchoice] : 0);
+                whichchoice = selected - NB_CAMPAIGN_MENU_ITEM - Account::active().getCampaignChoicesMade();
+                actuallevel = (Account::active().getCampaignChoicesMade() > 0 ? campaignlevels[Account::active().getCampaignChoicesMade() - 1].nextlevel[whichchoice] : 0);
                 visibleloading = 1;
                 stillloading = 1;
                 Loadlevel(campaignlevels[actuallevel].mapname.c_str());
@@ -756,14 +756,14 @@ void Menu::Tick()
             case 6:
                 vector<string> campaigns = ListCampaigns();
                 vector<string>::iterator c;
-                if ((c = find(campaigns.begin(), campaigns.end(), Account::active->getCurrentCampaign())) == campaigns.end()) {
+                if ((c = find(campaigns.begin(), campaigns.end(), Account::active().getCurrentCampaign())) == campaigns.end()) {
                     if (!campaigns.empty())
-                        Account::active->setCurrentCampaign(campaigns.front());
+                        Account::active().setCurrentCampaign(campaigns.front());
                 } else {
                     c++;
                     if (c == campaigns.end())
                         c = campaigns.begin();
-                    Account::active->setCurrentCampaign(*c);
+                    Account::active().setCurrentCampaign(*c);
                 }
                 Load();
                 break;
@@ -790,7 +790,7 @@ void Menu::Tick()
                 Account::setActive(selected - 1);
             } else if (selected == Account::getNbAccounts() + 1) {
                 flash();
-                if (Account::active) {
+                if (Account::hasActive()) {
                     mainmenu = 5;
                 } else {
                     mainmenu = 1;
@@ -804,11 +804,11 @@ void Menu::Tick()
             fireSound();
             flash();
             if (selected <= 2)
-                Account::active->setDifficulty(selected);
+                Account::active().setDifficulty(selected);
             mainmenu = 5;
             break;
         case 9:
-            if (selected < numchallengelevels && selected <= Account::active->getProgress()) {
+            if (selected < numchallengelevels && selected <= Account::active().getProgress()) {
                 fireSound();
                 flash();