summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTom Black <[email protected]>2017-02-17 00:50:09 -0500
committerTom Black <[email protected]>2017-02-17 00:50:09 -0500
commite11976895af419e7411ae1d326779ff04909b6ca (patch)
tree912d41835e5600f2899942568ccb25656073591d
parentb017a89612b3e264467eeb481f2c4bfeee81e825 (diff)
downloadruby2d-e11976895af419e7411ae1d326779ff04909b6ca.tar.gz
ruby2d-e11976895af419e7411ae1d326779ff04909b6ca.zip
Add sound and music classes
-rwxr-xr-xbin/ruby2d2
-rw-r--r--lib/ruby2d.rb2
-rw-r--r--lib/ruby2d/music.rb16
-rw-r--r--lib/ruby2d/sound.rb14
-rw-r--r--test/audio.rb53
5 files changed, 78 insertions, 9 deletions
diff --git a/bin/ruby2d b/bin/ruby2d
index 06e1fd2..244e958 100755
--- a/bin/ruby2d
+++ b/bin/ruby2d
@@ -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