ray 0.0.1 → 0.1.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (188) hide show
  1. data/.gemtest +0 -0
  2. data/.yardopts +4 -0
  3. data/README.md +17 -21
  4. data/Rakefile +18 -139
  5. data/VERSION +1 -1
  6. data/ext/audio.cpp +723 -0
  7. data/ext/{color.c → color.cpp} +25 -13
  8. data/ext/drawable.cpp +91 -0
  9. data/ext/event.cpp +460 -0
  10. data/ext/extconf.rb +5 -104
  11. data/ext/font.cpp +190 -0
  12. data/ext/image.cpp +733 -0
  13. data/ext/input.cpp +74 -0
  14. data/ext/ray.cpp +168 -0
  15. data/ext/ray.hpp +356 -0
  16. data/ext/{rect.c → rect.cpp} +51 -37
  17. data/ext/shader.cpp +169 -0
  18. data/ext/shape.cpp +409 -0
  19. data/ext/sprite.cpp +306 -0
  20. data/ext/text.cpp +181 -0
  21. data/ext/vector.cpp +215 -0
  22. data/guide.md +619 -0
  23. data/lib/ray/audio.rb +0 -41
  24. data/lib/ray/color.rb +32 -10
  25. data/lib/ray/drawable.rb +16 -0
  26. data/lib/ray/dsl/event_listener.rb +25 -2
  27. data/lib/ray/dsl/event_runner.rb +33 -5
  28. data/lib/ray/dsl/event_translator.rb +66 -30
  29. data/lib/ray/dsl/handler.rb +3 -2
  30. data/lib/ray/dsl/matcher.rb +58 -14
  31. data/lib/ray/font.rb +38 -96
  32. data/lib/ray/font_set.rb +8 -8
  33. data/lib/ray/game.rb +87 -66
  34. data/lib/ray/helper.rb +105 -10
  35. data/lib/ray/image.rb +150 -24
  36. data/lib/ray/image_set.rb +3 -1
  37. data/lib/ray/input.rb +10 -0
  38. data/lib/ray/music_set.rb +5 -3
  39. data/lib/ray/ray.rb +21 -9
  40. data/lib/ray/rect.rb +48 -7
  41. data/lib/ray/rmagick.rb +41 -0
  42. data/lib/ray/scene.rb +99 -43
  43. data/lib/ray/scene_list.rb +67 -0
  44. data/lib/ray/shape.rb +132 -0
  45. data/lib/ray/sound_set.rb +4 -2
  46. data/lib/ray/sprite.rb +49 -111
  47. data/lib/ray/text.rb +101 -0
  48. data/lib/ray/text_helper.rb +37 -0
  49. data/lib/ray/turtle.rb +215 -0
  50. data/lib/ray/vector.rb +226 -0
  51. data/samples/audio/spacial.rb +44 -0
  52. data/samples/hello_world/hello.rb +9 -13
  53. data/samples/hello_world/hello_dsl.rb +8 -12
  54. data/samples/hello_world/text.rb +15 -0
  55. data/samples/opengl/binding.rb +38 -0
  56. data/samples/opengl/image.rb +32 -0
  57. data/samples/opengl/opengl.rb +34 -0
  58. data/samples/opengl/shader.rb +42 -0
  59. data/samples/pong/pong.rb +14 -10
  60. data/samples/run_scene.rb +53 -0
  61. data/samples/shaders/scene.rb +40 -0
  62. data/samples/shaders/shaders.rb +42 -0
  63. data/samples/shaders/shape.rb +34 -0
  64. data/samples/sokoban/sokoban.rb +18 -18
  65. data/samples/test/actual_scene.rb +41 -0
  66. data/samples/test/scene_riot.rb +39 -0
  67. data/samples/test/scene_spec.rb +32 -0
  68. data/samples/test/scene_test_unit.rb +25 -0
  69. data/samples/turtle/byzantium.rb +45 -0
  70. data/samples/turtle/hilbert.rb +48 -0
  71. data/samples/turtle/koch.rb +55 -0
  72. data/samples/turtle/mandala.rb +61 -0
  73. data/samples/turtle/tree.rb +57 -0
  74. data/test/audio_test.rb +69 -0
  75. data/test/color_test.rb +77 -0
  76. data/test/drawable_test.rb +19 -0
  77. data/test/dsl_test.rb +93 -0
  78. data/test/font_test.rb +57 -0
  79. data/test/helpers.rb +94 -0
  80. data/test/image_test.rb +82 -0
  81. data/test/ray_test.rb +25 -0
  82. data/test/rect_test.rb +121 -0
  83. data/{spec → test}/res/VeraMono.ttf +0 -0
  84. data/{spec → test}/res/aqua.bmp +0 -0
  85. data/{spec → test}/res/aqua.png +0 -0
  86. data/{spec → test}/res/aqua2.bmp +0 -0
  87. data/{spec → test}/res/not_a_jpeg.jpeg +0 -0
  88. data/{spec → test}/res/pop.wav +0 -0
  89. data/test/resource_set_test.rb +99 -0
  90. data/test/run_all.rb +7 -0
  91. data/test/shape_test.rb +101 -0
  92. data/test/sprite_test.rb +89 -0
  93. data/test/text_test.rb +78 -0
  94. data/test/turtle_test.rb +176 -0
  95. data/test/vector_test.rb +111 -0
  96. data/yard_ext.rb +0 -28
  97. metadata +95 -139
  98. data/.gitignore +0 -23
  99. data/.gitmodules +0 -3
  100. data/.rspec +0 -3
  101. data/ext/audio.c +0 -473
  102. data/ext/event.c +0 -557
  103. data/ext/font.c +0 -287
  104. data/ext/image.c +0 -933
  105. data/ext/joystick.c +0 -145
  106. data/ext/ray.c +0 -489
  107. data/ext/ray.h +0 -245
  108. data/ext/ray_osx.m +0 -161
  109. data/lib/ray/joystick.rb +0 -30
  110. data/psp/SDL_psp_main.c +0 -84
  111. data/psp/bigdecimal/README +0 -60
  112. data/psp/bigdecimal/bigdecimal.c +0 -4697
  113. data/psp/bigdecimal/bigdecimal.h +0 -216
  114. data/psp/bigdecimal/lib/bigdecimal/jacobian.rb +0 -85
  115. data/psp/bigdecimal/lib/bigdecimal/ludcmp.rb +0 -84
  116. data/psp/bigdecimal/lib/bigdecimal/math.rb +0 -235
  117. data/psp/bigdecimal/lib/bigdecimal/newton.rb +0 -77
  118. data/psp/bigdecimal/lib/bigdecimal/util.rb +0 -65
  119. data/psp/digest/bubblebabble/bubblebabble.c +0 -142
  120. data/psp/digest/defs.h +0 -20
  121. data/psp/digest/digest.c +0 -643
  122. data/psp/digest/digest.h +0 -32
  123. data/psp/digest/lib/digest.rb +0 -50
  124. data/psp/digest/lib/md5.rb +0 -27
  125. data/psp/digest/lib/sha1.rb +0 -27
  126. data/psp/digest/md5/md5.c +0 -420
  127. data/psp/digest/md5/md5.h +0 -80
  128. data/psp/digest/md5/md5init.c +0 -40
  129. data/psp/digest/rmd160/rmd160.c +0 -457
  130. data/psp/digest/rmd160/rmd160.h +0 -56
  131. data/psp/digest/rmd160/rmd160init.c +0 -40
  132. data/psp/digest/sha1/sha1.c +0 -269
  133. data/psp/digest/sha1/sha1.h +0 -39
  134. data/psp/digest/sha1/sha1init.c +0 -40
  135. data/psp/digest/sha2/lib/sha2.rb +0 -73
  136. data/psp/digest/sha2/sha2.c +0 -919
  137. data/psp/digest/sha2/sha2.h +0 -109
  138. data/psp/digest/sha2/sha2init.c +0 -52
  139. data/psp/enumerator/enumerator.c +0 -298
  140. data/psp/etc/etc.c +0 -559
  141. data/psp/ext.c +0 -289
  142. data/psp/fcntl/fcntl.c +0 -187
  143. data/psp/lib/rbconfig.rb +0 -178
  144. data/psp/nkf/lib/kconv.rb +0 -367
  145. data/psp/nkf/nkf-utf8/config.h +0 -88
  146. data/psp/nkf/nkf-utf8/nkf.c +0 -6040
  147. data/psp/nkf/nkf-utf8/utf8tbl.c +0 -8500
  148. data/psp/nkf/nkf-utf8/utf8tbl.h +0 -34
  149. data/psp/nkf/nkf.c +0 -654
  150. data/psp/socket/addrinfo.h +0 -173
  151. data/psp/socket/getaddrinfo.c +0 -676
  152. data/psp/socket/getnameinfo.c +0 -270
  153. data/psp/socket/pspsocket.c +0 -71
  154. data/psp/socket/pspsocket.h +0 -28
  155. data/psp/socket/socket.c +0 -4662
  156. data/psp/socket/sockport.h +0 -76
  157. data/psp/stringio/stringio.c +0 -1306
  158. data/psp/strscan/strscan.c +0 -1320
  159. data/psp/syck/bytecode.c +0 -1166
  160. data/psp/syck/emitter.c +0 -1242
  161. data/psp/syck/gram.c +0 -1894
  162. data/psp/syck/gram.h +0 -79
  163. data/psp/syck/handler.c +0 -174
  164. data/psp/syck/implicit.c +0 -2990
  165. data/psp/syck/node.c +0 -408
  166. data/psp/syck/rubyext.c +0 -2367
  167. data/psp/syck/syck.c +0 -504
  168. data/psp/syck/syck.h +0 -456
  169. data/psp/syck/token.c +0 -2725
  170. data/psp/syck/yaml2byte.c +0 -257
  171. data/psp/syck/yamlbyte.h +0 -170
  172. data/psp/thread/thread.c +0 -1175
  173. data/psp/zlib/zlib.c +0 -3547
  174. data/script.rb +0 -10
  175. data/spec/ray/audio_spec.rb +0 -146
  176. data/spec/ray/color_spec.rb +0 -57
  177. data/spec/ray/event_spec.rb +0 -80
  178. data/spec/ray/font_spec.rb +0 -93
  179. data/spec/ray/image_set_spec.rb +0 -48
  180. data/spec/ray/image_spec.rb +0 -162
  181. data/spec/ray/joystick_spec.rb +0 -21
  182. data/spec/ray/matcher_spec.rb +0 -50
  183. data/spec/ray/ray_spec.rb +0 -88
  184. data/spec/ray/rect_spec.rb +0 -154
  185. data/spec/ray/resource_set_spec.rb +0 -105
  186. data/spec/ray/sprite_spec.rb +0 -163
  187. data/spec/spec.opts +0 -4
  188. data/spec/spec_helper.rb +0 -8
