0
\$\begingroup\$

To begin with I am developing a PSP application so I have no clue how to debug a PSP application. That doesn't mean I do not know how debug period. Anyway, when I run the game everything runs fine but when I press the X button on the PSP I see no bullet being drawn or updated on the screen. I know the problem doesn't lie with the pad because I press start and it exits my game so I do not think it is the pad that is causing the problem. Maybe the bullet is being drawn out of the screen already. But I am not sure since I cannot debug maybe somebody can see the flaw in my code. main.cpp

class Missile
{
    private:
        static const double angle = (3.14159265358979323846 / 2);
    public:
        bool Alive;
        static const int V = 5;
        double X;
        double Y;
        void Init(bool alive, int x, int y)
        {
            Alive = alive;
            X = x;
            Y = y;
        }
        void Update()
        {
            Y -= V;
        }
        void Kill()
        {
            Alive = false;
        }
        void Draw(BITMAP* buffer, BITMAP* sprite)
        {

            draw_sprite(buffer, sprite, X, Y);
        }
};
std::vector<Missile*>* bullets = new std::vector<Missile*>();
void FireBullet(Ship* s)
{
        if (bullets->size() < Constants::MAXBULLETS)
        {
            Missile* missile = new Missile();
            missile->Init(true, s->X, s->Y);
            bullets->push_back(missile);
        }
}

void CleanUp()
{
    for(unsigned int index = 0; index < bullets->size(); index++)
    {
        if (bullets->at(index)->Alive == false)
        {
            bullets->erase(bullets->begin() + index);
        }
    }
}
void UpdateBullets()
{
    for(unsigned int index = 0; index < bullets->size(); index++)
    {
        if (bullets->at(index)->Y < 0)
        {
            bullets->at(index)->Update();
        }
        else
        {
            bullets->at(index)->Kill();
        }
    }
}
void DrawBullets(BITMAP* buffer, BITMAP* sprite)
{
    for(unsigned int index = 0; index < bullets->size(); index++)
    {
        if (bullets->at(index)->Alive == true)
        {
            bullets->at(index)->Draw(buffer, sprite);
        }
    }
}
//Entry point of the application
int main(void)
{   

    Ship* s = new Ship();
    int x = (WIDTH / 2) - 64;
    allegro_message("Initialzing ship class");
    s->Init(x);
    int frame = 0;
    BITMAP* buffer = NULL;
    BITMAP* background = NULL;
    BITMAP* ship = NULL;
    SceCtrlData pad;
    bool done = false;
    allegro_message("Initializing Game...");
    int rval = allegro_init();
    if (allegro_init() != 0)
    {
        allegro_message("Error initializing Game Because it returned: %i", rval);
        return 1;
    }
    allegro_message("Setting Graphics Mode...Press X To Begin Game. Default Missile count: %i", bullets->size());
    set_color_depth(32);
    int ret = set_gfx_mode(GFX_AUTODETECT,480,272,0,0);
    if (ret != 0)
    {
        allegro_message("Error setting grahpic mode! Because of it returned: %i", ret);
        return ret;
    }
    background = load_bmp("background.bmp", NULL);
    ship = load_bmp("ship.bmp", NULL);
    BITMAP* m = load_bmp("missile.bmp", NULL);
    if (background == NULL || ship == NULL || m == NULL){
        allegro_message("Couldn't load one or more sprites...");
        return 0;
    }
    buffer = create_bitmap(WIDTH, HEIGHT);
    if (buffer == NULL)
    {
        allegro_message("Couldn't create buffer!");
        return 0;
    }
    while(!done)
    {

        sceCtrlReadBufferPositive(&pad, 1); 
        if (pad.Buttons & PSP_CTRL_START)
        {
            done = true;
        }
        else if (pad.Buttons & PSP_CTRL_CROSS)
        {
                FireBullet(s);
        }
        else if (pad.Buttons & PSP_CTRL_LEFT)
        {
            s->MoveLeft();
        }
        else if (pad.Buttons & PSP_CTRL_RIGHT)
        {
            s->MoveRight();
        }
        if (bullets->size() > 0)
        {
            UpdateBullets();
            CleanUp();
        }
        clear(buffer);
        draw_sprite(buffer, background, 0, 0);
        s->Draw(buffer, ship, frame);
        DrawBullets(buffer, m);
        masked_blit(buffer, screen, 0, 0, 0, 0, WIDTH, HEIGHT);
        if (frame == (60 * 10))
        {
            frame = 0;
        }
        frame++;
        vsync();

    }
    allegro_message("Clearing resources!");
    clear(buffer);  
    clear(ship);
    clear(background);
    clear(screen);
    allegro_message("Thank you for playing!");
    return 0;
}
END_OF_MAIN()
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

I see two problems right off the bat, the first has to do with the marked line below:

void UpdateBullets()
{
    for(unsigned int index = 0; index < bullets->size(); index++)
    {
        if (bullets->at(index)->Y < 0) //<-- HERE
        {
            bullets->at(index)->Update();
        }
        else
        {
            bullets->at(index)->Kill();
        }
    }
}

The origin for screen coordinates is the upper left corner of the screen, as such, the line if(bullets->at(index)->Y < 0) is always false, making every bullet fired immediately set Alive = false. Swapping the lines bullets->at(index)->Update(); and bullets->at(index)->Kill(); should fix the problem.

The second issue is:

int rval = allegro_init();
if (allegro_init() != 0) {
...
}

Calling allegro_init() multiple times is ineffective, you are already storing the return value in rval so just check its value instead: if(rval != 0) { ... }

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .