Jump to content
  • Advertisement
Sign in to follow this  
tharealjohn

Handling textures with XNA and inheritance

This topic is 2589 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

Hello,

I am having some architecture level problems with my application. I am new to OOP and am seeing some problems with how I have things layered in my code. This is kind of a general OOP problem, but I posted in here so that someone may be able to make specific references to XNA to help me with example fixes.

Here is an example of what I have:

Base class for handling sprites:

public Sprite(Texture2D spriteTexture, Vector2 spritePosition, Rectangle textureSourceRectangle)



This class handles the drawing of the texture I want from a sheet of textures given the parameters.

I want to inherit from this for all other classes that will be drawn to the screen. So I have something similar to:


public class DrawMe : Sprite


public DrawMe(Texture2D drawTextures, Vector2 position)
: base(drawTextures, position, drawMeSource)



The problem is with the DrawMe class, I want to be able to define that class as having a drawMeSource that is only for the DrawMe class. This way I can make a new class (DrawYou) that can define a different textureSourceRectangle. I'm not sure how to implement this. The base constructor gets called before the derived so Im not sure how to pass in a special value for drawMeSource without passing it from the constructor of DrawMe. I do not want to define the drawMeSource in the constructor of DrawMe because every time I need a new object of DrawMe I do not want to have to define the same source rectangle.

I feel like it should be a field and/or a property of the DrawMe class but how do I implement this to keep this structure?


If this is completely bad design by some opinion, what do you suggest a better architecture for handling this scenario is?


Thanks for any help,

John

Share this post


Link to post
Share on other sites
Advertisement
static members of a class definition remain available across all instances of that class

so within DrawMe's definition:


private static Rectangle drawMeSource;

you can then set that once and it will be available to all DrawMe Objects only.

that what you are looking for?

It isn't the cleanest, but it's not exactly as bad as a global variable either.

you will probably want to write static accessor functions as well. otherwise you will have to figure out some way to set the value.

Share this post


Link to post
Share on other sites
static members of a class definition remain available across all instances of that classso within DrawMe's definition:private static Rectangle drawMeSource;you can then set that once and it will be available to all DrawMe Objects only.that what you are looking for?It isn't the cleanest, but it's not exactly as bad as a global variable either.you will probably want to write static accessor functions as well. otherwise you will have to figure out some way to set the value.
Interesting, you may be on to something here. Im not sure this is exactly what I want because I dont want DrawMe's source rectangle to be available to every DrawMe object. Lets say I use DrawMe as a base class to inherit from new DrawMeAgain objects that use a different source rectangle. So Class DrawMe has some fields, but I want to set them in each derived class because they can all be different. Maybe this is a better example of how I have things structured:
Base class for handling drawing.
class Sprite (Texture2D spriteTexture, Vector2 spritePosition, Rectangle textureSourceRectangle)

Inherited from Sprite for drawing, but has its own fields like health, position, lives, etc.
class Character : Sprite
protected int health;
// etc.


Inherited from Character but here I want to set health, position, lives because a Soldier has more then the next new class Defender.This is the level at which I want to specify each objects own source rectangle for the sprite sheet.
class Soldier : Character
Rectangle textureSource = new Rectangle(x, y, width, height);
int health = 100;
// etc.


Another character but with different values.
class Defender : Character
Rectangle textureSource = new Rectangle(x, y, width, height);
int health = 200;
// etc.


Let me know if there is more I can do to explain my situation. Thanks,

Share this post


Link to post
Share on other sites
well you can declare a static in each of the child classes if that is what you want.
That will make it so all soldiers use the soldier's static rectangle and all the defenders use the defender's static rectangle.

but if each instance of each class needs its own personal rectangle, then you already have it defined just fine.

even if you added a protected Rectangle to the character class, it would work fine, just like health.

I was under the impression you only wanted to define the rectangle once for all instances of the class. <-- in this case a class static is worthwhile.

Share this post


Link to post
Share on other sites
To be honest I am not sure. If you declare a static Rectangle in the base class too, polymorphism may work when using a Sprite pointer, but I am not sure if it does apply to statics.

If you want it to be available via polymorphism and pointers to the base class then i would suggest making it a normal member of the base class and then just set it whenevr you instantiate any child objects.
All this skirting around trying to set a constant/class static may no be worth your time.



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!