File without changes
@@ -0,0 +1,4 @@
1
+ --private
2
+ -e ./yard_ext.rb
3
+ -
4
+ guide.md
data/README.md CHANGED
@@ -4,24 +4,24 @@
4
4
 
5
5
  class HelloScene < Ray::Scene
6
6
  scene_name :hello
7
-
7
+
8
8
  def setup
9
- @font = font(path_of("VeraMono.ttf"), 12)
9
+ @font = font("VeraMono.ttf")
10
10
  end
11
-
11
+
12
12
  def render(win)
13
- @font.draw("Hello world!", :on => win, :at => [0, 0])
13
+ @font.draw("Hello world!", :on => win, :at => [0, 0], :size => 12)
14
14
  end
15
15
  end
16
16
 
17
17
  class HelloWorld < Ray::Game
18
18
  def initialize
19
19
  super("Hello world!")
20
-
20
+
21
21
  HelloScene.bind(self)
22
22
  push_scene :hello
23
23
  end
24
-
24
+
25
25
  def register
26
26
  add_hook :quit, method(:exit!)
27
27
  end
@@ -35,28 +35,24 @@
35
35
  register do
36
36
  add_hook :quit, method(:exit!)
37
37
  end
38
-
38
+
39
39
  scene :hello do
40
- @font = font(path_of("VeraMono.ttf"), 12)
41
-
40
+ @font = font("VeraMono.ttf")
41
+
42
42
  render do |win|
43
- @font.draw("Hello world", :on => win, :at => [0, 0])
43
+ @font.draw("Hello world", :on => win, :at => [0, 0], :size => 12)
44
44
  end
45
45
  end
46
-
46
+
47
47
  push_scene :hello
48
48
  end
