]> git.jsancho.org Git - lugaru.git/commitdiff
cleanup of campaign levels handling.
authorCôme BERNIGAUD <come.bernigaud@gmail.com>
Sat, 14 May 2011 14:01:18 +0000 (16:01 +0200)
committerCôme BERNIGAUD <come.bernigaud@gmail.com>
Sat, 14 May 2011 14:01:18 +0000 (16:01 +0200)
Source/Game.cpp
Source/Game.h
Source/GameDraw.cpp
Source/GameInitDispose.cpp
Source/GameTick.cpp

index a441e0cd9f8fc78a212d57fd4816be7c84e44fcd..1f48171647e57b751cd84952c484a1e82a0eddde 100644 (file)
@@ -103,17 +103,6 @@ Game::Game()
 */
        stealthloading = 0;
 
-       campaignnumlevels = 0;
-
-       memset(campaignmapname, 0, sizeof(campaignmapname));
-       memset(campaigndescription, 0, sizeof(campaigndescription));
-       memset(campaignchoosenext, 0, sizeof(campaignchoosenext));
-       memset(campaignnumnext, 0, sizeof(campaignnumnext));
-       memset(campaignnextlevel, 0, sizeof(campaignnextlevel));
-       memset(campaignlocationx, 0, sizeof(campaignlocationx));
-       memset(campaignlocationy, 0, sizeof(campaignlocationy));
-       memset(campaignlocationy, 0, sizeof(campaignlocationy));
-
        whichchoice = 0;
        actuallevel = 0;
 
index 97e3487d61dcabda13282b8e0fa7bc9616230bf1..19604e1d5047931b3aeecb09407d358aab8d9e40 100644 (file)
@@ -65,6 +65,65 @@ extern GLuint rabbittexture;
 
 struct TextureInfo;
 
