Sign in to follow this  
ryan554

Allegro Help

Recommended Posts

Im trying to make a scrolling spaceship shooter,but right now im just testing my collisions. When I compile this code I get no compiler errors,but when it runs the screen is black for a second then it shuts down. I worked before I included the object structure, I added the structure to make collision detection easier since BITMAP dosent include x and y just w and h
#include <allegro.h>

volatile long speed_counter = 0;

void increment_speed_counter()
{
    speed_counter++;  
}
END_OF_FUNCTION(increment_speed_counter);

struct object{
    int x;
    int y;
    int w;
    int h;
    BITMAP *sprite;
};

int collision(object *image1,object *image2)
{
    int bb_left_1 = image1->x;
    int bb_right_1 = (bb_left_1 + image1->w);
    int bb_top_1 = image1->y;
    int bb_bottom_1 = (bb_top_1 + image1->h);
    
    int bb_left_2 = image2->x;
    int bb_right_2 = (bb_left_2 + image2->w);
    int bb_top_2 = image2->y;
    int bb_bottom_2 = (bb_top_2 + image2->h);
    
    if (bb_bottom_1 < bb_top_2) return(0);
    if (bb_top_1 > bb_bottom_2) return(0);

    if (bb_right_1 < bb_left_2) return(0);
    if (bb_left_1 > bb_right_2) return(0);

    return (1);
}


int main()
{
    allegro_init();
    install_keyboard();
    install_timer()
    
    LOCK_VARIABLE(speed_counter);
    LOCK_FUNCTION(increment_speed_counter);
    
    install_int_ex(increment_speed_counter,BPS_TO_TIMER(60));
    
    set_color_depth(16);
    set_gfx_mode(GFX_AUTODETECT,640,480,0,0);
    
    BITMAP *buffer;
    buffer = create_bitmap(640,480);
    
    BITMAP *player_sprite;
    player_sprite = load_bitmap("player.bmp",NULL);
    object *player;
    player->sprite = player_sprite;
    player->x = 320;
    player->y = 400;
    player->w = player_sprite->w;
    player->h = player_sprite->h;
    
    BITMAP *enemy_sprite;
    enemy_sprite = load_bitmap("enemy.bmp",NULL);
    object *enemy;
    enemy->sprite = enemy_sprite;
    enemy->x = 20;
    enemy->y = 20;
    enemy->w = enemy_sprite->w;
    enemy->h = enemy_sprite->h;
    
         
    while(!key[KEY_ESC])
    {
        while(speed_counter > 0)
        {
            if(key[KEY_RIGHT] && player->x < 640 && !collision(player,enemy))
            {
                player->x++;
            }
            else if(key[KEY_LEFT] && player->x > 0 && !collision(player,enemy))
            {
                player->x--;
            }
            else if(key[KEY_UP] && player->y < 480 && !collision(player,enemy))
            {
                player->y--;
            }
            else if(key[KEY_DOWN] && player->y > 0 && !collision(player,enemy))
            {
                player->y++;
            }

            speed_counter--;
        }
        draw_sprite(buffer,player->sprite,player->x,player->y);
        draw_sprite(buffer,enemy->sprite,enemy->x,enemy->y);
        masked_blit(buffer,screen,0,0,0,0,640,480);
        clear_bitmap(buffer);
    }
    
    destroy_bitmap(player_sprite);
    destroy_bitmap(enemy_sprite);
    destroy_bitmap(buffer);
    return 0;
}
END_OF_MAIN()

Share this post


Link to post
Share on other sites
It looks like you're causing an access violation because you never allocate memory for the object pointers. When you initialize the player, make sure to put this line of code in:

player = malloc(sizeof(object));

and make sure to put:

free(player);

also do this with your enemy object.

Share this post


Link to post
Share on other sites
You need to read up on how to allocate memory from the heap - either in C or C++.

I'm not sure whether you're using C or C++, you haven't specified which, and I imagine that the program would compile using either.

Anyway, the bottom line is, you need to use "new" or "malloc" to allocate memory for your object. If you use "malloc", you will need to cast the returned pointer, thus:


object * something;
something = (object *) malloc(sizeof(object));


But you need to read and understand how to use free store allocation / deallocation in order to avoid problems such as memory leaks and dangling pointers.

Mark

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:

I'm not sure whether you're using C or C++, you haven't specified which, and I imagine that the program would compile using either.


It is Allegro. hence the #include. Have you tried changing your main() to int main(int argc, char* argv[])? Im not sure if this will help you at all but it is worth a try and I believe that that line should be included in all allegro programs.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
I'm not sure whether you're using C or C++, you haven't specified which, and I imagine that the program would compile using either.
It is Allegro. hence the #include.

That's not evidence of whether or not he's using C or C++, though ...

Share this post


Link to post
Share on other sites
He's using c++ as I think you don't have to cast malloc using c, also the struct isn't typedef'd.

So prefer new/delete to malloc/free

object *player = new object;

delete player;

Prefer std::auto_ptr to new/delete
( or boost::shared_ptr, if it makes sense for your program )

#include <memory>

std::auto_ptr<object> player( new object );

or just stack allocate it:

object player;

player.x = ...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this