Animating with Allegro within VC++ 2005  
Author Message
jd_1604





PostPosted: Visual C++ General, Animating with Allegro within VC++ 2005 Top

G'Day all. After working some time on a code that enables the user to animate a .bmp image on screen using the arrow keys, it has been a story of so close yet so far. After going through and fixing any sytntax errors, my program compiled and linked fine, the window came up with my first animation frame on it but as soon as I went to move it with the arrow keys, 'Bing Bing' ".....Corruption of the Heap , Debug Assertion Failed (file: dbgheap.c), ...runtime to terminate in an unusual way . Now I am lost and getting very frustrated. Can someone please give me any clues as too what the hell is going on !!


Visual C++14  
 
 
Damien Watkins - MSFT





PostPosted: Visual C++ General, Animating with Allegro within VC++ 2005 Top

Hello

Re: Animating with Allegro within VC++ 2005

We need (a lot) more information before we can help, such as what line of code causes the error (and probably a posting of the entire function that has that line) and what settings are you using on the project and what environment you have.

Thanks

Damien



 
 
jd_1604





PostPosted: Visual C++ General, Animating with Allegro within VC++ 2005 Top

G'Day Damien.

First off, I am really not sure the exact line of code that is causing the error. I will insert the entire code which comprises of two source files and one header. I am using VC8, downloaded and installed what I presume to be the latest Allegro enviroment and use the following settings within the project properties:
1. I uncheck Create directory for solution first up.
2. Within config properties c/c++/general, Debug Information Format is changed to Program Database for edit and continue (/ZI)
3. Optimization is disabled
4. Code Generation is changed to Multi-Threaded Debug DLL (/MDd)
5. Linker/Input, alld.lib is added to Additional Dependencies
6. Generate Debug info is set to Yes.
I transferred the code across to DEV C++ which also has Allegro enviroment installed. Again it will link and compile, I get the window up with my first frame of animation, but as soon as an arrow key is pressed, I get the error:" Allegro has encountered a problem and needs to close. We are sorry........"

Here is the code in full:
//RobotMoves.cpp

#include <allegro.h>
#include "Character.h"



BITMAP* buffer;
Character myRobot;

void Setup(){

buffer= create_bitmap(640, 480);
myRobot.SetImagesLeft( "G:\\My Images\\RobotWalk\\Left01.bmp", "Left02.bmp", "Left03.bmp");
myRobot.SetImagesRight("G:\\My Images\\RobotWalk\\Right01.bmp", "Right02.bmp", "Right03.bmp");
myRobot.SetImagesUp("G:\\My Images\RobotWalk\\Up01.bmp", "Up02.bmp", "Up03.bmp");
myRobot.SetImagesDown("G:\\My Images\\RobotWalk\\Down01.bmp","Down02.bmp", "Down03.bmp");

myRobot.Show( screen);

}

void movePlayer(){

if( key[KEY_LEFT]){
myRobot.MoveOnX( LEFT, buffer, screen);
} else if( key[KEY_RIGHT]){
myRobot.MoveOnX( RIGHT, buffer, screen);
} else if( key[KEY_UP]){
myRobot.MoveOnY( UP, buffer,screen);
} else if( key[KEY_DOWN]){
myRobot.MoveOnY( DOWN, buffer,screen);
}

}

int main(int argc, char *argv[])
{
allegro_init();
install_mouse();
install_keyboard();
set_color_depth(16);

set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);

Setup();

while( !key[KEY_ESC]){
movePlayer();
}

return 0;
}
END_OF_MAIN();
__________
//Character.cpp
#include "Character.h"

Character::Character(){

leftDone= false;
rightDone= false;
upDone= false;
downDone= false;

direction = DOWN;

int x= 0;
int y= 0;

}

Character::~Character(){

delete [] images;

}

void Character::SetImagesLeft(char image1[], char image2[], char image3[]){

images[0][0]= load_bitmap(image1, NULL);
images[0][1]= load_bitmap(image2, NULL);
images[0][2]= load_bitmap(image3, NULL);

leftDone= true;

}

void Character::SetImagesRight(char image1[], char image2[], char image3[]){

images[1][0]= load_bitmap(image1, NULL);
images[1][1]= load_bitmap(image2, NULL);
images[1][2]= load_bitmap(image3, NULL);

rightDone= true;

}

void Character::SetImagesUp(char image1[], char image2[], char image3[]){

images[2][0]= load_bitmap(image1, NULL);
images[2][1]= load_bitmap(image2, NULL);
images[2][2]= load_bitmap(image3, NULL);

upDone= true;

}

