From f2289238df0451c5a9070d9a80c157fa2c00e5fa Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Sun, 2 Jun 2024 19:01:40 +0200 Subject: [PATCH] fixed initial cam pos, it's now always centered --- config.go | 13 +++++++++++++ scene-play.go | 14 ++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/config.go b/config.go index 3729a3d..6b547bc 100644 --- a/config.go +++ b/config.go @@ -29,6 +29,7 @@ type Config struct { Restart, RestartGrid, RestartCache bool StartWithMenu bool Zoomfactor int + InitialCamPos []float64 DelayedStart bool // if true game, we wait. like pause but program induced // for internal profiling @@ -91,6 +92,18 @@ func (config *Config) ParseGeom(geom string) error { config.Cellsize = DEFAULT_CELLSIZE config.Zoomfactor = DEFAULT_ZOOMFACTOR + // calculate the initial cam pos. It is negative if the total grid + // size is smaller than the screen in a centered position, but + // it's zero if it's equal or larger than the screen. + config.InitialCamPos = make([]float64, 2) + if config.Width*config.Cellsize < config.ScreenWidth { + config.InitialCamPos[0] = float64(((config.ScreenWidth - (config.Width * config.Cellsize)) / 2) * -1) + } + + if config.Height*config.Cellsize < config.ScreenHeight { + config.InitialCamPos[1] = float64(((config.ScreenHeight - (config.Height * config.Cellsize)) / 2) * -1) + } + return nil } diff --git a/scene-play.go b/scene-play.go index b989cf1..99208e7 100644 --- a/scene-play.go +++ b/scene-play.go @@ -18,6 +18,10 @@ type Images struct { Black, White, Age1, Age2, Age3, Age4, Old *ebiten.Image } +const ( + DEBUG_FORMAT = "FPS: %0.2f, TPG: %d, M: %0.2fMB, Generations: %d\nScale: %.02fZoom: %d, Cam: %.02f,%.02f Cursor: %d,%d %s" +) + type ScenePlay struct { Game *Game Config *Config @@ -396,7 +400,7 @@ func (scene *ScenePlay) ToggleCellOnCursorPos(alive int64) { x := int(worldX) / scene.Config.Cellsize y := int(worldY) / scene.Config.Cellsize - if x > -1 && y > -1 { + if x > -1 && y > -1 && x < scene.Config.Width && y < scene.Config.Height { scene.Grids[scene.Index].Data[y][x] = alive scene.History.Data[y][x] = 1 } @@ -485,11 +489,13 @@ func (scene *ScenePlay) DrawDebug(screen *ebiten.Image) { paused = "-- paused --" } + x, y := ebiten.CursorPosition() debug := fmt.Sprintf( - "FPS: %0.2f, TPG: %d, Mem: %0.2fMB, Gen: %d, Scale: %.02f, Z: %d, Cam: %.02f,%.02f %s", + DEBUG_FORMAT, ebiten.ActualTPS(), scene.TPG, GetMem(), scene.Generations, scene.Game.Scale, scene.Camera.ZoomFactor, scene.Camera.Position[0], scene.Camera.Position[1], + x, y, paused) FontRenderer.Renderer.SetSizePx(10 + int(scene.Game.Scale*10)) @@ -503,6 +509,7 @@ func (scene *ScenePlay) DrawDebug(screen *ebiten.Image) { fmt.Println(debug) } + } // load a pre-computed pattern from RLE file @@ -648,6 +655,9 @@ func (scene *ScenePlay) Init() { if scene.Config.Zoomfactor < 0 || scene.Config.Zoomfactor > 0 { scene.Camera.ZoomFactor = scene.Config.Zoomfactor } + + scene.Camera.Position[0] = scene.Config.InitialCamPos[0] + scene.Camera.Position[1] = scene.Config.InitialCamPos[1] } // count the living neighbors of a cell