Skip to content

Commit a4b7dc7

Browse files
author
Allen Ray
committed
Add purego support for unix
1 parent 83d871a commit a4b7dc7

File tree

7 files changed

+64
-12
lines changed

7 files changed

+64
-12
lines changed

raylib/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module github.com/gen2brain/raylib-go/raylib
33
go 1.21
44

55
require (
6-
github.com/ebitengine/purego v0.6.0-alpha.1.0.20231122024802-192c5e846faa
7-
golang.org/x/sys v0.14.0
6+
github.com/ebitengine/purego v0.6.1
7+
golang.org/x/sys v0.18.0
88
)

raylib/go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
github.com/ebitengine/purego v0.6.0-alpha.1.0.20231122024802-192c5e846faa h1:Ik7QikRgeH+bFOfAcMpttCbs6XxWXxCLXMm4awxtOXk=
2-
github.com/ebitengine/purego v0.6.0-alpha.1.0.20231122024802-192c5e846faa/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
3-
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
4-
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
1+
github.com/ebitengine/purego v0.6.1 h1:sjN8rfzbhXQ59/pE+wInswbU9aMDHiwlup4p/a07Mkg=
2+
github.com/ebitengine/purego v0.6.1/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
3+
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
4+
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

raylib/purego.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//go:build !cgo
2+
// +build !cgo
3+
4+
package rl
5+
6+
const (
7+
requiredVersion = "5.0"
8+
)

raylib/purego_unix.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//go:build !cgo && (darwin || openbsd || freebsd || linux)
2+
3+
package rl
4+
5+
import (
6+
"fmt"
7+
"runtime"
8+
"unsafe"
9+
10+
"github.com/ebitengine/purego"
11+
"golang.org/x/sys/unix"
12+
)
13+
14+
// loadLibrary loads the raylib dll and panics on error
15+
func loadLibrary() uintptr {
16+
libname := "./raylib.so"
17+
switch runtime.GOOS {
18+
case "darwin":
19+
libname = "./raylib.dylib"
20+
}
21+
22+
handle, err := purego.Dlopen(libname, purego.RTLD_NOW|purego.RTLD_GLOBAL)
23+
if err != nil {
24+
panic(fmt.Errorf("cannot load library %s: %w", libname, err))
25+
}
26+
27+
proc, err := purego.Dlsym(handle, "raylib_version")
28+
if err != nil {
29+
panic(err)
30+
}
31+
32+
version := unix.BytePtrToString(**(***byte)(unsafe.Pointer(&proc)))
33+
if version != requiredVersion {
34+
panic(fmt.Errorf("version %s of %s doesn't match the required version %s", version, libname, requiredVersion))
35+
}
36+
37+
return uintptr(handle)
38+
}
39+
40+
func traceLogCallbackWrapper(fn TraceLogCallbackFun) uintptr {
41+
return purego.NewCallback(func(logLevel int32, text *byte) uintptr {
42+
fn(int(logLevel), unix.BytePtrToString(text))
43+
return 0
44+
})
45+
}

raylib/purego_windows.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import (
1212
)
1313

1414
const (
15-
libname = "raylib.dll"
16-
requiredVersion = "5.0"
15+
libname = "raylib.dll"
1716
)
1817

1918
// loadLibrary loads the raylib dll and panics on error

raylib/raylib_purego.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build !cgo && windows
2-
// +build !cgo,windows
1+
//go:build !cgo
2+
// +build !cgo
33

44
package rl
55

raylib/rlgl_purego.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build !cgo && windows
2-
// +build !cgo,windows
1+
//go:build !cgo
2+
// +build !cgo
33

44
package rl
55

0 commit comments

Comments
 (0)