Skip to content

Commit 024b35c

Browse files
authored
refactor: streamline world/window metrics setup (#1583)
* refactor: simplify world/window metrics setup * refactor: extract world window metrics application
1 parent c28599b commit 024b35c

2 files changed

Lines changed: 54 additions & 16 deletions

File tree

game_load.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ func (p *Game) setupDisplayConfig(proj *coreproject.ProjectConfig) {
7171
engine.SetDebugMode(p.debugState.Debug)
7272
}
7373

74+
func (p *Game) applyWorldWindowMetrics(metrics coreproject.WorldWindowMetrics) {
75+
p.displayState.WorldWidth = metrics.WorldWidth
76+
p.displayState.WorldHeight = metrics.WorldHeight
77+
p.displayState.MinWorldX = metrics.MinWorldX
78+
p.displayState.MinWorldY = metrics.MinWorldY
79+
p.displayState.MapMode = metrics.MapMode
80+
p.displayState.WindowWidth = metrics.WindowWidth
81+
p.displayState.WindowHeight = metrics.WindowHeight
82+
}
83+
7484
func (p *Game) setupWorldAndWindow(proj *coreproject.ProjectConfig) {
7585
proj.Map = coreproject.ResolveMapConfig(proj.Map, p.tilemapMgr.hasData(), baseScreenWidth, baseScreenHeight)
7686
backdrops := proj.GetBackdrops()
@@ -89,30 +99,16 @@ func (p *Game) setupWorldAndWindow(proj *coreproject.ProjectConfig) {
8999
}
90100
spxlog.Debug("SetWorldSize: %d, %d", p.displayState.WorldWidth, p.displayState.WorldHeight)
91101

102+
p.doWindowSize()
92103
metrics := coreproject.ResolveWorldWindowMetrics(
93104
p.displayState.WorldWidth,
94105
p.displayState.WorldHeight,
95106
p.displayState.WindowWidth,
96107
p.displayState.WindowHeight,
97108
coreproject.ToMapMode(proj.Map.Mode),
98109
)
99-
p.displayState.WorldWidth = metrics.WorldWidth
100-
p.displayState.WorldHeight = metrics.WorldHeight
101-
p.displayState.MinWorldX = metrics.MinWorldX
102-
p.displayState.MinWorldY = metrics.MinWorldY
103-
p.displayState.MapMode = metrics.MapMode
104-
p.doWindowSize()
110+
p.applyWorldWindowMetrics(metrics)
105111
spxlog.Debug("SetWindowSize: %d, %d", p.displayState.WindowWidth, p.displayState.WindowHeight)
106-
107-
metrics = coreproject.ResolveWorldWindowMetrics(
108-
p.displayState.WorldWidth,
109-
p.displayState.WorldHeight,
110-
p.displayState.WindowWidth,
111-
p.displayState.WindowHeight,
112-
p.displayState.MapMode,
113-
)
114-
p.displayState.WindowWidth = metrics.WindowWidth
115-
p.displayState.WindowHeight = metrics.WindowHeight
116112
}
117113

118114
func (p *Game) setupPlatformAndCamera(proj *coreproject.ProjectConfig) {

game_load_window_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package spx
2+
3+
import (
4+
"testing"
5+
6+
coreproject "github.com/goplus/spx/v2/internal/core/project"
7+
)
8+
9+
func TestSetupWorldAndWindowClampsBackdropWindowToWorld(t *testing.T) {
10+
game := &Game{}
11+
proj := &coreproject.ProjectConfig{
12+
Map: coreproject.MapConfig{
13+
Width: 320,
14+
Height: 180,
15+
Mode: "repeat",
16+
},
17+
Backdrops: []*coreproject.BackdropConfig{
18+
{
19+
CostumeConfig: coreproject.CostumeConfig{
20+
Path: "bg.png",
21+
ImageWidth: 640,
22+
ImageHeight: 480,
23+
},
24+
},
25+
},
26+
}
27+
28+
game.setupWorldAndWindow(proj)
29+
30+
if game.displayState.WorldWidth != 320 || game.displayState.WorldHeight != 180 {
31+
t.Fatalf("world size = %dx%d, want 320x180", game.displayState.WorldWidth, game.displayState.WorldHeight)
32+
}
33+
if game.displayState.WindowWidth != 320 || game.displayState.WindowHeight != 180 {
34+
t.Fatalf("window size = %dx%d, want 320x180", game.displayState.WindowWidth, game.displayState.WindowHeight)
35+
}
36+
if game.displayState.MinWorldX != -160 || game.displayState.MinWorldY != -90 {
37+
t.Fatalf("world min = (%d, %d), want (-160, -90)", game.displayState.MinWorldX, game.displayState.MinWorldY)
38+
}
39+
if game.displayState.MapMode != coreproject.MapModeRepeat {
40+
t.Fatalf("map mode = %d, want %d", game.displayState.MapMode, coreproject.MapModeRepeat)
41+
}
42+
}

0 commit comments

Comments
 (0)