Skip to content
Alexander Scheurer edited this page Feb 14, 2014 · 3 revisions

Structure: GameWorld

Disclaimer: This class is not a necessity, more like a flavour I (the writer of this tutorial) like. You can put all this into the main class and be fine with it, but I like this layer between the engine and the application. It gives you another hierarchy to play with that might be useful in further down the line. This also means the constructor of the GameWorld becomes the Init-method for everything below it. And after this lesson we won't have to mess with Main.cs ever again.

Variables

We'll start low, but the contents of this class will at least double until the end.

private readonly RenderContext _rc;
private readonly List<GameEntity> _furniture = new List<GameEntity>();
private readonly float4x4 _camMatrix;

Not much explanation needed. We already expand to a List of GameEntities so we can easily handle multiple of them. I named it furniture because this will later be the place where all the static objects in the room will be stored.

Constructor

As I said this is the new Init, this is where you initialize everything in your scene.

public GameWorld(RenderContext rc)
{
   _rc = rc;

   _camMatrix = float4x4.LookAt(0, 200, 500, 0, 0, 0, 0, 1, 0);

   _furniture.Add(new GameEntity("Assets/cube.obj.model", _rc));
   _furniture[0].SetColor(1, 0, 0, 1);
   _furniture[0].SetCorrectionMatrix(float4x4.Scale(0.5f));

   _furniture.Add(new GameEntity("Assets/cube.obj.model", _rc, 0, 0, -100));
   _furniture[1].SetColor(0, 1, 0, 1);
}

In addition to the neccecary code this also adds a red and a green cube.

Methods

There are just two methods we need right now, one is RenderAFrame() and Resize() both are called from by the respective method in the main class. RenderAFrame for now only itterates through the List of GameEntities and tells them to render. Resize is a stub for now.

public void RenderAFrame()
{
   foreach (var gameEntity in _furniture)
   {
      gameEntity.Render(_camMatrix);
   }
}
public void Resize()
{
}

Main.cs

We just do the same we did the last time, replace everything we previously did with our GameEntity with GameWorld. Additionaly we remove the camera matrix and update resize() to call resize() in its GameWorld.

Exercise

  • Play around with adding objects and scaling them

[> Lesson 06 - Structure: Player](Lesson 06)