Sunday, April 9, 2017

DIY Game Financial System Part 2: Trade Algorithm


This is part of my series on DIY Game Financial System
For other posts in this series check out the following links:
DIY Game Financial System Part 1: Intro and Accounts
DIY Game Financial System Part 2: Trade Algorithm

For the full source code for the financial system discussed in this series, look in the repository.
This post we will look at the actual trade algorithm used. It is a quite naive implementation but works for me. Feel free to extend it for your own needs.

In the Marketplace class, we have an Update method that iterates through all the buy and sell orders available and tries to match them with each other. This simple implementation tries to find an exact match but there is potential to extend it to match multiple sell to one buy, or one sell to multiple sell.
In the demo application this is triggered by a timer.

public void Update()
{
 bool updated = false;
 lock(_sellOrders)
 {
  lock (_buyOrders)
  {
   foreach (var buyName in _buyOrders)
   {
    List<SellOrder> sellOrders;
    if (!_sellOrders.TryGetValue(buyName.Key, out sellOrders))
     continue;
    // naive
    foreach (var buyOrder in buyName.Value)
    {
     foreach (var sellOrder in sellOrders)
     {
      if (buyOrder.Quantity == sellOrder.Quantity
       && buyOrder.PricePerItem == sellOrder.PricePerItem)
      {
       updated = true;
       buyOrder.Execute(sellOrder);
      }
     }
    }
   }
   foreach (var order in _sellOrdersToBeRemoved)
   {
    _sellOrders[order.SecurityName].Remove(order);
   }
   foreach (var order in _buyOrdersToBeRemoved)
   {
    _buyOrders[order.SecurityName].Remove(order);
   }

  }
 }
 if (updated)
 {
  Updated?.Invoke(this, this);
 }
}

The key lines above are the
if (buyOrder.Quantity == sellOrder.Quantity
 && buyOrder.PricePerItem == sellOrder.PricePerItem)
{
 updated = true;
 buyOrder.Execute(sellOrder);
}
This tries to exactly match each buy with a sell and then trigger the Execute method:
public void Execute(SellOrder sellOrder)
{
    OrderAccount.DepositInto(sellOrder.OrderAccount, sellOrder.Quantity * sellOrder.PricePerItem);
    Security = sellOrder.ForSale;
    foreach (var action in _executeActions)
    {
        action(this);
    }
    sellOrder.Execute();
}
This is where the money is transferred between the two orders, all actions connected to this BuyOrder are triggered and then finally the sellOrder is executed as well.

When the buy order was added to the Marketplace, it associated a Cancel and Execute action with it to be triggered when the order was successful or canceled.
public void Buy(BuyOrder order)
{
 lock (_buyOrders)
 {
  if (_buyOrders.ContainsKey(order.SecurityName))
   _buyOrders[order.SecurityName].Add(order);
  else
   _buyOrders.Add(order.SecurityName, new List<BuyOrder> { order });
  order.AddCancelAction(RemoveBuy);
  order.AddExecuteAction(RemoveBuy);
 }
}
For both cases, we are just interested in removing the order from the list of available orders. The same goes for the Sell method.
private void RemoveBuy(BuyOrder order)
{
 _buyOrdersToBeRemoved.Add(order);
}
As this is triggered from an iteration of the _buyOrders collection, we need to deffer the removal to after the iteration has completed, hence the addition to a secondary removal list.
Just writing this I spotted a bug in the code where the order needs to be flagged as executed so that the same order isn't executed multiple times. I will not fix this in the demo application for now. 


Last in the Update method, a call to the Updated event handlers associated with this marketplace object.
if (updated)
{
 Updated?.Invoke(this, this);
}
This way a GUI can update itself if there was an update. The Updated event is also fired each time an order is added to the marketplace.

So there, thank you for reading. Hope this helps someone out there : )

Until next time: Work to Live, Don’t Live to Work

Friday, March 10, 2017

DIY Game Financial System Part 1: Intro and Accounts

picture of commerce on river banks in the evening

I'm quite interested in finance as can be seen on my other blog dreamstateliving, and thus I decided to start working on a small but powerful in game financial system that can be used in my future games.
The goal for this system is to allow for
  • trading at a marketplace, much like the stock market
  • buying and selling stuff from vendors
Note that the code provided with this post is intended to be used in games, a lot of shortcuts have been taken. This is not a fool-proof system, more like a base to start building your own in game financial system on.

In this part we will introduce the scenario and go through the Account and TradeAccount classes.

