From be713c960e2b9f675e04c6d7cdabb3cd61a0d62d Mon Sep 17 00:00:00 2001 From: Tom Black Date: Tue, 1 Jan 2019 10:33:02 -0600 Subject: Add ability to take screenshot --- .gitignore | 1 + ext/ruby2d/extconf.rb | 2 +- ext/ruby2d/ruby2d.c | 18 ++++++++++++++++++ lib/ruby2d/dsl.rb | 4 ++-- lib/ruby2d/window.rb | 22 +++++++++++++++++++--- test/testcard.rb | 8 ++++++++ 6 files changed, 49 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 40672d1..e3e4fd0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ build/ +test/screenshot* *.gem Gemfile.lock diff --git a/ext/ruby2d/extconf.rb b/ext/ruby2d/extconf.rb index 3cd2302..a079241 100644 --- a/ext/ruby2d/extconf.rb +++ b/ext/ruby2d/extconf.rb @@ -1,7 +1,7 @@ require 'mkmf' require_relative '../../lib/ruby2d/colorize' -S2D_VERSION = '1.0.1' # Simple 2D minimum version required +S2D_VERSION = '1.1.0' # Simple 2D minimum version required $errors = [] def print_errors diff --git a/ext/ruby2d/ruby2d.c b/ext/ruby2d/ruby2d.c index f9cf3b4..de22762 100644 --- a/ext/ruby2d/ruby2d.c +++ b/ext/ruby2d/ruby2d.c @@ -154,6 +154,21 @@ double normalize_controller_axis(int val) { } +/* + * Ruby2D#self.ext_screenshot + */ +#if MRUBY +static R_VAL ruby2d_ext_screenshot(mrb_state* mrb, R_VAL self) { + mrb_value path; + mrb_get_args(mrb, "o", &path); +#else +static R_VAL ruby2d_ext_screenshot(R_VAL self, R_VAL path) { +#endif + S2D_Screenshot(window, RSTRING_PTR(path)); + return R_NIL; +} + + /* * Ruby2D::Triangle#ext_render */ @@ -1183,6 +1198,9 @@ void Init_ruby2d() { // Ruby2D::Window#ext_show r_define_method(ruby2d_window_class, "ext_show", ruby2d_window_ext_show, r_args_none); + // Ruby2D::Window#ext_screenshot + r_define_method(ruby2d_window_class, "ext_screenshot", ruby2d_ext_screenshot, r_args_req(1)); + // Ruby2D::Window#ext_close r_define_method(ruby2d_window_class, "ext_close", ruby2d_window_ext_close, r_args_none); diff --git a/lib/ruby2d/dsl.rb b/lib/ruby2d/dsl.rb index a6818fa..62352b4 100644 --- a/lib/ruby2d/dsl.rb +++ b/lib/ruby2d/dsl.rb @@ -4,8 +4,8 @@ module Ruby2D::DSL Ruby2D::Window.new - def get(sym) - Window.get(sym) + def get(sym, opts = nil) + Window.get(sym, opts) end def set(opts) diff --git a/lib/ruby2d/window.rb b/lib/ruby2d/window.rb index 7db80a9..a952134 100644 --- a/lib/ruby2d/window.rb +++ b/lib/ruby2d/window.rb @@ -111,9 +111,10 @@ module Ruby2D def mouse_x; get(:mouse_x) end def mouse_y; get(:mouse_y) end def diagnostics; get(:diagnostics) end + def screenshot(opts = nil); get(:screenshot, opts) end - def get(sym) - @@window.get(sym) + def get(sym, opts = nil) + @@window.get(sym, opts) end def set(opts) @@ -156,7 +157,7 @@ module Ruby2D # Public instance methods # Retrieve an attribute of the window - def get(sym) + def get(sym, opts = nil) case sym when :window; self when :title; @title @@ -182,6 +183,7 @@ module Ruby2D when :mouse_x; @mouse_x when :mouse_y; @mouse_y when :diagnostics; @diagnostics + when :screenshot; screenshot(opts) end end @@ -383,6 +385,20 @@ module Ruby2D ext_show end + # Take screenshot + def screenshot(path) + if path + ext_screenshot(path) + else + if RUBY_ENGINE == 'ruby' + time = Time.now.utc.strftime '%Y-%m-%d--%H-%M-%S' + else + time = Time.now.utc.to_i + end + ext_screenshot("./screenshot-#{time}.png") + end + end + # Close the window def close ext_close diff --git a/test/testcard.rb b/test/testcard.rb index 13ee01a..9e9cce5 100644 --- a/test/testcard.rb +++ b/test/testcard.rb @@ -275,6 +275,14 @@ on :key_down do |event| if event.key == 'r' rotate = rotate ? false : true; end + + if event.key == 's' + puts "Taking screenshots..." + get :screenshot + get :screenshot, './screenshot-get.png' + Window.screenshot + Window.screenshot './screenshot-window.png' + end end on :mouse_down do -- cgit v1.2.3