49
- # Installation
50
- Run the following command:
51
49
 
52
- gem install ray
53
-
54
- This requires the SDL to be installed. SDL_TTF, SDL_image, SDL_gfx, and
55
- SDL_mixer are also needed for several features, though you can install ray
56
- without them.
57
50
  # Features
58
- * Hides the event loop where registred blocks or methods are run when something
51
+ * Can hide the event loop where registred blocks or methods are run when something
59
52
  happens.
60
53
  * Provides a cache for resources like images, fonts, sounds, ...
61
- * Documented, using YARD. You can check the documentation on
62
- [rubydoc.info](http://rubydoc.info/github/Mon-Ouie/ray/master/frames)
54
+ * Implements turtle graphics
55
+ * Nothing more serious than a toy.
56
+
57
+ # Installation & Getting started
58
+ See [the guide](guide.md)
data/Rakefile CHANGED
@@ -3,7 +3,7 @@ begin
3
3
  require 'yard'
4
4
 
5
5
  YARD::Rake::YardocTask.new do |t|
6
- t.files = ['lib/**/*.rb', 'ext/**/*.c', 'psp/*.c']
6
+ t.files = ['lib/**/*.rb', 'ext/**/*.cpp']
7
7
  t.options |= ["--private", "-e", "./yard_ext.rb"]
8
8
  end
9
9
  rescue LoadError
@@ -25,42 +25,24 @@ begin
25
25
  s.authors = ["Mon ouïe"]
26
26
 
27
27
  s.files |= FileList["lib/**/*.rb"]
28
- s.files |= FileList["ext/**/*.{c,h,m}"]
28
+ s.files |= FileList["ext/**/*.{cpp,h}"]
29
+ s.files |= FileList["*.md"]
30
+ s.files << ".gemtest" << "Rakefile"
29
31
 
30
32
  s.has_rdoc = "yard"
31
33
 
32
34
  s.extensions = ["ext/extconf.rb"]
33
-
35
+
34
36
  s.add_development_dependency "jeweler"
35
37
  s.add_development_dependency "yard"
36
- s.add_development_dependency "rspec"
37
- s.add_development_dependency "psp_task"
38
+ s.add_development_dependency "riot"
39
+ s.add_development_dependency "rr"
38
40
  end
39
41
  rescue LoadError
40
42
  $stderr.puts("Jeweler is not installed. Please install it " +
41
43
  "with the following command: gem intall jeweler")
42
44
  end
43
45
 
44
- begin
45
- begin
46
- require 'spec/rake/spectask'
47
-
48
- Spec::Rake::SpecTask.new(:spec) do |t|
49
- t.spec_files = FileList['spec/**/*_spec\.rb']
50
- t.fail_on_error = false
51
- end
52
- rescue LoadError
53
- require 'rspec/core/rake_task'
54
-
55
- RSpec::Core::RakeTask.new(:spec) do |t|
56
- t.fail_on_error = false
57
- end
58
- end
59
- rescue LoadError
60
- $stderr.puts("RSpec is not installed. Please install it " +
61
- "with the following command: gem install rspec")
62
- end
63
-
64
46
  namespace :ext do
65
47
  desc "Rebuilds the C extension"
66
48
  task :rebuild do
@@ -78,123 +60,20 @@ end
78
60
  desc "Builds the C extension"
79
61
  task :ext => ["ext:build"]
80
62
 
81
- begin
82
- require 'rake/psp_task'
83
-
84
- libs = %w(socket/socket.c socket/getnameinfo.c socket/getaddrinfo.c
85
- socket/pspsocket.c
86
- bigdecimal/bigdecimal.c
87
- digest/sha2/sha2init.c digest/sha2/sha2.c
88
- digest/rmd160/rmd160.c digest/rmd160/rmd160init.c
89
- digest/bubblebabble/bubblebabble.c
90
- digest/sha1/sha1.c digest/sha1/sha1init.c
91
- digest/md5/md5.c digest/md5/md5init.c
92
- digest/digest.c
93
- enumerator/enumerator.c
94
- fcntl/fcntl.c
95
- stringio/stringio.c
96
- strscan/strscan.c
97
- thread/thread.c
98
- zlib/zlib.c
99
- nkf/nkf.c
100
- etc/etc.c
101
- syck/bytecode.c syck/gram.c syck/implicit.c syck/rubyext.c
102
- syck/token.c syck/emitter.c syck/handler.c syck/node.c syck/syck.c
103
- syck/yaml2byte.c).map { |i| "psp/#{i}" }
104
-
105
- static_libs = ["-lSDL_ttf", "-lSDL_mixer", "-lSDL_gfx", "-lSDL_image",
106
- "-lSDL", "-lGL", "-lpspvfpu", "-lfreetype", "-lvorbisfile",
107
- "-lvorbis", "-lmikmod", "-logg",
108
- "-lpspgu", "-lpspaudio", "-lpsprtc", "-lpsphprm", "-lpspwlan",
109
- "-lruby", "-lpng", "-ljpeg", "-lz","-lm", "-lc"]
110
-
111
- flags = ["-Wall", "-Wno-unused-parameter", "-D_PSP_ -DHAVE_STRUCT_TIMESPEC",
112
- "-DHAVE_SDL_SDL_IMAGE_H", "-G0", "-DHAVE_SDL_SDL_ROTOZOOM_H",
113
- "-DHAVE_SDL_SDL_TTF_H", "-DHAVE_SDL_SDL_MIXER_H", "-DUSE_RWOPS"]
114
-
115
- begin
116
- PSPTask.new do |t|
117
- t.objdir = "./obj/"
118
- t.srcs = Dir['ext/*.c', 'psp/*.c'] | libs
119
- t.libs = static_libs | t.libs
120
- t.cflags |= flags
121
- t.incdir << "-I./ext" << "-I./psp"
122
- t.target = "ray_release"
123
-
124
- t.build_prx = true
125
- t.build_eboot = true
126
- end
127
-
128
- PSPTask.new(:debug) do |t|
129
- t.objdir = "./obj_dbg/"
130
- t.srcs = Dir['ext/*.c', 'psp/*.c'] | libs
131
- t.libs = static_libs | t.libs
132
- t.cflags |= (flags | ["-O0", "-g"])
133
- t.incdir << "-I./ext" << "-I./psp"
134
- t.target = "ray"
135
-
136
- t.build_prx = true
137
- end
138
- rescue
139
- $stderr.puts "could not create PSP tasks"
140
- end
141
- rescue LoadError
142
- $stderr.puts("psp_task is not installed. Please install it " +
143
- "with the following command: gem intall psp_task")
144
- end
145
-
146
- require 'net/ftp'
147
-
148
- file "ruby-1.8.7-p248.tar.bz2" do
149
- ftp = Net::FTP.new
150
- ftp.connect("ftp.ruby-lang.org", 21)
151
- ftp.login("anonymous", "")
152
- puts "Downloading ruby-1.8.7-p248.tar.bz2..."
153
- ftp.getbinaryfile("pub/ruby/1.8/ruby-1.8.7-p248.tar.bz2",
154
- "ruby-1.8.7-p248.tar.bz2")
155
- ftp.close
156
- end
63
+ desc "Starts IRB"
64
+ task :irb do
65
+ require 'irb'
66
+ ARGV.clear
157
67
 
158
- directory "ruby_stdlib"
159
- file "ruby_stdlib" => ["ruby-1.8.7-p248.tar.bz2"] do
160
- sh "tar xvf ruby-1.8.7-p248.tar.bz2"
161
- cp_r "ruby-1.8.7-p248/lib", "ruby_stdlib"
68
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__), "ext"))
69
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__), "lib"))
162
70
 
