/
drawing_test.go
70 lines (48 loc) · 1.3 KB
/
drawing_test.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
package sprite
import (
"github.com/go-gl-legacy/gl"
glfw "github.com/go-gl/glfw3"
"github.com/joonazan/vec2"
"image"
"testing"
)
type Testbed struct {
window *glfw.Window
image *image.NRGBA
}
func NewTestbed(title string) *Testbed {
t := new(Testbed)
t.window = OpenWindow(500, 500, title)
glfw.SwapInterval(1)
t.image = LoadPNG("testimage.png").(*image.NRGBA)
return t
}
func (t *Testbed) UntilClose(drawingFunction func()) {
for !t.window.ShouldClose() {
glfw.PollEvents()
gl.Clear(gl.COLOR_BUFFER_BIT)
drawingFunction()
t.window.SwapBuffers()
}
t.window.Destroy()
}
func TestWindow(_ *testing.T) {
t := NewTestbed("displays test image")
t.UntilClose(func() {
gl.DrawPixels(t.image.Bounds().Dx(), t.image.Bounds().Dy(), gl.RGBA, gl.UNSIGNED_BYTE, t.image.Pix)
})
}
func TestSpriteDrawer(_ *testing.T) {
t := NewTestbed("displays rotating test image")
spritedrawer := NewSpriteDrawer(t.window, 1)
texcoord := Upload(t.image, spritedrawer.Texture, 0, 0, 0)
sprite := Sprite{Image: texcoord}
x := 0.0
t.UntilClose(func() {
transform := vec2.Translation(vec2.Vector{-float64(t.image.Rect.Max.X) / 2, -float64(t.image.Rect.Max.Y) / 2})
transform = vec2.Rotation(x).Mul(transform)
x += 0.01
sprite.Transform = transform.To32()
spritedrawer.Draw([]Sprite{sprite})
})
}