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 --- lib/ruby2d/dsl.rb | 4 ++-- lib/ruby2d/window.rb | 22 +++++++++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) (limited to 'lib') 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 -- cgit v1.2.3