]> git.jsancho.org Git - lugaru.git/blobdiff - Source/GameDraw.cpp
cleanup of campaign levels handling.
[lugaru.git] / Source / GameDraw.cpp
index c1fb5532b20768efb0b010629316312cd603fdc7..173b2f8645df0ea0450a7d65d37254a1e2067f18 100644 (file)
@@ -2021,75 +2021,34 @@ vector<string> Game::ListCampaigns() {
 void Game::LoadCampaign() {
        if(!accountactive)
                return;
-       if(!Mainmenuitems[7]) {
-               ifstream test(ConvertFileName((":Data:Textures:"+accountactive->getCurrentCampaign()+":World.png").c_str()));
-               if(test.good()) {
-                       LoadTexture((":Data:Textures:"+accountactive->getCurrentCampaign()+":World.png").c_str(),&Mainmenuitems[7],0,0);
-               } else {
-                       LoadTexture(":Data:Textures:World.png",&Mainmenuitems[7],0,0);
-               }
-       }
        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();
 
-       for(int i=0;i<campaignnumlevels;i++){
-               levelvisible[i]=0;
-               levelhighlight[i]=0;
+       if(!Mainmenuitems[7]) {
+               ifstream test(ConvertFileName((":Data:Textures:"+accountactive->getCurrentCampaign()+":World.png").c_str()));
+               if(test.good()) {
+                       LoadTextureData((":Data:Textures:"+accountactive->getCurrentCampaign()+":World.png").c_str(),&Mainmenuitems[7],0,0);
+               } else {
+                       LoadTextureData(":Data:Textures:World.png",&Mainmenuitems[7],0,0);
+               }
        }
 
-       levelorder[0]=0;
-       levelvisible[0]=1;
-       for(int i=0;i<accountactive->getCampaignChoicesMade();i++){
-               levelorder[i+1]=campaignnextlevel[levelorder[i]][accountactive->getCampaignChoice(i)];
-               levelvisible[levelorder[i+1]]=1;
-       }
-       int whichlevelstart = accountactive->getCampaignChoicesMade()-1;
-       if(whichlevelstart<0){
+       if(accountactive->getCampaignChoicesMade()==0) {
                accountactive->setCampaignScore(0);
                accountactive->resetFasttime();
-               campaignchoicenum=1;
-               campaignchoicewhich[0]=0;
-       }
-       else
-       {
-               campaignchoicenum=campaignnumnext[levelorder[whichlevelstart]];
-               for(int i=0;i<campaignchoicenum;i++){
-                       campaignchoicewhich[i]=campaignnextlevel[levelorder[whichlevelstart]][i];
-                       levelvisible[campaignnextlevel[levelorder[whichlevelstart]][i]]=1;
-                       levelhighlight[campaignnextlevel[levelorder[whichlevelstart]][i]]=1;
-               }
        }
 }
 
-void Game::DrawMenu()
-{
-       int i,j;
+void Game::DrawMenu() {
        static float lastcheck;
 
        lastcheck+=multiplier;
@@ -2365,49 +2324,40 @@ void Game::DrawMenu()
                }
                
                if(mainmenu==5){                        
-                       nummenuitems=NB_CAMPAIGN_MENU_ITEM+1+(accountactive?accountactive->getCampaignChoicesMade():0)+campaignchoicenum;
+                       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
 
@@ -2417,25 +2367,37 @@ void Game::DrawMenu()
                        endx[NB_CAMPAIGN_MENU_ITEM]=startx[NB_CAMPAIGN_MENU_ITEM]+400;
                        endy[NB_CAMPAIGN_MENU_ITEM]=30+480-50;
 
-                       if((accountactive?accountactive->getCampaignChoicesMade():0)) {
-                               for(i=0;i<(accountactive?accountactive->getCampaignChoicesMade():0);i++) {
-                                       sprintf (menustring[NB_CAMPAIGN_MENU_ITEM+1+i], "%s", campaigndescription[levelorder[i]]);
-                                       startx[NB_CAMPAIGN_MENU_ITEM+1+i]=30+120+campaignlocationx[levelorder[i]]*400/512;
-                                       starty[NB_CAMPAIGN_MENU_ITEM+1+i]=30+30+(512-campaignlocationy[levelorder[i]])*400/512;
+                       if(accountactive->getCampaignChoicesMade()) {
+                               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;
                                }
-                       }
-
-                       if(campaignchoicenum>0) {
-                               for(i=(accountactive?accountactive->getCampaignChoicesMade():0);i<(accountactive?accountactive->getCampaignChoicesMade():0)+campaignchoicenum;i++){
-                                       sprintf (menustring[NB_CAMPAIGN_MENU_ITEM+1+i], "%s", campaigndescription[levelorder[i]]);
-                                       startx[NB_CAMPAIGN_MENU_ITEM+1+i]=30+120+campaignlocationx[campaignchoicewhich[i-((accountactive?accountactive->getCampaignChoicesMade():0))]]*400/512;
-                                       starty[NB_CAMPAIGN_MENU_ITEM+1+i]=30+30+(512-campaignlocationy[campaignchoicewhich[i-((accountactive?accountactive->getCampaignChoicesMade():0))]])*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 << "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 {
+                               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;
                        
                }
 
@@ -2498,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;
@@ -2510,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){                        
@@ -2539,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");
@@ -2604,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);
@@ -2615,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);
@@ -2690,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;
@@ -2801,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);
@@ -2831,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)
                                {
@@ -2878,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)
                                        {
@@ -2951,7 +2920,7 @@ void Game::DrawMenu()
                                                                Normalise(&offset);
                                                                glDisable(GL_TEXTURE_2D);                                                       
 
-                                                               if(j<NB_CAMPAIGN_MENU_ITEM+(accountactive?accountactive->getCampaignChoicesMade():0)){
+                                                               if(j<NB_CAMPAIGN_MENU_ITEM+accountactive->getCampaignChoicesMade()){
                                                                        glColor4f(0.5,0,0,1);
                                                                        endsize=.5;
                                                                } else {
@@ -2963,7 +2932,7 @@ void Game::DrawMenu()
                                                                linestart+=fac*4*startsize;
                                                                lineend-=fac*4*endsize;
 
-                                                               if(!(j>NB_CAMPAIGN_MENU_ITEM+1+(accountactive?accountactive->getCampaignChoicesMade():0)+campaignchoicenum)){
+                                                               //~ if(!(j>NB_CAMPAIGN_MENU_ITEM+1+accountactive->getCampaignChoicesMade()+campaignchoicenum)){
                                                                        glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
                                                                        glPushMatrix();
                                                                                glBegin(GL_QUADS);
@@ -2977,7 +2946,7 @@ void Game::DrawMenu()
                                                                                glVertex3f(lineend.x-offset.x*endsize,          lineend.y-offset.y*endsize, 0.0f);
                                                                                glEnd();
                                                                        glPopMatrix();
-                                                               }
+                                                               //~ }
                                                                glEnable(GL_TEXTURE_2D);
                                                        }
 
@@ -2996,11 +2965,11 @@ void Game::DrawMenu()
                                                        midpoint.x=(startx[j]+endx[j])/2;
                                                        midpoint.y=(starty[j]+endy[j])/2;
                                                        if      (j>NB_CAMPAIGN_MENU_ITEM &&
-                                                               (j-NB_CAMPAIGN_MENU_ITEM-1 < (accountactive?accountactive->getCampaignChoicesMade():0))) {
+                                                               (j-NB_CAMPAIGN_MENU_ITEM-1 < accountactive->getCampaignChoicesMade())) {
                                                                        itemsize*=.5;
                                                        }
-                                                       if(!(j-NB_CAMPAIGN_MENU_ITEM-1 > (accountactive?accountactive->getCampaignChoicesMade():0)+campaignchoicenum))
-                                                       {
+                                                       //~ if(!(j-NB_CAMPAIGN_MENU_ITEM-1 > accountactive->getCampaignChoicesMade()+campaignnumnext[accountactive->getCampaignChoicesMade()]))
+                                                       //~ {
                                                                glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
                                                                glPushMatrix();
                                                                        glBegin(GL_QUADS);
@@ -3017,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)
                                                                        {
@@ -3037,7 +3006,7 @@ void Game::DrawMenu()
                                                                                glPopMatrix();
                                                                        }
                                                                }
-                                                       }
+                                                       //~ }
                                                glPopMatrix();
                                        glPopMatrix();
                                        glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix