Adir
Adir

Reputation: 1423

Which of these choices is the better design for drawing objects?

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);

Upvotes: 1

Views: 87

Answers (1)

John Feminella
John Feminella

Reputation: 311645

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.

Upvotes: 2

Related Questions