-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.go
91 lines (73 loc) · 2.21 KB
/
renderer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package spacegame
import (
"fmt"
"golang.org/x/image/colornames"
"golang.org/x/image/font/basicfont"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
"github.com/faiface/pixel/text"
)
// This may be a prototype for a "subwindow" interface...
type Renderer interface {
Bounds() pixel.Rect
Center() pixel.Vec
Clear()
Render(renderable Entity, position pixel.Vec)
ResourceManager() ResourceManager
Text(txt string, position pixel.Vec) error
Update()
}
type PixelWindowRenderer struct {
window *pixelgl.Window
resourceManager ResourceManager
atlas *text.Atlas
}
func NewPixelWindowRenderer(window *pixelgl.Window, resourceManager ResourceManager) *PixelWindowRenderer {
atlas := text.NewAtlas(basicfont.Face7x13, text.ASCII)
return &PixelWindowRenderer{
window: window,
resourceManager: resourceManager,
atlas: atlas,
}
}
func (pwr *PixelWindowRenderer) Bounds() pixel.Rect {
return pwr.window.Bounds()
}
func (pwr *PixelWindowRenderer) Center() pixel.Vec {
return pwr.window.Bounds().Center()
}
func (pwr *PixelWindowRenderer) Clear() {
pwr.window.Clear(colornames.Black)
}
func (pwr *PixelWindowRenderer) Render(renderable Entity, position pixel.Vec) {
// return early if position is out of bounds
if pwr.window.Bounds().Intersect(renderable.Bounds().Moved(position)).Area() == 0 {
return
}
resource := pwr.resourceManager.Resource(renderable)
sprite := resource.sprite
bounds := resource.Bounds()
unitScalerX, unitScalerY := 1/bounds.W(), 1/bounds.H()
rect := resource.Entity().Bounds()
matrix := pixel.IM
matrix = matrix.ScaledXY(pixel.ZV, pixel.V(unitScalerX, unitScalerY))
matrix = matrix.ScaledXY(pixel.ZV, pixel.V(rect.W(), rect.H()))
matrix = matrix.Rotated(pixel.ZV, renderable.Angle())
matrix = matrix.Moved(position)
sprite.Draw(pwr.window, matrix)
}
func (pwr *PixelWindowRenderer) ResourceManager() ResourceManager {
return pwr.resourceManager
}
func (pwr *PixelWindowRenderer) Text(s string, position pixel.Vec) error {
txt := text.New(position, pwr.atlas)
_, err := fmt.Fprintln(txt, s)
if err != nil {
return err
}
txt.Draw(pwr.window, pixel.IM)
return nil
}
func (pwr *PixelWindowRenderer) Update() {
pwr.window.Update()
}