From df8d3ec40ba5be54d0fea2d74709e803a588f526 Mon Sep 17 00:00:00 2001 From: Tom Black Date: Wed, 26 Sep 2018 17:27:57 -0700 Subject: Handle adding controller mappings through Window class --- ext/ruby2d/ruby2d.c | 42 +++++++++++++++++++++--------------------- lib/ruby2d/window.rb | 12 ++++++++++++ 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/ext/ruby2d/ruby2d.c b/ext/ruby2d/ruby2d.c index 12e868e..5878d7e 100644 --- a/ext/ruby2d/ruby2d.c +++ b/ext/ruby2d/ruby2d.c @@ -926,6 +926,22 @@ static R_VAL ruby2d_window_ext_get_display_dimensions(R_VAL self) { } +/* + * Ruby2D::Window#ext_add_controller_mappings + */ +#if MRUBY +static R_VAL ruby2d_window_ext_add_controller_mappings(mrb_state* mrb, R_VAL self) { + mrb_value path; + mrb_get_args(mrb, "o", &path); +#else +static R_VAL ruby2d_window_ext_add_controller_mappings(R_VAL self, R_VAL path) { +#endif + S2D_Log(S2D_INFO, "Adding controller mappings from `%s`", RSTRING_PTR(path)); + S2D_AddControllerMappingsFromFile(RSTRING_PTR(path)); + return R_NIL; +} + + /* * Ruby2D::Window#ext_show */ @@ -941,27 +957,8 @@ static R_VAL ruby2d_window_ext_show(R_VAL self) { S2D_Diagnostics(true); } - // Load controller mappings - #if !RUBY2D_IOS_TVOS && !WINDOWS - #include - - char *homedir; - if ((homedir = getenv("HOME")) == NULL) { - homedir = getpwuid(getuid())->pw_dir; - } - - char *mappings = "/.ruby2d/controllers.txt"; - char *full_mappings_path = malloc(strlen(homedir) + strlen(mappings) + 1); - strcpy(full_mappings_path, homedir); strcat(full_mappings_path, mappings); - printf("%s\n", full_mappings_path); - - // Load controller mappings, if DB file exists - if (S2D_FileExists(full_mappings_path)) { - S2D_AddControllerMappingsFromFile(full_mappings_path); - } - - free(full_mappings_path); - #endif + // Add controller mappings from file + r_funcall(self, "add_controller_mappings", 0); // Get window attributes char *title = RSTRING_PTR(r_iv_get(self, "@title")); @@ -1133,6 +1130,9 @@ void Init_ruby2d() { // Ruby2D::Window#ext_get_display_dimensions r_define_method(ruby2d_window_class, "ext_get_display_dimensions", ruby2d_window_ext_get_display_dimensions, r_args_none); + // Ruby2D::Window#ext_add_controller_mappings + r_define_method(ruby2d_window_class, "ext_add_controller_mappings", ruby2d_window_ext_add_controller_mappings, r_args_req(1)); + // Ruby2D::Window#ext_show r_define_method(ruby2d_window_class, "ext_show", ruby2d_window_ext_show, r_args_none); diff --git a/lib/ruby2d/window.rb b/lib/ruby2d/window.rb index 385070b..fee0893 100644 --- a/lib/ruby2d/window.rb +++ b/lib/ruby2d/window.rb @@ -54,6 +54,9 @@ module Ruby2D # Mouse X and Y position in the window @mouse_x, @mouse_y = 0, 0 + # Controller axis and button mappings file + @controller_mappings = File.expand_path('~') + "/.ruby2d/controllers.txt" + # Renderable objects currently in the window, like a linear scene graph @objects = [] @@ -314,6 +317,15 @@ module Ruby2D end end + # Add controller mappings from file + def add_controller_mappings + unless RUBY_ENGINE == 'opal' + if File.exists? @controller_mappings + ext_add_controller_mappings(@controller_mappings) + end + end + end + # Controller callback method, called by the native and web extentions def controller_callback(which, type, axis, value, button) # All controller events -- cgit v1.2.3