purego PoC for linux and freebsd
This commit is contained in:
parent
c21ba3a916
commit
498c661fcb
4 changed files with 79 additions and 4 deletions
|
@ -7,4 +7,7 @@ require (
|
|||
golang.org/x/sys v0.20.0
|
||||
)
|
||||
|
||||
require golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842
|
||||
require (
|
||||
github.com/jupiterrider/ffi v0.1.0-beta.8
|
||||
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842
|
||||
)
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
github.com/ebitengine/purego v0.7.1 h1:6/55d26lG3o9VCZX8lping+bZcmShseiqlh2bnUDiPA=
|
||||
github.com/ebitengine/purego v0.7.1/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
|
||||
github.com/jupiterrider/ffi v0.1.0-beta.8 h1:9hTtlgc3zHPyQLakD5DxVk0b3x+Ts/emoPxxjjkUApc=
|
||||
github.com/jupiterrider/ffi v0.1.0-beta.8/go.mod h1:sOp6VJGFaYyr4APi8gwy6g20QNHv5F8Iq1CVbtC900s=
|
||||
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM=
|
||||
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
|
||||
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
|
||||
|
|
|
@ -50,9 +50,6 @@ func loadLibrary() uintptr {
|
|||
panic(fmt.Errorf("version %s of %s doesn't match the required version %s", version, libname, requiredVersion))
|
||||
}
|
||||
|
||||
//DEBUG
|
||||
fmt.Println(version)
|
||||
|
||||
return uintptr(handle)
|
||||
}
|
||||
|
||||
|
|
73
raylib/raylib_purego_unix.go
Normal file
73
raylib/raylib_purego_unix.go
Normal file
|
@ -0,0 +1,73 @@
|
|||
//go:build !cgo && (freebsd || linux)
|
||||
|
||||
package rl
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/ebitengine/purego"
|
||||
"github.com/jupiterrider/ffi"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// bundle bundles the function pointer "sym" and the ffi call interface "cif"
|
||||
type bundle struct {
|
||||
sym uintptr
|
||||
cif ffi.Cif
|
||||
}
|
||||
|
||||
func newBundle(name string, rType *ffi.Type, aTypes ...*ffi.Type) *bundle {
|
||||
b := new(bundle)
|
||||
var err error
|
||||
|
||||
if b.sym, err = purego.Dlsym(raylibDll, name); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
nArgs := uint32(len(aTypes))
|
||||
|
||||
if status := ffi.PrepCif(&b.cif, ffi.DefaultAbi, nArgs, rType, aTypes...); status != ffi.OK {
|
||||
panic(status)
|
||||
}
|
||||
|
||||
return b
|
||||
}
|
||||
|
||||
var (
|
||||
// raylibDll is the pointer to the shared library
|
||||
raylibDll uintptr
|
||||
)
|
||||
|
||||
var (
|
||||
initWindow *bundle
|
||||
closeWindow *bundle
|
||||
setTraceLogCallback *bundle
|
||||
)
|
||||
|
||||
func init() {
|
||||
raylibDll = loadLibrary()
|
||||
|
||||
initWindow = newBundle("InitWindow", &ffi.TypeVoid, &ffi.TypeSint32, &ffi.TypeSint32, &ffi.TypePointer)
|
||||
closeWindow = newBundle("CloseWindow", &ffi.TypeVoid)
|
||||
setTraceLogCallback = newBundle("SetTraceLogCallback", &ffi.TypeVoid, &ffi.TypePointer)
|
||||
}
|
||||
|
||||
// InitWindow - Initialize window and OpenGL context
|
||||
func InitWindow(width int32, height int32, title string) {
|
||||
ctitle, err := unix.BytePtrFromString(title)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
ffi.Call(&initWindow.cif, initWindow.sym, nil, unsafe.Pointer(&width), unsafe.Pointer(&height), unsafe.Pointer(&ctitle))
|
||||
}
|
||||
|
||||
// CloseWindow - Close window and unload OpenGL context
|
||||
func CloseWindow() {
|
||||
ffi.Call(&closeWindow.cif, closeWindow.sym, nil)
|
||||
}
|
||||
|
||||
// SetTraceLogCallback - Set custom trace log
|
||||
func SetTraceLogCallback(fn TraceLogCallbackFun) {
|
||||
callback := traceLogCallbackWrapper(fn)
|
||||
ffi.Call(&setTraceLogCallback.cif, setTraceLogCallback.sym, nil, unsafe.Pointer(&callback))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue