|
2 class objects same reaction. |
Tony Montana
Member #7,365
June 2006
|
I have 2 Monsters, one's direction is left, one's right.
Later, I use allways DEF and change the sprite when it's needed, like DEF=B2; Monster monster(5,5,5,0,0);//4th variable is the direction Monster monster1(5,5,5,1,150); And drawing the objects: draw_rle_sprite(screen, monster.DEF, monster.x, monster.y); draw_rle_sprite(screen, monster1.DEF, monster1.x, monster1.y); Please help |
Elverion
Member #6,239
September 2005
|
That is a horrible way to manage the loading of bitmaps. Don't let each object handle it's own loading of them, or you will end up with copies. It might not be a problem with just 2 monsters of the same type, but imagine 100 of them, each loading the same bitmap, and all wasting RAM. You also have some memory leakage in the code you have posted. You load "bmp" 3 times, and only destroy it once. That means there's the memory for the other two loaded copies floating around...somewhere... It's almost impossible to tell where your problem is without more code. Specifically, more on the monster class. Post more, and lets see if we can't fix it. Also, if I remember correctly, RLE sprites don't do a whole lot for you anymore unless you are targetting older systems. Because of this, you might want to just pack all of your monster/player/npc graphics into one datafile, load it at the begining of the game, use the graphics directly from it, and unload the datafile at the end of the game. Someone correct me if I'm wrong here. To use a bitmap from a datafile, It's something like... BITMAP *bmp = (BITMAP *)data_f[MONSTER_1].dat; masked_blit(bmp, frame_buffer, 0, 0, x, y, bmp->w, bmp->h); where MONSTER_1 is defined as the bitmap's position in the datafile. Use grabber's auto-header for this. -- |
Tony Montana
Member #7,365
June 2006
|
I understand, now I'm working for a tilesystem, when expanding my project I'll us grabber.
|
Jonatan Hedborg
Member #4,886
July 2004
|
I usualy use a bitmap-handler singleton for managing that sort of thing. It becomes quite elegant; //In monsters constructor: BitmapHandler *b = BitmapHandler.getInstance(), myBitmap = b->getBitmap("monster1"); And the bitmap-handler looks through an stl map, looking any reference to a bitmap named monster1 - if it finds it, it returns the pointer to it. If not, it loads it, adds it to the map and returns the pointer. Also, OP; You should probably not create all your monsters etc on the stack like you do now. A better way is probably Monster *monster = new Monster(....); And then access the members with '->' insetad of '.' This way it wont be automaticly deleted when you leave the function it was created in, and you can send the pointer to a function without having to send all the data in the class.
|
Tony Montana
Member #7,365
June 2006
|
Very stupid of me (cost 2 days...): draw_rle_sprite(buffer, monster.DEF, monster.x, monster.y); draw_rle_sprite(buffer, monster.DEF, monster1.x, monster1.y); Now, corrected: draw_rle_sprite(buffer, monster.DEF, monster.x, monster.y); draw_rle_sprite(buffer, monster1.DEF, monster1.x, monster1.y);
|
Audric
Member #907
January 2001
|
Jonatan: |
Ron Novy
Member #6,982
March 2006
|
Yeah just make sure you fix those memory leaks! ex
---- |
|