views:

52

answers:

1

What would be a better parctice, writing the drawing method inside the GameObject class or in the Game class?

GameObject obj = new GameObject();
obj.Draw();

Or

GameObject obj = new GameObject();
DrawGameObject(obj);
+1  A: 

It depends on what Game and GameObject are responsible for. Typically, a GameObject would contain all the information about what to draw -- it'd have a model, for instance. But objects aren't independent of each other -- knowing what to draw isn't sufficient to know how to draw it.

So a Game probably knows a lot more than a GameObject. In particular, it might know:

  • whether or not a particular object should be drawn (e.g. it's occluded based on the current camera)
  • how to draw a particular object (shadows from other objects; reflections from nearby surfaces; ambient light factors; etc.)
  • etc.

Of the two options presented, it probably makes more sense to put the Draw method outside the GameObject.

John Feminella
Thank you, great comment, and thanks about the edit, didn't know how to express my question. Is there any performance difference?
Adir
It's probably _worse_ performance to put `Draw` on `GameObject`, because then each `GameObject` would have to know about all the others, while a `Game` can probably be smarter and resolve all these interactions more succinctly.
John Feminella
Also, if you're building an object graph of items to draw and need to only draw game objects that would be visible on screen, the game object manager would be the one to call draw on each of your game objects.Your second option implies the game object itself knows when to draw itself, which is outside of it's responsibility.
TreeUK