diff options
| -rwxr-xr-x | bin/ruby2d | 2 | ||||
| -rw-r--r-- | lib/ruby2d.rb | 2 | ||||
| -rw-r--r-- | lib/ruby2d/music.rb | 16 | ||||
| -rw-r--r-- | lib/ruby2d/sound.rb | 14 | ||||
| -rw-r--r-- | test/audio.rb | 53 |
5 files changed, 78 insertions, 9 deletions
@@ -27,6 +27,8 @@ end 'image', 'sprite', 'text', + 'sound', + 'music' ] diff --git a/lib/ruby2d.rb b/lib/ruby2d.rb index 125ad82..4be4f39 100644 --- a/lib/ruby2d.rb +++ b/lib/ruby2d.rb @@ -12,6 +12,8 @@ require 'ruby2d/triangle' require 'ruby2d/image' require 'ruby2d/sprite' require 'ruby2d/text' +require 'ruby2d/sound' +require 'ruby2d/music' require 'ruby2d/ruby2d' # load native extension include Ruby2D::DSL diff --git a/lib/ruby2d/music.rb b/lib/ruby2d/music.rb new file mode 100644 index 0000000..6d9475e --- /dev/null +++ b/lib/ruby2d/music.rb @@ -0,0 +1,16 @@ +# music.rb + +module Ruby2D + class Music + + attr_accessor :data, :loop + attr_reader :path + + def initialize(path) + # TODO: Check if file exists + @path = path + @loop = false + end + + end +end diff --git a/lib/ruby2d/sound.rb b/lib/ruby2d/sound.rb index edbbef9..4691f6d 100644 --- a/lib/ruby2d/sound.rb +++ b/lib/ruby2d/sound.rb @@ -3,16 +3,12 @@ module Ruby2D class Sound - def initialize(window, path) - unless File.exists? path - raise Error, "Cannot find sound file!" - end - window.create_audio(self, path) - @window, @path = window, path - end + attr_accessor :data + attr_reader :path - def play - @window.play_audio(self) + def initialize(path) + # TODO: Check if file exists + @path = path end end diff --git a/test/audio.rb b/test/audio.rb new file mode 100644 index 0000000..e57160c --- /dev/null +++ b/test/audio.rb @@ -0,0 +1,53 @@ +require 'ruby2d' + +if RUBY_ENGINE == 'opal' + media = "../test/media" +else + media = "media" +end + +set width: 300, height: 200, title: "Ruby 2D — Audio" + +on key: 'escape' do + close +end + +snd = Sound.new("#{media}/sound.wav") +mus = Music.new("#{media}/music.wav") + +on key_down: 'p' do + puts "Playing sound..." + snd.play +end + +on key_down: 'm' do + puts "Playing music..." + mus.play +end + +on key_down: 'l' do + puts "Loop music true..." + mus.loop = true +end + +on key_down: 'a' do + puts "Pause music..." + mus.pause +end + +on key_down: 'r' do + puts "Resume music..." + mus.resume +end + +on key_down: 's' do + puts "Stop music..." + mus.stop +end + +on key_down: 'f' do + puts "fade out music..." + mus.fadeout 2000 +end + +show |