This is part of my series on DIY Game Financial System
For other posts in this series check out the following links:
DIY Game Financial System Part 1: Intro and Accounts
DIY Game Financial System Part 2: Trade Algorithm

Introduction, a textual description

Every player, vendor etc. has their private TradeAccount consisting of a BalanceAccount and a list of Securities (items, shares whatever) owned at his current state.
Each TradeAccount is able to generate a BuyOrder based on a name of the Security to buy, the Quantity of Securities we are shopping for and the PricePerItem we are willing to pay. The BuyOrder  also contains a BalanceAccount with the amount of money needed to make the purchase. It is not possible to generate a BuyOrder if the TradeAccounts' BalanceAccount does not hold the amount of money needed to close the transaction.
In a similar fashion, each TradeAccount is able to generate a SellOrder for Securities currently held by that TradeAccount. The SellOrder also consists of the PricePerItem, Quantity and the Security for sale.

Both the SellOrder and the BuyOrder are sent to the Marketplace where the trade should be made. The Marketplace should be possible to instance as many times as needed. For example each vendor would have their own with things for sale, but the global Marketplace would allow for players to trade.

If there is an exact match on Security, Quantity and PricePerItem we will Execute the BuyOrder and transfer the money to the sellers TradeAccount and the securities to the buyers TradeAccount.
This will be our first naive algorithm to match orders.

Lets look at some code.


BalanceAccount

This is just a holder of money. Lets look at the interface.
public interface IAccount
{
    Guid Id { get; }
    ulong Balance { get; }
    bool CanAfford(ulong x);
    bool DepositInto(IAccount dest, ulong amount);
    void AddBalance(ulong amount);
}
So first we have an identifier, Id, just so that we can identify this account uniquely. Then we will store the actual current Balance of the account. I chose an unsigned long to store because of three reasons:

  • I don't really want to handle negative numbers, i.e. if you cannot afford it you can't buy it.
  • to be able to handle very large sums, I don't really know the applications for this yet, but don't want to limit someone for buying something for a ridiculous amount of money (18,446,744,073,709,551,615 to be exact)
  • I don't want to handle decimal numbers. Keep it simple, its a game :)
After that we have some functions, CanAfford checks if the account could afford to buy something costing x. 
public bool CanAfford(ulong x)
{
    return Balance >= x;
}
DepositInto withdraws money from this account and inserts it into the destination account.
public bool DepositInto(IAccount dest, ulong amount)
{
    lock (this)
    {
        if (!CanAfford(amount))
            return false;
        SubtractBalance(amount);
    }
    lock (dest)
    {
        dest.AddBalance(amount);
    }
    return true;
}
And lastly AddBalance creates money out of the blue (or you handle it somewhere else) and inserts into this account.
public void AddBalance(ulong amount)
{
    lock (this)
    {
        Balance += amount;
    }
}
private void SubtractBalance(ulong amount)
{
    Balance -= amount;
}
As you can see we add an extra lock in the AddBalance function as it is a public one and could be called from outside the DepositInto lock. For the private SubtractBalance method, we have full control and don't need the additional lock overhead.
There is a number of unit tests to verify this implementation in the source code in the repository. It gets quite long and thus not included in the post.

TradeAccount

public interface ITradeAccount
{
    event EventHandler<TradeAccount> Updated;
    IAccount BalanceAccount { get; }
    ulong ValueOfActiveBuyOrders { get; }
    Dictionary<string, Security> Securities { get; }
    List<BuyOrder> BuyOrders { get; }
    List<SellOrder> SellOrders { get; }

    SellOrder CreateSellOrder(string name, ulong quantity, ulong pricePerItem);
    BuyOrder CreateBuyOrder(string name, ulong quantity, ulong pricePerItem);
}

First off, we will expose an event, Updated, that will be triggered whenever the contents of this Trade Account change. This to be able to create event based updates of GUI elements at a later stage.
After that we have the BalanceAccount associated with this TradeAccount, for storage of money that can be used to buy stuff.
ValueOfActiveBuyOrders will return the amount of money bound in buy orders created by this TradeAccount.
public ulong ValueOfActiveBuyOrders
{
    get
    {
        ulong sum = 0;
        foreach (var item in BuyOrders)
        {
            sum += item.OrderAccount.Balance;
        }
        return sum;
    }
}
Securities is a dictionary of securities held by this account followed bu the currently active BuyOrders and SellOrders.

Lastly we expose functions to create both Sell and Buy orders.

CreateSellOrder

