Where to draw in your code?

Started by
5 comments, last by MENTAL 17 years, 8 months ago
I wasn't sure if I should put this in OpenGL or here. Say I'm making a breakout game. Do I have Draw() functions in each object (ie The Paddle, The Bricks, etc) and they draw themselves or do I have one or two large draw functions that draw everything? I'm having a hard time understanding exactly what Game / Graphics engines really do for some reason. There's really not any animation in this so maybe that's why I'm having a hard time understanding. Lets say I was doing some sort of person walking. The character class would control what frame it was on (or however you decided to handle it) right? What does a Graphics Engine do then? I had / have this picture of a Graphics engine being a giant class that handled certain drawing into the world but in reality it seems smarter to have each individual object handle its own drawing? Can anyone shed some light? Also perhaps a good read? I appreciate it.
Why is 99% of the Comp Sci population guys, and the other 1% not female? ;/
Advertisement
I guess in every program there is a main rendering / processing function after the window is created, right? This will handle all of the logic in the game itself? Each object will have its own abilities which is what their member functions would be.


So when having a Graphics Engine, would you make a seperate class for something like this, or is the engine itself that function?

If I'm going about this the wrong way, shed light =)
Why is 99% of the Comp Sci population guys, and the other 1% not female? ;/
My graphics renderer provides a basic Renderable object and several Renderable-inherited objects. Only these have the power to render themselves.

My game objects create such renderables, given them a position (so they know where they are rendered) and give them to the Renderer. Until they're removed from the renderer, all renderable objects will be drawn every frame at the correct position without further intervention from the non-rendering parts of the game.

Therefore, my game loop looks somewhat like this:

for(;;) {  timer.Update();  // Processing  game.Update(Timer.Now);  // Rendering  if(!active) timer.Sleep();  else renderer.Render(Timer.Now);}
Typically there are two possibilities. You can put the rendering code of your Objects into the Objectclass itself (normaly via inheritence) so every object can draw themself or you can write a class or function which gets the neccassary data of the objects like Vertex porsition, color, texture coordinates... and this class, function will render the scene (This would be the renderer). From my point of view the last possibility is better but harder to implement and i think used in graphics engines. So if you use a renderer you do not to have a draw functionfor each object. The object will only store the data (like Vertex and texture coordinates) and give this data to the renderer and thats all. Maybe this will help you.
Ahh basically the different scenarios I proposed were "proper" solutions...

Thanks for the help.

I dont see the point of creating a class for a graphics engine (at least of such small needs). Passing a pointer around too is also a pain and makes things sloppy.

I'll just settle with a function near the main logic to handle all of that.

Thanks again!
Why is 99% of the Comp Sci population guys, and the other 1% not female? ;/
In a very simple scene, you might as well have a separate function that renders everything directly. Render() --> screen

In a scene that is not so simple, that function would not actually do the rendering, but would tell each object to render itself. Render() --> Object --> screen

In a complicated scene, the object would no longer render itself, but would instead tell a renderer to render it. Render() --> Object --> Renderer --> screen

John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
I have a renderable class that each object inherits. The renderable class automatically registers itself with the scene graph, so all I have to do is call SceneGraph->Draw () and it will go through every renderable object in the game and draw it.

This topic is closed to new replies.

Advertisement