I am using Direct2D to write a simple rendering system that I could use to easily write a game. I have a function that renders each object every frame. Now, if my game is going to be dynamic, I can't hardcode every object into that function. I would much prefer to simply be able to instance a class and it show up until I remove it.

I considered a table containing all the objects, but that would be too tedious and hard to manage. I was wondering what the best way to do this would be. Any ideas?

Member Avatar for embooglement

You could try having a std::vector of sprite pointers. Each draw call would loop through each sprite pointed to in the vector and draw it. The constructor for the sprite class would push its this pointer into the vector, and the destructor would remove that element. Would that work?

If I'm reading this right, you're looking for a way to store your objects externally so you can call on them when you need them and dispose of them when you don't.

My suggestion would be to create a manager class (or several) which will be able to create objects on the fly and keep your data stored in XML or whatever your favorite data file type is.

Example:
You can have a sprite manager class which when called upon, reaches out to your data file to get all the information for the sprite type you want to create.

// Depending on how you want to manipulate your sprites, either pass back an int or Sprite *, or whatever else you need
int SpriteManager::CreateSprite(string spriteName)
{
  XMLData spriteData = GetSpriteInfo(spriteName);
  Sprite *temp = new Sprite(spriteData.name, spriteData.dimX, spriteData.dimY, ...);

  // Push the sprite * onto a vector<Sprite *>
  spriteVec.push(temp);

  return this spriteCount++;
  // or
  return temp;
}
class tile
{
  private:
    Sprite *sprite;
    // or
    int spriteID;

  public:
    void DrawCharacter()
    {
      RenderManager::GetInstance()->Draw(sprite);
      // or
      RenderManager::GetInstance()->Draw(spriteID);
    }
}

This code has a lot to be desired, I just don't have the time to write out everything going on in my head about writing a 2D renderer.

You could also have render bins in which you deposit textures & positions where the render manager would pull from every frame to decide what to draw. This way, a game component could decide what to draw based on where an object is in relation to what can actually be seen, because you don't want to waste time drawing things that are nowhere close to your current focus.

Sorry if this is confusing or goes beyond the scope of your question. I tend to think a lot more than necessary when it comes to answering game development questions, even my own. =)

Good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.