micro-optimize background poly to only update when size changes

This commit is contained in:
Cynthia Foxwell 2023-12-18 22:29:48 -07:00
parent 3d10c63702
commit 10e902ddcf
1 changed files with 17 additions and 5 deletions

View File

@ -99,9 +99,11 @@ local function add_rounded_poly(poly, x, y, rad, seg, offset)
end
end
local function RoundedBoxPoly(x, y, w, h, rad, seg)
local function RoundedBoxPoly(w, h, rad, seg)
local poly = {}
local x, y = 0, 0
add_rounded_poly(poly, x + rad, y + rad, rad, seg, seg)
poly[#poly + 1] = {
@ -131,9 +133,11 @@ local function RoundedBoxPoly(x, y, w, h, rad, seg)
add_rounded_poly(poly, x + rad, y + (h - rad), rad, seg)
surface_DrawPoly(poly)
return poly
end
local PREV_W, PREV_H, BACKGROUND_POLY
local function CreateChatbox()
if IsValid(cbox.chatbox.panels.frame) then
cbox.chatbox.panels.frame:Remove()
@ -150,7 +154,7 @@ local function CreateChatbox()
-- TODO: make this configurable
frame:SetMinWidth(dw)
frame:SetMinWidth(dh)
frame:SetMinHeight(dh)
local x = frame:GetCookie("pos_x", dx)
local y = frame:GetCookie("pos_y", dy)
@ -158,6 +162,9 @@ local function CreateChatbox()
local h = frame:GetCookie("height", dh)
frame:SetPos(x, y)
frame:SetSize(w, h)
PREV_W = w
PREV_H = h
BACKGROUND_POLY = RoundedBoxPoly(w, h, 8, 24)
frame:SetVisible(false)
@ -197,7 +204,12 @@ local function CreateChatbox()
self.animFade:Run()
-- TODO: save position and size
local w, h = self:GetSize()
if w ~= PREV_W or h ~= PREV_H then
PREV_W = w
PREV_H = h
BACKGROUND_POLY = RoundedBoxPoly(w, h, 8, 24)
end
end
function frame:Paint(w, h)
@ -220,7 +232,7 @@ local function CreateChatbox()
draw_NoTexture()
surface_SetDrawColor(255, 255, 255)
RoundedBoxPoly(0, 0, w, h, 8, 24)
surface_DrawPoly(BACKGROUND_POLY)
-- Only draw things that are in the stencil buffer
render_SetStencilCompareFunction( STENCIL_EQUAL )