Like Tree1Likes
  • 1 Post By jimblumberg

C++ Code Breaking at certain part. Tried to fix for weeks. Any help would be nice.

This is a discussion on C++ Code Breaking at certain part. Tried to fix for weeks. Any help would be nice. within the C++ Programming forums, part of the General Programming Boards category; Good afternoon C++ Programmers. I was unsure whether to post this in here or Game Programming, but since my issue ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    1

    Exclamation C++ Code Breaking at certain part. Tried to fix for weeks. Any help would be nice.

    Good afternoon C++ Programmers. I was unsure whether to post this in here or Game Programming, but since my issue relates more to C++ code interactions than libraries, graphics, sound, etc. this board seemed a better fit.

    Here's the problem:
    My game is a PvP tower defence stye game where the player not only builds towers on his side of the field, but sends minions down to the other player's side. The first part I am done with. The second part is where the issue lies.

    When a player selects a lane to send a minion down, a panel pops up that allows the player to chose which minion to send. There are 10 possible minion types to send, and 9 of them work fine. The 10th one crashes the game when it is selected. Normally this would just sound like an off-by-one error or an issue with his image not loading, but that is not the case. If the player selects the 10th minion first, it works fine, but if it is selected any other time it breaks.
    I have been trying to fix this for two weeks and it is starting to make me upset. I don't know which part of the code to upload, so I will just do the whole class right now.

    I would be super grateful for any help/assistance/advice you guys have for me.



    Code:
    #include <allegro.h>
    #include <string>
    #include <iostream>
    #include <vector>
    #include "HUD.h"
    HUD::HUD(BITMAP *done)
    {
        palette = done;
        
    }
    void HUD::initThings()
    {
        BPanel1 = load_bitmap("Images/HUD/MPanel1.bmp",NULL);
        BPanel2 = load_bitmap("Images/HUD/MPanel2.bmp",NULL);
        BPanel5 = load_bitmap("Images/HUD/MPanel5.bmp",NULL);
        C2BM1 = load_bitmap("Images/ClickToBuildMinion1.bmp",NULL);
        ERROR3 = load_bitmap("Images/HUD/ERROR3.bmp",NULL);
        MinInfo[0] = load_bitmap("Images/Minions/Info/Mi0.bmp",NULL);
        MinInfo[1] = load_bitmap("Images/Minions/Info/Mi1.bmp",NULL);
        MinInfo[2] = load_bitmap("Images/Minions/Info/Mi2.bmp",NULL);
        MinInfo[3] = load_bitmap("Images/Minions/Info/Mi3.bmp",NULL);
        MinInfo[4] = load_bitmap("Images/Minions/Info/Mi4.bmp",NULL);
        MinInfo[5] = load_bitmap("Images/Minions/Info/Mi5.bmp",NULL);
        MinInfo[6] = load_bitmap("Images/Minions/Info/Mi6.bmp",NULL);
        MinInfo[7] = load_bitmap("Images/Minions/Info/Mi7.bmp",NULL);
        MinInfo[8] = load_bitmap("Images/Minions/Info/Mi8.bmp",NULL);
        MinInfo[9] = load_bitmap("Images/Minions/Info/Mi9.bmp",NULL);
        
        
        
        selMinion = -1;
        selLane = 0;
        MenuP = false;
        ArmyPos = 0;
        
        Cheap.name = "Cheap";
        Cheap.health = 10;
        Cheap.str = 1;
        Cheap.def = 0;
        Cheap.spd = 2;
        Cheap.income = 1;
        Cheap.range = 10;
        Cheap.icon = load_bitmap("Images/Minions/m0.bmp",NULL);
        Basic.name = "Basic";
        Basic.health = 30;
        Basic.str = 3;
        Basic.def = 1;
        Basic.spd = 4;
        Basic.income = 4;
        Basic.range = 10;
        Basic.icon = load_bitmap("Images/Minions/m1.bmp",NULL);
        Fast.name = "Fast";
        Fast.health = 20;
        Fast.str = 0;
        Fast.def = 0;
        Fast.spd = 7;
        Fast.income = 7;
        Fast.range = 10;
        Fast.icon = load_bitmap("Images/Minions/om2.bmp",NULL);
        Fat.name = "Fat";
        Fat.health = 50;
        Fat.str = 3;
        Fat.def = 4;
        Fat.spd = 3;
        Fat.income = 7;
        Fat.range = 10;
        Fat.icon = load_bitmap("Images/Minions/om3.bmp",NULL);
        Sword.name = "Sword";
        Sword.health = 40;
        Sword.str = 5;
        Sword.def = 2;
        Sword.spd = 5;
        Sword.income = 13;
        Sword.range = 10;
        Sword.icon = load_bitmap("Images/Minions/om4.bmp",NULL);
        Cow.name = "Cow";
        Cow.health = 60;
        Cow.str = 2;
        Cow.def = 2;
        Cow.spd = 4;
        Cow.income = 16;
        Cow.range = 10;
        Cow.icon = load_bitmap("Images/Minions/m5.bmp",NULL);
        Lizard.name = "Lizard";
        Lizard.health = 50;
        Lizard.str = 5;
        Lizard.def = 1;
        Lizard.spd = 5;
        Lizard.income = 16;
        Lizard.range = 10;
        Lizard.icon = load_bitmap("Images/Minions/m6.bmp",NULL);
        Archer.name = "Archer";
        Archer.health = 40;
        Archer.str = 4;
        Archer.def = 0;
        Archer.spd = 4;
        Archer.income = 18;
        Archer.range = 30;
        Archer.icon = load_bitmap("Images/Minions/om7.bmp",NULL);
        Swarm.name = "Swarm";
        Swarm.health = 15;
        Swarm.str = 1;
        Swarm.def = 0;
        Swarm.spd = 8;
        Swarm.income = 17;
        Swarm.range = 8;
        Swarm.icon = load_bitmap("Images/Minions/om8.bmp",NULL);
        Tank.name = "Tank";
        Tank.health = 100;
        Tank.str = 2;
        Tank.def = 10;
        Tank.spd = 1;
        Tank.income = 23;
        Tank.range = 15;
        Tank.icon = load_bitmap("Images/Minions/om9.bmp",NULL);
        
    }
    void HUD::buildUI()
    {
        //blit(palette
    }
    void HUD::displayMinQue(BITMAP *destination)
    {
        if(selMinion != -1)
        {
            blit(BPanel1, destination, 0,0, 665, 160, BPanel1->w, BPanel1->h);
        }
        // blit your MOTHER
        
    }
    void HUD::displayMouseOver(BITMAP *destination)
    {
        //cout << "fick";
        if(mosScrX < 653 && mosScrY < 455)
        { 
            //cout << "fark";
            if((mouseX > 12 && mouseX < 38)&&(mouseY > 415 && mouseY < 430))
            {
                if(mbut)
                    selLane = 1;
                blit(C2BM1, destination,0,0,int(mouse_x+10),int(mouse_y+30),C2BM1->w,C2BM1->h);
            }
            else if((mouseX > 70 && mouseX < 85)&&(mouseY > 415 && mouseY < 430))
            {
                if(mbut)
                    selLane = 2;
                blit(C2BM1, destination,0,0,int(mouse_x+10),int(mouse_y+30),C2BM1->w,C2BM1->h);
            }
            else if((mouseX > 70 && mouseX < 85)&&(mouseY > 455 && mouseY < 478))
            {
                if(mbut)
                    selLane = 3;
                blit(C2BM1, destination,0,0,int(mouse_x+10),int(mouse_y+30),C2BM1->w,C2BM1->h);
            }
            else
            {
                if(mbut == true)   
                    selLane = 0;
            }
        }
        //cout << "faq: " << selLane << "\n";
        //cout << "but: " << mbut << "\n";
        
    }
    void HUD::displayMinSelect(BITMAP *destination)
    {
        //cout << "here";
        Minion Stable [] = {Cheap, Basic, Fast, Fat, Sword, Cow, Lizard, Archer, Swarm, Tank};
        if(selLane > 0)
        {
            masked_blit(BPanel5, destination,0,0, 100, 466, BPanel5->w, BPanel5->h);
            for(int j = 0; j < 2; j++)
            {
                for(int i = 0; i < 5; i++)
                {
                    blit(Stable[i+(5*j)].icon, destination, 0, 0, 101 + i*41, 487 + j*41 , 40,40);
                }    
            }
            if(mbut)
            {
                bool i........ = false;
                for(int b = 0; b < 10; b++)
                {
                    // nic sauce!
                    if(((mosScrX > 101+((b%5)*41) && mosScrX < 142+((b%5)*41))&&(mosScrY > 487+((b<5)?0:41) && mosScrY < 528+((b<5)?0:41))))
                    {
                        i........ = true;
                        selMinion = b;
                        break;
                    }
                }
            if(!i........ && !(mosScrX > 280 && mosScrX < 400 && mosScrY>470 && mosScrY < 600))
                selMinion = -1;
            }    
            cout << selMinion;
        }
        
    }
    void HUD::displayMinInfo(BITMAP *destination)
    {
        if(selMinion > -1 && selMinion < 10 && selLane > 0)
        {
            //blit(MinInfo[selMinion], destination, 0, 0, 320, 474, BPanel2->w, BPanel2->h);
            blit(MinInfo[selMinion], destination, 0, 0, 320, 474, 321, BPanel2->h);
            MenuP = true;
        }
        else
            MenuP = false;
        
    }
    void HUD::addMinion(BITMAP *destination)
    {
        //vector<Minion> Herp;
        //cout << "so";
        
        if(MenuP && mbut && mosScrX > (320+4) && mosScrX < (320+4+55) && mosScrY > (474+69) && mosScrY < (474+69+41))
        {
            //cout << "lol";
            if(!(Arsh[17].health > 0))
            {
                //cout << "wut/n";
                switch(selMinion)
                {
                    case 0:
                    {
                        Arsh[ArmyPos] = Cheap;
                        break;
                    }
                    case 1:
                    {
                        Arsh[ArmyPos] = Basic;
                        break;
                    }
                    case 2:
                    {
                        Arsh[ArmyPos] = Fast;
                        break;
                    }
                    case 3:
                    {
                        Arsh[ArmyPos] = Fat;
                        break;
                    }
                    case 4:
                    {
                        Arsh[ArmyPos] = Sword;
                        break;
                    }
                    case 5:
                    {
                        Arsh[ArmyPos] = Cow;
                        break;
                    }
                    case 6:
                    {
                        Arsh[ArmyPos] = Lizard;
                        break;
                    }
                    case 7:
                    {
                        Arsh[ArmyPos] = Archer;
                        break;
                    }
                    case 8:
                    {
                        Arsh[ArmyPos] = Swarm;
                        break;
                    }
                    case 9:
                    {
                        Arsh[ArmyPos] = Tank;
                        break;
                    }
                }
                ArmyPos ++;
                cout << "\nMa! Ma! I got one! Come here ma!!\n";
                selMinion = -1;
            }
            else
            {
                // show error message for 2 seconds
                blit(ERROR3, destination, 0, 0, 320, 465, ERROR3->w, ERROR3->h);
            }
        }
        
        
    }
    void HUD::displayArmy(BITMAP *destination)
    {
        int col =0;
        if(ArmyPos > 0)
            masked_blit(BPanel1, destination, 0, 0, 666, 175, BPanel1->w, BPanel1->h);
        for(int s = 0; s < ArmyPos;s++)
        {
            int row = s%3;
            if(row==0 && s > 1)
                col += 41;
            blit(Arsh[s].icon, destination, 0, 0, 667+(row*41), 176+col, 40, 40);
        }
        
    }
    void HUD::printArmy()
    {
        /*
        for(int i = 0; i < 18; i++)
        {
            cout<< Arsh[i].name;
        }   
        cout<<"\n\n";
        */
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    Have you run this program with your debugger? The debugger should be able to tell you exactly where the problem is detected and you should be able to view the variables at the time of the crash.

    Jim
    rags_to_riches likes this.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    This is what a constructor is for:
    Code:
         Cheap.name = "Cheap";
         Cheap.health = 10;
         Cheap.str = 1;
         Cheap.def = 0;
         Cheap.spd = 2;
         Cheap.income = 1;
         Cheap.range = 10;
         Cheap.icon = load_bitmap("Images/Minions/m0.bmp",NULL);
    With constructor:
    Code:
         Cheap = SomeType("Cheap", 10, 1, 0, 2, 1, 10, "Images/Minions/m0.bmp");
    Or if you're worried about losing the field names, you can use the Named parameter idiom.
    Code:
         Cheap = SomeType("Cheap", "Images/Minions/m0.bmp").health(10).str(1).def(0).spd(2).income(1).range(10);
    Or last but not least, you could store this all in some kind of config file and load it in when the program starts

    Why don't you keep these things in an array like you have on line 168 to replace lines 222 to 273 with a two-liner for example?
    Last edited by iMalc; 05-05-2012 at 02:24 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Breaking up source code into multiple files
    By mherald81 in forum C Programming
    Replies: 9
    Last Post: 10-15-2010, 04:55 PM
  2. 3D Modeling Software, 10 weeks, is it posible??
    By ChrisPepper1989 in forum C++ Programming
    Replies: 15
    Last Post: 12-13-2006, 09:33 AM
  3. Can someone look over my code? part 2
    By brooklyn in forum C++ Programming
    Replies: 10
    Last Post: 04-18-2006, 06:33 AM
  4. Gone for 2 weeks...
    By biosninja in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 12-19-2003, 08:42 AM
  5. Replies: 3
    Last Post: 06-18-2003, 03:09 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21