I have a problem using the vector, using allegro and C++

Started by
2 comments, last by RDragon1 15 years, 6 months ago
I am trying to use the vector for my story... mmm... state? Drawings are supposedly put into the storyvector, and then the drawgame() draws the picture at draw_sprite(buffer,storyvector[storypage],0,0); My problem is that the program draws a big S12 in big paintbrush stroke, which is the picture for s12 and then gives me an encountered an error box. I can't figure out what I am doing wrong so please help me :)

#include <allegro.h>
#include <cstdlib>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;












#define EXITGAME 69
#define MENU 1
#define INFOMENU 2
#define STORY 4
#define INGAME 8












int gamestatus = MENU;
int gamelevel = 1;
int storypage = 1;
int numpages = 5;

BITMAP*buffer;
BITMAP* menupic, *infomenupic;
BITMAP* background;

BITMAP*s11, *s12, *s13;

vector<BITMAP *>storyvector;
vector<BITMAP *>:: iterator sv;














volatile long int logiccount = 1;
volatile long int actioncount = 1;

void logictime()
{
    if(logiccount >= 3)
    {
        logiccount = 0;
    }
    logiccount ++;
}END_OF_FUNCTION(logictime);

void actiontime()
{
    if(actioncount >= 4)
    {
        actioncount = 0;
    }
    actioncount ++;
}END_OF_FUNCTION(actiontime);












void datareset()
{
    gamelevel = 1;
    storyvector.erase(storyvector.begin(),storyvector.begin()+storyvector.size());
    storyvector.push_back(s11);
    storyvector.push_back(s12);
    storyvector.push_back(s13);
    numpages = storyvector.size();
    storypage = numpages;

}












void updategame()
{
    if(gamestatus == MENU)
    {
        if(mouse_b & 2)
        {
            gamestatus = INFOMENU;
        }
        if(mouse_b & 1)
        {
            datareset();
            gamestatus = STORY;
        }
        if(key[KEY_ESC])
        {
            gamestatus = EXITGAME;
        }
    }
    if(gamestatus == INFOMENU)
    {
        if(key[KEY_ESC] || mouse_b & 1)
        {
            gamestatus = MENU;
            rest(80);
        }
    }
    if(gamestatus == STORY)
    {
        if(key[KEY_ESC])
        {
            gamestatus = MENU;
            rest(80);
        }
        if(mouse_b & 1){storypage --;}
        if(mouse_b & 2){storypage ++;}
        if(storypage <= 0){gamestatus = INGAME;}
        if(storypage > numpages){storypage --;}

    }
    if(gamestatus == INGAME)
    {
        if(key[KEY_ESC])
        {
            gamestatus = MENU;
            rest(80);
        }


    }

}













void drawgame()
{
    draw_sprite(screen,buffer,0,0);
    show_mouse(buffer);

    if(gamestatus == MENU)
    {
        draw_sprite(buffer,menupic,0,0);

    }
    if(gamestatus == INFOMENU)
    {
        draw_sprite(buffer,infomenupic,0,0);

    }
    if(gamestatus == STORY)
    {
        draw_sprite(buffer,storyvector[storypage],0,0);

    }
    if(gamestatus == INGAME)
    {
        draw_sprite(buffer,background,0,0);


    }
}












int main(int argc, char*argv[])
{
    //Install stuff
    allegro_init();
    install_keyboard();
    install_mouse();
    install_timer();

    LOCK_VARIABLE(logiccount);
    LOCK_FUNCTION(logictime);

    LOCK_VARIABLE(actioncount);
    LOCK_FUNCTION(actiontime);

    install_int_ex(logictime,MSEC_TO_TIMER(80));


    set_color_depth(16);
    set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0);













    buffer = create_bitmap(640,480);
    menupic = load_bitmap("menupic.bmp",NULL);
    infomenupic = load_bitmap("infomenupic.bmp",NULL);

    s11 = load_bitmap("s11.bmp",NULL);
    s12 = load_bitmap("s12.bmp",NULL);
    s13 = load_bitmap("s13.bmp",NULL);
    background = load_bitmap("bg.bmp",NULL);











    //Game loop
    while(gamestatus != EXITGAME)
    {
        if(logiccount = 2)
        {
            updategame();
        }
        drawgame();

    }

    show_mouse(NULL);
    destroy_bitmap(buffer);
    destroy_bitmap(menupic);
    destroy_bitmap(infomenupic);

    destroy_bitmap(s11);
    destroy_bitmap(s12);
    destroy_bitmap(s13);






    clear_keybuf();
    return 0;
}END_OF_MAIN();



Also wondering why I am seeing the picture s12 and not s11 or s13 if I am doing push.back() backwards. Please help me here as well ^_^
Advertisement
Sorry, still require help!
Your problem is probably due to accessing out of bounds. Are you sure that storypage is always within range [0, storyvector.size())? (Sorry, but because of the liberal use of globals, your logic is very hard to follow...)

Aha, since all indexing start at 0, and item[size()] is already out of bounds, this line seems to fail to guarantee sanity:
if(storypage > numpages){storypage --;}//storypage still out of bounds


And a small recommendation:
storyvector.erase(storyvector.begin(),storyvector.begin()+storyvector.size());//is equivalent tostoryvector.erase(storyvector.begin(), storyvector.end());//is equivalent tostoryvector.clear();
Run it in a debugger. It will tell you where it crashed and let you examine the program state, so you can figure out what's wrong at that point.

This topic is closed to new replies.

Advertisement