Sign in to follow this  
Gamester3333

Allegro Blitting Problem

Recommended Posts

I entered my game's code like this...
#include <allegro.h>

void init();
void deinit();

int main() {
	init();

    //Load Spaceship
	BITMAP* spaceship;        spaceship      = load_bitmap("Spaceship Stationary.bmp",NULL);
    //Load Spaceship Moving Left
    BITMAP* leftship;         leftship       = load_bitmap("Spaceship Moving Left.bmp",NULL);
    //Load Spaceship Moving Right
    BITMAP* rightship;        rightship      = load_bitmap("Spaceship Moving Right.bmp",NULL);
    //Load Spaceship Moving Forward
    BITMAP* forwardship;      forwardship    = load_bitmap("Spaceship Moving Forward.bmp",NULL);
    //Load Spaceship Moving Backwards
    BITMAP* backship;         backship       = load_bitmap("Spaceship Backing Up.bmp",NULL);
    //Load Spaceship Moving Left and Forward
    BITMAP* leftupship;       leftupship     = load_bitmap("Spaceship Moving Forward + Left.bmp",NULL);
    //Load Spaceship Moving Right and Forward
    BITMAP* rightupship;      rightupship    = load_bitmap("Spaceship Moving Forward + Right.bmp",NULL);
    //Load Spaceship Moving Left and Down
    BITMAP* leftdownship;     leftdownship   = load_bitmap("Spaceship Backing Up + Left.bmp",NULL);
    //Load Spaceship Moving Right and Down
    BITMAP* rightdownship;    rightdownship  = load_bitmap("Spaceship Backing Up + Right.bmp",NULL);
    //Load Spaceship Speeding Forward
    BITMAP* hyperdrive;       hyperdrive     = load_bitmap("Spaceship Speeding Forward.bmp",NULL);
    
    //Load the bitmap we will copy these all to (for convienience)
    BITMAP *movemaps;         movemaps       = create_bitmap(400, 400);
    
    
    
    
    
    
    //Load ALL the Projectiles
    BITMAP* red1;             red1           = load_bitmap("Red Blast LVL 1",NULL);
    BITMAP* red2;             red2           = load_bitmap("Red Blast LVL 2",NULL);
    BITMAP* red3;             red3           = load_bitmap("Red Blast LVL 3",NULL);
    BITMAP* red4;             red4           = load_bitmap("Red Blast LVL 4",NULL);
    BITMAP* red5;             red5           = load_bitmap("Red Blast LVL 5",NULL);
    BITMAP* redu;             redu           = load_bitmap("Red Blast LVL ULTRA",NULL);
    BITMAP* orange1;          orange1        = load_bitmap("Orange Blast LVL 1",NULL);
    BITMAP* orange2;          orange2        = load_bitmap("Orange Blast LVL 2",NULL);
    BITMAP* orange3;          orange3        = load_bitmap("Orange Blast LVL 3",NULL);
    BITMAP* orange4;          orange4        = load_bitmap("Orange Blast LVL 4",NULL);
    BITMAP* orange5;          orange5        = load_bitmap("Orange Blast LVL 5",NULL);
    BITMAP* orangeu;          orangeu        = load_bitmap("Orange Blast LVL ULTRA",NULL);
    BITMAP* yellow1;          yellow1        = load_bitmap("Yellow Blast LVL 1",NULL);
    BITMAP* yellow2;          yellow2        = load_bitmap("Yellow Blast LVL 2",NULL);
    BITMAP* yellow3;          yellow3        = load_bitmap("Yellow Blast LVL 3",NULL);
    BITMAP* yellow4;          yellow4        = load_bitmap("Yellow Blast LVL 4",NULL);
    BITMAP* yellow5;          yellow5        = load_bitmap("Yellow Blast LVL 5",NULL);
    BITMAP* yellowu;          yellowu        = load_bitmap("Yellow Blast LVL ULTRA",NULL);
    BITMAP* green1;           green1         = load_bitmap("Green Blast LVL 1",NULL);
    BITMAP* green2;           green2         = load_bitmap("Green Blast LVL 2",NULL);
    BITMAP* green3;           green3         = load_bitmap("Green Blast LVL 3",NULL);
    BITMAP* green4;           green4         = load_bitmap("Green Blast LVL 4",NULL);
    BITMAP* green5;           green5         = load_bitmap("Green Blast LVL 5",NULL);
    BITMAP* greenu;           greenu         = load_bitmap("Green Blast LVL ULTRA",NULL);
    BITMAP* blue1;            blue1          = load_bitmap("Blue Blast LVL 1",NULL);
    BITMAP* blue2;            blue2          = load_bitmap("Blue Blast LVL 2",NULL);
    BITMAP* blue3;            blue3          = load_bitmap("Blue Blast LVL 3",NULL);
    BITMAP* blue4;            blue4          = load_bitmap("Blue Blast LVL 4",NULL);
    BITMAP* blue5;            blue5          = load_bitmap("Blue Blast LVL 5",NULL);
    BITMAP* blueu;            blueu          = load_bitmap("Blue Blast LVL ULTRA",NULL);
    BITMAP* purple1;          purple1        = load_bitmap("Purple Blast LVL 1",NULL);
    BITMAP* purple2;          purple2        = load_bitmap("Purple Blast LVL 2",NULL);
    BITMAP* purple3;          purple3        = load_bitmap("Purple Blast LVL 3",NULL);
    BITMAP* purple4;          purple4        = load_bitmap("Purple Blast LVL 4",NULL);
    BITMAP* purple5;          purple5        = load_bitmap("Purple Blast LVL 5",NULL);
    BITMAP* purpleu;          purpleu        = load_bitmap("Purple Blast LVL ULTRA",NULL);
    BITMAP* white1;           white1         = load_bitmap("White Blast LVL 1",NULL);
    BITMAP* white2;           white2         = load_bitmap("White Blast LVL 2",NULL);
    BITMAP* white3;           white3         = load_bitmap("White Blast LVL 3",NULL);
    BITMAP* white4;           white4         = load_bitmap("White Blast LVL 4",NULL);
    BITMAP* white5;           white5         = load_bitmap("White Blast LVL 5",NULL);
    BITMAP* whiteu;           whiteu         = load_bitmap("White Blast LVL ULTRA",NULL);
    BITMAP* thebigone;        thebigone      = load_bitmap("Missile",NULL);
    
    
    //Create multiple file holders to reference(one for each color)
    BITMAP* redholder;         redholder     = create_bitmap(100, 100); 
    BITMAP* orangeholder;      orangeholder  = create_bitmap(100, 100);
    BITMAP* yellowholder;      yellowholder  = create_bitmap(100, 100);
    BITMAP* greenholder;       greenholder   = create_bitmap(100, 100);
    BITMAP* blueholder;        blueholder    = create_bitmap(100, 100);
    BITMAP* purpleholder;      purpleholder  = create_bitmap(100, 100);
    BITMAP* whiteholder;       whiteholder   = create_bitmap(100, 100);
    
    
    
    
    
    //Load the Buffer
    BITMAP *buffer = create_bitmap(1024, 768);
    
    
    
    
    
    
    while (!key[KEY_ESC]) 
    {
		int    red;               red                 = 1;
        int    orange;            orange              = 1;
        int    yellow;            yellow              = 1;
        int    green;             green               = 1;
        int    blue;              blue                = 1;
        int    purple;            purple              = 1;
        
        int    shipspeedx;        shipspeedx          = 0;
        int    shipspeedy;        shipspeedy          = 0;
        int    shipbitmap;        shipbitmap          = 0;
        int    shipposx;          shipposx            = 200;
        int    shipposy;          shipposy            = 200;
        int    shiphp;            shiphp              = 5;
        
        int    spaceshipx;        spaceshipx          = 0;
        
        int    forwardshipx;      forwardshipx        = 1;
        
        int    leftshipx;         leftshipx           = 2;
        
        int    rightshipx;        rightshipx          = 3;
        
        int    backshipx;         backshipx           = 4;
        
        int    leftupshipx;       leftupshipx         = 5;
        
        int    rightupshipx;      rightupshipx        = 6;
        
        int    leftdownshipx;     leftdownshipx       = 7;
        
        int    rightdownshipx;    rightdownshipx      = 8;
        
        int    hyperdrivex;       hyperdrivex         = 9;
        
        
        
        int    bullet1;           bullet1             = 0;
        int    bullet2;           bullet2             = 0;
        int    bullet3;           bullet3             = 0;
        int    bullet4;           bullet4             = 0;
        int    bullet5;           bullet5             = 0;
        
        
        
        
        int    red1x;             red1x               = 0;
        int    red1y;             red1y               = 0;
        int    red2x;             red2x               = 0;
        int    red2y;             red2y               = 12;
        int    red3x;             red3x               = 12;
        int    red3y;             red3y               = 0;
        int    red4x;             red4x               = 12;
        int    red4y;             red4y               = 12;
        int    red5x;             red5x               = 0;
        int    red5y;             red5y               = 24;
        int    redux;             redux               = 24;
        int    reduy;             reduy               = 0;
        
        
        
        
        
        
        int    starmove;          starmove            = 2;
        int    missilemove;       missilemove         = 2;
        
        int    redammo;           redammo             = 20;
        int    yellowammo;        yellowammo          = 20;
        int    blueammo;          blueammo            = 20;
        int    ammocharge;        ammocharge          = 1;
        
        double score;             score               = 0;
        double time;              time                = 1;
        bool   gameover;          gameover            = FALSE;
        
        typedef struct RGB
        {
        unsigned char r, g, b;
        } RGB;
        
        
        
        
        
        
        while(!key[KEY_ESC] && time > 0)
        {
                            
                            
                            //--------------------------------------------------
                            //Slow the ship down
                            //--------------------------------------------------
                            if(shipspeedx > 0)
                            {
                                          shipspeedx = shipspeedx - .5;
                            }
                            
                            if(shipspeedx < 0)
                            {
                                          shipspeedx = shipspeedx + .5;
                            }
                            
                            if(shipspeedy > 0)
                            {
                                          shipspeedy = shipspeedy - .5;
                            }
                            
                            if(shipspeedy < 0)
                            {
                                          shipspeedy = shipspeedy + .5;
                            }
                            
                            if(shipspeedy <= 0)
                            {
                                          shipbitmap = spaceshipx;
                            }
                            
                            if(shipspeedx >= 0)
                            {
                                          shipbitmap = spaceshipx;
                            }
                            
                            if(shipspeedx <= 0)
                            {
                                          shipbitmap = spaceshipx;
                            }
                            
                            if(shipspeedy >= 0)
                            {
                                          shipbitmap = spaceshipx;
                            }
                            //--------------------------------------------------
                            //Move the ship according to the speed
                            //--------------------------------------------------
                            
                            if(shipspeedx != 0)
                            {
                                          shipposx = shipposx + shipspeedx;
                            }
                            
                            if(shipspeedy != 0)
                            {
                                          shipposy = shipposy + shipspeedy;
                            }
                            //--------------------------------------------------
                            //Keep the ship inside the play area
                            //--------------------------------------------------
                            
                            if(shipposx <= 0)
                            {
                                          shipposx = 0;
                            }
                            
                            if(shipposx >= 976)
                            {
                                          shipposx = 976;
                            }
                            
                            if(shipposy <= 0)
                            {
                                          shipposy = 0;
                            }
                            
                            if(shipposy >= 550)
                            {
                                          shipposy = 550;
                            }
                            
                            //--------------------------------------------------
                            //See if the ship is dead, if so, gameover
                            //--------------------------------------------------
                            
                            if(shiphp <= 0)
                            {
                                          textout_centre_ex(screen, font, "GAME OVER!!!", 300, 300,
                                          makecol(255, 255, 255), -1);
                                          destroy_bitmap(spaceship);
                                          rest(10000);
                                          return 0;
                            }
                            
                            //--------------------------------------------------
                            //Process user input
                            //--------------------------------------------------
                            
                            if(key[KEY_RIGHT] && !key[KEY_UP] && !key[KEY_DOWN])
                            {
                                           shipspeedx = shipspeedx + 2;
                                           shipbitmap = rightshipx;
                            }
                            
                            if(key[KEY_LEFT] && !key[KEY_UP] && !key[KEY_DOWN])
                            {
                                           shipspeedx = shipspeedx - 2;
                                           shipbitmap = leftshipx;
                            }
                            
                            if(key[KEY_UP] && !key[KEY_LEFT] && !key[KEY_RIGHT] && !key[KEY_SPACE])
                            {
                                           shipspeedy = shipspeedy - 2;
                                           shipbitmap = forwardshipx;
                            }
                            
                            if(key[KEY_UP] && !key[KEY_LEFT] && !key[KEY_RIGHT] && key[KEY_SPACE])
                            {
                                           shipspeedy = shipspeedy - 4;
                                           shipbitmap = hyperdrivex;
                            }
                            
                            if(key[KEY_DOWN] && !key[KEY_LEFT] && !key[KEY_RIGHT])
                            {
                                           shipspeedy = shipspeedy + 2;
                                           shipbitmap = backshipx;
                            }
                            
                            if(key[KEY_UP] && key[KEY_RIGHT])
                            {
                                           shipspeedy = shipspeedy - 2;
                                           shipspeedx = shipspeedx + 2;
                                           shipbitmap = rightupshipx;
                            }
                            
                            if(key[KEY_UP] && key[KEY_LEFT])
                            {
                                           shipspeedy = shipspeedy - 2;
                                           shipspeedx = shipspeedx - 2;
                                           shipbitmap = leftupshipx;
                            }
                            
                            if(key[KEY_DOWN] && key[KEY_RIGHT])
                            {
                                           shipspeedy = shipspeedy + 2;
                                           shipspeedx = shipspeedx + 2;
                                           shipbitmap = rightdownshipx;
                            }
                            
                            if(key[KEY_DOWN] && key[KEY_LEFT])
                            {
                                           shipspeedy = shipspeedy + 2;
                                           shipspeedx = shipspeedx - 2;
                                           shipbitmap = leftdownshipx;
                            }
                            
                            if(key[KEY_X])
                            {
                                          if(red == 1)
                                          {
                                                 masked_blit(red1, buffer, 0, 0, shipposx + 11, shipposy -6, 12, 12);
                                                 redammo = redammo -1;
                                                 rest(1000);
                                          }
                                          
                                          if(red == 2)
                                          {
                                                 masked_blit(red2, buffer, 0, 0, shipposx + 11, shipposy -6, 12, 12);
                                                 redammo = redammo -2;
                                                 rest(1000);
                                          }
                            }
                            
                            
                            
                            
                            
                            //--------------------------------------------------
                            //Cheats (Muwah ha ha...)
                            //--------------------------------------------------
                            if(key[KEY_ALT] && key[KEY_E])
                            {
                                          red = red + 1;
                                          rest(100);
                            }
                            
                            if(!key[KEY_ALT] && key[KEY_E])
                            {
                                          redammo = redammo + 1;
                            }
                            
                            if(key[KEY_ALT] && key[KEY_R])
                            {
                                          yellow = yellow + 1;
                                          rest(100);
                            }
                            
                            if(!key[KEY_ALT] && key[KEY_R])
                            {
                                          yellowammo = yellowammo + 1;
                            }
                            
                            if(key[KEY_ALT] && key[KEY_T])
                            {
                                          blue = blue + 1;
                                          rest(100);
                            }
                            
                            if(!key[KEY_ALT] && key[KEY_T])
                            {
                                          blueammo = blueammo + 1;
                            }
                            
                            //--------------------------------------------------
                            //Keep the ship moving at a reasonable speed
                            //--------------------------------------------------
                            
                            if(shipspeedx <= -10)
                            {
                                           shipspeedx = -10;
                            }  
                            
                            if(shipspeedx >= 10)
                            {
                                           shipspeedx = 10;
                            }  
                            
                            if(shipspeedy <= -10)
                            {
                                           shipspeedy = -10;
                            }  
                            
                            if(shipspeedy >= 10)
                            {
                                           shipspeedy = 10;
                            }  
                            
                            
                            
                            //--------------------------------------------------
                            //Draw Everything
                            //--------------------------------------------------
                            
                            
                            
                            
                            clear_bitmap(buffer);
                            
                            
                            textprintf_ex(buffer, font, 100, 610, makecol(255, 0, 0),
                            -1, "Red Ammo Remaining: %d", redammo);
                            
                            textprintf_ex(buffer, font, 100, 635, makecol(255, 255, 0),
                            -1, "Yellow Ammo Remaining: %d", yellowammo);
                            
                            textprintf_ex(buffer, font, 100, 660, makecol(0, 0, 255),
                            -1, "Blue Ammo Remaining: %d", blueammo);
                            
                            textprintf_ex(buffer, font, 800, 610, makecol(255, 255, 255),
                            -1, "Life Remaining: %d", shiphp);
                            
                            textprintf_ex(buffer, font, 400, 610, makecol(255, 0, 0),
                            -1, "Red Level: %d", red);
                            
                            textprintf_ex(buffer, font, 400, 635, makecol(255, 255, 0),
                            -1, "Yellow Level: %d", yellow);
                            
                            textprintf_ex(buffer, font, 400, 660, makecol(0, 0, 255),
                            -1, "Blue Level: %d", blue);
                            
                            textprintf_ex(buffer, font, 600, 610, makecol(255, 192, 0),
                            -1, "Orange Level: %d", orange);
                            
                            textprintf_ex(buffer, font, 600, 635, makecol(0, 255, 0),
                            -1, "Green Level: %d", green);
                            
                            textprintf_ex(buffer, font, 600, 660, makecol(255, 0, 255),
                            -1, "Purple Level: %d", purple);
                            
                            
                            if(shipbitmap == 0)
                            {
                                          masked_blit(spaceship, buffer, 0, 0, 
                                          shipposx, shipposy, 50, 50);
                            }
                            
                            if(shipbitmap == 1)
                            {
                                          masked_blit(leftship, buffer, 0, 0, 
                                          shipposx, shipposy, 50, 50);
                            }
                            
                            if(shipbitmap == 2)
                            {
                                          masked_blit(rightship, buffer, 0, 0, 
                                          shipposx, shipposy, 50, 50);
                            }
                            
                            if(shipbitmap == 3)
                            {
                                          masked_blit(forwardship, buffer, 0, 0, 
                                          shipposx, shipposy, 50, 50);
                            }
                            
                            if(shipbitmap == 4)
                            {
                                          masked_blit(backship, buffer, 0, 0, 
                                          shipposx, shipposy, 50, 50);
                            }
                            
                            if(shipbitmap == 5)
                            {
                                          masked_blit(leftupship, buffer, 0, 0, 
                                          shipposx, shipposy, 50, 50);
                            }
                            
                            if(shipbitmap == 6)
                            {
                                          masked_blit(rightupship, buffer, 0, 0, 
                                          shipposx, shipposy, 50, 50);
                            }
                            
                            if(shipbitmap == 7)
                            {
                                          masked_blit(leftdownship, buffer, 0, 0, 
                                          shipposx, shipposy, 50, 50);
                            }
                            
                            if(shipbitmap == 8)
                            {
                                          masked_blit(rightdownship, buffer, 0, 0, 
                                          shipposx, shipposy, 50, 50);
                            }
                            
                            if(shipbitmap == 9)
                            {
                                          masked_blit(hyperdrive, buffer, 0, 0,
                                          shipposx, shipposy, 50, 50);
                            }
                            
                            
                            
                            blit(buffer, screen, 0, 0, 0, 0, 1024, 768);
                            
                            //--------------------------------------------------
                            //Other Calculations
                            //--------------------------------------------------
                            if(red > yellow)
                            {
                                   orange = yellow;
                            }
                            
                            if(red < yellow)
                            {
                                   orange = red;
                            }
                            
                            if(red == yellow)
                            {
                                   orange = red;
                            }
                            
                            if(yellow > blue)
                            {
                                   green = blue;
                            }
                            
                            if(yellow < blue)
                            {
                                   green = yellow;
                            }
                            
                            if(yellow == blue)
                            {
                                   green = yellow;
                            }
                            
                            if(blue > red)
                            {
                                   purple = red;
                            }
                            
                            if(blue < red)
                            {
                                   purple = blue;
                            }
                            
                            if(blue == red)
                            {
                                   purple = blue;
                            }
                            //Keep the levels below 6
                            if(red >= 6)
                            {
                                    red = 6;
                            }
                            
                            if(yellow >= 6)
                            {
                                    yellow = 6;
                            }
                            
                            if(blue >= 6)
                            {
                                    blue = 6;
                            }
                            
                            
                            
                            rest(10);
                            
                            time = time;
                            
        }
                            
                            
                  
        
        
        /* put your code here */
	}
	destroy_bitmap(movemaps);
	destroy_bitmap(screen);
	destroy_bitmap(spaceship);

	deinit();
	return 0;
}
END_OF_MAIN();

