Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » c++ design...

This thread is locked; no one can reply to it. rss feed Print
 1   2 
c++ design...
Don Freeman
Member #5,110
October 2004
avatar

What I mean is that you will only have one instance and only one pointer to this image and this will make your code faster and use less memory since you don't need 50 pointers to the same image! Just use one pointer for the image you need. I did notice I didn't show you how to use the static data from the class so here it goes:

// [Global scope:]
class Data
{
static int iX, iY;
};
int Data::iX = 0;
int Data::iY = 0;

// [Function scope:]
void SomeFunction()
{
Data *pData = new Data;
pData->iX = 50;
}
pData->iY = 40;

Hope that helps!:)

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

23yrold3yrold
Member #1,134
March 2001
avatar

Your code, Mr. Freeman ...

1class Enemy
2{
3 public:
4 
5 Enemy() { pImage = NULL; }
6 ~Enemy() { if(pImage) { delete pImage; pImage = NULL; } }
7 
8 int LoadImage(char *pFileName)
9 {
10 pImage = load_bitmap(pFileName,NULL); // truecolor doesn't need pallete
11 if ( !pImage )
12 return -1;
13 return 0;
14 }
15 
16 static BITMAP *pImage;
17};

Okay, I create 50 enemies, call LoadImage() so they all have the image, one enemy dies ... and I'm screwed. ;D I also like the idea of seperate pointers for enemies so I could, say, create 4 slightly different images/animations for an enemy for variety's sake, so it doesn't just look like the same boring enemy clones running around. Or you could use the same enemy class with different level-specific graphics (tons of platformers and shooters do this).

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

ReyBrujo
Moderator
January 2001
avatar

You cannot delete a bitmap :P

I prefer the idea of a singleton holding all the images, and the mobiles requesting pointer to a const BITMAP for them to blit.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

You cannot delete a bitmap

And the check for NULL before deleting is a waste and the constructor code should be in the initialization list, etc. Just talking about the topic here. :) ;)

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Don Freeman
Member #5,110
October 2004
avatar

To 23yrold3yrold:
Quote:
Okay, I create 50 enemies, call LoadImage() so they all have the image, one enemy dies ... and I'm screwed. I also like the idea of seperate pointers for enemies so I could, say, create 4 slightly different images/animations for an enemy for variety's sake, so it doesn't just look like the same boring enemy clones running around. Or you could use the same enemy class with different level-specific graphics (tons of platformers and shooters do this).

If you read his openning question on this thread...
Quote:
...the problem is that ALL darkstar have the same image...

I KNOW that if you did what you say you would not be able to use differant images, but he said that he only has one!;)

Also, checking to see if the pointer is NULL or not will save a run time error if you delete a pointer twice...it is also good coding practice!:P

I DID do a no no though:(, I didn't write destroy_bitmap(pImage);)
My bad...I didn't mean to type delete pImage...8-)

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

Also, checking to see if the pointer is NULL or not will save a run time error if you delete a pointer twice...it is also good coding practice! :P

No, it is a bad coding practice because it wastes time. Deleting a NULL pointer is guarenteed to do nothing. No error, no crash.

Quote:

I KNOW that if you did what you say you would not be able to use differant images, but he said that he only has one!;)

That's fine, but it still doesn't change the fact that when one dies, the image is lost. A better idea might be to call the initialization function before a level, and another function to destroy the bitmap after. And he may change his mind later. ;) It's not like pointers are memory hogs ...

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Don Freeman
Member #5,110
October 2004
avatar

I retract my statements...I messed up:D

You could use ref counts such as:

class Object
{
public:
Object()
{
pImage = NULL;
iRefCount++;
}
~Object()
{
iRefCount--;
if (iRefCount==0)
destroy_bitmap(pImage);
}
static BITMAP *pImage;
unsigned long iRefCount;
};

And yes, having just the pointer around harms no one, but when he's got 50 or so pointing to and having loaded all of the same image it is a huge waste and unneccassary when all you need is one BITMAP loaded!:o

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

it is a huge waste

50 bytes? ;D

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Don Freeman
Member #5,110
October 2004
avatar

I'm talking about when he has all 50 loaded using load_bitmap::)...not the 50 pointers...even though if you could use one instead of 50...well that's still another improvement...I KNOW NOT MUCH:P

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

I'm talking about when he has all 50 loaded using load_bitmap

Nobody suggested he do that, though ...

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

 1   2 


Go to: