Hard Light Productions Forums

Modding, Mission Design, and Coding => The Scripting Workshop => Topic started by: Nuke on July 21, 2009, 12:04:27 pm

Title: function for rendering bitmap based statusbars
Post by: Nuke on July 21, 2009, 12:04:27 pm
Code: [Select]
--draws a texture which is a composite of a and b at x,y, split at p (where p is a 0-1 percentage of some value you wish to represent)
--h if true assumes the bar splits horizontally, or vertically if false, r can be true or false, if true it inverts p (-p + 1)
--a and b are texture handles, this seems to work best with 32 bit textures and images with at least a 1 pixel transparent border
--a is the "empty" texture and b is the "full" texture.
drawbar = function(a,b,x,y,p,h,r)
if p > 1 then p = 1 end
if p < 0 then p = 0 end
local iw = a:getWidth()
local ih = a:getHeight()
local y2 = y+ih
local x2 = x+iw
if r then p = -p + 1 end
if h then --horizontal bar
local xc = x+(p*iw)
gr.drawImage(a,x,y,xc,y2,0,0,p,1)
gr.drawImage(b,xc,y,xc+iw,y2,p,0,1,1)
else --vertical bar
local yc = y+(p*ih)
gr.drawImage(a,x,y,x2,yc,0,0,1,p)
gr.drawImage(b,x,yc,x2,yc+ih,0,p,1,1)
end
end

this is useful if you want to draw gauges like the retail energy/afterburner gauges.
Title: Re: function for rendering bitmap based statusbars
Post by: Aardwolf on July 21, 2009, 12:21:15 pm
Is this based on the health/ssjd charge meter from the RTS mod?

Interesting generalization, either way.
Title: Re: function for rendering bitmap based statusbars
Post by: Nuke on July 21, 2009, 03:45:19 pm
no, i wrote this from scratch yesterday while trying to lua-ize sections of the freespace hud. it worked well so i thought id share. unlike the charbe meter, which draws a bunch of rectangles, this takes 2 bitmaps and sets the uv coords so that the imags splits down the middile, then both images are rendered. ive been wanting to use more bitmaps in hud graphics for some time. but the draw functions have always been buggy. actually im suprized this worked at all.
Title: Re: function for rendering bitmap based statusbars
Post by: Aardwolf on July 21, 2009, 04:29:39 pm
Er... you've just described exactly how my meters (the health / ssjd charge ones) work.

Code: [Select]
drawbar = function(X1, Y1, X2, Y2, Percent, Type)
local Frac = math.max(0, math.min(Percent / 100.0))

local Anti = 1 - Frac

local DivisionX = Anti * X1 + Frac * X2

if math.abs(DivisionX - X2) >= 1 then
gr.drawImage(gr.loadTexture("bar_empty.tga"), DivisionX, Y1, X2, Y2, Frac, 0, Anti, 1)
end

if math.abs(DivisionX - X1) >= 1 then
local tex
if Type == "hp" then
tex = gr.loadTexture("bar_hp.tga")
elseif Type == "ssd" then
tex = gr.loadTexture("bar_ssd.tga")
end

gr.drawImage(tex, X1, Y1, DivisionX, Y2, 0, 0, Frac, 1)
end

end
Title: Re: function for rendering bitmap based statusbars
Post by: Nuke on July 22, 2009, 04:14:52 am
yours has the added bonus that it can be stretched. the other difference i see is that you use an actual percent, i ose a number 0-1. my fsr2s code is probibly weeks out of date.
Title: Re: function for rendering bitmap based statusbars
Post by: Aardwolf on July 22, 2009, 07:00:59 pm
Actually, 0-1 is probably better.

The code to compute an integer percentage that goes 0 for 0, 1-99 for somewhat damaged, and 100 for full health, was a bit more complicated than I would have thought.
Title: Re: function for rendering bitmap based statusbars
Post by: Nuke on July 22, 2009, 07:13:03 pm
and max/current is usually in a rage of 0-1 since a lot of the statusbarical features of scripting have max and current, go figure