Most efficient way to draw a 16x10 group of rectangles?
5 replies, posted
I had it before with no lag, now it lags like crazy. Im making a tag system, current draw code:
for k,v in pairs(Tags) do
for x=1,16 do
for y=1,10 do
if(!Tags[x]) then break end
if(!Tags[x][y]) then break end
local Col=Tags[x][y]
local Pos=(LocalPlayer():GetPos()+Vector(12-x*2,0,2.5-y*2+100)):ToScreen()
if(Pos.visible) then
--surface.SetDrawColor(Col.r,Col.g,Col.b,255)
draw.RoundedBox(2,Pos.x,Pos.y,6,6,Col)
end
end
end
end
Whats the most inefficient part of this
Ehm if you explain what the code should do we might can find a much better solution.
But 3 loops in each other for drawing is pretty badass.
Oh an rounded boxes are using 4 textured rotated rects and 5 normal rects, you only should use them if you really need rounded corners.
[QUOTE=Map in a box;24680971]
[code]for k,v in pairs(Tags) do
for x=1,16 do
for y=1,10 do
if(!Tags[x]) then break end
if(!Tags[x][y]) then break end
local Col=Tags[x][y]
local Pos=(LocalPlayer():GetPos()+Vector(12-x*2,0,2.5-y*2+100)):ToScreen()
if(Pos.visible) then
--surface.SetDrawColor(Col.r,Col.g,Col.b,255)
draw.RoundedBox(2,Pos.x,Pos.y,6,6,Col)
end
end
end
end
[/code]
[/QUOTE]
The most inefficient parts would be:
Calling LocalPlayer 160 * Tags times each frame
Calling GetPos an equal number of times
Creating twice as many vectors ( vec + vec creates a vector )
Calling Vector:ToScreen an equal number of times
Calling draw.RoundedBox an equal number of times ( which draws 4 textured rects plus at least three untextured rects )
By using some simple math, we can replace your x and y loops with a single loop and still have the x and y values, and by creating the vector and fetching the position outside of the loop, we reduce the expense greatly.
[lua]HEIGHT = 10
START_INDEX = 1
local k, v, x, y, i, pos, vec, vec2
pos = LocalPlayer( ):GetPos( )
vec = Vector( 0, 0, 0 )
for k, v in pairs( Tags ) do
for i = 0, 159 do
x = START_INDEX + math.floor( i / HEIGHT )
y = START_INDEX + i % HEIGHT
if v[ x ] and v[ x ][ y ] then
vec.x = 12 - x * 2
vec.z = 2.5 - y * 2 + 100
vec2 = ( pos + vec ):ToScreen( )
if vec2.visible then
draw.RoundedBox( 2, vec2.x, vec2.y, 6, 6, v[ x ][ y ] )
end
end
end
end[/lua]
This still will create 160 vectors from the addition, calls ToScreen 160 times, draw.RoundedBox 160 times, but it should run a lot faster now.
You should just use a normal rect for the "pixel", but that's your decision.
The tags have to be above the player, so yeah LocalPlayer will change.
But still, I remember using draw.RoundedBox and it dropped my fps only by 2, not by 50
[editline]04:46PM[/editline]
And I think i found out why, 16 loops for tags. So in other words i do over 16000 draws(guestimated).
[editline]04:47PM[/editline]
oh and looky, luapad failed to save again
I use luapad for quick debugging
Sorry, you need to Log In to post a reply to this thread.