Jump to content

Quick Pointers Question


coldfire
 Share

Recommended Posts

Hello,

 

I started tinkering with a class to use for a sprite manger, and I'm almost certain I'm doing it wrong. The class loads an image which I think should be a pointer so I may assign the same texture to multiple sprites without using more video memory. The problem is no matter how I set it up with pointers, I would get errors. Then again, I don't use pointers very often at all either. I think my code is copying the image as opposed to referencing it.

 

class Sprite
{
private:
int x, y, width, height;
public:
TTexture texture;
Sprite()
{
	Position(0,0);
	Size(-1,-1);
};
void SetTexture(TTexture tex)
{
	texture = tex;
};
void Draw()
{
	DrawImage(texture, x, y, width, height);
};
void Position(int xpos, int ypos)
{
	x = xpos;
	y = ypos;
};
void Size(int w, int h)
{
	width = w;
	height = h;
}
protected:
};

 

Thanks in advance on any advice.

 

Coldfire

x_coldfire_x.png

AMD 64 X2 3800+ | 3G DDR2 667 | Windows 7 Ultimate 64 Bit | Nvidia Geforce 9800 GT 512MB DDR3

Link to comment
Share on other sites

Can you show the code usage? What you pass in to SetTexture() might not be valid. It could be you don't have the texture in your path and it's not loading correctly so it would fail anytime you try to use it. Step through your code and make sure your texture object is not null.

Link to comment
Share on other sites

Can you show the code usage? What you pass in to SetTexture() might not be valid. It could be you don't have the texture in your path and it's not loading correctly so it would fail anytime you try to use it. Step through your code and make sure your texture object is not null.

 

Thanks for the quick reply. In my main code I define as such:

 

TTexture TestTexture = LoadTexture( "abstract::testsprite.dds" );
Sprite TestSprite;
TestSprite.SetTexture(TestTexture);

 

Then, before the flip I call TestSprite.Draw()

x_coldfire_x.png

AMD 64 X2 3800+ | 3G DDR2 667 | Windows 7 Ultimate 64 Bit | Nvidia Geforce 9800 GT 512MB DDR3

Link to comment
Share on other sites

As long as TestSprite is in the same scope or higher (global) than where your flip call is then it wouldn't be your TestSprite object, it would be your texture not loading.

 

Step 1) Check what TestTexture is returning. Check if it's NULL because if it is, then that's your problem.

Step 2) Without seeing the entire code it's hard to say what scope level TestSprite is being created in.

Link to comment
Share on other sites

As it is right now, it works fine. The issue I'm having is that say I want to make a 2d game that has many of the same enemy on the screen at once. With my class, I would be loading the same texture every time I created a sprite, right? And if so, couldn't I take care of that redundancy using pointers instead?

x_coldfire_x.png

AMD 64 X2 3800+ | 3G DDR2 667 | Windows 7 Ultimate 64 Bit | Nvidia Geforce 9800 GT 512MB DDR3

Link to comment
Share on other sites

You could if you wanted to, but in LE if an asset is already loaded LE doesn't load it again, it just uses the currently loaded one.

 

In other engines (and in LE if you wanted to) you would create a SpriteManager class that loads all the unique sprites you need. Then you would give them some kind of unique id so you can identify each one. Then in your sprite class you would have position, location, sprite sheet id, etc. When you are ready to draw you call the sprites draw, which uses it's postion & rotation, and then queries the sprite managers texture by id to get the actual bitmap image returned. You could do a pointer, but it would be somewhat unsafe to give each sprite access to the actual bitmap data.

Link to comment
Share on other sites

Cool, I'm glad its set up that way. I also plan on adding a isVisible boolean along with a SpriteManager class that will eventually handle all the drawing itself. I was afraid the route I was going would consume extra resources. I'm attempting to recreate a 2d tower defense game that I recently programmed in DarkBasic Pro before I attempt a 3D version(in both DBP and LW) so I wanted to make sure I wasn't gonna kill it by having lotsa sprites. Thank you very much for the info.

x_coldfire_x.png

AMD 64 X2 3800+ | 3G DDR2 667 | Windows 7 Ultimate 64 Bit | Nvidia Geforce 9800 GT 512MB DDR3

Link to comment
Share on other sites

Thanks for the quick reply. In my main code I define as such:

 

TTexture TestTexture = LoadTexture( "abstract::testsprite.dds" );
Sprite TestSprite;
TestSprite.SetTexture(TestTexture);

 

Then, before the flip I call TestSprite.Draw()

 

I know this issue seems to be resolved now, but just for general programming practice, in your class declaration, you should move the

TTexture texture;

into the private or protected areas, because right now, the SetTexture function is not needed:

 

TTexture TestTexture = LoadTexture( "abstract::testsprite.dds" );
Sprite TestSprite;
TestSprite.texture = TestTexture;

 

At the moment, that's valid. But in OOP, we don't like code like that. By moving the texture into the private or protected areas, you now must use the SetTexture function, which is a better way of doing it, because it's almost impossible to change the value by accident.

LE Version: 2.50 (Eventually)

Link to comment
Share on other sites

I know this issue seems to be resolved now, but just for general programming practice, in your class declaration, you should move the

TTexture texture;

into the private or protected areas, because right now, the SetTexture function is not needed:

 

TTexture TestTexture = LoadTexture( "abstract::testsprite.dds" );
Sprite TestSprite;
TestSprite.texture = TestTexture;

 

At the moment, that's valid. But in OOP, we don't like code like that. By moving the texture into the private or protected areas, you now must use the SetTexture function, which is a better way of doing it, because it's almost impossible to change the value by accident.

 

 

Yep thats actually what I plan on. I noticed I have a tendency to put all variables in public at first and just set members, then I'll go back after I'm satisfied with it functionality and then move it to the private area and make a function in public ;). Thanks for the heads up though.

x_coldfire_x.png

AMD 64 X2 3800+ | 3G DDR2 667 | Windows 7 Ultimate 64 Bit | Nvidia Geforce 9800 GT 512MB DDR3

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...