Draw(image, geom) -> Draw(image, opts)

This commit is contained in:
Josh Deprez 2021-08-12 04:06:01 +00:00
parent 6354df558f
commit efddeb7d6c
8 changed files with 30 additions and 22 deletions

View file

@ -16,10 +16,12 @@ type Camera struct {
//Rotation float64 // radians //Rotation float64 // radians
Zoom float64 // unitless Zoom float64 // unitless
Filter ebiten.Filter // Apply to game?
game *Game game *Game
} }
func (c *Camera) Draw(screen *ebiten.Image, geom ebiten.GeoM) { func (c *Camera) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
// If the camera bounds are smaller than the screen dimensions, that // If the camera bounds are smaller than the screen dimensions, that
// places a lower bound on zoom. // places a lower bound on zoom.
// If the configured centre still puts the camera out of bounds, move it. // If the configured centre still puts the camera out of bounds, move it.
@ -51,14 +53,16 @@ func (c *Camera) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
// Apply camera controls to geom. // Apply camera controls to geom.
// 1. Move c.Centre to the origin // 1. Move c.Centre to the origin
geom.Translate(-float64(centre.X), -float64(centre.Y)) opts.GeoM.Translate(-float64(centre.X), -float64(centre.Y))
// 2. Zoom and rotate // 2. Zoom and rotate
geom.Scale(zoom, zoom) opts.GeoM.Scale(zoom, zoom)
//geom.Rotate(c.Rotation) //geom.Rotate(c.Rotation)
// 3. Move the origin to the centre of screen space. // 3. Move the origin to the centre of screen space.
geom.Translate(sw2, sh2) opts.GeoM.Translate(sw2, sh2)
c.Scene.Draw(screen, geom) opts.Filter = c.Filter
c.Scene.Draw(screen, opts)
} }
func (c *Camera) Update() error { return c.Scene.Update() } func (c *Camera) Update() error { return c.Scene.Update() }

View file

@ -23,7 +23,7 @@ type PerfDisplay struct {
Hidden bool Hidden bool
} }
func (p PerfDisplay) Draw(screen *ebiten.Image, _ ebiten.GeoM) { func (p PerfDisplay) Draw(screen *ebiten.Image, _ ebiten.DrawImageOptions) {
if p.Hidden { if p.Hidden {
return return
} }

View file

@ -14,9 +14,9 @@ type Fill struct {
ZPos ZPos
} }
func (f *Fill) Draw(screen *ebiten.Image, _ ebiten.GeoM) { func (f *Fill) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
if f.Hidden { if f.Hidden {
return return
} }
screen.Fill(f.Color) screen.Fill(opts.ColorM.Apply(f.Color))
} }

View file

@ -21,7 +21,7 @@ type Game struct {
// Draw draws the entire thing, with no geometric transform. // Draw draws the entire thing, with no geometric transform.
func (g *Game) Draw(screen *ebiten.Image) { func (g *Game) Draw(screen *ebiten.Image) {
g.Scene.Draw(screen, ebiten.GeoM{}) g.Scene.Draw(screen, ebiten.DrawImageOptions{})
} }
// Layout returns the configured screen width/height. // Layout returns the configured screen width/height.

View file

@ -15,7 +15,7 @@ type Collider interface {
// Each component is responsible for calling Draw on its child components // Each component is responsible for calling Draw on its child components
// (so that hiding the parent can hide the children, etc). // (so that hiding the parent can hide the children, etc).
type Drawer interface { type Drawer interface {
Draw(screen *ebiten.Image, geom ebiten.GeoM) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions)
} }
// Identifier components have a sense of self. This makes it easier for // Identifier components have a sense of self. This makes it easier for

View file

@ -22,13 +22,13 @@ type Scene struct {
} }
// Draw draws all components in order. // Draw draws all components in order.
func (s *Scene) Draw(screen *ebiten.Image, geom ebiten.GeoM) { func (s *Scene) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
if s.Hidden { if s.Hidden {
return return
} }
for _, i := range s.Components { for _, i := range s.Components {
if d, ok := i.(Drawer); ok { if d, ok := i.(Drawer); ok {
d.Draw(screen, geom) d.Draw(screen, opts)
} }
} }
} }

View file

@ -24,21 +24,22 @@ type Sprite struct {
anim *Anim anim *Anim
} }
func (s *Sprite) Draw(screen *ebiten.Image, geom ebiten.GeoM) { func (s *Sprite) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
if s.Hidden { if s.Hidden {
return return
} }
var op ebiten.DrawImageOptions
dp := s.Pos.Add(s.FrameOffset) dp := s.Pos.Add(s.FrameOffset)
op.GeoM.Translate(float64(dp.X), float64(dp.Y)) var geom ebiten.GeoM
op.GeoM.Concat(geom) geom.Translate(float64(dp.X), float64(dp.Y))
geom.Concat(opts.GeoM)
opts.GeoM = geom
frame := s.anim.CurrentFrame() frame := s.anim.CurrentFrame()
src := s.Src.Image() src := s.Src.Image()
w, _ := src.Size() w, _ := src.Size()
sp := image.Pt((frame*s.FrameSize.X)%w, ((frame*s.FrameSize.X)/w)*s.FrameSize.Y) sp := image.Pt((frame*s.FrameSize.X)%w, ((frame*s.FrameSize.X)/w)*s.FrameSize.Y)
screen.DrawImage(src.SubImage(image.Rectangle{sp, sp.Add(s.FrameSize)}).(*ebiten.Image), &op) screen.DrawImage(src.SubImage(image.Rectangle{sp, sp.Add(s.FrameSize)}).(*ebiten.Image), &opts)
} }
func (s *Sprite) Scan() []interface{} { return []interface{}{&s.Actor} } func (s *Sprite) Scan() []interface{} { return []interface{}{&s.Actor} }

View file

@ -52,25 +52,28 @@ func (t *Tilemap) CollidesWith(r image.Rectangle) bool {
} }
// Draw draws the tilemap. // Draw draws the tilemap.
func (t *Tilemap) Draw(screen *ebiten.Image, geom ebiten.GeoM) { func (t *Tilemap) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
if t.Hidden { if t.Hidden {
return return
} }
src := t.Src.Image() src := t.Src.Image()
w, _ := src.Size() w, _ := src.Size()
og := opts.GeoM
var geom ebiten.GeoM
for j, row := range t.Map { for j, row := range t.Map {
for i, tile := range row { for i, tile := range row {
if tile == nil { if tile == nil {
continue continue
} }
var op ebiten.DrawImageOptions geom.Reset()
op.GeoM.Translate(float64(i*t.TileSize), float64(j*t.TileSize)) geom.Translate(float64(i*t.TileSize), float64(j*t.TileSize))
op.GeoM.Concat(geom) geom.Concat(og)
opts.GeoM = geom
s := tile.TileIndex() * t.TileSize s := tile.TileIndex() * t.TileSize
sx, sy := s%w, (s/w)*t.TileSize sx, sy := s%w, (s/w)*t.TileSize
src := src.SubImage(image.Rect(sx, sy, sx+t.TileSize, sy+t.TileSize)).(*ebiten.Image) src := src.SubImage(image.Rect(sx, sy, sx+t.TileSize, sy+t.TileSize)).(*ebiten.Image)
screen.DrawImage(src, &op) screen.DrawImage(src, &opts)
} }
} }
} }