fixed grid lines performance problem

This commit is contained in:
2024-06-15 18:21:54 +02:00
committed by T.v.Dein
parent 604cbea127
commit eb95c72538
2 changed files with 46 additions and 54 deletions

View File

@@ -46,23 +46,23 @@ type ScenePlay struct {
Clear bool Clear bool
Grids []*Grid // 2 grids: one current, one next Grids []*Grid // 2 grids: one current, one next
History History // holds state of past dead cells for evolution traces History History // holds state of past dead cells for evolution traces
Index int // points to current grid Index int // points to current grid
Generations int64 // Stats Generations int64 // Stats
TicksElapsed int // tick counter for game speed TicksElapsed int // tick counter for game speed
Camera Camera // for zoom+move Camera Camera // for zoom+move
World, Cache, GridImage *ebiten.Image // actual image we render to World, Cache *ebiten.Image // actual image we render to
WheelTurned bool // when user turns wheel multiple times, zoom faster WheelTurned bool // when user turns wheel multiple times, zoom faster
Dragging bool // middle mouse is pressed, move canvas Dragging bool // middle mouse is pressed, move canvas
LastCursorPos []float64 // used to check if the user is dragging LastCursorPos []float64 // used to check if the user is dragging
MarkTaken bool // true when mouse1 pressed MarkTaken bool // true when mouse1 pressed
MarkDone bool // true when mouse1 released, copy cells between Mark+Point MarkDone bool // true when mouse1 released, copy cells between Mark+Point
Mark, Point image.Point // area to marks+save Mark, Point image.Point // area to marks+save
RunOneStep bool // mutable flags from config RunOneStep bool // mutable flags from config
TPG int // current game speed (ticks per game) TPG int // current game speed (ticks per game)
Theme Theme Theme Theme
RuleCheckFunc func(uint8, uint8) uint8 RuleCheckFunc func(uint8, uint8) uint8
} }
func NewPlayScene(game *Game, config *Config) Scene { func NewPlayScene(game *Game, config *Config) Scene {
@@ -505,13 +505,6 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
} }
} }
if scene.Config.ShowGrid {
// draw the pre-drawn grid onto the world
op.GeoM.Reset()
op.GeoM.Translate(0, 0)
scene.World.DrawImage(scene.GridImage, op)
}
scene.DrawMark(scene.World) scene.DrawMark(scene.World)
scene.Camera.Render(scene.World, screen) scene.Camera.Render(scene.World, screen)
@@ -610,22 +603,19 @@ func (scene *ScenePlay) InitPattern() {
// pre-render offscreen cache image // pre-render offscreen cache image
func (scene *ScenePlay) InitCache() { func (scene *ScenePlay) InitCache() {
op := &ebiten.DrawImageOptions{} // setup theme
scene.Theme.SetGrid(scene.Config.ShowGrid)
if scene.Config.ShowGrid { if !scene.Config.ShowGrid {
scene.Cache.Fill(scene.Theme.Color(ColGrid))
} else {
scene.Cache.Fill(scene.Theme.Color(ColDead)) scene.Cache.Fill(scene.Theme.Color(ColDead))
return
} }
for y := 0; y < scene.Config.Height; y++ { op := &ebiten.DrawImageOptions{}
// horizontal grid line
vector.StrokeLine(scene.GridImage,
float32(0), float32(y*scene.Config.Cellsize),
float32(scene.Config.Width*scene.Config.Cellsize), float32(y*scene.Config.Cellsize),
1, scene.Theme.Color(ColGrid), false,
)
scene.Cache.Fill(scene.Theme.Color(ColGrid))
for y := 0; y < scene.Config.Height; y++ {
for x := 0; x < scene.Config.Width; x++ { for x := 0; x < scene.Config.Width; x++ {
op.GeoM.Reset() op.GeoM.Reset()
op.GeoM.Translate( op.GeoM.Translate(
@@ -634,13 +624,6 @@ func (scene *ScenePlay) InitCache() {
) )
scene.Cache.DrawImage(scene.Theme.Tile(ColDead), op) scene.Cache.DrawImage(scene.Theme.Tile(ColDead), op)
// vertical grid line
vector.StrokeLine(scene.GridImage,
float32(x*scene.Config.Cellsize), float32(0),
float32(x*scene.Config.Cellsize), float32(scene.Config.Height*scene.Config.Cellsize),
1, scene.Theme.Color(ColGrid), false,
)
} }
} }
} }
@@ -687,11 +670,6 @@ func (scene *ScenePlay) Init() {
scene.Config.Height*scene.Config.Cellsize, scene.Config.Height*scene.Config.Cellsize,
) )
scene.GridImage = ebiten.NewImage(
scene.Config.Width*scene.Config.Cellsize,
scene.Config.Height*scene.Config.Cellsize,
)
scene.Theme = scene.Config.ThemeManager.GetCurrentTheme() scene.Theme = scene.Config.ThemeManager.GetCurrentTheme()
scene.InitCache() scene.InitCache()

View File

@@ -25,9 +25,11 @@ const (
// the colors and the actual tile images here, so that they are // the colors and the actual tile images here, so that they are
// readily available from play.go // readily available from play.go
type Theme struct { type Theme struct {
Tiles map[int]*ebiten.Image Tiles map[int]*ebiten.Image
Colors map[int]color.RGBA GridTiles map[int]*ebiten.Image
Name string Colors map[int]color.RGBA
Name string
ShowGrid bool
} }
type ThemeDef struct { type ThemeDef struct {
@@ -84,10 +86,14 @@ func NewTheme(def ThemeDef, cellsize int, name string) Theme {
} }
theme.Tiles = make(map[int]*ebiten.Image, 6) theme.Tiles = make(map[int]*ebiten.Image, 6)
theme.GridTiles = make(map[int]*ebiten.Image, 6)
for cid, col := range theme.Colors { for cid, col := range theme.Colors {
theme.Tiles[cid] = ebiten.NewImage(cellsize, cellsize) theme.Tiles[cid] = ebiten.NewImage(cellsize, cellsize)
FillCell(theme.Tiles[cid], cellsize, col) FillCell(theme.Tiles[cid], cellsize, col, 0)
theme.GridTiles[cid] = ebiten.NewImage(cellsize, cellsize)
FillCell(theme.GridTiles[cid], cellsize, col, 1)
} }
return theme return theme
@@ -97,6 +103,10 @@ func NewTheme(def ThemeDef, cellsize int, name string) Theme {
// unknown type is being used, which is ok, since the code is the only // unknown type is being used, which is ok, since the code is the only
// user anyway // user anyway
func (theme *Theme) Tile(col int) *ebiten.Image { func (theme *Theme) Tile(col int) *ebiten.Image {
if theme.ShowGrid {
return theme.GridTiles[col]
}
return theme.Tiles[col] return theme.Tiles[col]
} }
@@ -104,6 +114,10 @@ func (theme *Theme) Color(col int) color.RGBA {
return theme.Colors[col] return theme.Colors[col]
} }
func (theme *Theme) SetGrid(showgrid bool) {
theme.ShowGrid = showgrid
}
type ThemeManager struct { type ThemeManager struct {
Theme string Theme string
Themes map[string]Theme Themes map[string]Theme
@@ -152,11 +166,11 @@ func (manager *ThemeManager) SetCurrentTheme(theme string) {
// //
// So we don't draw a grid, we just left a grid behind, which saves us // So we don't draw a grid, we just left a grid behind, which saves us
// from a lot of drawing operations. // from a lot of drawing operations.
func FillCell(tile *ebiten.Image, cellsize int, col color.RGBA) { func FillCell(tile *ebiten.Image, cellsize int, col color.RGBA, x int) {
vector.DrawFilledRect( vector.DrawFilledRect(
tile, tile,
float32(0), float32(x),
float32(0), float32(x),
float32(cellsize), float32(cellsize),
float32(cellsize), float32(cellsize),
col, false, col, false,