Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separation of game and rendering logic

What is the best way to separate rendering code from the actually game engine/logic code? And is it even a good idea to separate those?

Let's assume we have a game object called Knight. The Knight has to be rendered on the screen for the user to see. We're now left with two choices. Either we give the Knight a Render/Draw method that we can call, or we create a renderer class that takes care of rendering all knights.

In the scenario where the two are separated the Knight should the Knight still contain all the information needed to render him, or should this be separated as well?

In the last project we created we decided to let all the information required to render an object be stored inside the object itself, but we had a separate component to actually read that information and render the objects. The object would contain information such as size, rotation, scale, and which animation was currently playing and based on this the renderer object would compose the screen.

Frameworks such as XNA seem to think joining the object and rendering is a good idea, but we're afraid to get tied up to a specific rendering framework, whereas building a separate rendering component gives us more freedom to change framework at any given time.

like image 718
Kasper Holdum Avatar asked May 03 '10 07:05

Kasper Holdum


2 Answers

I would create a separate KnightRenderer class. Advantages:

  • Clean separation between the game logic and the rendering. The Knight himself might even run on a game server and not know anything about rendering at all.
  • Smaller, simpler classes, concerned with one and only one piece of functionality.

Everything the KnightRenderer needs to know about the Knight (position, status) has to be publically readable in Knight.

Properties specific to rendering would go into the KnightRenderer class. For example, maybe you want to make the knight flash when he's hit, so you would need to store a counter or time value.

like image 140
Thomas Avatar answered Sep 17 '22 23:09

Thomas


I know i'm coming late, but for future readers, you might be interested in a tutorial I wrote.

I'm no real expert but this is how I see proper separation of concerns:
http://aurelienribon.wordpress.com/2011/04/26/logic-vs-render-separation-of-concerns/

alt text

like image 39
Aurelien Ribon Avatar answered Sep 18 '22 23:09

Aurelien Ribon