163
- cp_r "psp/bigdecimal/lib/bigdecimal", "ruby_stdlib/lib"
71
+ require 'ray'
164
72
 
165
- mkdir_p "ruby_stdlib/lib/digest" unless File.exist? "ruby_stdlib/lib/digest"
166
- cp "psp/digest/lib/digest.rb", "ruby_stdlib/lib"
167
- cp "psp/digest/lib/sha1.rb", "ruby_stdlib/lib/digest"
168
- cp "psp/digest/lib/md5.rb", "ruby_stdlib/lib/digest"
169
-
170
- cp "psp/nkf/lib/kconv.rb", "ruby_stdlib/lib"
73
+ IRB.start
171
74
  end
172
75
 
173
- ruby_libs = ["socket.rb", "bigdecimal.rb", "digest/bubblebabble.rb",
174
- "fcntl.rb", "stringio.rb", "strscan.rb", "thread.rb",
175
- "zlib.rb", "nkf.rb", "etc.rb", "syck.rb", "enumerator.rb"]
176
-
177
- ruby_libs.each do |filename|
178
- ext_name = case filename
179
- when /(\w+)\.rb/
180
- $1
181
- when /\w+\/(\w+)\.rb/
182
- $1
183
- else
184
- filename
185
- end
186
-
187
- dir = File.dirname("ruby_stdlib/lib/#{filename}")
188
-
189
- directory dir
190
- file "ruby_stdlib/lib/#{filename}" => ["ruby_stdlib", dir] do
191
- open("ruby_stdlib/lib/#{filename}", 'w') do |file|
192
- file.puts "Ray.init_internal(\"#{ext_name}\")"
193
- end
194
- end
76
+ desc "Runs tests"
77
+ task :test do
78
+ load File.expand_path(File.join(File.dirname(__FILE__), "test", "run_all.rb"))
195
79
  end
