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

43 lines
973 B
Go
Raw Normal View History

// SPDX-License-Identifier: MIT
package gl
import (
"fmt"
"unsafe"
)
// Ptr takes a slice or pointer (to a singular scalar value or the first
// element of an array or slice) and returns its GL-compatible address.
//
// For example:
//
2022-11-09 04:51:02 +01:00
// var data []byte
2022-08-03 13:48:02 +02:00
// ...
// gl.TexImage2D(glconst.TEXTURE_2D, ..., glconst.UNSIGNED_BYTE, gl.Ptr(&data[0]))
func Ptr(data any) unsafe.Pointer {
if data == nil {
return unsafe.Pointer(nil)
}
var addr unsafe.Pointer
switch v := data.(type) {
case *uint8:
addr = unsafe.Pointer(v)
case *uint16:
addr = unsafe.Pointer(v)
case *float32:
addr = unsafe.Pointer(v)
case []uint8:
addr = unsafe.Pointer(&v[0])
case []uint16:
addr = unsafe.Pointer(&v[0])
case []uint32:
addr = unsafe.Pointer(&v[0])
case []float32:
addr = unsafe.Pointer(&v[0])
default:
panic(fmt.Errorf("unsupported type %T; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v))
}
return addr
}