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

@@ -52,7 +52,7 @@ type ScenePlay struct {
Generations int64 // Stats
TicksElapsed int // tick counter for game speed
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
Dragging bool // middle mouse is pressed, move canvas
LastCursorPos []float64 // used to check if the user is dragging
@@ -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.Camera.Render(scene.World, screen)
@@ -610,22 +603,19 @@ func (scene *ScenePlay) InitPattern() {
// pre-render offscreen cache image
func (scene *ScenePlay) InitCache() {
op := &ebiten.DrawImageOptions{}
// setup theme
scene.Theme.SetGrid(scene.Config.ShowGrid)
if scene.Config.ShowGrid {
scene.Cache.Fill(scene.Theme.Color(ColGrid))
} else {
if !scene.Config.ShowGrid {
scene.Cache.Fill(scene.Theme.Color(ColDead))
return
}
for y := 0; y < scene.Config.Height; y++ {
// 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,
)
op := &ebiten.DrawImageOptions{}
scene.Cache.Fill(scene.Theme.Color(ColGrid))
for y := 0; y < scene.Config.Height; y++ {
for x := 0; x < scene.Config.Width; x++ {
op.GeoM.Reset()
op.GeoM.Translate(
@@ -634,13 +624,6 @@ func (scene *ScenePlay) InitCache() {
)
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.GridImage = ebiten.NewImage(
scene.Config.Width*scene.Config.Cellsize,
scene.Config.Height*scene.Config.Cellsize,
)
scene.Theme = scene.Config.ThemeManager.GetCurrentTheme()
scene.InitCache()

View File

@@ -26,8 +26,10 @@ const (
// readily available from play.go
type Theme struct {
Tiles map[int]*ebiten.Image
GridTiles map[int]*ebiten.Image
Colors map[int]color.RGBA
Name string
ShowGrid bool
}
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.GridTiles = make(map[int]*ebiten.Image, 6)
for cid, col := range theme.Colors {
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
@@ -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
// user anyway
func (theme *Theme) Tile(col int) *ebiten.Image {
if theme.ShowGrid {
return theme.GridTiles[col]
}
return theme.Tiles[col]
}
@@ -104,6 +114,10 @@ func (theme *Theme) Color(col int) color.RGBA {
return theme.Colors[col]
}
func (theme *Theme) SetGrid(showgrid bool) {
theme.ShowGrid = showgrid
}
type ThemeManager struct {
Theme string
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
// 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(
tile,
float32(0),
float32(0),
float32(x),
float32(x),
float32(cellsize),
float32(cellsize),
col, false,