void init() {
	int depth, res;
	allegro_init();
	depth = desktop_color_depth();
	if (depth == 0) depth = 32;
	set_color_depth(depth);
	res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 1024, 768, 0, 0);
	if (res != 0) {
		allegro_message(allegro_error);
		exit(-1);
	}

	install_timer();
	install_keyboard();
	install_mouse();
	/* add other initializations here */
}

void deinit() {
	clear_keybuf();
	
	/* add other deinitializations here */
}



(I know, not very efficient or graceful...) But, my problem isnt the game, its the blitting... It only blits 9 times, after that, it crashes, no matter what the tenth blit is! This is a weird problem, and I'm a noob, probably some error on my part... If anyone has any idea what I'm doing wrong, please tell me.

Share this post


Link to post
Share on other sites
im no expert either but this area made me thin for a sec..

while (!key[KEY_ESC])
{
int red; red = 1;
int orange; orange = 1;
int yellow; yellow = 1;
int green; green = 1;
int blue; blue = 1;
int purple; purple = 1;

that is just a piece of it. it seems that every time this loops you are creating vars. now does your program crash or just stop blitting? i may be wrong but i believe something like this may cause an error but again im no expert /shrug try creatig these vars outside the loop and see what happens. only thing i can think of sorry

Share this post


Link to post
Share on other sites
No, that's a good idea, but that is only the first loop.

it goes through it once and hits the SECOND loop.

Both loops check for the same break condition....

So when you exit one, the other loop automatically terminates!

It's not very orthodox, but it's my style to be clever.

Anyway, I had that problem before, and my game barely got started before it crashed my whole system...

What I mean, is that allegro only allows 9 blits in a program!!!

After that, it crashes horribly, however, it will repeat a blit!!!

I need a way to have a bitmap for a variable to allow my game to function properly...

Share this post


Link to post
Share on other sites
Some things I'd suggest by skimming through your code:

First of all, you really should split the code up into functions, each performing one and only one task. Right now, your main() performs pretty much everything, from logic updates (input, movement, etc) to rendering, making it a mess.

Second, please use structsto group relevant information together (ie a SHIP struct which might contain all info pertinent to a ship, from the BITMAP to its hp, position, velocity and bullets left).

Third, please comment what each loop does - right now it's a bit blurry.

These out of the way, let's move to some real issues:

- You do not free all resources on exit. You should destroy _all_ BITMAPs. One way to do this is to put all needed destroy_bitmap() calls into a cleanup function, and call this on all possible exit points (as far as I can see, both in the 'if (shiphp <= )' clause, and at the end of the main() function).

- You do not make sure that all resources are loaded correctly. I'd suggest you create a load() function that returns a bool (if this is C++, which you haven't specified) if all BITMAPs are loaded correctly. To see if a BITMAP has been loaded, check that it is not NULL.

