Skip to content

Instantly share code, notes, and snippets.

@lian
Last active December 23, 2015 22:40
Show Gist options
  • Save lian/6705248 to your computer and use it in GitHub Desktop.
Save lian/6705248 to your computer and use it in GitHub Desktop.
is there a nicer way for Face#faces_from_one_ptr and Face#faces_from_multiple_ptrs
=begin
# C dummy code
struct face
{
unsigned int mNumIndices;
unsigned int* mIndices;
}
struct mesh
{
unsigned int mNumFaces;
face* mFaces;
face** mFaces_ptrs;
}
=end
class Face < FFI::Struct
layout(
:num_indices, :uint32,
:indices, :pointer,
)
def indices
self[:indices].get_array_of_uint32(0, self[:num_indices])
end
end
class Mesh < FFI::Struct
layout(
:num_faces, :uint32,
:faces, :pointer,
:faces_ptrs, :pointer,
)
def faces_from_one_ptr
offset = -Face.size
Array.new(self[:num_faces]){
Face.new(self[:faces] + (offset += Face.size)).indices
}
end
def faces_from_multiple_ptrs
self[:faces_ptrs].get_array_of_pointer(0, self[:num_faces]).map{|i| Face.new(i) }
end
end
# question: whats a nicer ffi way for Face#faces_from_one_ptr and Face#faces_from_multiple_ptrs
@lian
Copy link
Author

lian commented Sep 25, 2013

thanks postmodern. seems like there is no other way to handle those struct layout cases in ffi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment