I am making a prototype for a 2D fighting game in C++ using allegro, but I am having trouble incorporating classes into it. I have narrowed the one function in my kCharacter class to
BITMAP *kCharacter::Walk ()
{
return CharacterImage;
}
In int main (), the code I have for assigning this bitmap is
kCharacter * Player1; //Player1 class
BITMAP * Player1Image //Player1's current Image
if (key[KEY_LEFT])
{
Player1Image = Player1->Walk ();
}
I'm not really sure what is going on. I have CharacterImage declared in kCharacter and I have it initialised in the constructor. What happens is, the text I have in the code before the if statement works grand, but then when it gets to the if statement windows experiences a problem with the applicatino a closes it down. Any help would be appreciated, thanks.
P.S. Please don't flame me.
More code.
Steve.h
#include <allegro.h>
class CCharacter
{
public:
CCharacter ();
~CCharacter ();
int x;
int y;
BITMAP * CharacterWait;
BITMAP * Wait ();
};
CCharacter::CCharacter ()
{
x = 20;
y = 50;
CharacterWait = create_bitmap (32, 32);
clear_bitmap (CharacterWait);
CharacterWait = load_bitmap ("Character_Wait.bmp", NULL);
}
CCharacter::~CCharacter ()
{
x = 0;
y = 0;
destroy_bitmap (CharacterWait);
}
BITMAP * CCharacter::Wait ()
{
return CharacterWait;
}
Main.cpp
#include "Steve.h"
#define MODE GFX_AUTODETECT_WINDOWED
#define WIDTH 500
#define HEIGHT 100
#define WHITE makecol (255, 255, 255)
int main ()
{
allegro_init();
install_timer();
install_keyboard();
set_color_depth(32);
set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0);
CCharacter * Player1;
BITMAP * Player1_Image;
textprintf_ex (screen, font, 0, 0, WHITE, 0, "Text Sample Number 1");
while (!key[KEY_ESC])
{
textprintf_ex (screen, font, 0, 10, WHITE, 0, "Text Sample Number 2");
{
Player1_Image = Player1->Wait ();
}
textprintf_ex (screen, font, 0, 90, WHITE, 0, "Text Sample Number 3");
draw_sprite (screen, Player1_Image, Player1->x, Player1->y);
rest (100);
}
Player1->~CCharacter ();
allegro_exit ();
return 0;
}END_OF_MAIN();
There are many things wrong here.
1) You never create a CCharacter object, just declare a pointer to one.
2) You create a bitmap and then just throw it away in the constructor.
3) You manually call the destructor, which you shouldn't do (unless you really, really know what you're doing, and those cases are rare).
4) You didn't use the code tags when making this post.
1) You never create a CCharacter object, just declare a pointer to one.
RE: How do I do that?
2) You create a bitmap and then just throw it away in the constructor.
RE: How did I do that?
3) You manually call the destructor, which you shouldn't do (unless you really, really know what you're doing, and those cases are rare).
RE: I'm going fixing this now.
4) You didn't use the code tags when making this post.
RE: Sorry about that. Do you use BBcode or HTML on this site?
1)
CCharacter * Player1 = new CCharacter();
2)
CharacterWait = create_bitmap (32, 32); clear_bitmap (CharacterWait); CharacterWait = load_bitmap ("Character_Wait.bmp", NULL);
The first two lines are superflous. load_bitmap will create a bitmap for you. All you need is
CharacterWait = load_bitmap ("Character_Wait.bmp", NULL);
4) [.code] ... [./code] without the .'s after the [
Thank you soo much everyone! This is all valuable programming experience!
May I kindly suggest that you consider reading a C++ introduction? It seems like you might have a lot of potential to improve your knowledge of the basics of this language.
Don't worry, I have read enough C++ stuff (I know that isn't possible), i.e. "Game Programming all in One 1st and 3rd ed.", and a number of online tutorials. It's just practical experience I lack, especially with classes as there aren't many tutorials on game programming with them.
In that case I'd suggest that you consider reading the books and playing around with the examples presented there. I am aware that the examples in these books won't be as exciting as writing a game. But please consider that learning the language and then using that knowledge to create a game will be more fun that starting with the game, getting frustrated and knowing neither the language or how to program games at the end.
Another option would be to read the books. Where reading means "reading the book in a way, that you actually remember what was written in it."