forked from drhodes/go-sfml
/
gfx-sprite.go
281 lines (253 loc) · 10 KB
/
gfx-sprite.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
package sfml
// #cgo LDFLAGS:-lcsfml-graphics
// #include <SFML/Graphics/Export.h>
// #include <SFML/Graphics/BlendMode.h>
// #include <SFML/Graphics/Color.h>
// #include <SFML/Graphics/Rect.h>
// #include <SFML/Graphics/Types.h>
// #include <SFML/Graphics/Sprite.h>
// #include <SFML/System/Vector2.h>
import "C"
import "errors"
type Sprite struct {
Cref *C.sfSprite
}
// Create a new sprite
// \return A new sfSprite object, or NULL if it failed
// sfSprite* sfSprite_create(void);
func NewSprite() (Sprite, error) {
spr := C.sfSprite_create()
if spr == nil {
return Sprite{nil}, errors.New("Couldn't make a sprite")
}
return Sprite{spr}, nil
}
// Copy an existing sprite
// \param sprite Sprite to copy
// \return Copied object
// sfSprite* sfSprite_copy(sfSprite* sprite);
func (self Sprite) Copy() Sprite {
return Sprite{C.sfSprite_copy(self.Cref)}
}
// Destroy an existing sprite
// \param sprite Sprite to delete
// void sfSprite_destroy(sfSprite* sprite);
func (self Sprite) Destroy() {
C.sfSprite_destroy(self.Cref)
}
// Set the position of a sprite
// This function completely overwrites the previous position.
// See sfSprite_move to apply an offset based on the previous position instead.
// The default position of a sprite Sprite object is (0, 0).
// \param sprite Sprite object
// \param position New position
// void sfSprite_setPosition(sfSprite* sprite, sfVector2f position);
func (self Sprite) SetPosition(x, y float32) {
v := C.sfVector2f{C.float(x), C.float(y)}
C.sfSprite_setPosition(self.Cref, v)
}
// Set the orientation of a sprite
// This function completely overwrites the previous rotation.
// See sfSprite_rotate to add an angle based on the previous rotation instead.
// The default rotation of a sprite Sprite object is 0.
// \param sprite Sprite object
// \param angle New rotation, in degrees
// void sfSprite_setRotation(sfSprite* sprite, float angle);
func (self Sprite) SetRotation(angle float32) {
C.sfSprite_setRotation(self.Cref, C.float(angle))
}
// Set the scale factors of a sprite
// This function completely overwrites the previous scale.
// See sfSprite_scale to add a factor based on the previous scale instead.
// The default scale of a sprite Sprite object is (1, 1).
// \param sprite Sprite object
// \param scale New scale factors
// void sfSprite_setScale(sfSprite* sprite, sfVector2f scale);
func (self Sprite) SetScale(x, y float32) {
v := C.sfVector2f{C.float(x), C.float(y)}
C.sfSprite_setScale(self.Cref, v)
}
// Set the local origin of a sprite
// 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 sprite Sprite object is (0, 0).
// \param sprite Sprite object
// \param origin New origin
// void sfSprite_setOrigin(sfSprite* sprite, sfVector2f origin);
func (self Sprite) SetOrigin(x, y float32) {
v := C.sfVector2f{C.float(x), C.float(y)}
C.sfSprite_setOrigin(self.Cref, v)
}
// Get the position of a sprite
// \param sprite Sprite object
// \return Current position
// sfVector2f sfSprite_getPosition(const sfSprite* sprite);
func (self Sprite) Position() (float32, float32) {
v := C.sfSprite_getPosition(self.Cref)
return float32(v.x), float32(v.y)
}
// Get the orientation of a sprite
// The rotation is always in the range [0, 360].
// \param sprite Sprite object
// \return Current rotation, in degrees
// float sfSprite_getRotation(const sfSprite* sprite);
func (self Sprite) Rotation() float32 {
return float32(C.sfSprite_getRotation(self.Cref))
}
// Get the current scale of a sprite
// \param sprite Sprite object
// \return Current scale factors
// sfVector2f sfSprite_getScale(const sfSprite* sprite);
func (self Sprite) GetScale() (float32, float32) {
v := C.sfSprite_getScale(self.Cref)
return float32(v.x), float32(v.y)
}
// Get the local origin of a sprite
// \param sprite Sprite object
// \return Current origin
// sfVector2f sfSprite_getOrigin(const sfSprite* sprite);
func (self Sprite) Origin() (float32, float32) {
v := C.sfSprite_getOrigin(self.Cref)
return float32(v.x), float32(v.y)
}
// Move a sprite by a given offset
// This function adds to the current position of the object,
// unlike sfSprite_setPosition which overwrites it.
// \param sprite Sprite object
// \param offset Offset
// void sfSprite_move(sfSprite* sprite, sfVector2f offset);
func (self Sprite) Move(x, y float32) {
offset := C.sfVector2f{C.float(x), C.float(y)}
C.sfSprite_move(self.Cref, offset)
}
// Rotate a sprite
// This function adds to the current rotation of the object,
// unlike sfSprite_setRotation which overwrites it.
// \param sprite Sprite object
// \param angle Angle of rotation, in degrees
// void sfSprite_rotate(sfSprite* sprite, float angle);
func (self Sprite) Rotate(angle float32) {
C.sfSprite_rotate(self.Cref, C.float(angle))
}
// Scale a sprite
// This function multiplies the current scale of the object,
// unlike sfSprite_setScale which overwrites it.
// \param sprite Sprite object
// \param factors Scale factors
// void sfSprite_scale(sfSprite* sprite, sfVector2f factors);
func (self Sprite) Scale(x, y float32) {
v := C.sfVector2f{C.float(x), C.float(y)}
C.sfSprite_scale(self.Cref, v)
}
// Get the combined transform of a sprite
// \param sprite Sprite object
// \return Transform combining the position/rotation/scale/origin of the object
// const sfTransform* sfSprite_getTransform(const sfSprite* sprite);
func (self Sprite) Transform() Transform {
return Transform{C.sfSprite_getTransform(self.Cref)}
}
// Get the inverse of the combined transform of a sprite
// \param sprite Sprite object
// \return Inverse of the combined transformations applied to the object
// const sfTransform* sfSprite_getInverseTransform(const sfSprite* sprite);
func (self Sprite) InverseTransform() Transform {
return Transform{C.sfSprite_getInverseTransform(self.Cref)}
}
// Change the source texture of a sprite
// The \a texture argument refers to a texture that must
// exist as long as the sprite uses it. Indeed, the sprite
// doesn't store its own copy of the texture, but rather keeps
// a pointer to the one that you passed to this function.
// If the source texture is destroyed and the sprite tries to
// use it, the behaviour is undefined.
// If \a resetRect is true, the TextureRect property of
// the sprite is automatically adjusted to the size of the new
// texture. If it is false, the texture rect is left unchanged.
// \param sprite Sprite object
// \param texture New texture
// \param resetRect Should the texture rect be reset to the size of the new texture?
// void sfSprite_setTexture(sfSprite* sprite, const sfTexture* texture, sfBool resetRect);
func (self Sprite) SetTexture(tex Texture, resetRect bool) {
C.sfSprite_setTexture(self.Cref, tex.Cref, Bool(resetRect))
}
// Set the sub-rectangle of the texture that a sprite 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.
// \param sprite Sprite object
// \param rectangle Rectangle defining the region of the texture to display
// void sfSprite_setTextureRect(sfSprite* sprite, sfIntRect rectangle);
func (self Sprite) SetTextureRect(rect IntRect) {
C.sfSprite_setTextureRect(self.Cref, *rect.Cref)
}
// Set the global color of a sprite
// This color is modulated (multiplied) with the sprite's
// texture. It can be used to colorize the sprite, or change
// its global opacity.
// By default, the sprite's color is opaque white.
// \param sprite Sprite object
// \param color New color of the sprite
// void sfSprite_setColor(sfSprite* sprite, sfColor color);
func (self Sprite) SetColor(color Color) {
C.sfSprite_setColor(self.Cref, color.Cref)
}
// Get the source texture of a sprite
// If the sprite has no source texture, a NULL pointer is returned.
// The returned pointer is const, which means that you can't
// modify the texture when you retrieve it with this function.
// \param sprite Sprite object
// \return Pointer to the sprite's texture
// const sfTexture* sfSprite_getTexture(const sfSprite* sprite);
func (self Sprite) Texture() Texture {
return Texture{C.sfSprite_getTexture(self.Cref)}
}
// Get the sub-rectangle of the texture displayed by a sprite
// \param sprite Sprite object
// \return Texture rectangle of the sprite
// sfIntRect sfSprite_getTextureRect(const sfSprite* sprite);
func (self Sprite) TextureRect() IntRect {
ref := C.sfSprite_getTextureRect(self.Cref)
return IntRect{&ref}
}
// Get the global color of a sprite
// \param sprite Sprite object
// \return Global color of the sprite
// sfColor sfSprite_getColor(const sfSprite* sprite);
func (self Sprite) Color() Color {
return Color{C.sfSprite_getColor(self.Cref)}
}
// Get the local bounding rectangle of a sprite
// 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.
// \param sprite Sprite object
// \return Local bounding rectangle of the entity
// sfFloatRect sfSprite_getLocalBounds(const sfSprite* sprite);
func (self Sprite) LocalBounds() FloatRect {
ref := C.sfSprite_getLocalBounds(self.Cref)
return FloatRect{&ref}
}
// Get the global bounding rectangle of a sprite
// 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.
// \param sprite Sprite object
// \return Global bounding rectangle of the entity
// sfFloatRect sfSprite_getGlobalBounds(const sfSprite* sprite);
func (self Sprite) GlobalBounds() FloatRect {
ref := C.sfSprite_getGlobalBounds(self.Cref)
return FloatRect{&ref}
}
func Bool(b bool) C.sfBool {
if b {
return C.sfBool(1)
}
return C.sfBool(0)
}