From 3890ba402842f3dba985ae8de5a6f25d396899ad Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Mon, 15 Jul 2024 01:07:23 -0500 Subject: [PATCH] perf(game): Retain the view buffer between renders --- internal/game/game.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/internal/game/game.go b/internal/game/game.go index 81ba7a6..01e2155 100644 --- a/internal/game/game.go +++ b/internal/game/game.go @@ -1,11 +1,11 @@ package game import ( + "bytes" "context" "image" "slices" "strconv" - "strings" "time" "github.com/charmbracelet/bubbles/help" @@ -64,6 +64,7 @@ type Game struct { mode Mode wrap bool speed int + viewBuf bytes.Buffer } func (g *Game) Init() tea.Cmd { @@ -266,23 +267,25 @@ func (g *Game) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } func (g *Game) View() string { - var view strings.Builder + defer func() { + g.viewBuf.Reset() + }() if len(g.pattern.Grid) != 0 { - view.Grow(g.viewW * g.viewH) + g.viewBuf.Grow((g.viewW*2 + 1) * g.viewH) for _, row := range g.pattern.Grid[g.y:min(g.y+g.viewH, len(g.pattern.Grid))] { for _, cell := range row[g.x:min(g.x+g.viewW, len(row))] { if cell == 1 { - view.WriteRune('█') - view.WriteRune('█') + g.viewBuf.WriteRune('█') + g.viewBuf.WriteRune('█') } else { - view.WriteByte(' ') - view.WriteByte(' ') + g.viewBuf.WriteByte(' ') + g.viewBuf.WriteByte(' ') } } - view.WriteByte('\n') + g.viewBuf.WriteByte('\n') } } - return view.String() + g.help.ShortHelpView(g.keymap.ShortHelp()) + return g.viewBuf.String() + g.help.ShortHelpView(g.keymap.ShortHelp()) } type tick struct{}