public SellOrder CreateSellOrder(string name, ulong quantity, ulong pricePerItem)
{
    Security security; 
    if (!Securities.TryGetValue(name, out security))
        return null;
    var split = security.Split(quantity);
    if (split == null)
        return null;
    var order = new SellOrder(split, pricePerItem);
    order.AddCancelAction(CancelSellOrder);
    order.AddExecuteAction(ExecuteSellOrder);
    lock (SellOrders)
    {
        SellOrders.Add(order);
    }
    Updated?.Invoke(this, this);
    return order;
}
To createa a SellOrder we first need to check if we actually own any securities with the name provided, if we don't we will stop directly. After that we split our owned security into a new one with the quantity we want to sell. We expect the Split method to return null if the split is invalid (tries to sell more then owned) and we stop there as well. If all is well, we setup the SellOrder, connect both Cancel and Execute handlers to this TradeAccount so that it will know its way home when the mission is accomplished or canceled. Lastly, we add it to the list of active SellOrders and invoke the Updated event.
private void ExecuteSellOrder(SellOrder order)
{
    order.OrderAccount.DepositInto(BalanceAccount, order.OrderAccount.Balance);
    lock (SellOrders)
    {
        SellOrders.Remove(order);
    }
    Updated?.Invoke(this, this);
}
The ExecuteSellOrder is responsible to transfer money to the selling TradeAccount when a trade is completing. As seen, the SellOrder has already received the money from the buying account so this is just a final put the money where it belongs and cleanup things handler.
private void CancelSellOrder(SellOrder order)
{
    Security security;
    if (!Securities.TryGetValue(order.SecurityName, out security))
    {
        Securities.Add(order.SecurityName, order.ForSale);
    }
    else
    {
        Securities[order.SecurityName].Merge(order.ForSale);
    }
    lock (SellOrders)
    {
        SellOrders.Remove(order);
    }
    Updated?.Invoke(this, this);
}
The CancelSellOrder is a little more complicated. We are not receiving any money here but we need to put the Securities back where they belong. So find the security we were trying to sell and either merge with the existing one or create a new entry (if it has been cleaned up for any reason). After that some cleanup and update event invoking.

CreateBuyOrder

The CreateBuyOrder basically follows the same pattern as the sell order creator but here we check that we have enough money to make the purchase that we want to.
We do this by creating a new account and trying to deposit the needed amount of money into it. If it fails, we stop there. If all is good, we create the BuyOrder and supply it the just created account and what to look for in the marketplace. Just as for the SellOrder, we connect handlers for both the Execute and Cancel outcomes and add it to the list of active buy orders.
public BuyOrder CreateBuyOrder(string name, ulong quantity, ulong pricePerItem)
{
    var orderAccount = new Account(0);
    if (!BalanceAccount.DepositInto(orderAccount, quantity*pricePerItem))
        return null;
    var order = new BuyOrder(name, pricePerItem, quantity, orderAccount);
    order.AddCancelAction(CancelBuyOrder);
    order.AddExecuteAction(ExecuteBuyOrder);
    lock (BuyOrders)
    {
        BuyOrders.Add(order);
    }
    Updated?.Invoke(this, this);
    return order;
}
The ExecuteBuyOrder looks very similar to the CancelSellOrder above, instead of putting back a canceled sell order, we will put in a newly purchased Security. It needs to find the already owned security and merge with it if it exists or create a new entry in the owned Securites dictionary. After that a cleanup is done.
private void ExecuteBuyOrder(BuyOrder order)
{
    Security security;
    if (!Securities.TryGetValue(order.SecurityName, out security))
    {
        Securities.Add(order.SecurityName, order.Security);
    }
    else
    {
        Securities[order.SecurityName].Merge(order.Security);
    }
    lock (BuyOrders)
    {
        BuyOrders.Remove(order);
    }
    Updated?.Invoke(this, this);
}
And surprise, surprise the CancelBuyOrder is similar to the ExecuteSellOrder. Here we put back the pocket-money we received when going to the market and then cleaning up.
private void CancelBuyOrder(BuyOrder order)
{
    order.OrderAccount.DepositInto(BalanceAccount, order.OrderAccount.Balance);
    lock (BuyOrders)
    {
        BuyOrders.Remove(order);
    }
    Updated?.Invoke(this, this);
}

So just like with the Account, there are a number of unit tests in the repository  that I will not cover here. Actually the repository contains the code for the first working version with a test GUI. We will cover the rest of the code in future posts in this series, but if you are interested you can look at the code already now :)

