-
Notifications
You must be signed in to change notification settings - Fork 27
/
assets.go
259 lines (208 loc) · 5.23 KB
/
assets.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2014-2016 Joseph Hager. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package engi
import (
"math"
"path"
"engo.io/audio"
"github.com/ajhager/webgl"
)
type Resource struct {
kind string
name string
url string
}
type Loader struct {
resources []Resource
images map[string]*Texture
jsons map[string]string
sounds map[string]*Sound
}
func NewLoader() *Loader {
return &Loader{
resources: make([]Resource, 1),
images: make(map[string]*Texture),
jsons: make(map[string]string),
sounds: make(map[string]*Sound),
}
}
func (l *Loader) Add(name, url string) {
kind := path.Ext(url)[1:]
l.resources = append(l.resources, Resource{kind, name, url})
}
func (l *Loader) Image(name string) *Texture {
return l.images[name]
}
func (l *Loader) Json(name string) string {
return l.jsons[name]
}
func (l *Loader) Sound(name string) *Sound {
return l.sounds[name]
}
func (l *Loader) Load(onFinish func()) {
for _, r := range l.resources {
switch r.kind {
case "png":
data, err := loadImage(r)
if err == nil {
l.images[r.name] = NewTexture(data)
}
case "json":
data, err := loadJson(r)
if err == nil {
l.jsons[r.name] = data
}
case "wav":
data, err := audio.NewSimplePlayer(r.url)
if err == nil {
l.sounds[r.name] = &Sound{data}
}
}
}
onFinish()
}
type Image interface {
Data() interface{}
Width() int
Height() int
}
func LoadShader(vertSrc, fragSrc string) *webgl.Program {
vertShader := gl.CreateShader(gl.VERTEX_SHADER)
gl.ShaderSource(vertShader, vertSrc)
gl.CompileShader(vertShader)
defer gl.DeleteShader(vertShader)
fragShader := gl.CreateShader(gl.FRAGMENT_SHADER)
gl.ShaderSource(fragShader, fragSrc)
gl.CompileShader(fragShader)
defer gl.DeleteShader(fragShader)
program := gl.CreateProgram()
gl.AttachShader(program, vertShader)
gl.AttachShader(program, fragShader)
gl.LinkProgram(program)
return program
}
type Region struct {
texture *Texture
u, v float32
u2, v2 float32
width, height float32
}
func NewRegion(texture *Texture, x, y, w, h int) *Region {
invTexWidth := 1.0 / float32(texture.Width())
invTexHeight := 1.0 / float32(texture.Height())
u := float32(x) * invTexWidth
v := float32(y) * invTexHeight
u2 := float32(x+w) * invTexWidth
v2 := float32(y+h) * invTexHeight
width := float32(math.Abs(float64(w)))
height := float32(math.Abs(float64(h)))
return &Region{texture, u, v, u2, v2, width, height}
}
func (r *Region) Width() float32 {
return float32(r.width)
}
func (r *Region) Height() float32 {
return float32(r.height)
}
func (r *Region) Texture() *webgl.Texture {
return r.texture.id
}
func (r *Region) View() (float32, float32, float32, float32) {
return r.u, r.v, r.u2, r.v2
}
type Texture struct {
id *webgl.Texture
width int
height int
}
func NewTexture(img Image) *Texture {
id := gl.CreateTexture()
gl.BindTexture(gl.TEXTURE_2D, id)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
if img.Data() == nil {
panic("Texture image data is nil.")
}
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img.Data())
return &Texture{id, img.Width(), img.Height()}
}
// Width returns the width of the texture.
func (t *Texture) Width() float32 {
return float32(t.width)
}
// Height returns the height of the texture.
func (t *Texture) Height() float32 {
return float32(t.height)
}
func (t *Texture) Texture() *webgl.Texture {
return t.id
}
func (r *Texture) View() (float32, float32, float32, float32) {
return 0.0, 0.0, 1.0, 1.0
}
type Sound struct {
*audio.Player
}
type Point struct {
X, Y float32
}
type Sprite struct {
Position *Point
Scale *Point
Anchor *Point
Rotation float32
Color uint32
Alpha float32
Region *Region
}
func NewSprite(region *Region, x, y float32) *Sprite {
return &Sprite{
Position: &Point{x, y},
Scale: &Point{1, 1},
Anchor: &Point{0, 0},
Rotation: 0,
Color: 0xffffff,
Alpha: 1,
Region: region,
}
}
func (s *Sprite) Render(batch *Batch) {
batch.Draw(s.Region, s.Position.X, s.Position.Y, s.Anchor.X, s.Anchor.Y, s.Scale.X, s.Scale.Y, s.Rotation, s.Color, s.Alpha)
}
func (s *Sprite) Width() float32 {
return s.Region.width * s.Scale.X
}
func (s *Sprite) Height() float32 {
return s.Region.height * s.Scale.Y
}
var batchVert = `
attribute vec2 in_Position;
attribute vec4 in_Color;
attribute vec2 in_TexCoords;
uniform vec2 uf_Projection;
varying vec4 var_Color;
varying vec2 var_TexCoords;
const vec2 center = vec2(-1.0, 1.0);
void main() {
var_Color = in_Color;
var_TexCoords = in_TexCoords;
gl_Position = vec4(in_Position.x / uf_Projection.x + center.x,
in_Position.y / -uf_Projection.y + center.y,
0.0, 1.0);
}`
var batchFrag = `
#ifdef GL_ES
#define LOWP lowp
precision mediump float;
#else
#define LOWP
#endif
varying vec4 var_Color;
varying vec2 var_TexCoords;
uniform sampler2D uf_Texture;
void main (void) {
gl_FragColor = var_Color * texture2D(uf_Texture, var_TexCoords);
}`