-
Notifications
You must be signed in to change notification settings - Fork 1
/
surface.go
202 lines (177 loc) · 4.53 KB
/
surface.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
package gofb
import (
"errors"
"image"
"image/draw"
"os"
"github.com/go-gl/gl/v2.1/gl"
)
// Surface represent pixel buffer
type Surface struct {
Width int
Height int
Scale int
Rotation float32
FlipHorizontal bool
FlipVertical bool
pixels *[]byte
texture *Texture
needsUpdate bool
}
// NewSurface create new empty surface
func NewSurface(width int, height int) *Surface {
pixels := make([]byte, width*height*4)
tex := NewTextureFromBytes(width, height, &pixels)
return &Surface{
Width: width,
Height: height,
Scale: 1,
FlipHorizontal: false,
FlipVertical: false,
pixels: &pixels,
texture: tex,
needsUpdate: false,
}
}
// NewSurfaceFromBytes create new surface from given input byte array (expecting RGBA format)
func NewSurfaceFromBytes(width int, height int, bytes *[]byte) *Surface {
tex := NewTextureFromBytes(width, height, bytes)
return &Surface{
Width: width,
Height: height,
Scale: 1,
Rotation: 0,
pixels: bytes,
texture: tex,
needsUpdate: false,
}
}
// NewSurfaceFromFile create surface from image file
func NewSurfaceFromFile(file string) (*Surface, error) {
imgFile, err := os.Open(file)
if err != nil {
return nil, err
}
img, _, err := image.Decode(imgFile)
if err != nil {
return nil, err
}
rgba := image.NewRGBA(img.Bounds())
if rgba.Stride != rgba.Rect.Size().X*4 {
return nil, errors.New("unsupported stride")
}
draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src)
return NewSurfaceFromBytes(rgba.Rect.Size().X, rgba.Rect.Size().Y, &rgba.Pix), nil
}
// IsInsidev check if given point is inside surface
func (s *Surface) IsInsidev(v Vec2) bool {
return s.IsInside(v.X, v.Y)
}
// IsInside check if given point is inside surface
func (s *Surface) IsInside(x, y int) bool {
return x > 0 && x < s.Width && y > 0 && y < s.Height
}
// SetPixelv draw pixel at surface
func (s *Surface) SetPixelv(v Vec2, c Color) {
s.SetPixel(v.X, v.Y, c)
}
// SetPixel draw pixel at surface
func (s *Surface) SetPixel(x int, y int, c Color) {
i := (y*s.Width + x) * 4
p := *s.pixels
p[i+0] = c.R
p[i+1] = c.G
p[i+2] = c.B
p[i+3] = c.A
s.needsUpdate = true
}
// GetPixelv return color of pixel
func (s *Surface) GetPixelv(v Vec2) Color {
return s.GetPixel(v.X, v.Y)
}
// GetPixel return color of pixel
func (s *Surface) GetPixel(x int, y int) Color {
i := (y*s.Width + x) * 4
p := *s.pixels
return Color{
R: p[i+0],
G: p[i+1],
B: p[i+2],
A: p[i+3],
}
}
// Clear surface with provided color
func (s *Surface) Clear(c Color) {
for i := 0; i < len(*s.pixels); i += 4 {
(*s.pixels)[i+0] = c.R
(*s.pixels)[i+1] = c.G
(*s.pixels)[i+2] = c.B
(*s.pixels)[i+3] = c.A
}
}
// Drawv surface on the screen
func (s *Surface) Drawv(v Vec2) {
s.draw(
v,
NewVec2(s.Width*s.Scale, s.Height*s.Scale),
NewVec2f(0, 0),
NewVec2f(1, 1),
s.texture,
)
}
// Draw surface on the screen
func (s *Surface) Draw(x int, y int) {
s.Drawv(NewVec2(x, y))
}
// DrawRegionv draw region of surface on the screen
func (s *Surface) DrawRegionv(v Vec2, r Region) {
s.draw(
v,
NewVec2(r.W*s.Scale, r.H*s.Scale),
NewVec2f(float64(r.X)/float64(s.Width), float64(r.Y)/float64(s.Height)),
NewVec2f(float64(r.X+r.W)/float64(s.Width), float64(r.Y+r.H)/float64(s.Height)),
s.texture,
)
}
// DrawRegion draw region of surface on the screen
func (s *Surface) DrawRegion(x int, y int, r Region) {
s.DrawRegionv(NewVec2(x, y), r)
}
// Release surface from memory and gpu
func (s *Surface) Release() {
s.pixels = nil
s.texture.Release()
}
func (s *Surface) update() {
s.texture.Update(s.Width, s.Height, s.pixels)
}
func (s *Surface) draw(pos Vec2, size Vec2, t1 Vec2f, t2 Vec2f, tex *Texture) {
if s.needsUpdate {
s.update()
s.needsUpdate = false
}
if s.FlipHorizontal {
t1.X, t2.X = t2.X, t1.X
}
if s.FlipVertical {
t1.Y, t2.Y = t2.Y, t1.Y
}
gl.Enable(gl.TEXTURE_2D)
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
tex.Bind()
gl.LoadIdentity()
gl.Translatef(pos.floatX()+size.floatX()/2, pos.floatY()+size.floatY()/2, 0)
gl.Rotatef(s.Rotation, 0, 0, 1)
ColorWhite.glColor()
gl.Begin(gl.QUADS)
gl.TexCoord2f(t1.floatX(), t1.floatY())
gl.Vertex2f(-size.floatX()/2, -size.floatY()/2)
gl.TexCoord2f(t2.floatX(), t1.floatY())
gl.Vertex2f(size.floatX()/2, -size.floatY()/2)
gl.TexCoord2f(t2.floatX(), t2.floatY())
gl.Vertex2f(size.floatX()/2, size.floatY()/2)
gl.TexCoord2f(t1.floatX(), t2.floatY())
gl.Vertex2f(-size.floatX()/2, size.floatY()/2)
gl.End()
}