Last active
December 23, 2015 22:40
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks postmodern. seems like there is no other way to handle those struct layout cases in ffi