void Character::SetImagesDown(char image1[], char image2[], char image3[]){

images[3][0]= load_bitmap(image1, NULL);
images[3][1]= load_bitmap(image2, NULL);
images[3][2]= load_bitmap(image3, NULL);

downDone= true;

}


bool Character::CheckImages(){

if( rightDone && leftDone && upDone && downDone)
return true;

return false;

}

int Character::GetX(){

return x;

}

int Character::GetY(){

return y;

}

void Character::SetX(int newValue){
x = newValue;
}

void Character::SetY(int newValue){
y = newValue;
}

void Character::MoveOnX( int newDirection, BITMAP* tempBitmap, BITMAP* mainBitmap){

direction = newDirection;

if( CheckImages()){
if( direction == LEFT){

for( int i=0; i<=3; i++){
EraseOldSprite( tempBitmap);
x -= 5;

if (i == 1 || i == 3)
DrawNewSprite( tempBitmap, images[0][0]);

if (i == 0)
DrawNewSprite( tempBitmap, images[0][1]);

if (i == 2)
DrawNewSprite( tempBitmap, images[0][2]);

draw_sprite( mainBitmap, tempBitmap, 0, 0);

rest(50);

}
}else {

for( int i=0; i<3; i++){
EraseOldSprite( tempBitmap);
x += 5;

if (i ==1 || i == 3)
DrawNewSprite( tempBitmap, images[1][0]);

if (i == 0)
DrawNewSprite( tempBitmap, images[1][1]);

if (i == 2)
DrawNewSprite( tempBitmap, images[1][2]);

draw_sprite( mainBitmap, tempBitmap, 0, 0);

rest(50);

}
}
}
}

void Character::MoveOnY( int newDirection, BITMAP* tempBitmap, BITMAP* mainBitmap){

direction = newDirection;

if( CheckImages()){

if( direction == UP){

for( int i=0; i<=3; i++){
EraseOldSprite( tempBitmap);
y -= 5;

if (i ==1 || i == 3)
DrawNewSprite( tempBitmap, images[2][0]);

if (i == 0)
DrawNewSprite( tempBitmap, images[2][1]);

if (i == 2)
DrawNewSprite( tempBitmap, images[2][2]);

draw_sprite( mainBitmap, tempBitmap, 0, 0);

rest(50);

}
} else {

for( int i=0; i<=3; i++){
EraseOldSprite( tempBitmap);
y += 5;

if (i ==1 || i ==3)
DrawNewSprite( tempBitmap, images[3][0]);

if (i == 0)
DrawNewSprite( tempBitmap, images[3][1]);

if (i == 2)
DrawNewSprite( tempBitmap, images[3][2]);

draw_sprite( mainBitmap, tempBitmap, 0, 0);

rest(50);

}
}
}
}




void Character::EraseOldSprite(BITMAP* tempBitmap){

rectfill( tempBitmap, GetX(), GetY(), GetX() + 20, GetY() + 20, makecol ( 0, 0, 0));

}

void Character::DrawNewSprite(BITMAP *tempBitmap, BITMAP *spriteToDraw){

draw_sprite( tempBitmap, spriteToDraw, GetX(), GetY());

}

void Character::Show(BITMAP* mainBitmap){

if( CheckImages())
draw_sprite( screen, images[3][0], 0, 0);

}

______
//Character.h

#ifndef CHARACTER_H
#define CHARACTER_H

#define LEFT 1
#define RIGHT 2
#define UP 3
#define DOWN 4

#include <allegro.h>

class Character{

public:
Character();
~Character();

//Set Images

void SetImagesLeft( char image1[], char image2[], char image3[]);
void SetImagesRight( char image1[], char image2[], char image3[]);
void SetImagesUp( char image1[], char image2[], char image3[]);
void SetImagesDown( char image1[], char image2[], char image3[]);

//Keep Program From Crashing

bool CheckImages();

int GetX();
void SetX( int newValue);
void MoveOnX( int newValue, BITMAP* tempBitmap, BITMAP* mainBitmap);

int GetY();
void SetY( int newValue);
void MoveOnY( int newValue, BITMAP* tempBitmap, BITMAP* mainBitmap);

void EraseOldSprite( BITMAP* tempBitmap);
void DrawNewSprite( BITMAP* tempBitmap, BITMAP* spriteToDraw);

void Show(BITMAP* mainBitmap);

private:
bool leftDone;
bool rightDone;
bool upDone;
bool downDone;

int direction;



int x;
int y;


// This will contain all images
BITMAP *images[4][3];

};

#endif