Skip to content

Commit

Permalink
NewType constructor added
Browse files Browse the repository at this point in the history
  • Loading branch information
JupiterRider committed Dec 22, 2024
1 parent 2fcfd90 commit 635a997
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
6 changes: 3 additions & 3 deletions examples/structs/raylib/main_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ type Image struct {
var gopher []byte

var (
TypeImage = ffi.Type{Type: ffi.Struct, Elements: &[]*ffi.Type{&ffi.TypePointer, &ffi.TypeSint32, &ffi.TypeSint32, &ffi.TypeSint32, &ffi.TypeSint32, nil}[0]}
TypeTexture = ffi.Type{Type: ffi.Struct, Elements: &[]*ffi.Type{&ffi.TypeUint32, &ffi.TypeSint32, &ffi.TypeSint32, &ffi.TypeSint32, &ffi.TypeSint32, nil}[0]}
TypeColor = ffi.Type{Type: ffi.Struct, Elements: &[]*ffi.Type{&ffi.TypeUint8, &ffi.TypeUint8, &ffi.TypeUint8, &ffi.TypeUint8, nil}[0]}
TypeImage = ffi.NewType(&ffi.TypePointer, &ffi.TypeSint32, &ffi.TypeSint32, &ffi.TypeSint32, &ffi.TypeSint32)
TypeTexture = ffi.NewType(&ffi.TypeUint32, &ffi.TypeSint32, &ffi.TypeSint32, &ffi.TypeSint32, &ffi.TypeSint32)
TypeColor = ffi.NewType(&ffi.TypeUint8, &ffi.TypeUint8, &ffi.TypeUint8, &ffi.TypeUint8)
)

var (
Expand Down
6 changes: 3 additions & 3 deletions examples/structs/raylib/main_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ type Image struct {
var gopher []byte

var (
TypeImage = ffi.Type{Type: ffi.Struct, Elements: &[]*ffi.Type{&ffi.TypePointer, &ffi.TypeSint32, &ffi.TypeSint32, &ffi.TypeSint32, &ffi.TypeSint32, nil}[0]}
TypeTexture = ffi.Type{Type: ffi.Struct, Elements: &[]*ffi.Type{&ffi.TypeUint32, &ffi.TypeSint32, &ffi.TypeSint32, &ffi.TypeSint32, &ffi.TypeSint32, nil}[0]}
TypeColor = ffi.Type{Type: ffi.Struct, Elements: &[]*ffi.Type{&ffi.TypeUint8, &ffi.TypeUint8, &ffi.TypeUint8, &ffi.TypeUint8, nil}[0]}
TypeImage = ffi.NewType(&ffi.TypePointer, &ffi.TypeSint32, &ffi.TypeSint32, &ffi.TypeSint32, &ffi.TypeSint32)
TypeTexture = ffi.NewType(&ffi.TypeUint32, &ffi.TypeSint32, &ffi.TypeSint32, &ffi.TypeSint32, &ffi.TypeSint32)
TypeColor = ffi.NewType(&ffi.TypeUint8, &ffi.TypeUint8, &ffi.TypeUint8, &ffi.TypeUint8)
)

var (
Expand Down
12 changes: 12 additions & 0 deletions ffi.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ type Type struct {
Elements **Type // Pointer to the first element of a nil-terminated slice.
}

// NewType can by used to create a new struct [Type].
//
// Example:
//
// ffi.NewType(&ffi.TypeFloat, &ffi.TypeFloat)
// // is equivalent to
// ffi.Type{Type: ffi.Struct, Elements: &[]*ffi.Type{&ffi.TypeFloat, &ffi.TypeFloat, nil}[0]}
func NewType(elements ...*Type) Type {
elements = append(elements, nil)
return Type{Type: Struct, Elements: &elements[0]}
}

// Cif stands for "Call InterFace". It describes the signature of a function.
//
// Use [PrepCif] to initialize it.
Expand Down

0 comments on commit 635a997

Please sign in to comment.