Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Is it best to have a renderer class in which you have a seperate function for each object you wish to draw? Or is it best to give each object a render function?

class Renderer {
    ...
}

Renderer::renderPlayer(Player *player)  {
    ...
}

vs

class Player  {
    ...
}

Player::render()  {
    ...
}
share|improve this question
1  
I see a theme in your questions. There's rarely any best way of doing things. Assuming there is makes the question a bit inflammatory. –  Lars Viklund Jul 15 '12 at 1:58
    
The answer to this question depends entirely on the size and scope of the application in question. –  Justin Skiles Jul 15 '12 at 2:53
    
@jskiles1 Well, this is for a 3D game so I guess it's quite big. –  Darestium Jul 15 '12 at 3:15
    
The question is valid in my opinion. For big rendering system its important decision and second way is not good actaully, as answer bellow explains pretty well. –  Notabene Jul 15 '12 at 5:26

2 Answers 2

up vote 7 down vote accepted

The question is a little bit vague and philosophical, im not sure it belongs to this website model, but here goes my opinion.

I would go with the first option, its my favourite way for the following reasons: The drawable object should not be able to know everything about how to draw itself, instead, a global(or not so global) renderer should be used as a dependency for all drawables, to influence how they are drawn.

If a drawable only has geometry data in its raw form, that allows global configurations in the renderer to influence all geometry, instead of changing all one by one. Also it may allow optimizations that would be otherwise impossible, if all geometry and draw code was hidden behind each drawable.

One example that comes to mind would be the ability to batch all geometry that uses a same texture, and have it draw as fast as it possible, with less state changes.

Sorry for the possible incorrectness, my sleepiness is winning , cheers

share|improve this answer

I would suggest option 2 as well, it keeps the code clean and is a little more maintainable.

However, if you have a lot of different objects implementing own draw commands, keep in mind that a virtual function call is more expensive than a simple function call. Consequently, when calling virtual render() on like 100K objects per frame, it may get less efficient.

In conclusion: It depends vastly on the number of different objects you want to render. Also, your compiler optimization may solve that problem by replacing the function calls where necessary so that the performance impact is neglectable (is that a valid word?).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.