Skip to content

Commit

Permalink
perf(game): Retain the view buffer between renders
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Jul 15, 2024
1 parent 9c360bc commit 3890ba4
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions internal/game/game.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package game

import (
"bytes"
"context"
"image"
"slices"
"strconv"
"strings"
"time"

"github.com/charmbracelet/bubbles/help"
Expand Down Expand Up @@ -64,6 +64,7 @@ type Game struct {
mode Mode
wrap bool
speed int
viewBuf bytes.Buffer
}

func (g *Game) Init() tea.Cmd {
Expand Down Expand Up @@ -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{}
Expand Down

0 comments on commit 3890ba4

Please sign in to comment.