So there, thank you for reading. Hope this helps someone out there : )

Until next time: Work to Live, Don’t Live to Work

Monday, February 27, 2017

OpenGL 4 with OpenTK in C# Part 14: Basic Text


In this post we will look at how to get basic text on screen so that we can display the score of the game to the player.

This is part 14 of my series on OpenGL4 with OpenTK.
For other posts in this series:
OpenGL 4 with OpenTK in C# Part 1: Initialize the GameWindow
OpenGL 4 with OpenTK in C# Part 2: Compiling shaders and linking them
OpenGL 4 with OpenTK in C# Part 3: Passing data to shaders
OpenGL 4 with OpenTK in C# Part 4: Refactoring and adding error handling
OpenGL 4 with OpenTK in C# Part 5: Buffers and Triangle
OpenGL 4 with OpenTK in C# Part 6: Rotations and Movement of objects
OpenGL 4 with OpenTK in C# Part 7: Vectors and Matrices
OpenGL 4 with OpenTK in C# Part 8: Drawing multiple objects
OpenGL 4 with OpenTK in C# Part 9: Texturing
OpenGL 4 with OpenTK in C# Part 10: Asteroid Invaders
OpenGL 4 with OpenTK in C# Part 11: Mipmap
OpenGL 4 with OpenTK in C# Part 12: Basic Moveable Camera
OpenGL 4 with OpenTK in C# Part 13: IcoSphere
OpenGL 4 with OpenTK in C# Part 14: Basic Text

As stated in the previous post, I am in no way an expert in OpenGL. I write these posts as a way to learn and if someone else finds these posts useful then all the better :)
If you think that the progress is slow, then know that I am a slow learner :P
This part will build upon the game window and shaders from part 13.

Generate font texture

First off, we need to generate a texture for the font that we want to use. For simplicity, all letters that we will need are put on a single line and each letter will get a fixed size rectangle in the texture.
private const string Characters = @"qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789µ§½!""#¤%&/()=?^*@£€${[]}\~¨'-_.:,;<>|°©®±¥";
public Bitmap GenerateCharacters(int fontSize, string fontName, out Size charSize)
{
    var characters = new List<Bitmap>();
    using (var font = new Font(fontName, fontSize))
    {
        for (int i = 0; i < Characters.Length; i++)
        {
            var charBmp = GenerateCharacter(font, Characters[i]);
            characters.Add(charBmp);
        }
        charSize = new Size(characters.Max(x => x.Width), characters.Max(x => x.Height));
        var charMap = new Bitmap(charSize.Width * characters.Count, charSize.Height);
        using (var gfx = Graphics.FromImage(charMap))
        {
            gfx.FillRectangle(Brushes.Black, 0, 0, charMap.Width, charMap.Height);
            for (int i = 0; i < characters.Count; i++)
            {
                var c = characters[i];
                gfx.DrawImageUnscaled(c, i * charSize.Width, 0);

                c.Dispose();
            }
        }
        return charMap;
    }
}

private Bitmap GenerateCharacter(Font font, char c)
{
    var size = GetSize(font, c);
    var bmp = new Bitmap((int)size.Width, (int)size.Height);
    using (var gfx = Graphics.FromImage(bmp))
    {
        gfx.FillRectangle(Brushes.Black, 0, 0, bmp.Width, bmp.Height);
        gfx.DrawString(c.ToString(), font, Brushes.White, 0, 0);
    }
    return bmp;
} 
private SizeF GetSize(Font font, char c)
{
    using (var bmp = new Bitmap(512, 512))
    {
        using (var gfx = Graphics.FromImage(bmp))
        {
            return  gfx.MeasureString(c.ToString(), font);
        }
    }
}


The GenerateCharacters method takes the name of the font that we want to use, the size of the font and outputs the size of a single character in the returned texture. So white on black.
output from font texture generator
Example output from above algorithm. Original texture was 3432x48 pixels
This texture will be used to calculate the alpha channel of the color that we want to render on screen. I.e. whatever is white on the texture will be rendered as the chose color and the black will be rendered as transparent.

Render Objects

Basically we want to render each character in a quad, so we need to generate a model with 2 triangles that can be reused. So into our RenderObjectFactory we add the following:
public static TexturedVertex[] CreateTexturedCharacter()
{
    float h = 1;
    float w = RenderText.CharacterWidthNormalized;
    float side = 1f / 2f; // half side - and other half

    TexturedVertex[] vertices =
    {
        new TexturedVertex(new Vector4(-side, -side, side, 1.0f),    new Vector2(0, h)),
        new TexturedVertex(new Vector4(side, -side, side, 1.0f),     new Vector2(w, h)),
        new TexturedVertex(new Vector4(-side, side, side, 1.0f),     new Vector2(0, 0)),
        new TexturedVertex(new Vector4(-side, side, side, 1.0f),     new Vector2(0, 0)),
        new TexturedVertex(new Vector4(side, -side, side, 1.0f),     new Vector2(w, h)),
        new TexturedVertex(new Vector4(side, side, side, 1.0f),      new Vector2(w, 0)),
    };
    return vertices;
}
It is the front facing quad from the generate cube method already there.
The RenderText.CharacterWidthNormalized is returning the 1/number of characters to get the correct alignment of the x-axis.

We will be needing 2 new render objects to accomplish putting text on the screen. RenderText that handles the whole string to be rendered, and RenderCharacter that handles each individual character in the string.
public class RenderText : AGameObject
{
    private readonly Vector4 _color;
    public const string Characters = @"qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789µ§½!""#¤%&/()=?^*@£€${[]}\~¨'-_.:,;<>|°©®±¥";
    private static readonly Dictionary<char, int> Lookup;
    public static readonly float CharacterWidthNormalized;
    // 21x48 per char, 
    public readonly List<RenderCharacter> Text;
    static RenderText()
    {
        Lookup = new Dictionary<char, int>();
        for (int i = 0; i < Characters.Length; i++)
        {
            if (!Lookup.ContainsKey(Characters[i]))
                Lookup.Add(Characters[i], i);
        }
        CharacterWidthNormalized = 1f / Characters.Length;
    }
    public RenderText(ARenderable model, Vector4 position, Color4 color, string value)
        : base(model, position, Vector4.Zero, Vector4.Zero, 0)
    {
        _color = new Vector4(color.R, color.G, color.B, color.A);
        Text = new List<RenderCharacter>(value.Length);
        _scale = new Vector3(0.02f);
        SetText(value);
    }
    public void SetText(string value)
    {
        Text.Clear();
        for (int i = 0; i < value.Length; i++)
        {
            int offset;
            if (Lookup.TryGetValue(value[i], out offset))
            {
                var c = new RenderCharacter(Model,
                    new Vector4(_position.X + (i * 0.015f),
                        _position.Y,
                        _position.Z,
                        _position.W),
                    (offset*CharacterWidthNormalized));
                c.SetScale(_scale);
                Text.Add(c);
            }
        }
    }
    public override void Render(ICamera camera)
    {
        _model.Bind();
        GL.VertexAttrib4(3, _color);
        for (int i = 0; i < Text.Count; i++)
        {
            var c = Text[i];
            c.Render(camera);
        }
    }
}
From the top.
Characters string containing all the characters in our font texture in the correct order.
Static constructor that initializes the lookup table, mapping each character to its index. Dictionary for faster lookup then doing the index of operation during for each string we want to show.
Instance constructor, just decomposes the Color struct to a Vector4, I still don't know why the GL.VertexAttrib4 doesn't support the Color struct out of the box.
SetText, allows for changing the contents of this RenderText object. This is a naive implementation that empties all content and then adds new. Optimization could be to try re-use whatever objects that already are in the collection. But for now, this works for me.
Render, just sets the color attribute and calls render for all RenderCharacters.

Next, all characters in the string
public class RenderCharacter : AGameObject
{
    private float _offset;

    public RenderCharacter(ARenderable model, Vector4 position, float charOffset)
        : base(model, position, Vector4.Zero, Vector4.Zero, 0)
    {
        _offset = charOffset;
        _scale = new Vector3(0.2f);
    }

    public void SetChar(float charOffset)
    {
        _offset = charOffset;
    }

    public override void Render(ICamera camera)
    {
        GL.VertexAttrib2(2, new Vector2(_offset, 0));
        var t2 = Matrix4.CreateTranslation(
            _position.X,
            _position.Y,
            _position.Z);
        var s = Matrix4.CreateScale(_scale);
        _modelView = s * t2 * camera.LookAtMatrix;
        GL.UniformMatrix4(21, false, ref _modelView);
        _model.Render();
    }
}
The key component for a character is the x-axis offset in the texture. The Render method just binds the offset attribute to the shader and renders the quad holding the character. At the moment the character is transformed with a Model-View matrix.


Shaders

Vertex Shader
#version 450 core
layout(location = 0) in vec4 position;
layout(location = 1) in vec2 textureCoordinate;
layout(location = 2) in vec2 textureOffset;
layout(location = 3) in vec4 color;

out vec2 vs_textureOffset;
out vec4 vs_color;
layout(location = 20) uniform  mat4 projection;
layout (location = 21) uniform  mat4 modelView;

void main(void)
{
 vs_textureOffset = textureCoordinate + textureOffset;
 gl_Position = projection * modelView * position;
 vs_color = color;
}
The first 2 inputs to our vertex shader are bound from buffers, and we introduce 2 new inputs that we set from the render methods. The texture offset and color.
We also need to send the color forward to the fragment shader so we need an out parameter for that.
The vs_textureOffset is the original texture coordinate plus the new offset to find a character. The original texture coordinate the X-axis was of the width of 1 character and that's why this works.

Fragment Shader
#version 450 core
in vec2 vs_textureOffset;
in vec4 vs_color;
uniform sampler2D textureObject;
out vec4 color;

void main(void)
{
 vec4 alpha = texture(textureObject, vs_textureOffset);
 color = vs_color;
 color.a = alpha.r;
}
The vertex shader reads the texture, as it is black and white the red, green and blue channels should have the same values, hence we can look at just one of them and set the alpha channel of our color to get transparency. i.e. the color just sticks to the characters and we cut out everything else.

Game Window

OnLoad
We need to setup some stuff in the OnLoad method of our game window. First out is to setup the new shaders needed to render our text objects.
_textProgram = new ShaderProgram();
_textProgram.AddShader(ShaderType.VertexShader, @"Components\Shaders\1Vert\textVert.c");
_textProgram.AddShader(ShaderType.FragmentShader, @"Components\Shaders\5Frag\textFrag.c");
_textProgram.Link();

Nothing fancy there, next to load our texture and add it to the list of models (for correct disposal)
var textModel = new TexturedRenderObject(RenderObjectFactory.CreateTexturedCharacter(), _textProgram.Id, @"Components\Textures\font singleline.bmp");
models.Add("Quad", textModel);

As we do this in our Asteroid Invaders game, we will be displaying the score. So we need a variable to store this in.
_text = new RenderText(models["Quad"], new Vector4(-0.2f, 0.1f, -0.4f, 1),  Color4.Red, "Score");

And lastly, we need to enable transparency. Otherwise the alpha blending wouldn't bite and we would have a black background instead.
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

OnRenderFrame
The major changes to our OnRenderFrame method consists of updating our score and adding a second render step after all normal game objects for rendering of our transparent objects. This because we need to have whatever we want to show in the transparent areas to be drawn before our text. Otherwise we would get the dark blue background color as a box for all characters.
protected override void OnRenderFrame(FrameEventArgs e)
{
 Title = $"{_title}: FPS:{1f / e.Time:0000.0}, obj:{_gameObjects.Count}, score:{_score}";
 _text.SetText($"Score: {_score}");
 GL.ClearColor(_backColor);
 GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

 int lastProgram = -1;
 foreach (var obj in _gameObjects)
 {
  lastProgram = RenderOneElement(obj, lastProgram);
 }
 // render after all opaque objects to get transparency right
 RenderOneElement(_text, lastProgram);
 SwapBuffers();
}

private int RenderOneElement(AGameObject obj, int lastProgram)
{
 var program = obj.Model.Program;
 if (lastProgram != program)
  GL.UniformMatrix4(20, false, ref _projectionMatrix);
 lastProgram = obj.Model.Program;
 obj.Render(_camera);
 return lastProgram;
}

Further work

This is not a fully featured way to render text, but enough to get a score on the screen. There's a lot of things that could be done to improve it and here comes a few:

As the solution is based on a bitmap, it scales kind of bad. There exists a lot of material on how to make that smooth and something that I will look at some time in the future. Distance fields is an area that seems promising and that takes advantage of the GPU. More information in the following links if you want to give it a try:

Positioning on the screen. Today the solution uses world coordinates, works for this static camera scene but not so well with a moving camera if you still want to have the text visible to the user.

Text wrapping, multi row text. Better scaling.
The list goes on and I'll probably end up coding some of the stuff on the list whenever I feel a need for it.

End results

For the complete source code for the tutorial at the end of this part, go to: https://github.com/eowind/dreamstatecoding

So there, thank you for reading. Hope this helps someone out there : )

Until next time: Work to Live, Don’t Live to Work