From 1fda02efc428a11b2045a36c2deca8f5048c2ddf Mon Sep 17 00:00:00 2001 From: groogy Date: Tue, 16 Nov 2010 09:03:15 +0000 Subject: [PATCH] Added VideoMode_ForceType so we can ensure we get a VideoMode. git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1651 4e206d99-4929-0410-ac5d-dfc041789085 --- .../ruby/sfml-window/window/VideoMode.cpp | 31 +++++++++++++++++++ .../ruby/sfml-window/window/VideoMode.hpp | 2 ++ 2 files changed, 33 insertions(+) diff --git a/bindings/ruby/sfml-window/window/VideoMode.cpp b/bindings/ruby/sfml-window/window/VideoMode.cpp index 00d1e3d7f..b9373ee06 100644 --- a/bindings/ruby/sfml-window/window/VideoMode.cpp +++ b/bindings/ruby/sfml-window/window/VideoMode.cpp @@ -57,6 +57,37 @@ */ VALUE globalVideoModeClass; +/* Internal function + * Forces the argument someValue to be a VideoMode. If it can convert it then it will. + * So you can always safely asume that this function returns a VideoMode object. + * If it fails then an exception will be thrown. + */ +VALUE VideoMode_ForceType( VALUE someValue ) +{ + if( rb_obj_is_kind_of( someValue, rb_cArray ) == true ) + { + VALUE arg1 = rb_ary_entry( someValue, 0 ); + VALUE arg2 = rb_ary_entry( someValue, 1 ); + if( FIX2INT( rb_funcall( someValue, rb_intern( "size" ), 0 ) ) == 3 ) + { + VALUE arg3 = rb_ary_entry( someValue, 2 ); + return rb_funcall( globalVideoModeClass, rb_intern( "new" ), 3, arg1, arg2, arg3 ); + } + else + { + return rb_funcall( globalVideoModeClass, rb_intern( "new" ), 2, arg1, arg2 ); + } + } + else if( rb_obj_is_kind_of( someValue, globalVideoModeClass ) == true ) + { + return someValue; + } + else + { + rb_raise( rb_eRuntimeError, "expected Array[width, height, bpp] or VideoMode" ); + } +} + /* Free a heap allocated object * Not accessible trough ruby directly! */ diff --git a/bindings/ruby/sfml-window/window/VideoMode.hpp b/bindings/ruby/sfml-window/window/VideoMode.hpp index 5e2f7cab7..d7afa374e 100644 --- a/bindings/ruby/sfml-window/window/VideoMode.hpp +++ b/bindings/ruby/sfml-window/window/VideoMode.hpp @@ -25,6 +25,8 @@ #include "ruby.h" +VALUE VideoMode_ForceType( VALUE someValue ); + // Ruby initiation function void Init_VideoMode( void );