/
scene.go
157 lines (129 loc) · 2.92 KB
/
scene.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package splish
import (
"errors"
"image"
"image/draw"
)
type Scene struct {
// Foreground is the highest level SubScene
Foreground *SubScene
// Background is the lowest level SubScene
Background *SubScene
window Window
// regions should be a sorted by ascending priority
regions []Region
view Rectangle
Sprites []Sprite
}
func NewScene(w Window) Scene {
s := Scene{window: w}
return s
}
func (s *Scene) NewSprite(img image.Image) *Sprite {
var rgba *image.RGBA
if ri, ok := img.(*image.RGBA); ok {
rgba = ri
} else {
rgba = image.NewRGBA(img.Bounds())
draw.Draw(rgba, rgba.Bounds(), img, image.ZP, draw.Src)
}
tex := s.window.UploadTexture(rgba)
spr := Sprite{Tex: tex, Img: rgba}
spr.SubSprite(NewRectangle(img.Bounds()))
s.Sprites = append(s.Sprites, spr)
return &spr
}
func (s *Scene) Draw() {
scenes := []*SubScene{}
if s.Background != nil {
scenes = append(scenes, s.Background)
}
for _, region := range s.regions {
if region.Area.Overlaps(s.view) && region.Scene != nil {
scenes = append(scenes, region.Scene)
}
}
if s.Foreground != nil {
scenes = append(scenes, s.Foreground)
}
s.window.Draw(
scenes,
s.view,
)
}
func (s *Scene) TranslateView(x, y float32) {
s.view.Min = s.view.Min.AddXY(x, y)
s.view.Max = s.view.Max.AddXY(x, y)
}
func (s *Scene) ZoomView(z int) {
}
type SubScene struct {
Global bool
Location Point
Layers []Layer2d
}
type Layer2d struct {
Sprites []SpriteInstance
Transform image.Rectangle
}
type Point struct {
X, Y float32
}
func NewPoint(ip image.Point) Point {
return Point{float32(ip.X), float32(ip.Y)}
}
func (p Point) Add(s Point) Point {
return Point{p.X + s.X, p.Y + s.Y}
}
func (p *Point) AddXY(x, y float32) Point {
return Point{p.X + x, p.Y + y}
}
func (p Point) ToImage() image.Point {
return image.Point{int(p.X), int(p.Y)}
}
type Rectangle struct {
Min, Max Point
}
func NewRectangle(ir image.Rectangle) Rectangle {
return Rectangle{NewPoint(ir.Min), NewPoint(ir.Max)}
}
func (r Rectangle) Overlaps(s Rectangle) bool {
return !r.Empty() && !s.Empty() &&
r.Min.X < s.Max.X && s.Min.X < r.Max.X &&
r.Min.Y < s.Max.Y && s.Min.Y < r.Max.Y
}
func (r Rectangle) Empty() bool {
return r.Min.X >= r.Max.X || r.Min.Y >= r.Max.Y
}
func (r Rectangle) Size() Point {
return Point{r.Max.X - r.Min.X, r.Max.Y - r.Min.Y}
}
func (r Rectangle) ToImage() image.Rectangle {
return image.Rectangle{r.Min.ToImage(), r.Max.ToImage()}
}
type SpriteInstance struct {
Sprite SpriteID
Location Point
Index int
Rotation int
Scale float32
}
type SpriteID int
type Sprite struct {
Tex Texture
Img *image.RGBA
Size Point
Sub []interface{}
}
func (s *Sprite) SubSprite(rect Rectangle) int {
s.Sub = append(s.Sub, s.Tex.MarkSubTexture(rect))
return len(s.Sub) - 1
}
type Region struct {
// Regions are drawn starting with low priority
At Point
Priority int
Scene *SubScene
Area Rectangle
}
var todoErr = errors.New("TODO")