Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

ank2

Reuse bitmap function

This topic is 6079 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i''ve created a function that blits a bitmap on screen. The problem is that this function only works once. When I try a second time the it does nothing. I''m pretty sure thats because player already contains something after being used once. My question is how do I make the function reusable so that I can blit multiple versions of the bitmap with the same function? Thanks. void DrawPlayerB(int x, int y) { BITMAP *player; player = create_bitmap_ex(32 ,50, 50); player = load_bitmap("playerB.bmp", NULL); Blit(player, x, y); }

Share this post


Link to post
Share on other sites
Advertisement
Try this - hopefully it should work.



void DrawPlayerB(BITMAP *player, const char *filename,
int x, int y)
{
player = create_bitmap_ex(32 ,50, 50);
player = load_bitmap(filename, NULL);
Blit(player, x, y);
}



This one is probably for different bitmaps.

I suppose you want to use 1 bitmap file which has a list of images and that is created by "create_bitmap_ex" , in that case you could have more parameters in the function.

Hopefully someone will correct if I am wrong - Thank you.


[edited by - flame_warrior on April 20, 2002 4:12:51 PM]

Share this post


Link to post
Share on other sites
Thanks flame_warrior, but i've now tried it like this. Is your example better? There won't be any memory problems will there? by the way i'm using Allegro.


//To be used by pcolor
const char blue[] = "playerB.bmp";//blue colored player
const char red[] = "playerR.bmp"; //red colored player

void DrawPlayer(const char pcolor[], int x, int y)
{
BITMAP *player=new BITMAP;
player = create_bitmap_ex(32 ,50, 50);
player = load_bitmap(pcolor, NULL);
Blit(player, x, y);
}


[edited by - ank2 on April 21, 2002 5:09:05 AM]

Share this post


Link to post
Share on other sites
Hi,

Here is how I see your code.

When the function is executed -
1. Create a player instance.
2. Execute Create_Bitmap_ex and put it into player
3. Load the bitmap and also put this into player. (something wrong here I think) ??

4. Blit to screen.

Seems like as 2 and 3 write into the same structure - something will probably get overwritten or get written at the wrong places.
Also why load a bitmap so many times, won''t you be using it more than just once - if you are using it more than once why not have it in memory ? Load It and set it before the Draw_Player function is executed.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yeah, something like this...

void DrawPlayer(BITMAP *player, int x, int y)
{
Blit(player, x, y);
}

BITMAP *LoadPlayer(char FileName[])
{
BITMAP *tmp;
tmp = create_bitmap_ex(32,50,50);
tmp = load_bitmap(FileName,NULL);
return tmp;
}


main ()
{
BITMAP *playerA, *playerB;
playerA = LoadPlayer("playerA.bmp";
playerB = LoadPlayer("playerB.bmp";

DrawPlayer(playerA,10,10);
DrawPlayer(playerB,100,100);
}

Billy - BillyB@mrsnj.com

Share this post


Link to post
Share on other sites
It looks like you''re allocating two areas of memory here. When you do a create_bitmap_ex it allocates enough memory for a bitmap of that size. And when you do load_bitmap it also allocates memory to hold the new bitmap. So you''re allocation twice the memory.

Try it like this:

void DrawPlayerB(BITMAP *player, const char name, int x, int y)
{
//player = create_bitmap_ex(32 ,50, 50);
player = load_bitmap(filename, NULL);
Blit(player, x, y);
}

and then convert it to this:

BITMAP *player = load_bitmap( "player.bmp", NULL );

void DrawPlayer( BITMAP *player, int x, int y )
{
Blit( player, x, y );
}

just so you dont load the bitmap everytime you want to draw it....because you would only be allocating more and more memory and never freeing it.

Denis Lamarche

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!