Jump to content
slick29

Using fonts

Recommended Posts

It's hard to tell from a screenshot - a simple example program that shows the problem by showing how you are drawing the text would be more helpful. But since we have to guess, are you loading multiple sized arial fonts or are you scaling the context when drawing? Do the numbers clear when they change or is there a smearing from the previous numbers?

Share this post


Link to post

Currently, I don't have a way to test this due to graphics card issues, but looking at the code I would make the following suggestions:

- Load the textures and fonts in the Script:Start() function

- In the Script:PostRender() function, always set the context's blendmode and color back to the standard Blend.Solid and (1,1,1,1) to avoid interference with other code that might be rendering images and texts.

- Once you set the blendmode to Alpha, you don't have to keep doing it prior to each Draw command

- Leadwerks colors use a color scale of 0-1 and not 0-255. The only thing that displays 0-255 is the editor's color property but it is actually using 0-1 to set the color property and it will return the 0-1 scale.

- I can't tell if your hud image requires alpha or not, if it does then ignore this - but if it doesn't then draw with a solid blendmode

 

example code that shows the above changes:

Script.size = Vec2 (175,25) --vec2 "Size"

Script.textcolor = Vec4(1,1,1,1) --color "Health Text "

Script.ammocolor = Vec4(1,1,1,1) --color "Ammo Text "

Script.player = nil

Script.healthtext = nil

Script.maxhealthtext = nil

Script.clipammotext = nil

Script.totalammotext = nil

Script.healthfactor = nil

Script.healthdisplay = nil

Script.ammodisplay = nil

Script.clipdisplay = nil

 

function Script:Start()

self.hudfont = Font:Load("fonts/Arial.ttf", 14)

self.clipfont = Font:Load("fonts/Arial.ttf", 32)

self.ammofont = Font:Load("fonts/Arial.ttf", 18)

self.image={}

self.image.ammoboxdisplay = Texture:Load("Materials/HUD/Status.tex")

self.player = self.entity: GetParent()

end

 

function Script:UpdateWorld()

self.healthtext = self.player.script.health

self.maxhealthtext = self.player.script.maxHealth

self.healthfactor = self.healthtext / self.maxhealthtext

self.clipammotext = self.player.script.weapons[self.player.script.currentweaponindex].clipammo

self.totalammotext = self.player.script.weapons[self.player.script.currentweaponindex].ammo

--self.Scoretext = killscore

self.healthdisplay = "+" ..self.healthtext

self.clipdisplay = "" .. self.clipammotext

self.ammodisplay = "" .. self.totalammotext

end

 

function Script:PostRender(context)

context:SetFont(self.hudfont)

--Draw HUD Display

context:DrawImage(self.image.ammoboxdisplay, context:GetWidth()*1.05-self.size.x, (context:GetHeight()*.9)- self.size.y, self.size.x*.65, self.size.y*3.7)

 

--Draw the Health

--DrawText (text to draw, xpos, ypos)

context:SetColor (self.textcolor)

context:SetBlendMode(Blend.Alpha)

context:DrawText(self.healthdisplay, (context:GetWidth()*.932)+self.size.x*.02, (context:GetHeight()*.915)- self.size.y*.5)

 

context:SetFont(self.clipfont)

--Draw the Clip Count

--DrawText (text to draw, xpos, ypos)

context:DrawText(self.clipammotext, (context:GetWidth()*.880)+self.size.x*.02, (context:GetHeight()*.928)- self.size.y*.5)

 

context:SetFont(self.ammofont)

--Draw the Ammo Count

--DrawTex (text to draw, xpos, ypos)

context:DrawText(self.totalammotext, (context:GetWidth()*.935)+self.size.x*.02, (context:GetHeight()*.955)-self.size.y*.5)

 

context:SetBlendMode(Blend.Solid)

context:SetColor(1,1,1,1)

end

Share this post


Link to post

Thanks for your help and the quick response. I implemented your changes and that did the trick. It now looks like it should.

 

Cheers biggrin.png!!!

Share this post


Link to post

How would I go about centering the clip count when it goes below 10? It's in the right position with double digits (screen capture on the left), but jumps to the left of the HUD when it's 9 or less (screen capture on the right)

post-703-0-31530000-1473737360_thumb.jpg

Share this post


Link to post

you can just adjust the position drawn when under 10 by using if statements, or you can format the number to show a leading zero when under 10.

 

--Draw the Ammo Count

context:SetFont(self.ammofont)

local ammo = string.format("%02d", self.totalammotext)

context:DrawText(ammo, (context:GetWidth()*.935)+self.size.x*.02, (context:GetHeight()*.955)-self.size.y*.5)

Share this post


Link to post

I've managed to implement the suggestions Macklebee (many thanks again sir) suggested concerning formatting the ammo count. I've tried both putting a "0" before single digits and using an " if " statement but prefer how it looks without a "0" in front of the number. My question is how do I format it so the number's position changes >100, < 100 and below <10. This is what I have so far. I only get results >100 and < 100.

HUD_01.lua

Share this post


Link to post

context:SetFont(self.ammofont)

local ammo = self.player.script.weapons[self.player.script.currentweaponindex].ammo

if ammo >= 100 then

context:DrawText(self.totalammotext, (context:GetWidth()*.935)+self.size.x*.02, (context:GetHeight()*.955)-self.size.y*.5)

elseif ammo < 100 and ammo >= 10 then

context:DrawText(self.totalammotext, (context:GetWidth()*.942)+self.size.x*.02, (context:GetHeight()*.960)- self.size.y*.5)

elseif ammo < 10 then

context:DrawText(self.totalammotext, (context:GetWidth()*.955)+self.size.x*.02, (context:GetHeight()*.965)- self.size.y*.5)

end

 

You never see the below 10 condition because you are satisfying the second condition since 10 < 100. Another way to do this without having to resort to putting multiple conditions together as I showed above using the 'and' would be to just rearrange the order of conditions:

 

context:SetFont(self.ammofont)

local ammo = self.player.script.weapons[self.player.script.currentweaponindex].ammo

if ammo < 10 then

context:DrawText(self.totalammotext, (context:GetWidth()*.955)+self.size.x*.02, (context:GetHeight()*.965)- self.size.y*.5)

elseif ammo < 100 then

context:DrawText(self.totalammotext, (context:GetWidth()*.942)+self.size.x*.02, (context:GetHeight()*.960)- self.size.y*.5)

else

context:DrawText(self.totalammotext, (context:GetWidth()*.935)+self.size.x*.02, (context:GetHeight()*.955)-self.size.y*.5)

end

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...