Aug 22, 2012 at 4:09am UTC
I tried to make a small "program" where a person moves around the screen and you control him, but every time i run it, it keeps crashing.
Here is my source code please help.
//MAIN.CPP
#include<allegro.h>
#include"Hero.h"
using namespace std;
BITMAP *buffer;
Character hero;
//inserting images (sprites)
void Setup(){
buffer=create_bitmap(640,480);
hero.SetImagesLeft("normalstand2.bmp","walkleft1.bmp","walkleft2.bmp");
hero.SetImagesRight("normalstand.bmp","walkright1.bmp","walkright2.bmp");
hero.Show(screen);
}
//Movement for the player
void movePlayer(){
if( key[KEY_LEFT]){
hero.MoveOnX( LEFT, buffer, screen);
} else if( key[KEY_RIGHT]){
hero.MoveOnX( RIGHT, buffer, screen);
}
}
//Main details
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]){
rest(50);
movePlayer();
}
return 0;
}
END_OF_MAIN();
##############################################################
//Hero.h
#ifndef CHARACTER_H
#define CHARACTER_H
#define LEFT 1
#define RIGHT 2
#include <allegro.h>
class Character{
public:
Character();
~Character();
// The following 2 methods will be used to set the images for the different
//stages of movement
void SetImagesLeft( char image1[],char image2[],char image3[]);
void SetImagesRight( char image1[],char image2[],char image3[]);
//This part is to 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;
int direction;
int x;
int y;
//This will contain all the images
BITMAP *images[2][3];
};
#endif
#############################################################
//Hero.cpp
#include "Hero.h"
Character::Character(){
leftDone= false;
rightDone= false;
direction =LEFT;
int x= 0;
int y= 0;
}
Character::~Character(){
delete [] images;
}
//Here I assign each image to their parts
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;
}
bool Character::CheckImages(){
if( rightDone && leftDone)
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;
//Here I make the loops for the images
if( CheckImages()){
if( direction == LEFT){
for( int i=0; i<=3; i++){
EraseOldSprite( tempBitmap);
x -= 2;
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 += 2;
if (i == 1 || i == 3)
DrawNewSprite( tempBitmap, images[1][1]);
if (i==0)
DrawNewSprite( tempBitmap, images[1][0]);
if (i==2)
DrawNewSprite( tempBitmap, images[1][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);
}
Last edited on Aug 22, 2012 at 3:02pm UTC