Basic inventory problems

I am having some unexpected trouble with a game I'm working on.
Basically I have a function with a parameter called flag, and judging by the flag I generate a specific menu.Whenever I run the program with a value for flag different from 0 it crashes.With flag = 0 it works perfectly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void Inventory::init(int X,int Y,int ROWS,int SLOTS_PER_ROW,int flag,const char* ADDR)
{
    [...]

    n = 0;
    x=X;
    y=Y;
    visible = false;
    FLAG = flag;

    if(FLAG == 0)
    {
        //do something
    }

    else
    {
       //do something else
    }
}
without seeing more of the code, we can't really determine what's causing the crash
The crash is likely somewhere in your "do something else" section.


If you are running with a debugger... it should tell you exactly which line the crash is occurring on.
here you go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void Inventory::init(int X,int Y,int ROWS,int SLOTS_PER_ROW,int flag,const char* ADDR)
{
    tiles = new InvTile[MAX_SIZE];
    n = 0;
    x=X;
    y=Y;
    visible = false;
    FLAG = flag;

    if(flag == 0)
    {
        for(int x1=X;x1<X+SLOTS_PER_ROW*32;x1+=32)
        {
            for(int y1=Y;y1<Y+ROWS*32;y1+=32)
            {
                tiles[n] = (InvTile)InvTile(x1,y1,32,32,ADDR);
                n++;
            }
        }
    }
    if(flag == 1)
    {
       for(int x1=X;x1<X+SLOTS_PER_ROW*32;x1+=32)
        {
            for(int y1=Y;y1<Y+ROWS*32;y1+=32)
            {
                tiles[n] = (InvTile)InvTile(x1,y1,32,32,ADDR);
                n++;
            }
        }
    }
}

This is the member of the class.

And this is the class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Inventory
{
private:
    int n;
    int x,y;
    bool visible;
    int FLAG;
public:
    InvTile* tiles;

    void init(int,int,int,int,int,const char*);
    void draw(SDL_Surface* window);
    void show();
    void hide();
    void add_xy(int,int,const char*);
    void add_in_slot(int,int,const char*);
    void add_in_next_slot(const char*);
    void remove_xy(int,int);
    void remove_from_slot(int,int);
    void remove_last();
    void update(SDL_Event event);

    int getN();
    int getFlag();

};


And this is my main function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main(int argc ,char* argv[])
{
    init();
    inv.init(64,32,3,5,1,"./resources/tile.png");
    inv.add_in_next_slot("./resources/chest.png");
    inv.add_in_next_slot("./resources/test_sword.png");
    inv.show();

    while(running)///MAIN GAME LOOP
    {
        update();
        inv.update(currentEvent);
        draw();
        inv.draw(window);
        processFPS();
        SDL_Flip(window);
    }

    SDL_FreeSurface(window);///DISPOSING THE WINDOW
    SDL_Quit();///STOPPING SDL

    return EXIT;///EXITING THE PROGRAM
}


BTW I'm running with SDL.
I tried copy-pasting the code from the if statement to the else statement.
Still crashing.
what are MAX_SIZE, ROWS, and SLOTS_PER_ROW?
are x and y pixel dimensions of the image?

*nevermind, they look like coordinates for placement. all the x y x1 y1 business gets muddy. You might want to rename some variables. Or maybe I should get more caffeine first
Last edited on
Nevermind i solved it.
I changed:
1
2
3
4
5
6
7
8
if(flag == 0)
{
    
}
else
{

}


to:
1
2
3
4
5
6
7
8
9
10
11
12
for(int x1=X;x1<X+SLOTS_PER_ROW*32;x1+=32)
        {
            for(int y1=Y;y1<Y+ROWS*32;y1+=32)
            {
                tiles[n] = (InvTile)InvTile(x1,y1,32,32,ADDR);
                n++;
                if(flag == 1)
                {
                  //do stuff
                 }
            }
        }


Thank you all for the support!
Topic archived. No new replies allowed.