N
N
Nikolay Paukov2017-07-09 16:27:45
OOP
Nikolay Paukov, 2017-07-09 16:27:45

How to organize the drawing of objects?

When learning C ++ and OpenGL, I encountered some difficulties.
Can you please tell me how, from the point of view of architecture, it is more correct to organize the interaction of classes when drawing objects on the screen?
Let's say I want to display several game objects, each of which has a separate class: Player, Enemy, and so on. There are ResourceManager, Sprite, Renderer classes.
So far I'm considering two options:
1) In each class that can be drawn, add a draw () method and use something like this:

playerSprite->draw();
enemySprite->draw();

2) Implement methods for drawing objects in the Renderer class and use it like this:
renderer->drawSprite(playerSprite, coors, orientation);

In both cases, there are difficulties with resource management. I would like to do the loading of sprites and manage the entire display in game object classes. But then you will have to pass a pointer to the resource manager to each of them when creating it, and in the second option also to the Renderer. It seems to me that this is not a very correct solution. There is an option to make the ResourceManager and Renderer classes Singletons and use them in any desired place, then there will be no problems, but I have read many arguments against such a method. How to organize all this correctly so that you can easily expand and refine in the future?
Player* player = new Player(resMgr, renderer);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
devalone, 2017-07-09
@devalone

Well, if you have a purely 2D engine, then, for example, something like this:
Base class for 2D game entities:

class GameObject2D : public GameObject {
public:
  const Sprite& getSprite() const { return sprite; };
private:
  Sprite sprite;
}

Player:
class Player : public GameObject2D {
// его особенные методы
}

renderer:
class Renderer {
public:
  void drawScene(const Scene& scene)
  {
    auto& visibleObjects = scene.getVisibleObjects();

    for(auto& gameObject : visibleObjects) {
      drawSprite(gameObject->getPosition(), gameObject->getSprite());
    }
  }
}

K
Konstantin Kitmanov, 2017-07-09
@k12th

There is an option to make the ResourceManager and Renderer classes Singletons and use them in any desired place, then there will be no problems, but I have read many arguments against such a method.

Instead of singletons, you should use Dependency Injection.
I also recommend this book: gameprogrammingpatterns.com

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question