The Update Loop, Input and You!

This tutorial will cover the basic game loop and handling keyboard inputs.

Start Your Engines!

Creating the Update function is identical to the Render function from before. But as we do that, lets also create an Engine class.

public class Engine
{
    private RLRootConsole rootConsole;
    public Engine(RLRootConsole console)
    {
        rootConsole = console;
        rootConsole.Render += Render;
        rootConsole.Update += Update;
    }

    public void Render(object sender, UpdateEventArgs e)
    {
    }

    public void Update(object sender, UpdateEventArgs e)
    {
    }
}

This simplifies things in our Main function

rootConsole = new RLRootConsole("ascii_8x8.png", 60, 40, 8, 8);
engine = new Engine(rootConsole);
rootConsole.Run();
Where you @?

Now we let’s create our player. Right now we just need to track where the player is and draw it.

private int playerX;
private int playerY;

public void Render(object sender, UpdateEventArgs e)
{
    rootConsole.Clear();
    rootConsole.Set(playerX, playerY, RLColor.White, null, '@');
    rootConsole.Draw();
}

Ah, but it wouldn’t be a game without interaction, time to add Update code.

public void Update(object sender, UpdateEventArgs e)
{
    RLKeyPress keyPress = rootConsole.Keyboard.GetKeyPress();
    if (keyPress != null)
    {
        switch (keyPress.Key)
        {
            case RLKey.Up: playerY--; break;
            case RLKey.Down: playerY++; break;
            case RLKey.Left: playerX--; break;
            case RLKey.Right: playerX++; break;
        }
    }
}

Compile and run, to run around.

Getting Started

Creating the Project
  1. First download the source or dll for RLNET here.
  2. In Visual Studio, create a new project. Select Empty Project under Visual C# / Windows.
  3. Right click the project and select Add Reference.. browse and add “RLNET.dll” and “OpenTK.dll”
  4. Right click the project and select Add then Existing Item. Locate “ascii_8x8.png” and add. Remember to set the Copy to Output Directory property of the png to Copy if newer.
  5. Right click the project and open the properties. On the application tab change the Output to Windows Application.
 Hello World!

Now to make sure RLNET is working correctly. Create a Program class. Add a using for the RLNET namespace.

using RLNET;

Then add a static variable for the root console.

public static RLRootConsole rootConsole;

In your main function, construct the root console.

rootConsole = new RLRootConsole("ascii_8x8.png", 60, 40, 8, 8);

The first parameter is the filepath to the png. The second and third are the width and height of the window in tiles. The fourth and fifth parameters are the width and height of the tiles in pixels. Next you need to assign the render event of the root console to a function. Visual Studio will probably create the function for you after you type “+=”.

rootConsole.Render += rootConsole_Render;
static void rootConsole_Render(object sender, UpdateEventArgs e)
{
    throw new NotImplementedException();
}

Replace the exception with the following code.

rootConsole.Clear();
rootConsole.Print(1, 1, "Hello World!", RLColor.White);
rootConsole.Draw();

Finally tell the Root Console to run on the last line of your main function.

rootConsole.Run();

Once this is executed, it will enter into a loop triggering the Render event until the window closes. (Or if we stop it ourselves, more on that later.)

Putting it all together
using RLNET;
using System;

namespace RLNET_Tutorial
{
    public class Program
    {
        public static RLRootConsole rootConsole;

        public static void Main()
        {
            rootConsole = new RLRootConsole("ascii_8x8.png", 60, 40, 8, 8);
            rootConsole.Render += rootConsole_Render;
            rootConsole.Run();
        }

        static void rootConsole_Render(object sender, UpdateEventArgs e)
        {
            rootConsole.Clear();
            rootConsole.Print(1, 1, "Hello World!", RLColor.White);
            rootConsole.Draw();
        }
    }
}