-
Notifications
You must be signed in to change notification settings - Fork 1
/
convexShape.go
290 lines (252 loc) · 9.48 KB
/
convexShape.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright (C) 2012-2014 by krepa098. All rights reserved.
// Use of this source code is governed by a zlib-style
// license that can be found in the license.txt file.
package gosfml2
// #include <SFML/Graphics/ConvexShape.h>
// #include <SFML/Graphics/RenderWindow.h>
// #include <SFML/Graphics/RenderTexture.h>
import "C"
import "runtime"
/////////////////////////////////////
/// STRUCTS
/////////////////////////////////////
type ConvexShape struct {
cptr *C.sfConvexShape
texture *Texture //to prevent the GC from deleting the texture
}
/////////////////////////////////////
/// FUNCS
/////////////////////////////////////
func NewConvexShape() (*ConvexShape, error) {
if cptr := C.sfConvexShape_create(); cptr != nil {
shape := &ConvexShape{cptr, nil}
runtime.SetFinalizer(shape, (*ConvexShape).destroy)
return shape, nil
}
return nil, genericError
}
//Copy an existing convex shape
func (this *ConvexShape) Copy() *ConvexShape {
shape := &ConvexShape{C.sfConvexShape_copy(this.cptr), this.texture}
runtime.SetFinalizer(shape, (*ConvexShape).destroy)
return shape
}
func (this *ConvexShape) destroy() {
C.sfConvexShape_destroy(this.cptr)
}
// Set the position of a convex shape
//
// This function completely overwrites the previous position.
// See sfConvexShape_move to apply an offset based on the previous position instead.
// The default position of a circle Shape object is (0, 0).
func (this *ConvexShape) SetPosition(pos Vector2f) {
C.sfConvexShape_setPosition(this.cptr, pos.toC())
}
// Set the local origin of a convex shape
//
// The origin of an object defines the center point for
// all transformations (position, scale, rotation).
// The coordinates of this point must be relative to the
// top-left corner of the object, and ignore all
// transformations (position, scale, rotation).
// The default origin of a circle Shape object is (0, 0).
func (this *ConvexShape) SetScale(scale Vector2f) {
C.sfConvexShape_setScale(this.cptr, scale.toC())
}
// Set the local origin of a convex shape
//
// The origin of an object defines the center point for
// all transformations (position, scale, rotation).
// The coordinates of this point must be relative to the
// top-left corner of the object, and ignore all
// transformations (position, scale, rotation).
// The default origin of a circle Shape object is (0, 0).
func (this *ConvexShape) SetOrigin(orig Vector2f) {
C.sfConvexShape_setOrigin(this.cptr, orig.toC())
}
// Set the scale factors of a convex shape
//
// This function completely overwrites the previous scale.
// See sfConvexShape_scale to add a factor based on the previous scale instead.
// The default scale of a circle Shape object is (1, 1).
func (this *ConvexShape) SetRotation(rot float32) {
C.sfConvexShape_setRotation(this.cptr, C.float(rot))
}
// Get the orientation of a convex shape
func (this *ConvexShape) GetRotation() float32 {
return float32(C.sfConvexShape_getRotation(this.cptr))
}
// Get the position of a convex shape
func (this *ConvexShape) GetPosition() (position Vector2f) {
position.fromC(C.sfConvexShape_getPosition(this.cptr))
return
}
// Get the current scale of a convex shape
func (this *ConvexShape) GetScale() (scale Vector2f) {
scale.fromC(C.sfConvexShape_getScale(this.cptr))
return
}
// Get the local origin of a convex shape
func (this *ConvexShape) GetOrigin() (origin Vector2f) {
origin.fromC(C.sfConvexShape_getOrigin(this.cptr))
return
}
// Move a convex shape by a given offset
//
// This function adds to the current position of the object,
// unlike ConvexShape.SetPosition which overwrites it.
func (this *ConvexShape) Move(offset Vector2f) {
C.sfConvexShape_move(this.cptr, offset.toC())
}
// Scale a convex shape
//
// This function multiplies the current scale of the object,
// unlike ConvexShape.SetScale which overwrites it.
func (this *ConvexShape) Scale(factor Vector2f) {
C.sfConvexShape_scale(this.cptr, factor.toC())
}
// Rotate a convex shape
//
// This function adds to the current rotation of the object,
// unlike ConvexShape.SetRotation which overwrites it.
func (this *ConvexShape) Rotate(angle float32) {
C.sfConvexShape_rotate(this.cptr, C.float(angle))
}
// Change the source texture of a convex shape
//
// The texture argument can be nil to disable texturing.
// If resetRect is true, the TextureRect property of
// the shape is automatically adjusted to the size of the new
// texture. If it is false, the texture rect is left unchanged.
func (this *ConvexShape) SetTexture(texture *Texture, resetRect bool) {
C.sfConvexShape_setTexture(this.cptr, texture.toCPtr(), goBool2C(resetRect))
this.texture = texture
}
// Set the sub-rectangle of the texture that a convex shape will display
//
// The texture rect is useful when you don't want to display
// the whole texture, but rather a part of it.
// By default, the texture rect covers the entire texture.
func (this *ConvexShape) SetTextureRect(rect IntRect) {
C.sfConvexShape_setTextureRect(this.cptr, rect.toC())
}
// Set the fill color of a convex shape
//
// This color is modulated (multiplied) with the shape's
// texture if any. It can be used to colorize the shape,
// or change its global opacity.
// You can use sfTransparent to make the inside of
// the shape transparent, and have the outline alone.
// By default, the shape's fill color is opaque white.
func (this *ConvexShape) SetFillColor(color Color) {
C.sfConvexShape_setFillColor(this.cptr, color.toC())
}
// Set the outline color of a convex shape
//
// You can use sfTransparent to disable the outline.
// By default, the shape's outline color is opaque white.
func (this *ConvexShape) SetOutlineColor(color Color) {
C.sfConvexShape_setOutlineColor(this.cptr, color.toC())
}
// Set the thickness of a convex shape's outline
//
// This number cannot be negative. Using zero disables
// the outline.
// By default, the outline thickness is 0.
func (this *ConvexShape) SetOutlineThickness(thickness float32) {
C.sfConvexShape_setOutlineThickness(this.cptr, C.float(thickness))
}
// Get the source texture of a convex shape
//
// If the shape has no source texture, a nil pointer is returned.
func (this *ConvexShape) GetTexture() *Texture {
return this.texture
}
// Get the sub-rectangle of the texture displayed by a convex shape
func (this *ConvexShape) GetTextureRect() (rect IntRect) {
rect.fromC(C.sfConvexShape_getTextureRect(this.cptr))
return
}
// Get the combined transform of a convex shape
func (this *ConvexShape) GetTransform() (transform Transform) {
transform.fromC(C.sfConvexShape_getTransform(this.cptr))
return
}
// Get the inverse of the combined transform of a convex shape
func (this *ConvexShape) GetInverseTransform() (transform Transform) {
transform.fromC(C.sfConvexShape_getInverseTransform(this.cptr))
return
}
// Get the fill color of a convex shape
func (this *ConvexShape) GetFillColor() (color Color) {
color.fromC(C.sfConvexShape_getFillColor(this.cptr))
return
}
// Get the outline color of a convex shape
func (this *ConvexShape) GetOutlineColor() (color Color) {
color.fromC(C.sfConvexShape_getOutlineColor(this.cptr))
return
}
// Get the outline thickness of a convex shape
func (this *ConvexShape) GetOutlineThickness() float32 {
return float32(C.sfConvexShape_getOutlineThickness(this.cptr))
}
// Get the total number of points of a convex shape
func (this *ConvexShape) GetPointCount() uint {
return uint(C.sfConvexShape_getPointCount(this.cptr))
}
// Get a point of a convex shape
//
// The result is undefined if index is out of the valid range.
func (this *ConvexShape) GetPoint(index uint) (point Vector2f) {
point.fromC(C.sfConvexShape_getPoint(this.cptr, C.size_t(index)))
return
}
// Set the number of points of a convex shap
//
// count must be greater than 2 to define a valid shape.
func (this *ConvexShape) SetPointCount(count uint) {
C.sfConvexShape_setPointCount(this.cptr, C.size_t(count))
}
// Set the position of a point in a convex shape
//
// Don't forget that the polygon must remain convex, and
// the points need to stay ordered!
// SetPointCount must be called first in order to set the total
// number of points. The result is undefined if index is out
// of the valid range.
func (this *ConvexShape) SetPoint(index uint, point Vector2f) {
C.sfConvexShape_setPoint(this.cptr, C.size_t(index), point.toC())
}
// Get the local bounding rectangle of a convex shape
//
// The returned rectangle is in local coordinates, which means
// that it ignores the transformations (translation, rotation,
// scale, ...) that are applied to the entity.
// In other words, this function returns the bounds of the
// entity in the entity's coordinate system.
func (this *ConvexShape) GetLocalBounds() (rect FloatRect) {
rect.fromC(C.sfConvexShape_getLocalBounds(this.cptr))
return
}
// Get the global bounding rectangle of a convex shape
//
// The returned rectangle is in global coordinates, which means
// that it takes in account the transformations (translation,
// rotation, scale, ...) that are applied to the entity.
// In other words, this function returns the bounds of the
// sprite in the global 2D world's coordinate system.
func (this *ConvexShape) GetGlobalBounds() (rect FloatRect) {
rect.fromC(C.sfConvexShape_getGlobalBounds(this.cptr))
return
}
// Draws a convex Shape on a render target
func (this *ConvexShape) Draw(target RenderTarget, renderStates RenderStates) {
rs := renderStates.toC()
switch target.(type) {
case *RenderWindow:
C.sfRenderWindow_drawConvexShape(target.(*RenderWindow).cptr, this.cptr, &rs)
case *RenderTexture:
C.sfRenderTexture_drawConvexShape(target.(*RenderTexture).cptr, this.cptr, &rs)
}
}