Handling textures with XNA and inheritance

Started by
4 comments, last by Infernal0010 12 years, 5 months ago
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

jmillerdev.com

Follow me @jmillerdev

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.
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,

jmillerdev.com

Follow me @jmillerdev

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.
Ok putting a static var in for the class rectangle might work but how do I get that back up to the base class, Sprite?

jmillerdev.com

Follow me @jmillerdev

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.



This topic is closed to new replies.

Advertisement