Another approach that might help you is to basically invert the relationship between the game objects and the renderer. Instead of the game objects holding references to the renderer (singleton or otherwise), the renderer holds a reference to an interface for accessing the game objects. You could call this interface the "model" or "world". The renderer would have one main method called DrawFrame() or the like, in which it would use the model interface to access the various game objects and basically ask them how they should be drawn.
I got started recently with DirectX and think this is a good approach to use there. It allows you to keep all or most of the DirectX interaction within one area, namely the renderer class and any supporting classes.
Pattern alternative to singleton?
Quote:Original post by RobAU78
Another approach that might help you is to basically invert the relationship between the game objects and the renderer. Instead of the game objects holding references to the renderer (singleton or otherwise), the renderer holds a reference to an interface for accessing the game objects. You could call this interface the "model" or "world". The renderer would have one main method called DrawFrame() or the like, in which it would use the model interface to access the various game objects and basically ask them how they should be drawn.
I think there are two components in this approach: a generic, low-level renderer "service" that draws stuff (understanding model, sprite, animation etc. data structures and APIs, taking care of DirectX or OpenGL data structures and calls, and providing general purpose functionality like sorting geometry and caching textures); and a high-level renderer controller that has the described access to game objects and uses it to decide what to draw and implement game specific behaviour, e.g. querying the game world model for soldiers, smoke and muzzle flashes, sorting them together, then drawing all friendly projectiles, then enemy projectiles (more important), then all laser beams with additive blending, and finally the HUD.
Quote:Original post by LorenzoGatti
I think there are two components in this approach: a generic, low-level renderer "service" that draws stuff (understanding model, sprite, animation etc. data structures and APIs, taking care of DirectX or OpenGL data structures and calls, and providing general purpose functionality like sorting geometry and caching textures); and a high-level renderer controller that has the described access to game objects and uses it to decide what to draw and implement game specific behaviour, e.g. querying the game world model for soldiers, smoke and muzzle flashes, sorting them together, then drawing all friendly projectiles, then enemy projectiles (more important), then all laser beams with additive blending, and finally the HUD.
For a generalized graphics system, I'd say you're right. But if you're taking a simpler approach (i.e. making a game, not an engine), then I don't think you have to separate these behaviors into two layers. :)
Quote:Original post by owlQuote:Original post by Madhed
IMO you should take the render function out of your game objects.
The code you have provided clearly shows that the render function needs way to much information about the internals of the renderer class.
Instead game objects should register more low-level objects (meshes, etc.) with the renderer which has the functionality to draw them.
This way you can decouple the game objects from the actual renderering process and decrease dependancy.
Quoted in agreement.
Thirded. To as great an extent as possible, a class should have a single well-defined responsibility; making game object classes responsible for rendering AND game logic AND physics AND audio AND whatever else is a bad idea. Naturally you need something tying the various components together into a single package, but it can be done by aggregating simpler objects (like 'Mesh' or 'ParticleEffect') together. This could be hardcoded, or you could take a component-oriented approach like Unity does.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement