January 12, 2014

Add Primitive HUD To Leadwerks Indie Edition

If you want to add a simple HUD to your Leadwerks Indie Edition game, here you go.

First, open FPSPlayer.lua.
At the top, under all the Script.* lines, add:

Script.hudFont = nil

Somewhere in Script:Start(), add:
self.hudFont = Font:Load("Fonts/Arial.ttf",20)

Scroll down to Script:PostRender(context).
After the code to add the blood splatter on the screen and before context:SetColor(1,1,1,1), add:

if self.hudFont~=nil then
    context:SetFont(self.hudFont)
    context:SetColor(1,1,1,1)
    local fontHeight = self.hudFont:GetHeight()
    local hudText = "Health: "..self.health
    local x = 0
    local y = context:GetHeight() - fontHeight
    context:DrawText(hudText, x, y)

    if self.weapon~=nil then
        hudText = "Ammo: "..self.weapon.clipammo.."/"..self.weapon.ammo
        x = context:GetWidth() - self.hudFont:GetTextWidth(hudText)
        context:DrawText(hudText, x, y)
    end

end

The only downside to this is that the debug text doesn't set its own font appropriately.  To fix that, open your App.lua file.  In App:Start(), add:

self.font = Font:Load("Fonts/Arial.ttf",10)

And in App:Loop, right after the "Render statistics" comment, add:

self.context:SetFont(self.font)

That should give you a HUD similar to the following:

1 comment:

Marco Griep said...

Nice Post. This helps alot and is a good template to extend this Script.