minor refactor in camera

This commit is contained in:
Josh Deprez 2021-08-18 15:46:28 +10:00
parent a50731023a
commit 3b452fc3d0

View file

@ -28,8 +28,6 @@ func (c *Camera) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
return return
} }
// Compute the geometry matrix for the camera controls.
// The lower bound on zoom is the larger of // The lower bound on zoom is the larger of
// { (ScreenWidth / BoundsWidth), (ScreenHeight / BoundsHeight) } // { (ScreenWidth / BoundsWidth), (ScreenHeight / BoundsHeight) }
zoom := c.Zoom zoom := c.Zoom
@ -62,8 +60,17 @@ func (c *Camera) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
// Apply other options // Apply other options
opts.Filter = c.Filter opts.Filter = c.Filter
// Compute common matrix (parts independent of CoordScale).
// Moving centre to the origin happens per component.
var comm ebiten.GeoM
// 2. Zoom (this is also where rotation would be)
comm.Scale(zoom, zoom)
// 3. Move the origin to the centre of screen space.
comm.Translate(sw2, sh2)
// 4. Apply transforms from the caller.
comm.Concat(opts.GeoM)
// Draw everything. // Draw everything.
og := opts.GeoM
for _, i := range c.Scene.Components { for _, i := range c.Scene.Components {
if d, ok := i.(Drawer); ok { if d, ok := i.(Drawer); ok {
cs := 1.0 cs := 1.0
@ -73,12 +80,7 @@ func (c *Camera) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
var geom ebiten.GeoM var geom ebiten.GeoM
// 1. Move centre to the origin, subject to CoordScale // 1. Move centre to the origin, subject to CoordScale
geom.Translate(-float64(centre.X)*cs, -float64(centre.Y)*cs) geom.Translate(-float64(centre.X)*cs, -float64(centre.Y)*cs)
// 2. Zoom (this is also where rotation would be) geom.Concat(comm)
geom.Scale(zoom, zoom)
// 3. Move the origin to the centre of screen space.
geom.Translate(sw2, sh2)
// 4. Apply transforms from the caller.
geom.Concat(og)
opts.GeoM = geom opts.GeoM = geom
d.Draw(screen, opts) d.Draw(screen, opts)
} }