Apart from these, I can't see what could be wrong. It would help if you did tell us what error is reported when it crashes (my mind reading skills tell me it is a null reference ;) )

If you need more help, please find out on what line exactly it crashes (which blit exactly is the faulty one). It would be helpful if you also created a small package with all needed files so we could check it out.

Share this post


Link to post
Share on other sites
I'm sorry, I'm new to this kind of thing...

I don't exactly know how to "optimize" my code, but I do keep it organized and easy to understand.

The faulty blit is the tenth one called, no matter where or which it was.

The error message is a windows error, and I don't speak HEX in programs I don't 100% understand, even if that's what you're looking for (fixed that one)

It still bothers me, but I managed a workaround...

I re-assign a bitmap depending on a keypress.

It goes like this... (snippets)


BITMAP* ship; ship = load_bitmap("ship.bmp",NULL);
int shipspeedy; shipspeedy = 0;

if(key[KEY_UP])
{
ship = load_bitmap("shipforward.bmp",NULL);
shipspeedy = -4;
}

if(key[KEY_DOWN])
{
ship = load_bitmap("shipback.bmp",NULL);
shipspeedy = 4;
}

masked_blit(ship, buffer, 0, 0, shipposx, shipposy, 50, 50)








It isn't efficient or proper, but it's the only way I know how to do it (I still don't completely understand the basics of Functions...)

[Edited by - Gamester3333 on November 12, 2005 11:54:29 PM]

Share this post


Link to post
Share on other sites
Ouch, this is bad! The 'load_bitmap(...)' function allocates memory for the bitmap you're loading. The 'BITMAP *ship' is a pointer to this allocated memory. In this snippet you repeadetely allocate memory every time the player presses a key, without previously releasing it (by calling 'destroy_bitmap'), which means you'll soon run out of memory!

The approach you had in the first snippet was more correct: load all resources on startup, use them and destroy them when the program exits. You should generally not load resources in the program loop, as it will kill your speed.

Apart from this, you must find more about the problem. You say whichever blit is called tenth will crash. What do these tenth blits have in common? (Do they try to blit a specific bitmap for example?).

Lastly, one thing you should really do is error checking: make sure all BITMAPs are loaded correctly. To do this change the loading code as follows:


// Instead of this:
BITMAP* spaceship; spaceship = load_bitmap("Spaceship Stationary.bmp",NULL);

// Do this:
BITMAP* spaceship = NULL;
spaceship = load_bitmap("Spaceship Stationary.bmp",NULL);
if (spaceship == NULL)
allegro_message("ERROR: 'Spaceship Stationary.bmp' was not found!\n");



If some resource is not correctly loaded and you try to use it, the program will crash (I believe this is what's happening in your case).

Share this post


Link to post
Share on other sites
being that your code structure is hap hazard and hard to read, icant exactly tell you where your problem is. I did observe that you use way too many bitmaps however. You can make one bitmap for each color of shot by putting all the shots of that type on one larger bitmap. So you would load the bitmap yellowshots for example, and then when you blit you use differnt coordinates to render differnt parts of the yellowshots bitmap, instead of using multiple bitmaps for each shot.

that could help shorten your code and help memory issues alot.

Share this post


Link to post
Share on other sites
I'm a noob, but I do belive that my program runs correctly.

Because the pointer BITMAP* points to a bitmap, I merely reassign that pointer to a new bitmap.

I have a good computer, though, so I may not notice memory issues...

how long would it take for me to actually notice a problem???

but, I digress...

I have a new problem now...

I can't get my lazer to fire...

my code goes like this


if(key[KEY_X] && !key[KEY_C] && !key[KEY_V] && lazerdelay == 50 && redammo > 0)
{
if(blast1posx == 0)
{
blast1posx = shipposx + 18;
}

if(blast1posx == shipposx + 18)
{
blast1posx = blast1posx + blast1speedx;
}

if(blast1posy == 0)
{
blast1posy = shipposy - 3;
}

if(blast1posy == shipposy)
{
blast1posy = blast1posy + blast1speedy;
}

if(blast1posy >= 400)
{
destroy_bitmap(blast1);
}

blast1posy = blast1posy - blast1speedy;

blast1posx = blast1posx + 0;

redammo = redammo - red;
lazerdelay = 0;
}



..............

masked_blit(blast1, buffer, 0, 0,
blast1posx, blast1posy, 12, 12);

blit(buffer, screen, 0, 0, 0, 0, 1024, 768);

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Look up "Allegro Datafiles", they will allow you to (un)load all your bitmaps in with one line function.

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