+class CampaignLevel
+{
+private:
+       struct Position
+       {
+               int x;
+               int y;
+       };
+public: 
+       std::string mapname;
+       std::string description;
+       int choosenext;
+               /*      
+               0 = Immediately load next level at the end of this one.
+               1 = Go back to the world map.
+               2 = Don't bring up the Fiery loading screen. Maybe other things, I've not investigated.
+               */
+       //int numnext; // 0 on final level. As David said: he meant to add story branching, but he eventually hadn't. 
+       std::vector<int> nextlevel;
+       Position location;
+       
+       CampaignLevel() {
+               choosenext = 1;
+               location.x = 0;
+               location.y = 0;
+       }
+       
+       istream& operator<< (istream& is) {
+               is.ignore(256,':');
+               is.ignore(256,':');
+               is.ignore(256,' ');
+               is >> mapname;
+               is.ignore(256,':');
+               is >> description;
+               for(int pos = description.find('_');pos!=string::npos;pos = description.find('_',pos)) {
+                       description.replace(pos,1,1,' ');
+               }
+               is.ignore(256,':');
+               is >> choosenext;
+               is.ignore(256,':');
+               int numnext,next;
+               is >> numnext;
+               for(int j=0;j<numnext;j++) {
+                       is.ignore(256,':');
+                       is >> next;
+                       nextlevel.push_back(next-1);
+               }
+               is.ignore(256,':');
+               is >> location.x;
+               is.ignore(256,':');
+               is >> location.y;
+               return is;
+       }
+       
+       friend istream& operator>> (istream& is, CampaignLevel& cl) {
+               return cl << is;
+       }
+};
+
 class Game
 {
        public:
@@ -135,14 +194,7 @@ class Game
 
                bool stealthloading;
 
-               int campaignnumlevels;
-               char campaignmapname[50][256];
-               char campaigndescription[50][256];
-               int campaignchoosenext[50];
-               int campaignnumnext[50]; // 0 on final level. As David said: he meant to add story branching, but he eventually hadn't. 
-               int campaignnextlevel[50][10];
-               int campaignlocationx[50];
-               int campaignlocationy[50];
+               std::vector<CampaignLevel> campaignlevels;
                int whichchoice;
                int actuallevel;
 
index 2795bd75b159eb39152747bdf3224d3dadbd34b0..173b2f8645df0ea0450a7d65d37254a1e2067f18 100644 (file)
@@ -2023,30 +2023,13 @@ void Game::LoadCampaign() {
                return;
        ifstream ipstream(ConvertFileName((":Data:Campaigns:"+accountactive->getCurrentCampaign()+".txt").c_str()));
        ipstream.ignore(256,':');
-       ipstream >> campaignnumlevels;
-       for(int i=0;i<campaignnumlevels;i++) {
-               ipstream.ignore(256,':');
-               ipstream.ignore(256,':');
-               ipstream.ignore(256,' ');
-               ipstream >> campaignmapname[i];
-               ipstream.ignore(256,':');
-               ipstream >> campaigndescription[i];
-               for(int j=0;j<256;j++){
-                       if(campaigndescription[i][j]=='_')campaigndescription[i][j]=' ';
-               }
-               ipstream.ignore(256,':');
-               ipstream >> campaignchoosenext[i];
-               ipstream.ignore(256,':');
-               ipstream >> campaignnumnext[i];
-               for(int j=0;j<campaignnumnext[i];j++){
-                       ipstream.ignore(256,':');
-                       ipstream >> campaignnextlevel[i][j];
-                       campaignnextlevel[i][j]-=1;
-               }
-               ipstream.ignore(256,':');
-               ipstream >> campaignlocationx[i];
-               ipstream.ignore(256,':');
-               ipstream >> campaignlocationy[i];
+       int numlevels;
+       ipstream >> numlevels;
+       campaignlevels.clear();
+       for(int i=0;i<numlevels;i++) {
+               CampaignLevel cl;
+               ipstream >> cl;
+               campaignlevels.push_back(cl);
        }
        ipstream.close();
 
@@ -2066,7 +2049,6 @@ void Game::LoadCampaign() {
 }
 
 void Game::DrawMenu() {
-       int i,j;
        static float lastcheck;
 
        lastcheck+=multiplier;
@@ -2342,49 +2324,40 @@ void Game::DrawMenu() {
                }
                
                if(mainmenu==5){                        
-                       nummenuitems=NB_CAMPAIGN_MENU_ITEM+1+accountactive->getCampaignChoicesMade()+(accountactive->getCampaignChoicesMade()>0?campaignnumnext[accountactive->getCampaignChoicesMade()-1]:1);
+                       nummenuitems=NB_CAMPAIGN_MENU_ITEM+1+accountactive->getCampaignChoicesMade()+(accountactive->getCampaignChoicesMade()>0?campaignlevels[accountactive->getCampaignChoicesMade()-1].nextlevel.size():1);
 
                        sprintf (menustring[0], "%s",accountactive->getName());
                        startx[0]=5;
                        starty[0]=400;
-                       endx[0]=startx[0]+strlen(menustring[0])*10;
-                       endy[0]=starty[0]+20;
 
                        sprintf (menustring[1], "Tutorial");
                        startx[1]=5;
                        starty[1]=300;
-                       endx[1]=startx[1]+strlen(menustring[1])*10;
-                       endy[1]=starty[1]+20;
 
                        sprintf (menustring[2], "Challenge");
                        startx[2]=5;
                        starty[2]=240;
-                       endx[2]=startx[2]+strlen(menustring[2])*10;
-                       endy[2]=starty[2]+20;
 
                        sprintf (menustring[3], "Delete User");
                        startx[3]=400;
                        starty[3]=10;
-                       endx[3]=startx[3]+strlen(menustring[3])*10;
-                       endy[3]=starty[3]+20;
 
                        sprintf (menustring[4], "Main Menu");
                        startx[4]=5;
                        starty[4]=10;
-                       endx[4]=startx[4]+strlen(menustring[4])*10;
-                       endy[4]=starty[4]+20;
 
                        sprintf (menustring[5], "Change User");
                        startx[5]=5;
-                       endx[5]=startx[5]+strlen(menustring[5])*10;
                        starty[5]=180;
-                       endy[5]=starty[5]+20;
                        
                        sprintf (menustring[6], "Campaign : %s", accountactive->getCurrentCampaign().c_str());
                        startx[6]=200;
-                       endx[6]=startx[6]+strlen(menustring[6])*10;
                        starty[6]=420;
-                       endy[6]=starty[6]+20;
+
+                       for(int i=0;i<NB_CAMPAIGN_MENU_ITEM;++i) {
+                               endx[i]=startx[i]+strlen(menustring[i])*10;
+                               endy[i]=starty[i]+20;
+                       }
 
                        //World
 
@@ -2395,28 +2368,36 @@ void Game::DrawMenu() {
                        endy[NB_CAMPAIGN_MENU_ITEM]=30+480-50;
 
                        if(accountactive->getCampaignChoicesMade()) {
-                               for(i=0;i<accountactive->getCampaignChoicesMade();i++) {
-                                       sprintf (menustring[NB_CAMPAIGN_MENU_ITEM+1+i], "%s", campaigndescription[i]);
-                                       startx[NB_CAMPAIGN_MENU_ITEM+1+i]=30+120+campaignlocationx[i]*400/512;
-                                       starty[NB_CAMPAIGN_MENU_ITEM+1+i]=30+30+(512-campaignlocationy[i])*400/512;
+                               cout << "niveaux passés" << endl;
+                               for(int i=0;i<accountactive->getCampaignChoicesMade();i++) {
+                                       cout << campaignlevels[i].location.x << "x" << campaignlevels[i].location.y << endl;
+                                       sprintf (menustring[NB_CAMPAIGN_MENU_ITEM+1+i], "%s", campaignlevels[i].description.c_str());
+                                       startx[NB_CAMPAIGN_MENU_ITEM+1+i]=30+120+campaignlevels[i].location.x*400/512;
+                                       starty[NB_CAMPAIGN_MENU_ITEM+1+i]=30+30+(512-campaignlevels[i].location.y)*400/512;
                                        endx[NB_CAMPAIGN_MENU_ITEM+1+i]=startx[NB_CAMPAIGN_MENU_ITEM+1+i]+10;
                                        endy[NB_CAMPAIGN_MENU_ITEM+1+i]=starty[NB_CAMPAIGN_MENU_ITEM+1+i]+10;
+                                       cout << "-->" << startx[NB_CAMPAIGN_MENU_ITEM+1+i] << "x" << starty[NB_CAMPAIGN_MENU_ITEM+1+i] << endl;
                                }
-                               for(i=0;i<campaignnumnext[accountactive->getCampaignChoicesMade()-1];i++) {
-                                       int j = campaignnextlevel[accountactive->getCampaignChoicesMade()-1][i];
-                                       sprintf (menustring[NB_CAMPAIGN_MENU_ITEM+1+j], "%s", campaigndescription[j]);
-                                       startx[NB_CAMPAIGN_MENU_ITEM+1+j]=30+120+campaignlocationx[j]*400/512;
-                                       starty[NB_CAMPAIGN_MENU_ITEM+1+j]=30+30+(512-campaignlocationy[j])*400/512;
+                               cout << "niveaux à choisir" << endl;
+                               for(int i=0;i<campaignlevels[accountactive->getCampaignChoicesMade()-1].nextlevel.size();i++) {
+                                       int j = campaignlevels[accountactive->getCampaignChoicesMade()-1].nextlevel[i];
+                                       cout << campaignlevels[j].location.x << "x" << campaignlevels[j].location.y << endl;
+                                       sprintf (menustring[NB_CAMPAIGN_MENU_ITEM+1+j], "%s", campaignlevels[j].description.c_str());
+                                       startx[NB_CAMPAIGN_MENU_ITEM+1+j]=30+120+campaignlevels[j].location.x*400/512;
+                                       starty[NB_CAMPAIGN_MENU_ITEM+1+j]=30+30+(512-campaignlevels[j].location.y)*400/512;
                                        endx[NB_CAMPAIGN_MENU_ITEM+1+j]=startx[NB_CAMPAIGN_MENU_ITEM+1+j]+10;
                                        endy[NB_CAMPAIGN_MENU_ITEM+1+j]=starty[NB_CAMPAIGN_MENU_ITEM+1+j]+10;
+                                       cout << "-->" << startx[NB_CAMPAIGN_MENU_ITEM+1+j] << "x" << starty[NB_CAMPAIGN_MENU_ITEM+1+j] << endl;
                                }
                        } else {
-                               sprintf (menustring[NB_CAMPAIGN_MENU_ITEM+1], "%s", campaigndescription[0]);
-                               startx[NB_CAMPAIGN_MENU_ITEM+1]=30+120+campaignlocationx[0]*400/512;
-                               starty[NB_CAMPAIGN_MENU_ITEM+1]=30+30+(512-campaignlocationy[0])*400/512;
+                               cout << "premier niveau" << endl;
+                               sprintf (menustring[NB_CAMPAIGN_MENU_ITEM+1], "%s", campaignlevels[0].description.c_str());
+                               startx[NB_CAMPAIGN_MENU_ITEM+1]=30+120+campaignlevels[0].location.x*400/512;
+                               starty[NB_CAMPAIGN_MENU_ITEM+1]=30+30+(512-campaignlevels[0].location.y)*400/512;
                                endx[NB_CAMPAIGN_MENU_ITEM+1]=startx[NB_CAMPAIGN_MENU_ITEM+1]+10;
                                endy[NB_CAMPAIGN_MENU_ITEM+1]=starty[NB_CAMPAIGN_MENU_ITEM+1]+10;
                        }
+                       cout << nummenuitems << " items" << endl;
                        
                }
 
@@ -2479,7 +2460,7 @@ void Game::DrawMenu() {
 
 
                        num=1;
-                       for(i=0;i<Account::getNbAccounts();i++){
+                       for(int i=0;i<Account::getNbAccounts();i++){
                                sprintf (menustring[num], "%s",Account::get(i)->getName());
                                startx[num]=10;
                                starty[num]=360-20-20*num;
@@ -2491,8 +2472,8 @@ void Game::DrawMenu() {
 
                        sprintf (menustring[num], "Back");
                        startx[num]=10;
-                       endx[num]=startx[num]+strlen(menustring[num])*10;
                        starty[num]=10;
+                       endx[num]=startx[num]+strlen(menustring[num])*10;
                        endy[num]=starty[num]+20;
                }
                if(mainmenu==8){                        
@@ -2520,26 +2501,28 @@ void Game::DrawMenu() {
                        nummenuitems=2+numchallengelevels;
                        char temp[255];
 
-                       for(j=0;j<numchallengelevels;j++){
-                               for(i=0;i<255;i++)menustring[j][i]='\0';
-                               sprintf (temp, "Level %d",j+1);
-                               strcpy(menustring[j],temp);
-                               for(i=0;i<17;i++)if(menustring[j][i]=='\0')menustring[j][i]=' ';
-                               menustring[j][17]='\0';
-                               sprintf (temp, "%d",(int)accountactive->getHighScore(j));
-                               strcat(menustring[j],temp);
-                               for(i=18;i<32;i++)if(menustring[j][i]=='\0')menustring[j][i]=' ';
-                               menustring[j][32]='\0';
-                               sprintf (temp, "%d:",(int)(((int)accountactive->getFastTime(j)-(int)(accountactive->getFastTime(j))%60)/60));
-                               strcat(menustring[j],temp);
-                               if((int)(accountactive->getFastTime(j))%60<10)strcat(menustring[j],"0");
-                               sprintf (temp, "%d",(int)(accountactive->getFastTime(j))%60);
-                               strcat(menustring[j],temp);
-
-                               startx[j]=10;
-                               starty[j]=400-j*25;
-                               endx[j]=startx[j]+strlen(menustring[j])*10;
-                               endy[j]=starty[j]+20;
+                       for(int i=0;i<numchallengelevels;i++){
+                string name="";
+                sprintf (temp, "Level %d",i+1);
+                for(int j=strlen(temp);j<17;j++)
+                        strcat(temp," ");
+                name+=temp;
+                sprintf (temp, "%d",(int)accountactive->getHighScore(i));
+                for(int j=strlen(temp);j<(32-17);j++)
+                        strcat(temp," ");
+                name+=temp;
+                sprintf (temp, "%d:",(int)(((int)accountactive->getFastTime(i)-(int)(accountactive->getFastTime(i))%60)/60));
+                if((int)(accountactive->getFastTime(i))%60<10)strcat(temp,"0");
+                name+=temp;
+                sprintf (temp, "%d",(int)(accountactive->getFastTime(i))%60);
+                name+=temp;
+
+                               sprintf(menustring[i],"%s",name.c_str());
+
+                               startx[i]=10;
+                               starty[i]=400-i*25;
+                               endx[i]=startx[i]+strlen(menustring[i])*10;
+                               endy[i]=starty[i]+20;
                        }
 
                        sprintf (menustring[numchallengelevels], "Back");
@@ -2585,10 +2568,13 @@ void Game::DrawMenu() {
                        starty[3]=10;
                        endy[3]=starty[3]+20;
 
-                       for(i=0;i<255;i++)menustring[4][i]='\0';
+                       for(int i=0;i<255;i++)
+                               menustring[4][i]='\0';
                        sprintf (temp, "Your score:");
                        strcpy(menustring[4],temp);
-                       for(i=0;i<20;i++)if(menustring[4][i]=='\0')menustring[4][i]=' ';
+                       for(int i=0;i<20;i++)
+                               if(menustring[4][i]=='\0')
+                                       menustring[4][i]=' ';
                        menustring[4][20]='\0';
                        sprintf (temp, "%d",(int)accountactive->getCampaignScore());
                        strcat(menustring[4],temp);
@@ -2596,10 +2582,12 @@ void Game::DrawMenu() {
                        endx[4]=startx[4]+strlen(menustring[4])*10;
                        starty[4]=200;
                        endy[4]=starty[4]+20;
-                       for(i=0;i<255;i++)menustring[5][i]='\0';
+                       for(int i=0;i<255;i++)
+                               menustring[5][i]='\0';
                        sprintf (temp, "Highest score:");
                        strcpy(menustring[5],temp);
-                       for(i=0;i<20;i++)if(menustring[5][i]=='\0')menustring[5][i]=' ';
+                       for(int i=0;i<20;i++)
+                               if(menustring[5][i]=='\0')menustring[5][i]=' ';
                        menustring[5][20]='\0';
                        sprintf (temp, "%d",(int)accountactive->getCampaignHighScore());
                        strcat(menustring[5],temp);
@@ -2671,14 +2659,14 @@ void Game::DrawMenu() {
 
        selected=-1;
 
-       for(i=0;i<nummenuitems;i++) {
+       for(int i=0;i<nummenuitems;i++) {
                if((mousecoordh/screenwidth*640)>startx[i]&&(mousecoordh/screenwidth*640)<endx[i]&&480-(mousecoordv/screenheight*480)>starty[i]&&480-(mousecoordv/screenheight*480)<endy[i]) {
                        if((mainmenu!=5) && (mainmenu!=1) && (mainmenu!=2)) selected=i; // username in menu 5 and game title in menu 1&2 can't be selected
                        else if( (i>0) && (i!=NB_CAMPAIGN_MENU_ITEM) ) selected=i;
                }
        }
 
-       for(i=0;i<nummenuitems;i++) {
+       for(int i=0;i<nummenuitems;i++) {
                if(selected==i) {
                        selectedlong[i]+=multiplier*5;
                        if(selectedlong[i]>1) selectedlong[i]=1;
@@ -2782,7 +2770,7 @@ void Game::DrawMenu() {
        glPushMatrix();                                                                         // Store The Modelview Matrix
        glLoadIdentity();                                                               // Reset The Modelview Matrix
        glEnable(GL_TEXTURE_2D);
-       for(j=0;j<nummenuitems;j++)
+       for(int j=0;j<nummenuitems;j++)
        {
                //glDisable(GL_BLEND);
                glEnable(GL_ALPHA_TEST);
@@ -2812,7 +2800,7 @@ void Game::DrawMenu() {
                        glEnable(GL_BLEND);
                        //glDisable(GL_ALPHA_TEST);
                        glBlendFunc(GL_SRC_ALPHA,GL_ONE);
-                       for(i=0;i<10;i++)
+                       for(int i=0;i<10;i++)
                        {
                                if(1-((float)i)/10-(1-selectedlong[j])>0)
                                {
@@ -2859,7 +2847,7 @@ void Game::DrawMenu() {
                                glPopMatrix();
                                glEnable(GL_BLEND);
                                glBlendFunc(GL_SRC_ALPHA,GL_ONE);
-                               for(i=0;i<15;i++)
+                               for(int i=0;i<15;i++)
                                {
                                        if(1-((float)i)/15-(1-selectedlong[j])>0)
                                        {
@@ -2998,7 +2986,7 @@ void Game::DrawMenu() {
                                                                glEnable(GL_BLEND);
                                                                //glDisable(GL_ALPHA_TEST);
                                                                if(j<4) glBlendFunc(GL_SRC_ALPHA,GL_ONE); // Black is transparent
-                                                               for(i=0;i<10;i++)
+                                                               for(int i=0;i<10;i++)
                                                                {
                                                                        if(1-((float)i)/10-(1-selectedlong[j])>0)
                                                                        {
index 7edf75d9e6f44b6027335ea68d94a0e31fdb8fde..65780a0846643a2b98eaeec28519d8005ef5b0f2 100644 (file)
@@ -776,9 +776,9 @@ void Game::InitGame()
 
        LOG("Initializing sound system...");
 
-    int output = -1;
-
     #if PLATFORM_LINUX
+    int output = -1;
+    
     extern bool cmdline(const char *cmd);
     unsigned char rc = 0;
     output = OPENAL_OUTPUT_ALSA;  // Try alsa first...
index 4cae8b1415cc0eacc038c8ca4d45c26aa45d11f7..4048f8783dbe7960a84857db1f80d8e125cc3e5e 100644 (file)
@@ -5612,10 +5612,10 @@ void Game::MenuTick(){
                                        else
                                                LoadStuff();
                                        whichchoice=selected-NB_CAMPAIGN_MENU_ITEM-1-accountactive->getCampaignChoicesMade();
-                                       actuallevel=(accountactive->getCampaignChoicesMade()>0?campaignnextlevel[accountactive->getCampaignChoicesMade()-1][whichchoice]:0);
+                                       actuallevel=(accountactive->getCampaignChoicesMade()>0?campaignlevels[accountactive->getCampaignChoicesMade()-1].nextlevel[whichchoice]:0);
                                        visibleloading=1;
                                        stillloading=1;
-                                       Loadlevel(campaignmapname[actuallevel]);
+                                       Loadlevel(campaignlevels[actuallevel].mapname.c_str());
                                        campaign=1;
                                        mainmenu=0;
                                        gameon=1;
@@ -5871,7 +5871,7 @@ void Game::Tick(){
                if(mainmenu&&endgame==1)
             mainmenu=10;
         //go to level select after completing a campaign level
-        if(campaign&&winfreeze&&mainmenu==0&&campaignchoosenext[actuallevel]==1) {
+        if(campaign&&winfreeze&&mainmenu==0&&campaignlevels[actuallevel].choosenext==1) {
             mainmenu=5;
             gameon=0;
             winfreeze=0;
@@ -7699,7 +7699,7 @@ void Game::TickOnceAfter(){
 
                                        fireSound(firestartsound);
 
-                                       Loadlevel(campaignmapname[accountactive->getCampaignChoicesMade()]);
+                                       Loadlevel(campaignlevels[accountactive->getCampaignChoicesMade()].mapname.c_str());
 
                                        fireSound();
 
@@ -7733,11 +7733,11 @@ void Game::TickOnceAfter(){
                 // 0 = load next level
                 // 1 = go back to level select screen
                 // 2 = stealthload next level
-                               if(mainmenu==0&&winfreeze&&(campaignchoosenext[actuallevel])==1){
-                                       if(campaignnumnext[actuallevel]==0)
+                               if(mainmenu==0&&winfreeze&&(campaignlevels[actuallevel].choosenext)==1){
+                                       if(campaignlevels[actuallevel].nextlevel.empty())
                                                endgame=1;
                                } else if(mainmenu==0&&winfreeze) {
-                                       stealthloading = (campaignchoosenext[actuallevel]==2);
+                                       stealthloading = (campaignlevels[actuallevel].choosenext==2);
 
                                        if(!stealthloading){
                                                fireSound(firestartsound);
@@ -7755,10 +7755,10 @@ void Game::TickOnceAfter(){
                                        if(!firstload)
                                                LoadStuff();
                                        whichchoice=0;
-                                       actuallevel=campaignnextlevel[actuallevel][0];
+                                       actuallevel=campaignlevels[actuallevel].nextlevel.front();
                                        visibleloading=1;
                                        stillloading=1;
-                                       Loadlevel(campaignmapname[actuallevel]);
+                                       Loadlevel(campaignlevels[actuallevel].mapname.c_str());
                                        campaign=1;
                                        mainmenu=0;
                                        gameon=1;