Answer the question
In order to leave comments, you need to log in
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();
renderer->drawSprite(playerSprite, coors, orientation);
Player* player = new Player(resMgr, renderer);
Answer the question
In order to leave comments, you need to log in
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;
}
class Player : public GameObject2D {
// его особенные методы
}
class Renderer {
public:
void drawScene(const Scene& scene)
{
auto& visibleObjects = scene.getVisibleObjects();
for(auto& gameObject : visibleObjects) {
drawSprite(gameObject->getPosition(), gameObject->getSprite());
}
}
}
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question