Sign in to follow this  

first attempt on moving a sprite in allegro :(

This topic is 3724 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#include <stdlib.h>
#include <allegro.h>

typedef struct SPRITE
{
    int x, y;
    int w, h;
    int xspeed, yspeed;
    int xdelay, ydelay;
    int xcount, ycount;
    
}SPRITE;

void erase_sprite(BITMAP *dest, SPRITE *spr);
void update_sprite(SPRITE *spr);
     
int main(void)
{
    
    //INIT
    allegro_init();
    install_keyboard();
    install_timer();
    srand(time(NULL));
    
    
    //VIDEO
    set_color_depth(16);
    set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
    
    //SETUP
    SPRITE *DOUBLEKILL;
    BITMAP *double_kill;
    char *dk = "double_kill.bmp";
    double_kill = load_bmp(dk, NULL);
    
    DOUBLEKILL->x = 0;
    DOUBLEKILL->y = 120;
    DOUBLEKILL->w = 60;
    DOUBLEKILL->h = 60;
    DOUBLEKILL->xspeed = 10;
    DOUBLEKILL->yspeed = 0;
    DOUBLEKILL->xdelay = 2;
    DOUBLEKILL->ydelay = 0;
    DOUBLEKILL->xcount = 0;
    DOUBLEKILL->ycount = 0;
    
    draw_sprite(screen, double_kill, DOUBLEKILL->x, DOUBLEKILL->y);
    
    while(DOUBLEKILL->x < 210){
        
        //ERASE SPRITE
        erase_sprite(screen, DOUBLEKILL);
        
        //UPDATE SPRITE
        update_sprite(DOUBLEKILL);
        
        //GET SCREEN
        acquire_screen();
        
        //DRAW SPRITE
        draw_sprite(screen, double_kill, DOUBLEKILL->x, DOUBLEKILL->y);
        
        //RETURN SCREEN
        release_screen();
        rest(50);
        }
        
    while(!key[KEY_ESC]){}
    
    allegro_exit();
    return 0;
}
END_OF_MAIN()

void erase_sprite(BITMAP *dest, SPRITE *spr)
{
     rectfill(dest, spr->x, spr->y, spr->x + spr->w, spr->y + spr->h, 0);
     
}

void update_sprite(SPRITE *spr)
{
     //UPDATE X
     if(++spr->xcount > spr->xdelay){
         spr->xcount = 0;
         spr->x += spr->xspeed;
         }
         
     //UPDATE Y
     if(++spr->ycount > spr->ydelay){
         spr->ycount = 0;
         spr->y += spr->yspeed;
         }
         
}

Ok I don't know much yet but I think this is a pretty solid attempt. But for some resaon this just crashes as soon as I run it. At least it compliled :P. Can anyone tell me why half of my program crash???? I thought this was some good code. [Edited by - X_GRAYWOLF_X on October 1, 2007 9:28:25 PM]

Share this post


Link to post
Share on other sites
you should edit your post to use source tags, but just curious does this even compile and it crashes when you run it, or is it just not compiling?

what IDE and Compiler do you use?

what is this "END_OF_MAIN()"?

Share this post


Link to post
Share on other sites
I made this in allegro, which means i have to put "END_OF_MAIN()" there at the end to let allegro know i'm done with main and it can exit out of its resources and stuff.

It compile's fine but it immediately gives me a runtime error on startup. I am using vista with 1 gb ram and plenty of hd space so that shouldn't be an issue either.

Share this post


Link to post
Share on other sites
Quote:
Original post by X_GRAYWOLF_X
I made this in allegro, which means i have to put "END_OF_MAIN()" there at the end to let allegro know i'm done with main and it can exit out of its resources and stuff.

It compile's fine but it immediately gives me a runtime error on startup. I am using vista with 1 gb ram and plenty of hd space so that shouldn't be an issue either.


Yeah, I'd put money on that being your problem. Damn, you gotta love Macros!

Share this post


Link to post
Share on other sites
LOL

Yea well I don't think its the problem because only half of the little programs I have been making crash and ALL of them have that snippet so.

I just found out that i didn't have the image in the folder but when i did put it in and recompiled it still wouldn't run!!!!!!!!!

Share this post


Link to post
Share on other sites
The end of main thing is fine - your code however has some problems.


SPRITE *DOUBLEKILL;


You allocate a pointer to a sprite struct. Note that it doesn't point anywhere in particular. Stylistically all caps is generally reserved for macros or constants - but that's not the cause of your problem.

DOUBLEKILL->x = 0;
DOUBLEKILL->y = 120;
DOUBLEKILL->w = 60;
DOUBLEKILL->h = 60;
DOUBLEKILL->xspeed = 10;
DOUBLEKILL->yspeed = 0;
DOUBLEKILL->xdelay = 2;
DOUBLEKILL->ydelay = 0;
DOUBLEKILL->xcount = 0;
DOUBLEKILL->ycount = 0;


Now you fill in the values for that sprite object.
Unfortunately there is no sprite object - just a pointer. So you're writing to some random portion of memory (most likely 0) and get a runtime error. You need an actual sprite object - not just a pointer.

Share this post


Link to post
Share on other sites
Hello,

At a glance everything looks solid. However, Vista is known to have quite a few problems with gfx driver stuff. At what point does the program crash ( does the video mode at least change, or does it crash prior to that )? My reccommendation is to put a breakpoint right at the programs entry-point and step through it until the crash is repeated. Also, you may want to check the return values on the allegro calls to ensure that they are completing as expected...

ahayweh

<edit>
cshowe hit the nail on the head. Since DOUBLEKILL isn't initialized, you are getting null pointer exceptions. So either point it to an initialzed struct, or change the pointer access op ( -> ) to just a plan member access operator ( . )

Share this post


Link to post
Share on other sites

This topic is 3724 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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