From 10e902ddcf38cd0c1c37f3f5bf1e7a156831dcb7 Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Mon, 18 Dec 2023 22:29:48 -0700 Subject: [PATCH] micro-optimize background poly to only update when size changes --- lua/cbox/cl_chatbox.lua | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lua/cbox/cl_chatbox.lua b/lua/cbox/cl_chatbox.lua index 1d7de29..3ab8bcb 100644 --- a/lua/cbox/cl_chatbox.lua +++ b/lua/cbox/cl_chatbox.lua @@ -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 )