ebiten/internal/graphicsdriver/opengl/gl/conversions_nocgo.go

44 lines
842 B
Go
Raw Normal View History

// SPDX-License-Identifier: MIT
//go:build darwin || windows
package gl
import (
"runtime"
"unsafe"
)
// GoStr takes a null-terminated string returned by OpenGL and constructs a
// corresponding Go string.
2022-11-09 04:51:02 +01:00
func GoStr(cstr *byte) string {
if cstr == nil {
return ""
}
x := unsafe.Slice(cstr, 1e9)
for i, c := range x {
if c == 0 {
str := make([]byte, i)
copy(str, x[:i])
return string(str)
}
}
2022-11-09 04:51:02 +01:00
return ""
}
// CStr takes a Go string (with or without null-termination)
// and returns the C counterpart.
//
// The returned free function must be called once you are done using the string
// in order to free the memory.
func CStr(str string) (cstr *byte, free func()) {
bs := []byte(str)
if len(bs) == 0 || bs[len(bs)-1] != 0 {
bs = append(bs, 0)
}
return &bs[0], func() {
runtime.KeepAlive(bs)
bs = nil
}
}