surface.DrawTexturedRect()

Hello,

for my hud i have the background image and then an overlay image with the health bar, the height is relative to their health but when i lower the height to 50% the image stays at the full image but stretches down.

Is there any way to make it so it’s not the full image 2x smaller but half the image?

surface.DrawTexturedRectUV

I looked at the drawtexturedrect() but don’t really get how to use the startU and startV

edit: i got it displaying but when i half the health it takes the bottom half instead of from the top

Have a look at render.SetScissorRect

Draws a textured rectangle with a repeated or partial texture. U and V refer to texture coordinates. 0,0 is the bottom left corner of the texture, 1,0 is the bottom right, 1,1 is the top right and 0,1 is the top left.

Think of UV as 2D X/Y coordinates for where to place the texture’s top left and bottom right corners.

[lua]
local iWidth = 256
local iHeight = 32
local iPadding = 24
local tForeground = Color(255, 0, 0)
local tBackground = Color(127, 0, 0)
local mTexture = Material(“icon16/box.png”, “noclamp”)

hook.Add(“HUDPaint”, “HealthBar”, function()
– Anchor it to the bottom left.w
local x = iPadding
local y = ScrH() - iHeight - iPadding

-- Draw the static background
surface.SetDrawColor(tBackground)
surface.DrawRect(x, y, iWidth, iHeight)

-- 100 standing for the max possible health.
local fFrac = LocalPlayer():Health() / 100

-- Adjust the draw width according to how much health there is left.
local iDrawWidth = iWidth * fFrac

-- Draw the dynamic foreground
surface.SetMaterial(mTexture)
surface.SetDrawColor(255,255,255,255)
surface.DrawTexturedRectUV(x, y, iDrawWidth, iHeight, 0, 0, fFrac, 1)

end)[/lua]

Now what this does is it creates the ugliest health bar known to man. But you can fix that by incorporating pictures that actually fit; rather than a box icon and a dark red background. :slight_smile:

You can also make it go vertically and any which way direction you’d like. Here it is next to my own vertical orbs.

http://puu.sh/9df9b/c964cc7eb5.png

pretty aint it

thanks, got it working perfectly :smiley: solved

Then click the Solved button right above your first post.