Skip to content

Instantly share code, notes, and snippets.

@vaiorabbit
Created July 21, 2013 10:40
Show Gist options
  • Select an option

  • Save vaiorabbit/6048201 to your computer and use it in GitHub Desktop.

Select an option

Save vaiorabbit/6048201 to your computer and use it in GitHub Desktop.
ruby-ffi (https://github.com/ffi/ffi) を使ってDLLの関数を呼ぶテスト。 RMath3D (https://github.com/vaiorabbit/rmath3d) の .dylib をビルド→その中にある RVec4SetElements をRubyから呼ぶところまで。
require 'ffi'
module RMath3D
extend FFI::Library
# FFI
case FFI::Platform::OS
when "darwin"
# $ clang -dynamiclib RMtx3.c RMtx4.c RVec3.c RVec4.c RQuat.c -o libRMath3D.dylib
ffi_lib "libRMath3D.dylib"
when "linux"
ffi_lib "libRMath3D.so"
when "windows"
ffi_lib "libRMath3D.dll"
else
raise RuntimeError, "RMath3D: Unknown OS"
end
# typedef
typedef :double, :rmreal
# Class
class RVec4 < FFI::Struct
layout :x, :rmreal,
:y, :rmreal,
:z, :rmreal,
:w, :rmreal
def x() return self[:x] end
def y() return self[:y] end
def z() return self[:z] end
def w() return self[:w] end
def x=(value) self[:x] = value end
def y=(value) self[:y] = value end
def z=(value) self[:z] = value end
def w=(value) self[:w] = value end
end
# method
attach_function :native_RVec4SetElements, :RVec4SetElements, [:pointer, :rmreal, :rmreal, :rmreal, :rmreal], :void
# function
module_function
def rvec4_set_elements( vec4, x, y, z, w )
native_RVec4SetElements( vec4.to_ptr, x, y, z, w )
end
end
if __FILE__ == $0
v = RMath3D::RVec4.new
p v[:x], v.x
v.x = 1.0
p v[:x], v.x
RMath3D::rvec4_set_elements( v, 2, 3, 4, 5 )
p v.x, v.y, v.z, v.w
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment