-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
140 lines (123 loc) · 4.2 KB
/
utils.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
package main
import (
"image"
"image/draw"
"log"
"os"
"github.com/go-gl/gl/v2.1/gl"
"github.com/paulmach/go.geo"
)
// NewTexture loads texture from file.
func NewTexture(file string) (texture uint32, bounds Rect) {
imgFile, err := os.Open(file)
if err != nil {
log.Fatalf("texture %q not found on disk: %v\n", file, err)
}
img, _, err := image.Decode(imgFile)
if err != nil {
panic(err)
}
rgba := image.NewRGBA(img.Bounds())
if rgba.Stride != rgba.Rect.Size().X*4 {
panic("unsupported stride")
}
draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src)
//var texture uint32
// enable server-side GL capabilities
gl.Enable(gl.TEXTURE_2D)
// generate texture names
gl.GenTextures(1, &texture)
// bind a named texture to a texturing target
gl.BindTexture(gl.TEXTURE_2D, texture)
// set texture parameters
// texture minifying function
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
// texture magnification function
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
// set the wrap parameter for texture coordinate s (x dimension)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
// set the wrap parameter for texture coordinate t (y dimension)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
// set the wrap parameter for texture coordinate r (z dimension)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE)
// specify a two-dimensional texture image
gl.TexImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
int32(rgba.Rect.Size().X),
int32(rgba.Rect.Size().Y),
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
gl.Ptr(rgba.Pix))
// disable server-side GL capabilities
gl.Disable(gl.TEXTURE_2D)
// translate image size into game units
imgWidth := float32(img.Bounds().Max.X) / 100.0
imgHeight := float32(img.Bounds().Max.Y) / 100.0
bounds = Rect{
Left: Point{X: float32(0), Y: float32(0)},
Right: Point{X: imgWidth, Y: imgHeight},
}
return texture, bounds
}
// DrawTexture draws texture into rectange.
func DrawTexture(texture uint32, dst Rect) {
gl.ActiveTexture(gl.TEXTURE0)
gl.Enable(gl.TEXTURE_2D)
gl.BindTexture(gl.TEXTURE_2D, texture)
gl.Color4f(1, 1, 1, 1)
gl.Begin(gl.QUADS)
gl.TexCoord2f(0, 1)
gl.Vertex2f(dst.Left.X, dst.Left.Y)
gl.TexCoord2f(1, 1)
gl.Vertex2f(dst.Right.X, dst.Left.Y)
gl.TexCoord2f(1, 0)
gl.Vertex2f(dst.Right.X, dst.Right.Y)
gl.TexCoord2f(0, 0)
gl.Vertex2f(dst.Left.X, dst.Right.Y)
gl.End()
gl.Disable(gl.TEXTURE_2D)
}
// CheckBoundaries checks violation of boundaries.
func CheckBoundaries(rect Rect, boundaries ...Rect) (violated bool, violatedBound Rect) {
for _, bound := range boundaries {
p1 := geo.NewPath()
pp := []geo.Point{
geo.Point{float64(rect.Left.X), float64(rect.Left.Y)}, geo.Point{float64(rect.Right.X), float64(rect.Left.Y)}, // top
geo.Point{float64(rect.Right.X), float64(rect.Left.Y)}, geo.Point{float64(rect.Right.X), float64(rect.Right.Y)}, // right
geo.Point{float64(rect.Right.X), float64(rect.Right.Y)}, geo.Point{float64(rect.Left.X), float64(rect.Right.Y)}, // bottom
geo.Point{float64(rect.Left.X), float64(rect.Right.Y)}, geo.Point{float64(rect.Left.X), float64(rect.Left.Y)}, // left
}
p1.SetPoints(pp)
p2 := geo.NewPath()
pp = []geo.Point{
geo.Point{float64(bound.Left.X), float64(bound.Left.Y)}, geo.Point{float64(bound.Right.X), float64(bound.Left.Y)}, // top
geo.Point{float64(bound.Right.X), float64(bound.Left.Y)}, geo.Point{float64(bound.Right.X), float64(bound.Right.Y)}, // right
geo.Point{float64(bound.Right.X), float64(bound.Right.Y)}, geo.Point{float64(bound.Left.X), float64(bound.Right.Y)}, // bottom
geo.Point{float64(bound.Left.X), float64(bound.Right.Y)}, geo.Point{float64(bound.Left.X), float64(bound.Left.Y)}, // left
}
p2.SetPoints(pp)
if p1.Intersects(p2) {
return true, bound
}
}
return false, Rect{}
}
// IntToDigits returns array of digits from number
func IntToDigits(number int64) []int {
digits := make([]int, 0)
if number == 0 {
digits = append(digits, 0)
}
for number != 0 {
digit := number % 10
number = number / 10
digits = append(digits, int(digit))
}
for i, j := 0, len(digits)-1; i < j; i, j = i+1, j-1 {
digits[i], digits[j] = digits[j], digits[i]
}
return digits
}