196
-
197
- ruby_libs.map! { |i| "ruby_stdlib/lib/#{i}" }
198
-
199
- desc "Builds dependencies for running the specs on a PSP"
200
- task :psp_spec => (['debug:prx'] | ruby_libs)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.1.0.pre1
@@ -0,0 +1,723 @@
1
+ #include "ray.hpp"
2
+
3
+ void ray_audio_source::set_pitch(float value) {
4
+ source().SetPitch(value);
5
+ }
6
+
7
+ float ray_audio_source::pitch() const {
8
+ return source().GetPitch();
9
+ }
10
+
11
+ void ray_audio_source::set_position(const ray_vector3 &vector) {
12
+ source().SetPosition(vector);
13
+ }
14
+
15
+ ray_vector3 ray_audio_source::position() const {
16
+ return source().GetPosition();
17
+ }
18
+
19
+ void ray_audio_source::set_relative(bool value) {
20
+ source().SetRelativeToListener(value);
21
+ }
22
+
23
+ bool ray_audio_source::is_relative() const {
24
+ return source().IsRelativeToListener();
25
+ }
26
+
27
+ void ray_audio_source::set_min_distance(float value) {
28
+ source().SetMinDistance(value);
29
+ }
30
+
31
+ float ray_audio_source::min_distance() const {
32
+ return source().GetMinDistance();
33
+ }
34
+
35
+ void ray_audio_source::set_attenuation(float value) {
36
+ source().SetAttenuation(value);
37
+ }
38
+
39
+ float ray_audio_source::attenuation() const {
40
+ return source().GetAttenuation();
41
+ }
42
+
43
+ float ray_audio_source::volume() const {
44
+ return source().GetVolume();
45
+ }
46
+
47
+ void ray_audio_source::set_volume(float value) {
48
+ source().SetVolume(value);
49
+ }
50
+
51
+ void ray_sound::set_loop(bool value) {
52
+ sound.SetLoop(value);
53
+ }
54
+
55
+ bool ray_sound::is_looping() const {
56
+ return sound.GetLoop();
57
+ }
58
+
59
+ bool ray_sound::load_from_io(VALUE io) {
60
+ VALUE string = rb_funcall2(io, RAY_METH("read"), 0, NULL);
61
+ char *content = StringValuePtr(string);
62
+
63
+ if (!sound_buffer.LoadFromMemory(content, RSTRING_LEN(string)))
64
+ return false;
65
+
66
+ sound.SetBuffer(sound_buffer);
67
+ return true;
68
+ }
69
+
70
+ bool ray_sound::load_from_filename(const std::string &filename) {
71
+ if (!sound_buffer.LoadFromFile(filename))
72
+ return false;
73
+ sound.SetBuffer(sound_buffer);
74
+ return true;
75
+ }
76
+
77
+ bool ray_sound::create_from_sound(const ray_sound &source) {
78
+ sound_buffer = source.sound_buffer;
79
+ sound = source.sound;
80
+
81
+ sound.SetBuffer(sound_buffer);
82
+
83
+ return true;
84
+ }
85
+
86
+ void ray_sound::play() {
87
+ sound.Play();
88
+ }
89
+
90
+ void ray_sound::pause() {
91
+ sound.Pause();
92
+ }
93
+
94
+ void ray_sound::stop() {
95
+ sound.Stop();
96
+ }
97
+
98
+ sf::SoundSource::Status ray_sound::status() const {
99
+ return sound.GetStatus();
100
+ }
101
+
102
+ float ray_sound::current_time() const {
103
+ return sound.GetPlayingOffset();
104
+ }
105
+
106
+ void ray_sound::set_current_time(float value) {
107
+ sound.SetPlayingOffset(value);
108
+ }
109
+
110
+ sf::SoundSource &ray_sound::source() {
111
+ return sound;
112
+ }
113
+
114
+ const sf::SoundSource &ray_sound::source() const {
115
+ return sound;
116
+ }
117
+
118
+ bool ray_music::load_from_io(VALUE self, VALUE io) {
119
+ VALUE string = rb_funcall2(io, RAY_METH("read"), 0, NULL);
120
+ rb_iv_set(self, "@content", string);
121
+
122
+ char *content = StringValuePtr(string);
123
+ return music.OpenFromMemory(content, RSTRING_LEN(string));
124
+ }
125
+
126
+ bool ray_music::load_from_filename(const std::string &filename) {
127
+ return music.OpenFromFile(filename);
128
+ }
129
+
130
+ void ray_music::play() {
131
+ music.Play();
132
+ }
133
+
134
+ void ray_music::pause() {
135
+ music.Pause();
136
+ }
137
+
138
+ void ray_music::stop() {
139
+ music.Stop();
140
+ }
141
+
142
+ sf::SoundSource::Status ray_music::status() const {
143
+ return music.GetStatus();
144
+ }
145
+
146
+ void ray_music::set_loop(bool value) {
147
+ music.SetLoop(value);
148
+ }
149
+
150
+ bool ray_music::is_looping() const {
151
+ return music.GetLoop();
152
+ }
153
+
154
+ float ray_music::current_time() const {
155
+ return music.GetPlayingOffset();
156
+ }
157
+
158
+ void ray_music::set_current_time(float value) {
159
+ music.SetPlayingOffset(value);
160
+ }
161
+
162
+ float ray_music::duration() const {
163
+ return music.GetDuration();
164
+ }
165
+
166
+ int ray_music::sample_rate() const {
167
+ return music.GetSampleRate();
168
+ }
169
+
170
+ int ray_music::channel_count() const {
171
+ return music.GetChannelsCount();
172
+ }
173
+
174
+ sf::SoundSource &ray_music::source() {
175
+ return music;
176
+ }
177
+
178
+ const sf::SoundSource &ray_music::source() const {
179
+ return music;
180
+ }
181
+
182
+ VALUE ray_mAudio = Qnil;
183
+ VALUE ray_cSound = Qnil;
184
+ VALUE ray_cMusic = Qnil;
185
+ VALUE ray_cAudioSource = Qnil;
186
+
187
+ /*
188
+ @return [Float] The global volume. 0 is the minimum volume, whereas 100
189
+ is the maximum.
190
+ */
191
+ VALUE ray_audio_volume(VALUE self) {
192
+ float volume = sf::Listener::GetGlobalVolume();
193
+ return rb_float_new(volume * 100);
194
+ }
195
+
196
+ /* Sets the global volume */
197
+ VALUE ray_audio_set_volume(VALUE self, VALUE value) {
198
+ float volume = (float)NUM2DBL(value);
199
+ sf::Listener::SetGlobalVolume(volume);
200
+
201
+ return value;
202
+ }
203
+
204
+ /*
205
+ @return [Ray::Vector3] The position of the listener.
206
+ */
207
+ VALUE ray_audio_pos(VALUE self) {
208
+ sf::Vector3f pos = sf::Listener::GetPosition();
209
+ return ray_vector3_to_rb(pos);
210
+ }
211
+
212
+ /*
213
+ @overload pos=(pos)
214
+ @param [Ray::Vector3, #to_vector3] pos The [x, y, z] position of the listener.
215
+ */
216
+ VALUE ray_audio_set_pos(VALUE self, VALUE pos) {
217
+ sf::Listener::SetPosition(ray_convert_to_vector3(pos));
218
+ return pos;
219
+ }
220
+
221
+ /*
222
+ @return [Ray::Vector3] The orientation of the listener.
223
+ */
224
+ VALUE ray_audio_direction(VALUE self) {
225
+ sf::Vector3f dir = sf::Listener::GetDirection();
226
+ return ray_vector3_to_rb(dir);
227
+ }
228
+
229
+ /*
230
+ @overload direction=(dir)
231
+ @param [Ray::Vector3, #to_vector3] dir The orientation of the listener.
232
+ */
233
+ VALUE ray_audio_set_direction(VALUE self, VALUE dir) {
234
+ sf::Listener::SetDirection(ray_convert_to_vector3(dir));
235
+ return dir;
236
+ }
237
+
238
+ ray_audio_source *ray_rb2source(VALUE object) {
239
+ if (!RAY_IS_A(object, ray_cAudioSource)) {
240
+ rb_raise(rb_eTypeError, "Can't convert %s into Ray::AudioSource",
241
+ RAY_OBJ_CLASSNAME(object));
242
+ }
243
+
244
+ ray_audio_source *ptr = NULL;
245
+ Data_Get_Struct(object, ray_audio_source, ptr);
246
+
247
+ return ptr;
248
+ }
249
+
250
+ /* @return [Float] Volume of the source, between 0 and 100. */
251
+ VALUE ray_audio_source_volume(VALUE self) {
252
+ ray_audio_source *source = ray_rb2source(self);
253
+ return rb_float_new(source->volume());
254
+ }
255
+
256
+ /* Sets the volume of the source. */
257
+ VALUE ray_audio_source_set_volume(VALUE self, VALUE value) {
258
+ rb_check_frozen(self);
259
+
260
+ ray_audio_source *source = ray_rb2source(self);
261
+ float volume = (float)NUM2DBL(value);
262
+ source->set_volume(volume);
263
+
264
+ return value;
265
+ }
266
+
267
+ /* @return [Float] The pitch of the source. */
268
+ VALUE ray_audio_source_pitch(VALUE self) {
269
+ ray_audio_source *source = ray_rb2source(self);
270
+ return rb_float_new(source->pitch());
271
+ }
272
+
273
+ /*
274
+ @overload pitch=(pitch)
275
+ @param [Float] pitch the pitch value, which makes a sound more acute
276
+ or grave and also affects playing speed. Defaulted to 1.
277
+ */
278
+ VALUE ray_audio_source_set_pitch(VALUE self, VALUE pitch) {
279
+ rb_check_frozen(self);
280
+
281
+ ray_audio_source *source = ray_rb2source(self);
282
+ source->set_pitch(NUM2DBL(pitch));
283
+
284
+ return pitch;
285
+ }
286
+
287
+ /* @return [Ray::Vector3] The position of the source */
288
+ VALUE ray_audio_source_pos(VALUE self) {
289
+ ray_audio_source *source = ray_rb2source(self);
290
+ return ray_vector3_to_rb(source->position());
291
+ }
292
+
293
+ /*
294
+ @overload pos=(pos)
295
+ @param [Ray::Vector3, #to_vector3] pos The position of the source
296
+ */
297
+ VALUE ray_audio_source_set_pos(VALUE self, VALUE pos) {
298
+ rb_check_frozen(self);
299
+
300
+ ray_audio_source *source = ray_rb2source(self);
301
+ source->set_position(ray_convert_to_vector3(pos));
302
+
303
+ return pos;
304
+ }
305
+
306
+ /*
307
+ @return [true, false] True if the source position is relative to the listener,
308
+ as opposed to being absolute.
309
+ */
310
+ VALUE ray_audio_source_is_relative(VALUE self) {
311
+ ray_audio_source *source = ray_rb2source(self);
312
+ return source->is_relative() ? Qtrue : Qfalse;
313
+ }
314
+
315
+ /*
316
+ @overload relative=(rel)
317
+ @param [true, false] rel Whether the source position should be relative
318
+ to the listener's.
319
+ */
320
+ VALUE ray_audio_source_set_relative(VALUE self, VALUE val) {
321
+ rb_check_frozen(self);
322
+
323
+ ray_audio_source *source = ray_rb2source(self);
324
+ source->set_relative(RTEST(val));
325
+ return val;
326
+ }
327
+
328
+ /* @return [Float] the source's minimal distance */
329
+ VALUE ray_audio_source_min_distance(VALUE self) {
330
+ ray_audio_source *source = ray_rb2source(self);
331
+ return rb_float_new(source->min_distance());
332
+ }
333
+
334
+ /*
335
+ @overload min_distance=(dist)
336
+ @param [Float] dist the distance beyond which the source's volume will
337
+ decrease. Defaulted to 1.
338
+ */
339
+ VALUE ray_audio_source_set_min_distance(VALUE self, VALUE dist) {
340
+ rb_check_frozen(self);
341
+
342
+ ray_audio_source *source = ray_rb2source(self);
343
+ source->set_min_distance(NUM2DBL(dist));
344
+ return dist;
345
+ }
346
+
347
+ /* @return [Float] The source's attenuation */
348
+ VALUE ray_audio_source_attenuation(VALUE self) {
349
+ ray_audio_source *source = ray_rb2source(self);
350
+ return rb_float_new(source->attenuation());
351
+ }
352
+
353
+ /*
354
+ @overload attenuation=(attenuation)
355
+ @param [Float] atenuation Attenuation factor of the sound.
356
+ 0 will prevent the sound from being attenuated, 100 would
357
+ make the sound be attenuated much more quickly.
358
+ */
359
+ VALUE ray_audio_source_set_attenuation(VALUE self, VALUE attenuation) {
360
+ rb_check_frozen(self);
361
+
362
+ ray_audio_source *source = ray_rb2source(self);
363
+ source->set_attenuation(NUM2DBL(attenuation));
364
+ return attenuation;
365
+ }
366
+
367
+ ray_sound *ray_rb2sound(VALUE object) {
368
+ if (!RAY_IS_A(object, ray_cSound)) {
369
+ rb_raise(rb_eTypeError, "Can't convert %s into Ray::Sound",
370
+ RAY_OBJ_CLASSNAME(object));
371
+ }
372
+
373
+ ray_sound *ptr = NULL;
374
+ Data_Get_Struct(object, ray_sound, ptr);
375
+
376
+ return ptr;
377
+ }
378
+
379
+ void ray_free_sound(ray_sound *sound) {
380
+ delete sound;
381
+ }
382
+
383
+ VALUE ray_alloc_sound(VALUE self) {
384
+ ray_sound *ptr = new ray_sound;
385
+ VALUE ret = Data_Wrap_Struct(self, 0, ray_free_sound, ptr);
386
+
387
+ return ret;
388
+ }
389
+
390
+ /*
391
+ @overload initialize(io_or_string)
392
+ Creates a new sound from an IO object or a filename.
393
+ */
394
+ VALUE ray_init_sound(VALUE self, VALUE arg) {
395
+ int res = false;
396
+ ray_sound *sound = ray_rb2sound(self);
397
+
398
+ if (rb_respond_to(arg, RAY_METH("to_str"))) {
399
+ arg = rb_String(arg);
400
+ res = sound->load_from_filename(StringValuePtr(arg));
401
+ }
402
+ else if (rb_respond_to(arg, RAY_METH("read")))
403
+ res = sound->load_from_io(arg);
404
+ else {
405
+ rb_raise(rb_eTypeError, "Can't convert %s into String",
406
+ RAY_OBJ_CLASSNAME(arg));
407
+ }
408
+
409
+ if (!res)
410
+ rb_raise(rb_eRuntimeError, "Could not load sound");
411
+
412
+ return Qnil;
413
+ }
414
+
415
+ VALUE ray_init_sound_copy(VALUE self, VALUE other) {
416
+ ray_sound *sound = ray_rb2sound(self);
417
+ ray_sound *source = ray_rb2sound(other);
418
+
419
+ if (!sound->create_from_sound(*source))
420
+ rb_raise(rb_eRuntimeError, "Couldn't copy the sound");
421
+
422
+ return self;
423
+ }
424
+
425
+ /* Plays the sound. */
426
+ VALUE ray_sound_play(VALUE self) {
427
+ rb_check_frozen(self);
428
+ ray_rb2sound(self)->play();
429
+ return self;
430
+ }
431
+
432
+ /* Pauses the sound. */
433
+ VALUE ray_sound_pause(VALUE self) {
434
+ rb_check_frozen(self);
435
+ ray_rb2sound(self)->pause();
436
+ return self;
437
+ }
438
+
439
+ /* Stops the sound. */
440
+ VALUE ray_sound_stop(VALUE self) {
441
+ rb_check_frozen(self);
442
+ ray_rb2sound(self)->stop();
443
+ return self;
444
+ }
445
+
446
+ /*
447
+ @return [Symbol] :playing, :paused, or :stopped.
448
+ */
449
+ VALUE ray_sound_status(VALUE self) {
450
+ ray_sound *sound = ray_rb2sound(self);
451
+
452
+ switch (sound->status()) {
453
+ case sf::SoundSource::Playing: return RAY_SYM("playing");
454
+ case sf::SoundSource::Paused: return RAY_SYM("paused");
455
+ case sf::SoundSource::Stopped: return RAY_SYM("stopped");
456
+ }
457
+
458
+ return Qnil;
459
+ }
460
+
461
+ /* Sets whether the sound should loop */
462
+ VALUE ray_sound_set_loop(VALUE self, VALUE val) {
463
+ rb_check_frozen(self);
464
+ ray_rb2sound(self)->set_loop(RTEST(val));
465
+ return val;
466
+ }
467
+
468
+ /* @return [true, false] True if the sound loops */
469
+ VALUE ray_sound_loop(VALUE self) {
470
+ return ray_rb2sound(self)->is_looping() ? Qtrue : Qfalse;
471
+ }
472
+
473
+ /* @return [Float] The current position in the sound */
474
+ VALUE ray_sound_current_time(VALUE self) {
475
+ return rb_float_new(ray_rb2sound(self)->current_time());
476
+ }
477
+
478
+ /* Sets the cursor to a given position */
479
+ VALUE ray_sound_set_current_time(VALUE self, VALUE val) {
480
+ rb_check_frozen(self);
481
+ ray_rb2sound(self)->set_current_time(NUM2DBL(val));
482
+ return val;
483
+ }
484
+
485
+ ray_music *ray_rb2music(VALUE object) {
486
+ if (!RAY_IS_A(object, ray_cMusic)) {
487
+ rb_raise(rb_eTypeError, "Can't convert %s into Ray::Music",
488
+ RAY_OBJ_CLASSNAME(object));
489
+ }
490
+
491
+ ray_music *ptr = NULL;
492
+ Data_Get_Struct(object, ray_music, ptr);
493
+
494
+ return ptr;
495
+ }
496
+
497
+ void ray_free_music(ray_music *music) {
498
+ delete music;
499
+ }
500
+
501
+ VALUE ray_alloc_music(VALUE self) {
502
+ ray_music *ptr = new ray_music;
503
+ VALUE ret = Data_Wrap_Struct(self, 0, ray_free_music, ptr);
504
+
505
+ return ret;
506
+ }
507
+
508
+ bool ray_init_music_with_filename(VALUE self, VALUE filename) {
509
+ ray_music *music = ray_rb2music(self);
510
+ return music->load_from_filename(StringValuePtr(filename));
511
+ }
512
+
513
+ bool ray_init_music_with_io(VALUE self, VALUE io) {
514
+ ray_music *music = ray_rb2music(self);
515
+ return music->load_from_io(self, io);
516
+ }
517
+
518
+ /*
519
+ @overload initialize(io_or_string)
520
+ Creates a new music from an IO object or a filename.
521
+ */
522
+ VALUE ray_init_music(VALUE self, VALUE arg) {
523
+ bool res = false;
524
+
525
+ if (rb_respond_to(arg, RAY_METH("to_str")))
526
+ res = ray_init_music_with_filename(self, rb_String(arg));
527
+ else if (rb_respond_to(arg, RAY_METH("read")))
528
+ res = ray_init_music_with_io(self, arg);
529
+ else {
530
+ rb_raise(rb_eTypeError, "Can't convert %s into String",
531
+ RAY_OBJ_CLASSNAME(arg));
532
+ }
533
+
534
+ if (!res)
535
+ rb_raise(rb_eRuntimeError, "Could not load music");
536
+
537
+ return Qnil;
538
+ }
539
+
540
+ /*
541
+ Plays the music.
542
+ */
543
+ VALUE ray_music_play(VALUE self) {
544
+ rb_check_frozen(self);
545
+
546
+ ray_music *music = ray_rb2music(self);
547
+ music->play();
548
+
549
+ return Qnil;
550
+ }
551
+
552
+ /*
553
+ Pauses the music.
554
+ */
555
+ VALUE ray_music_pause(VALUE self) {
556
+ rb_check_frozen(self);
557
+
558
+ ray_music *music = ray_rb2music(self);
559
+ music->pause();
560
+
561
+ return Qnil;
562
+ }
563
+
564
+ /*
565
+ Stops the music.
566
+ */
567
+ VALUE ray_music_stop(VALUE self) {
568
+ rb_check_frozen(self);
569
+
570
+ ray_music *music = ray_rb2music(self);
571
+ music->stop();
572
+
573
+ return Qnil;
574
+ }
575
+
576
+ /*
577
+ @return [Symbol] :playing, :paused, or :stopped.
578
+ */
579
+ VALUE ray_music_status(VALUE self) {
580
+ ray_music *music = ray_rb2music(self);
581
+
582
+ switch (music->status()) {
583
+ case sf::SoundSource::Playing: return RAY_SYM("playing");
584
+ case sf::SoundSource::Paused: return RAY_SYM("paused");
585
+ case sf::SoundSource::Stopped: return RAY_SYM("stopped");
586
+ }
587
+
588
+ return Qnil;
589
+ }
590
+
591
+ /* Sets whether the music should loop */
592
+ VALUE ray_music_set_loop(VALUE self, VALUE val) {
593
+ rb_check_frozen(self);
594
+ ray_rb2music(self)->set_loop(RTEST(val));
595
+ return val;
596
+ }
597
+
598
+ /* @return [true, false] True if the music loops */
599
+ VALUE ray_music_loop(VALUE self) {
600
+ return ray_rb2music(self)->is_looping() ? Qtrue : Qfalse;
601
+ }
602
+
603
+ /* @return [Float] The current position in the music */
604
+ VALUE ray_music_current_time(VALUE self) {
605
+ return rb_float_new(ray_rb2music(self)->current_time());
606
+ }
607
+
608
+ /* Sets the cursor to a given position */
609
+ VALUE ray_music_set_current_time(VALUE self, VALUE val) {
610
+ rb_check_frozen(self);
611
+ ray_rb2music(self)->set_current_time(NUM2DBL(val));
612
+ return val;
613
+ }
614
+
615
+ /* @return [Float] The duration of the music, in second */
616
+ VALUE ray_music_duration(VALUE self) {
617
+ return rb_float_new(ray_rb2music(self)->duration());
618
+ }
619
+
620
+ /* @return [Integer] The sample rate of the music */
621
+ VALUE ray_music_sample_rate(VALUE self) {
622
+ return INT2FIX(ray_rb2music(self)->sample_rate());
623
+ }
624
+
625
+ /* @return [Integer] The amount of channels */
626
+ VALUE ray_music_channel_count(VALUE self) {
627
+ return INT2FIX(ray_rb2music(self)->channel_count());
628
+ }
629
+
630
+ /*
631
+ Document-class: Ray::AudioSource
632
+ Represents an object emitting sound. The only point of having
633
+ this class is to avoid repetitions between Ray::Music and Ray:Audio.
634
+ */
635
+
636
+ /*
637
+ Document-class: Ray::Music
638
+ Class used to stream music.
639
+ */
640
+
641
+ /*
642
+ Document-class: Ray::Sound
643
+ Class used to represent short sounds.
644
+ */
645
+
646
+ void Init_ray_audio() {
647
+ ray_mAudio = rb_define_module_under(ray_mRay, "Audio");
648
+
649
+ rb_define_module_function(ray_mAudio, "volume", ray_audio_volume, 0);
650
+ rb_define_module_function(ray_mAudio, "volume=", ray_audio_set_volume, 1);
651
+
652
+ rb_define_module_function(ray_mAudio, "pos", ray_audio_pos, 0);
653
+ rb_define_module_function(ray_mAudio, "pos=", ray_audio_set_pos, 1);
654
+
655
+ rb_define_module_function(ray_mAudio, "direction", ray_audio_direction, 0);
656
+ rb_define_module_function(ray_mAudio, "direction=", ray_audio_set_direction,
657
+ 1);
658
+
659
+ ray_cAudioSource = rb_define_class_under(ray_mRay, "AudioSource", rb_cObject);
660
+
661
+ rb_define_method(ray_cAudioSource, "pitch", ray_audio_source_pitch, 0);
662
+ rb_define_method(ray_cAudioSource, "pitch=", ray_audio_source_set_pitch, 1);
663
+
664
+ rb_define_method(ray_cAudioSource, "pos", ray_audio_source_pos, 0);
665
+ rb_define_method(ray_cAudioSource, "pos=",
666
+ ray_audio_source_set_pos, 1);
667
+
668
+ rb_define_method(ray_cAudioSource, "relative?", ray_audio_source_is_relative,
669
+ 0);
670
+ rb_define_method(ray_cAudioSource, "relative=", ray_audio_source_set_relative,
671
+ 1);
672
+
673
+ rb_define_method(ray_cAudioSource, "min_distance",
674
+ ray_audio_source_min_distance, 0);
675
+ rb_define_method(ray_cAudioSource, "min_distance=",
676
+ ray_audio_source_set_min_distance, 1);
677
+
678
+ rb_define_method(ray_cAudioSource, "attenuation",
679
+ ray_audio_source_attenuation, 0);
680
+ rb_define_method(ray_cAudioSource, "attenuation=",
681
+ ray_audio_source_set_attenuation, 1);
682
+
683
+ rb_define_method(ray_cAudioSource, "volume", ray_audio_source_volume, 0);
684
+ rb_define_method(ray_cAudioSource, "volume=", ray_audio_source_set_volume, 1);
685
+
686
+ ray_cSound = rb_define_class_under(ray_mRay, "Sound", ray_cAudioSource);
687
+ rb_define_alloc_func(ray_cSound, ray_alloc_sound);
688
+ rb_define_method(ray_cSound, "initialize", ray_init_sound, 1);
689
+ rb_define_method(ray_cSound, "initialize_copy", ray_init_sound_copy, 1);
690
+
691
+ rb_define_method(ray_cSound, "play", ray_sound_play, 0);
692
+ rb_define_method(ray_cSound, "pause", ray_sound_pause, 0);
693
+ rb_define_method(ray_cSound, "stop", ray_sound_stop, 0);
694
+
695
+ rb_define_method(ray_cSound, "status", ray_sound_status, 0);
696
+
697
+ rb_define_method(ray_cSound, "loop=", ray_sound_set_loop, 1);
698
+ rb_define_method(ray_cSound, "loop?", ray_sound_loop, 0);
699
+
700
+ rb_define_method(ray_cSound, "current_time==", ray_sound_set_current_time, 1);
701
+ rb_define_method(ray_cSound, "current_time", ray_sound_current_time, 0);
702
+
703
+ ray_cMusic = rb_define_class_under(ray_mRay, "Music", ray_cAudioSource);
704
+ rb_define_alloc_func(ray_cMusic, ray_alloc_music);
705
+ rb_define_method(ray_cMusic, "initialize", ray_init_music, 1);
706
+
707
+ rb_define_method(ray_cMusic, "play", ray_music_play, 0);
708
+ rb_define_method(ray_cMusic, "pause", ray_music_pause, 0);
709
+ rb_define_method(ray_cMusic, "stop", ray_music_stop, 0);
710
+
711
+ rb_define_method(ray_cMusic, "status", ray_music_status, 0);
712
+
713
+ rb_define_method(ray_cMusic, "loop=", ray_music_set_loop, 1);
714
+ rb_define_method(ray_cMusic, "loop?", ray_music_loop, 0);
715
+
716
+ rb_define_method(ray_cMusic, "current_time==", ray_music_set_current_time, 1);
717
+ rb_define_method(ray_cMusic, "current_time", ray_music_current_time, 0);
718
+
719
+ rb_define_method(ray_cMusic, "duration", ray_music_duration, 0);
720
+
721
+ rb_define_method(ray_cMusic, "sample_rate", ray_music_sample_rate, 0);
722
+ rb_define_method(ray_cMusic, "channel_count", ray_music_channel_count, 0);
723
+ }