forked from drhodes/go-sfml
/
gfx-rect.go
125 lines (103 loc) · 3.33 KB
/
gfx-rect.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
package sfml
// #cgo LDFLAGS:-lcsfml-graphics
// #include <SFML/Graphics/Export.h>
// #include <SFML/Graphics/Rect.h>
import "C"
import "fmt"
// sfFloatRect and sfIntRect are utility classes for
// manipulating rectangles.
// \brief Check if a point is inside a rectangle's area
//
// \param rect Rectangle to test
// \param x X coordinate of the point to test
// \param y Y coordinate of the point to test
//
// \return sfTrue if the point is inside
//
// sfBool sfFloatRect_contains(const sfFloatRect* rect, float x, float y);
type IntRect struct {
Cref *C.sfIntRect
}
func NewIntRect(left, top, width, height int32) IntRect {
fr := C.sfIntRect{
C.int(left),
C.int(top),
C.int(width),
C.int(height),
}
return IntRect{&fr}
}
// brief Check intersection between two rectangles
// param rect1 First rectangle to test
// param rect2 Second rectangle to test
// param intersection Rectangle to be filled with overlapping rect (can be NULL)
// return sfTrue if rectangles overlap
// sfBool sfIntRect_intersects(const sfIntRect* rect1, const sfIntRect* rect2, sfIntRect* // intersection);
func (self IntRect) Intersects(rect IntRect) (*IntRect, bool) {
intersect := new(C.sfIntRect)
b := C.sfIntRect_intersects(self.Cref, rect.Cref, intersect) == 1
return &IntRect{intersect}, b
}
func (self IntRect) Left() int32 {
return int32(self.Cref.left)
}
func (self IntRect) Top() int32 {
return int32(self.Cref.top)
}
func (self IntRect) Width() int32 {
return int32(self.Cref.width)
}
func (self IntRect) Height() int32 {
return int32(self.Cref.height)
}
func (self IntRect) Contains(x, y int32) bool {
return C.sfIntRect_contains(self.Cref, C.int(x), C.int(y)) == 1
}
func (self IntRect) Show() string {
return fmt.Sprintf("<left: %d, top %d, width %d, height %d>",
self.Cref.left, self.Cref.top, self.Cref.width, self.Cref.height)
}
// ------------------------------------------------------------------
type FloatRect struct {
Cref *C.sfFloatRect
}
func NewFloatRect(left, top, width, height float32) FloatRect {
fr := C.sfFloatRect{
C.float(left),
C.float(top),
C.float(width),
C.float(height),
}
//func SetFinalizer(x, f interface{})
return FloatRect{&fr}
}
func (self FloatRect) Left() float32 {
return float32(self.Cref.left)
}
func (self FloatRect) Top() float32 {
return float32(self.Cref.top)
}
func (self FloatRect) Width() float32 {
return float32(self.Cref.width)
}
func (self FloatRect) Height() float32 {
return float32(self.Cref.height)
}
func (self FloatRect) Contains(x, y float32) bool {
return C.sfFloatRect_contains(self.Cref, C.float(x), C.float(y)) == 1
}
func (self FloatRect) Show() string {
return fmt.Sprintf("<left: %f, top %f, width %f, height %f>",
self.Cref.left, self.Cref.top, self.Cref.width, self.Cref.height)
}
// brief Check intersection between two rectangles
// param rect1 First rectangle to test
// param rect2 Second rectangle to test
// param intersection Rectangle to be filled with overlapping rect (can be NULL)
// return sfTrue if rectangles overlap
// sfBool sfFloatRect_intersects(const sfFloatRect* rect1, const sfFloatRect* rect2, sfFloatRect* // intersection);
func (self FloatRect) Intersects(rect FloatRect) (*FloatRect, bool) {
intersect := new(C.sfFloatRect)
b := C.sfFloatRect_intersects(self.Cref, rect.Cref, intersect) == 1
return &FloatRect{intersect}, b
}