zorldo

Goofing around with Ebiten
git clone git://bsandro.tech/zorldo
Log | Files | Refs | README

conversions_notwindows.go (1001B)


      1 // SPDX-License-Identifier: MIT
      2 
      3 //go:build !windows
      4 // +build !windows
      5 
      6 package gl
      7 
      8 import (
      9 	"unsafe"
     10 )
     11 
     12 // #include <stdlib.h>
     13 import "C"
     14 
     15 // GoStr takes a null-terminated string returned by OpenGL and constructs a
     16 // corresponding Go string.
     17 func GoStr(cstr *uint8) string {
     18 	return C.GoString((*C.char)(unsafe.Pointer(cstr)))
     19 }
     20 
     21 // Strs takes a list of Go strings (with or without null-termination) and
     22 // returns their C counterpart.
     23 //
     24 // The returned free function must be called once you are done using the strings
     25 // in order to free the memory.
     26 //
     27 // If no strings are provided as a parameter this function will panic.
     28 func Strs(strs ...string) (cstrs **uint8, free func()) {
     29 	if len(strs) == 0 {
     30 		panic("Strs: expected at least 1 string")
     31 	}
     32 
     33 	css := make([]*uint8, 0, len(strs))
     34 	for _, str := range strs {
     35 		cs := C.CString(str)
     36 		css = append(css, (*uint8)(unsafe.Pointer(cs)))
     37 	}
     38 
     39 	return (**uint8)(&css[0]), func() {
     40 		for _, cs := range css {
     41 			C.free(unsafe.Pointer(cs))
     42 		}
     43 	}
     44 }