Author Topic: function for rendering bitmap based statusbars  (Read 2336 times)

0 Members and 1 Guest are viewing this topic.

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
function for rendering bitmap based statusbars
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.
I can no longer sit back and allow communist infiltration, communist indoctrination, communist subversion, and the international communist conspiracy to sap and impurify all of our precious bodily fluids.

Nuke's Scripting SVN

 

Offline Aardwolf

  • 211
  • Posts: 16,384
    • Minecraft
Re: function for rendering bitmap based statusbars
Is this based on the health/ssjd charge meter from the RTS mod?

Interesting generalization, either way.

 

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
Re: function for rendering bitmap based statusbars
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.
« Last Edit: July 21, 2009, 03:48:56 pm by Nuke »
I can no longer sit back and allow communist infiltration, communist indoctrination, communist subversion, and the international communist conspiracy to sap and impurify all of our precious bodily fluids.

Nuke's Scripting SVN

 

Offline Aardwolf

  • 211
  • Posts: 16,384
    • Minecraft
Re: function for rendering bitmap based statusbars
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

 

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
Re: function for rendering bitmap based statusbars
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.
I can no longer sit back and allow communist infiltration, communist indoctrination, communist subversion, and the international communist conspiracy to sap and impurify all of our precious bodily fluids.

Nuke's Scripting SVN

 

Offline Aardwolf

  • 211
  • Posts: 16,384
    • Minecraft
Re: function for rendering bitmap based statusbars
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.

  

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
Re: function for rendering bitmap based statusbars
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
I can no longer sit back and allow communist infiltration, communist indoctrination, communist subversion, and the international communist conspiracy to sap and impurify all of our precious bodily fluids.

Nuke's Scripting SVN