code.witches.io/go/sdl2@v0.1.1/rect.go (about) 1 package sdl 2 3 //#include <SDL2/SDL_rect.h> 4 import "C" 5 import ( 6 "unsafe" 7 8 "code.witches.io/go/sdl2/internal" 9 ) 10 11 type Rect struct { 12 X, Y, W, H int 13 } 14 15 func (r *Rect) fromInternal(rect internal.Rect) { 16 r.X = int(rect.X) 17 r.Y = int(rect.Y) 18 r.W = int(rect.W) 19 r.H = int(rect.H) 20 } 21 22 func (r *Rect) toInternal() (rect *internal.Rect) { 23 if r == nil { 24 return nil 25 } 26 rect = new(internal.Rect) 27 rect.X = int32(r.X) 28 rect.Y = int32(r.Y) 29 rect.W = int32(r.W) 30 rect.H = int32(r.H) 31 return rect 32 } 33 34 type Point struct { 35 X, Y int 36 } 37 38 func (p *Point) fromInternal(point internal.Point) { 39 p.X = int(point.X) 40 p.Y = int(point.Y) 41 } 42 43 func (p *Point) toInternal() (point *internal.Point) { 44 if p == nil { 45 return nil 46 } 47 point = new(internal.Point) 48 point.X = int32(p.X) 49 point.Y = int32(p.Y) 50 return point 51 } 52 53 func PointInRect(point Point, rect Rect) bool { 54 return C.SDL_PointInRect((*C.struct_SDL_Point)(unsafe.Pointer(point.toInternal())), (*C.struct_SDL_Rect)(unsafe.Pointer(rect.toInternal()))) == C.SDL_TRUE 55 } 56 57 func HasIntersection(a, b Rect) bool { 58 return C.SDL_HasIntersection((*C.struct_SDL_Rect)(unsafe.Pointer(a.toInternal())), (*C.struct_SDL_Rect)(unsafe.Pointer(b.toInternal()))) == C.SDL_TRUE 59 } 60 61 func IntersectRect(a, b Rect) (Rect, bool) { 62 var internalRect internal.Rect 63 r := C.SDL_IntersectRect((*C.struct_SDL_Rect)(unsafe.Pointer(a.toInternal())), (*C.struct_SDL_Rect)(unsafe.Pointer(b.toInternal())), (*C.struct_SDL_Rect)(unsafe.Pointer(&internalRect))) == C.SDL_TRUE 64 if !r { 65 return Rect{}, false 66 } 67 var rect Rect 68 rect.fromInternal(internalRect) 69 return rect, true 70 }