diff options
| -rw-r--r-- | README.md | 4 | ||||
| -rwxr-xr-x | bin/ruby2d | 33 | ||||
| -rw-r--r-- | ext/ruby2d/ruby2d-opal.rb | 289 | ||||
| -rw-r--r-- | lib/ruby2d/image.rb | 11 | ||||
| -rw-r--r-- | lib/ruby2d/music.rb | 8 | ||||
| -rw-r--r-- | lib/ruby2d/sound.rb | 8 | ||||
| -rw-r--r-- | lib/ruby2d/sprite.rb | 8 | ||||
| -rw-r--r-- | lib/ruby2d/text.rb | 8 | ||||
| -rw-r--r-- | lib/ruby2d/window.rb | 6 | ||||
| -rw-r--r-- | ruby2d.gemspec | 1 | ||||
| -rw-r--r-- | test/audio.rb | 10 | ||||
| -rw-r--r-- | test/contains.rb | 12 | ||||
| -rw-r--r-- | test/sprite.rb | 16 | ||||
| -rw-r--r-- | test/testcard.rb | 24 |
14 files changed, 34 insertions, 404 deletions
@@ -39,7 +39,7 @@ rake test:int testcard # Build `test/audio.rb` natively using MRuby and run rake test:native audio -# Build `test/mouse.rb` for the web using Opal and run in the default browser +# Build `test/mouse.rb` for the web using WebAssembly and run in the default browser rake test:web mouse ``` @@ -67,7 +67,7 @@ Whether adding a feature or fixing a bug, try to do the following to ensure your - **Check if there is an existing issue, and if not, open a new one to start a discussion.** Before dedicating time and energy to an idea or fix, let's make sure it's consistent with the principles and goals of the project, and that we have a solid strategy in place to implement and test. -- **Use a subset of Ruby that works everywhere.** Ruby 2D applications are, of course, written in Ruby. Some users may choose to harness the full power of the language, standard library, and ecosystem of gems by writing interpreted apps targeting the standard implementation, [MRI](https://en.wikipedia.org/wiki/Ruby_MRI). Others may want to target the web via [Opal](http://opalrb.org), so their app can be run in any browser. And others still may want to compile their app to native code via [MRuby](http://mruby.org), so they can embed it on platforms like the [Raspberry Pi](https://www.raspberrypi.org), or run it on mobile platforms like iOS and Android, or on the big screen with Apple TV or Amazon Fire TV. Or even further, some may want to do all three! Ruby 2D aims to support all of these use cases, even with the same app codebase. Your code contribution to this gem has to support a subset of Ruby that is compatible and behaves similarly across MRI, MRuby, and Opal. Beyond reading the documentation for each Ruby implementation, you can also try out code snippets on the command line using their respective REPLs: `irb` (MRI), `mirb` (MRuby), and `opal-repl` (Opal). +- **Use a subset of Ruby that works everywhere.** Ruby 2D applications are, of course, written in Ruby. Some users may choose to harness the full power of the language, standard library, and ecosystem of gems by writing interpreted apps targeting the standard implementation, [MRI](https://en.wikipedia.org/wiki/Ruby_MRI). Others may want to target the web via [WebAssembly](https://webassembly.org), mobile devices, or build native desktop applications, all which make use of a different Ruby implementation called [MRuby](http://mruby.org). Ruby 2D aims to support all of these use cases, even with the same app codebase. Your contribution must support a subset of Ruby that is compatible with and behaves similarly across MRI and MRuby. Beyond reading the documentation for each Ruby implementation, you can also try out code snippets on the command line using their respective REPLs: `irb` for MRI, and `mirb` for MRuby. - **Comprehensively test your change.** Unlike other Ruby libraries, not everything here can be easily covered with unit tests alone. We also need to make sure things look and sound right, inputs work as expected, and behavior is consistent across all [platforms Ruby 2D supports](http://www.ruby2d.com/platforms). @@ -115,38 +115,6 @@ end # Build a web-based version of the provided Ruby application def build_web(rb_file) puts "Warning: ".warn + "This feature is currently disabled while it's being upgraded." - return - - check_build_src_file(rb_file) - - # Assemble the Ruby 2D library in one `.rb` file and compile to JS - make_lib - `opal --compile --no-opal build/lib.rb > build/lib.js` - - # Read the provided Ruby source file, copy to build dir, and compile to JS - File.open('build/src.rb', 'w') { |file| file << strip_require(rb_file) } - `opal --compile --no-opal build/src.rb > build/src.js` - FileUtils.cp "#{@gem_dir}/ext/ruby2d/ruby2d-opal.rb", "build/" - `opal --compile --no-opal build/ruby2d-opal.rb > build/ruby2d-opal.js` - - # Combine contents of JS source files and compiled JS into one file - open('build/app.js', 'w') do |f| - f << File.read("#{@gem_dir}/assets/simple2d.js") << "\n\n" - f << File.read("#{@gem_dir}/assets/opal.js") << "\n\n" - f << File.read("build/lib.js") << "\n\n" - f << File.read("build/ruby2d-opal.js") << "\n\n" - f << File.read("build/src.js") << "\n\n" - end - - # Copy over HTML template - FileUtils.cp "#{@gem_dir}/assets/template.html", "build/app.html" - - # Clean up - clean_up unless @debug - - # Success! - puts "Web app created at `build/app.js`", - " Run by opening `build/app.html`" end @@ -204,7 +172,6 @@ end def clean_up(cmd = nil) FileUtils.rm( Dir.glob('build/{src,lib}.{rb,c,js}') + - Dir.glob('build/ruby2d-opal.{rb,js}') + Dir.glob('build/app.c') ) if cmd == :all diff --git a/ext/ruby2d/ruby2d-opal.rb b/ext/ruby2d/ruby2d-opal.rb deleted file mode 100644 index e888328..0000000 --- a/ext/ruby2d/ruby2d-opal.rb +++ /dev/null @@ -1,289 +0,0 @@ -# Web extension for Opal - -# Ruby 2D window -$R2D_WINDOW = nil - -# Simple 2D window -` -var win; - -// ruby2d.js - -function on_key(e) { - - switch (e.type) { - case S2D.KEY_DOWN: - #{type = :down}; - break; - case S2D.KEY_HELD: - #{type = :held}; - break; - case S2D.KEY_UP: - #{type = :up}; - break; - } - - #{$R2D_WINDOW.key_callback(type, `e.key`)}; -} - - -function on_mouse(e) { - - #{direction = nil} - #{button = nil} - - switch (e.type) { - case S2D.MOUSE_DOWN: - #{type = :down}; - break; - case S2D.MOUSE_UP: - #{type = :up}; - break; - case S2D.MOUSE_SCROLL: - #{type = :scroll}; - #{direction} = e.direction == S2D.MOUSE_SCROLL_NORMAL ? #{:normal} : #{:inverted}; - break; - case S2D.MOUSE_MOVE: - #{type = :move}; - break; - } - - if (e.type == S2D.MOUSE_DOWN || e.type == S2D.MOUSE_UP) { - switch (e.button) { - case S2D.MOUSE_LEFT: - #{button = :left}; - break; - case S2D.MOUSE_MIDDLE: - #{button = :middle}; - break; - case S2D.MOUSE_RIGHT: - #{button = :right}; - break; - case S2D.MOUSE_X1: - #{button = :x1}; - break; - case S2D.MOUSE_X2: - #{button = :x2}; - break; - } - } - - #{$R2D_WINDOW.mouse_callback( - type, button, direction, - `e.x`, `e.y`, `e.delta_x`, `e.delta_y` - )}; -} - - -function update() { - #{$R2D_WINDOW.mouse_x = `win.mouse.x`}; - #{$R2D_WINDOW.mouse_y = `win.mouse.y`}; - #{$R2D_WINDOW.frames = `win.frames`}; - #{$R2D_WINDOW.fps = `win.fps`}; - #{$R2D_WINDOW.update_callback}; -} - - -function render() { - - // Set background color - win.background.r = #{$R2D_WINDOW.get(:background).r}; - win.background.g = #{$R2D_WINDOW.get(:background).g}; - win.background.b = #{$R2D_WINDOW.get(:background).b}; - win.background.a = #{$R2D_WINDOW.get(:background).a}; - - var objects = #{$R2D_WINDOW.objects}; - - for (var i = 0; i < objects.length; i++) { - var el = objects[i]; - el['$ext_render'](); - } -} -` - - -module Ruby2D - class Triangle - def ext_render - `S2D.DrawTriangle( - #{self}.x1, #{self}.y1, #{self}.c1.r, #{self}.c1.g, #{self}.c1.b, #{self}.c1.a, - #{self}.x2, #{self}.y2, #{self}.c2.r, #{self}.c2.g, #{self}.c2.b, #{self}.c2.a, - #{self}.x3, #{self}.y3, #{self}.c3.r, #{self}.c3.g, #{self}.c3.b, #{self}.c3.a - );` - end - end - - class Quad - def ext_render - `S2D.DrawQuad( - #{self}.x1, #{self}.y1, #{self}.c1.r, #{self}.c1.g, #{self}.c1.b, #{self}.c1.a, - #{self}.x2, #{self}.y2, #{self}.c2.r, #{self}.c2.g, #{self}.c2.b, #{self}.c2.a, - #{self}.x3, #{self}.y3, #{self}.c3.r, #{self}.c3.g, #{self}.c3.b, #{self}.c3.a, - #{self}.x4, #{self}.y4, #{self}.c4.r, #{self}.c4.g, #{self}.c4.b, #{self}.c4.a - );` - end - end - - class Line - def ext_render - `S2D.DrawLine( - #{self}.x1, #{self}.y1, #{self}.x2, #{self}.y2, #{self}.width, - #{self}.c1.r, #{self}.c1.g, #{self}.c1.b, #{self}.c1.a, - #{self}.c2.r, #{self}.c2.g, #{self}.c2.b, #{self}.c2.a, - #{self}.c3.r, #{self}.c3.g, #{self}.c3.b, #{self}.c3.a, - #{self}.c4.r, #{self}.c4.g, #{self}.c4.b, #{self}.c4.a - );` - end - end - - class Image - def ext_init(path) - ` - #{self}.data = S2D.CreateImage(path, function() { - if (#{@width} == Opal.nil) { - #{@width} = #{self}.data.width; - } - if (#{@height} == Opal.nil) { - #{@height} = #{self}.data.height; - } - }); - ` - end - - def ext_render - ` - #{self}.data.x = #{self}.x; - #{self}.data.y = #{self}.y; - - if (#{self}.width != Opal.nil) #{self}.data.width = #{self}.width; - if (#{self}.height != Opal.nil) #{self}.data.height = #{self}.height; - - #{self}.data.color.r = #{self}.color.r; - #{self}.data.color.g = #{self}.color.g; - #{self}.data.color.b = #{self}.color.b; - #{self}.data.color.a = #{self}.color.a; - - S2D.DrawImage(#{self}.data); - ` - end - end - - class Sprite - def ext_init(path) - `#{self}.data = S2D.CreateSprite(path);` - end - - def ext_render - ` - #{self}.data.x = #{self}.x; - #{self}.data.y = #{self}.y; - - S2D.ClipSprite( - #{self}.data, - #{self}.clip_x, - #{self}.clip_y, - #{self}.clip_w, - #{self}.clip_h - ); - - S2D.DrawSprite(#{self}.data); - ` - end - end - - class Text - def ext_init - ` - #{self}.data = S2D.CreateText(#{self}.font, #{self}.text, #{self}.size); - #{@width} = #{self}.data.width; - #{@height} = #{self}.data.height; - ` - end - - def ext_set(msg) - ` - S2D.SetText(#{self}.data, #{msg}); - #{@width} = #{self}.data.width; - #{@height} = #{self}.data.height; - ` - end - - def ext_render - ` - #{self}.data.x = #{self}.x; - #{self}.data.y = #{self}.y; - - #{self}.data.color.r = #{self}.color.r; - #{self}.data.color.g = #{self}.color.g; - #{self}.data.color.b = #{self}.color.b; - #{self}.data.color.a = #{self}.color.a; - - S2D.DrawText(#{self}.data); - ` - end - end - - class Sound - def ext_init(path) - `#{self}.data = S2D.CreateSound(path);` - end - - def ext_play - `S2D.PlaySound(#{self}.data);` - end - end - - class Music - def ext_init(path) - `#{self}.data = S2D.CreateMusic(path);` - end - - def ext_play - `S2D.PlayMusic(#{self}.data, #{self}.loop);` - end - - def ext_pause - `S2D.PauseMusic();` - end - - def ext_resume - `S2D.ResumeMusic();` - end - - def ext_stop - `S2D.StopMusic();` - end - - def ext_music_fadeout(ms) - `S2D.FadeOutMusic(ms);` - end - end - - class Window - def ext_show - $R2D_WINDOW = self - - ` - var width = #{$R2D_WINDOW.get(:width)}; - var height = #{$R2D_WINDOW.get(:height)}; - - var vp_w = #{$R2D_WINDOW.get(:viewport_width)}; - var viewport_width = vp_w != Opal.nil ? vp_w : width; - - var vp_h = #{$R2D_WINDOW.get(:viewport_height)}; - var viewport_height = vp_h != Opal.nil ? vp_h : height; - - win = S2D.CreateWindow( - #{$R2D_WINDOW.get(:title)}, width, height, update, render, "ruby2d-app", {} - ); - - win.viewport.width = viewport_width; - win.viewport.height = viewport_height; - win.on_key = on_key; - win.on_mouse = on_mouse; - - S2D.Show(win); - ` - end - end -end diff --git a/lib/ruby2d/image.rb b/lib/ruby2d/image.rb index 793c25a..0516f4f 100644 --- a/lib/ruby2d/image.rb +++ b/lib/ruby2d/image.rb @@ -8,14 +8,10 @@ module Ruby2D attr_accessor :x, :y, :width, :height, :rotate, :data def initialize(path, opts = {}) - @path = path - - unless RUBY_ENGINE == 'opal' - unless File.exist? @path - raise Error, "Cannot find image file `#{@path}`" - end + unless File.exist? path + raise Error, "Cannot find image file `#{path}`" end - + @path = path @x = opts[:x] || 0 @y = opts[:y] || 0 @z = opts[:z] || 0 @@ -23,7 +19,6 @@ module Ruby2D @height = opts[:height] || nil @rotate = opts[:rotate] || 0 self.color = opts[:color] || 'white' - ext_init(@path) add end diff --git a/lib/ruby2d/music.rb b/lib/ruby2d/music.rb index bc4ec5a..ef8deb5 100644 --- a/lib/ruby2d/music.rb +++ b/lib/ruby2d/music.rb @@ -7,13 +7,9 @@ module Ruby2D attr_accessor :loop, :data def initialize(path) - - unless RUBY_ENGINE == 'opal' - unless File.exist? path - raise Error, "Cannot find audio file `#{path}`" - end + unless File.exist? path + raise Error, "Cannot find audio file `#{path}`" end - @path = path @loop = false ext_init(path) diff --git a/lib/ruby2d/sound.rb b/lib/ruby2d/sound.rb index 0a0b40d..2c78525 100644 --- a/lib/ruby2d/sound.rb +++ b/lib/ruby2d/sound.rb @@ -7,13 +7,9 @@ module Ruby2D attr_accessor :data def initialize(path) - - unless RUBY_ENGINE == 'opal' - unless File.exist? path - raise Error, "Cannot find audio file `#{path}`" - end + unless File.exist? path + raise Error, "Cannot find audio file `#{path}`" end - @path = path ext_init(path) end diff --git a/lib/ruby2d/sprite.rb b/lib/ruby2d/sprite.rb index cc9e31f..bd91cd1 100644 --- a/lib/ruby2d/sprite.rb +++ b/lib/ruby2d/sprite.rb @@ -8,12 +8,8 @@ module Ruby2D attr_accessor :rotate, :loop, :clip_x, :clip_y, :clip_width, :clip_height, :data def initialize(path, opts = {}) - - # Check if sprite file exists, unless running on the web - unless RUBY_ENGINE == 'opal' - unless File.exist? path - raise Error, "Cannot find sprite image file `#{path}`" - end + unless File.exist? path + raise Error, "Cannot find sprite image file `#{path}`" end # Sprite image file path diff --git a/lib/ruby2d/text.rb b/lib/ruby2d/text.rb index 93ea062..796cdb6 100644 --- a/lib/ruby2d/text.rb +++ b/lib/ruby2d/text.rb @@ -15,13 +15,9 @@ module Ruby2D @size = opts[:size] || 20 @rotate = opts[:rotate] || 0 @font = opts[:font] || Font.default - - unless RUBY_ENGINE == 'opal' - unless File.exist? @font - raise Error, "Cannot find font file `#{@font}`" - end + unless File.exist? @font + raise Error, "Cannot find font file `#{@font}`" end - self.color = opts[:color] || 'white' ext_init add diff --git a/lib/ruby2d/window.rb b/lib/ruby2d/window.rb index fa12fd0..e08d13c 100644 --- a/lib/ruby2d/window.rb +++ b/lib/ruby2d/window.rb @@ -320,10 +320,8 @@ module Ruby2D # Add controller mappings from file def add_controller_mappings - unless RUBY_ENGINE == 'opal' - if File.exist? @controller_mappings - ext_add_controller_mappings(@controller_mappings) - end + if File.exist? @controller_mappings + ext_add_controller_mappings(@controller_mappings) end end diff --git a/ruby2d.gemspec b/ruby2d.gemspec index fab977c..7afa33d 100644 --- a/ruby2d.gemspec +++ b/ruby2d.gemspec @@ -11,7 +11,6 @@ Gem::Specification.new do |s| s.email = '[email protected]' s.required_ruby_version = '>= 2.0.0' - s.add_dependency 'opal', '~> 0.11' s.add_development_dependency 'rspec', '~> 3.8' s.files = Dir.glob('lib/**/*') + diff --git a/test/audio.rb b/test/audio.rb index 8a0e8f6..64e580c 100644 --- a/test/audio.rb +++ b/test/audio.rb @@ -1,15 +1,9 @@ require 'ruby2d' -if RUBY_ENGINE == 'opal' - media = "../test/media" -else - media = "media" -end - set width: 300, height: 200, title: "Ruby 2D — Audio" -snd = Sound.new("#{media}/sound.wav") -mus = Music.new("#{media}/music.wav") +snd = Sound.new('media/sound.wav') +mus = Music.new('media/music.wav') volume_bar = Rectangle.new(color: 'green', width: 300, height: 50) diff --git a/test/contains.rb b/test/contains.rb index b24b898..d95a720 100644 --- a/test/contains.rb +++ b/test/contains.rb @@ -2,13 +2,7 @@ require 'ruby2d' set title: "Ruby 2D — Contains", height: 350 -if RUBY_ENGINE == 'opal' - media = "../test/media" - font = "sans-serif" -else - media = "media" - font = "#{media}/bitstream_vera/vera.ttf" -end +font = 'media/bitstream_vera/vera.ttf' objects = [] objects.push Square.new(x: 50, y: 50, size: 100) @@ -17,8 +11,8 @@ objects.push Quad.new(x1: 350, y1: 50, x2: 500, y2: 75, x3: 450, y3: 150, x4: 37 objects.push Triangle.new(x1: 550, y1: 50, x2: 600, y2: 125, x3: 500, y3: 150) objects.push Line.new(x1: 225, y1: 175, x2: 375, y2: 225, width: 20) objects.push Circle.new(x: 225, y: 275, radius: 50) -objects.push Image.new("#{media}/colors.png", x: 50, y: 200) -objects.push Text.new("Hello", x: 450, y: 200, size: 50, font: font) +objects.push Image.new('media/colors.png', x: 50, y: 200) +objects.push Text.new('Hello', x: 450, y: 200, size: 50, font: font) on :key_down do |event| close if event.key == 'escape' diff --git a/test/sprite.rb b/test/sprite.rb index e0042b7..321897c 100644 --- a/test/sprite.rb +++ b/test/sprite.rb @@ -1,15 +1,9 @@ require 'ruby2d' -if RUBY_ENGINE == 'opal' - media = "../test/media" -else - media = "media" -end - set title: "Ruby 2D — Sprite", width: 400, height: 300 coin1 = Sprite.new( - "#{media}/coin.png", + 'media/coin.png', clip_width: 84, time: 300, loop: true @@ -18,7 +12,7 @@ coin1 = Sprite.new( coin1.play coin2 = Sprite.new( - "#{media}/coin.png", + 'media/coin.png', y: 90, width: 42, height: 42, @@ -30,14 +24,14 @@ coin2 = Sprite.new( coin2.play boom = Sprite.new( - "#{media}/boom.png", + 'media/boom.png', x: 109, clip_width: 127, time: 75 ) hero = Sprite.new( - "#{media}/hero.png", + 'media/hero.png', x: 261, width: 78, height: 99, @@ -51,7 +45,7 @@ hero = Sprite.new( ) atlas = Sprite.new( - "#{media}/texture_atlas.png", + 'media/texture_atlas.png', x: 50, y: 90, animations: { count: [ diff --git a/test/testcard.rb b/test/testcard.rb index 21b627b..13ee01a 100644 --- a/test/testcard.rb +++ b/test/testcard.rb @@ -1,17 +1,11 @@ require 'ruby2d' -if RUBY_ENGINE == 'opal' - media = "../test/media" - font = "sans-serif" -else - media = "media" - font = "#{media}/bitstream_vera/vera.ttf" -end - set diagnostics: true set width: 700, height: 500, title: "Ruby 2D — Test Card" +font = 'media/bitstream_vera/vera.ttf' + # Read window attributes puts "=== Window Attributes === Title: #{get :title} @@ -206,15 +200,15 @@ Circle.new(x: 575, y: 225, radius: 17, sectors: 16, color: [0, 0, 0, 0.6]) rotate = false # Images -img_png = Image.new("#{media}/image.png", x: 600, y: 180) -img_jpg = Image.new("#{media}/image.jpg", x: 600, y: 290) -img_bmp = Image.new("#{media}/image.bmp", x: 600, y: 400) -img_r = Image.new("#{media}/colors.png", x: 400, y: 200, width: 50, height: 25) +img_png = Image.new('media/image.png', x: 600, y: 180) +img_jpg = Image.new('media/image.jpg', x: 600, y: 290) +img_bmp = Image.new('media/image.bmp', x: 600, y: 400) +img_r = Image.new('media/colors.png', x: 400, y: 200, width: 50, height: 25) img_r.color = [1.0, 0.3, 0.3, 1.0] -img_g = Image.new("#{media}/colors.png", x: 400, y: 225) +img_g = Image.new('media/colors.png', x: 400, y: 225) img_g.width, img_g.height = 25, 25 img_g.color = [0.3, 1.0, 0.3, 1.0] -img_b = Image.new("#{media}/colors.png", x: 425, y: 225) +img_b = Image.new('media/colors.png', x: 425, y: 225) img_b.width, img_b.height = 25, 25 img_b.color = [0.3, 0.3, 1.0, 1.0] @@ -228,7 +222,7 @@ fps = Text.new("", x: 10, y: 470, font: font) # Sprites spr = Sprite.new( - "#{media}/sprite_sheet.png", + 'media/sprite_sheet.png', x: 450, y: 200, clip_width: 50, time: 500, |
