rubygame 2.3.0-x86-linux

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. data/CREDITS +60 -0
  2. data/LICENSE +504 -0
  3. data/NEWS +252 -0
  4. data/README +123 -0
  5. data/ROADMAP +109 -0
  6. data/Rakefile +438 -0
  7. data/doc/extended_readme.rdoc +49 -0
  8. data/doc/getting_started.rdoc +47 -0
  9. data/doc/macosx_install.rdoc +70 -0
  10. data/doc/windows_install.rdoc +123 -0
  11. data/ext/rubygame/rubygame_core.so +0 -0
  12. data/ext/rubygame/rubygame_event.c +644 -0
  13. data/ext/rubygame/rubygame_event.h +48 -0
  14. data/ext/rubygame/rubygame_event.o +0 -0
  15. data/ext/rubygame/rubygame_gfx.c +942 -0
  16. data/ext/rubygame/rubygame_gfx.h +101 -0
  17. data/ext/rubygame/rubygame_gfx.o +0 -0
  18. data/ext/rubygame/rubygame_gfx.so +0 -0
  19. data/ext/rubygame/rubygame_gl.c +154 -0
  20. data/ext/rubygame/rubygame_gl.h +32 -0
  21. data/ext/rubygame/rubygame_gl.o +0 -0
  22. data/ext/rubygame/rubygame_image.c +252 -0
  23. data/ext/rubygame/rubygame_image.h +41 -0
  24. data/ext/rubygame/rubygame_image.o +0 -0
  25. data/ext/rubygame/rubygame_image.so +0 -0
  26. data/ext/rubygame/rubygame_joystick.c +247 -0
  27. data/ext/rubygame/rubygame_joystick.h +41 -0
  28. data/ext/rubygame/rubygame_joystick.o +0 -0
  29. data/ext/rubygame/rubygame_main.c +155 -0
  30. data/ext/rubygame/rubygame_main.h +36 -0
  31. data/ext/rubygame/rubygame_main.o +0 -0
  32. data/ext/rubygame/rubygame_mixer.c +1024 -0
  33. data/ext/rubygame/rubygame_mixer.h +36 -0
  34. data/ext/rubygame/rubygame_mixer.o +0 -0
  35. data/ext/rubygame/rubygame_mixer.so +0 -0
  36. data/ext/rubygame/rubygame_music.c +1017 -0
  37. data/ext/rubygame/rubygame_music.h +29 -0
  38. data/ext/rubygame/rubygame_music.o +0 -0
  39. data/ext/rubygame/rubygame_screen.c +448 -0
  40. data/ext/rubygame/rubygame_screen.h +43 -0
  41. data/ext/rubygame/rubygame_screen.o +0 -0
  42. data/ext/rubygame/rubygame_shared.c +272 -0
  43. data/ext/rubygame/rubygame_shared.h +68 -0
  44. data/ext/rubygame/rubygame_shared.o +0 -0
  45. data/ext/rubygame/rubygame_sound.c +863 -0
  46. data/ext/rubygame/rubygame_sound.h +29 -0
  47. data/ext/rubygame/rubygame_sound.o +0 -0
  48. data/ext/rubygame/rubygame_surface.c +1151 -0
  49. data/ext/rubygame/rubygame_surface.h +62 -0
  50. data/ext/rubygame/rubygame_surface.o +0 -0
  51. data/ext/rubygame/rubygame_time.c +183 -0
  52. data/ext/rubygame/rubygame_time.h +32 -0
  53. data/ext/rubygame/rubygame_time.o +0 -0
  54. data/ext/rubygame/rubygame_ttf.c +599 -0
  55. data/ext/rubygame/rubygame_ttf.h +69 -0
  56. data/ext/rubygame/rubygame_ttf.o +0 -0
  57. data/ext/rubygame/rubygame_ttf.so +0 -0
  58. data/lib/rubygame.rb +41 -0
  59. data/lib/rubygame/clock.rb +128 -0
  60. data/lib/rubygame/color.rb +79 -0
  61. data/lib/rubygame/color/models/base.rb +111 -0
  62. data/lib/rubygame/color/models/hsl.rb +153 -0
  63. data/lib/rubygame/color/models/hsv.rb +149 -0
  64. data/lib/rubygame/color/models/rgb.rb +78 -0
  65. data/lib/rubygame/color/palettes/css.rb +49 -0
  66. data/lib/rubygame/color/palettes/palette.rb +100 -0
  67. data/lib/rubygame/color/palettes/x11.rb +177 -0
  68. data/lib/rubygame/constants.rb +238 -0
  69. data/lib/rubygame/event.rb +313 -0
  70. data/lib/rubygame/ftor.rb +370 -0
  71. data/lib/rubygame/hotspot.rb +265 -0
  72. data/lib/rubygame/keyconstants.rb +237 -0
  73. data/lib/rubygame/mediabag.rb +94 -0
  74. data/lib/rubygame/named_resource.rb +254 -0
  75. data/lib/rubygame/queue.rb +288 -0
  76. data/lib/rubygame/rect.rb +612 -0
  77. data/lib/rubygame/sfont.rb +223 -0
  78. data/lib/rubygame/sprite.rb +511 -0
  79. data/samples/FreeSans.ttf +0 -0
  80. data/samples/GPL.txt +340 -0
  81. data/samples/README +40 -0
  82. data/samples/chimp.bmp +0 -0
  83. data/samples/chimp.rb +302 -0
  84. data/samples/demo_gl.rb +151 -0
  85. data/samples/demo_gl_tex.rb +197 -0
  86. data/samples/demo_music.rb +77 -0
  87. data/samples/demo_rubygame.rb +296 -0
  88. data/samples/demo_sfont.rb +52 -0
  89. data/samples/demo_ttf.rb +193 -0
  90. data/samples/demo_utf8.rb +53 -0
  91. data/samples/fist.bmp +0 -0
  92. data/samples/load_and_blit.rb +22 -0
  93. data/samples/panda.png +0 -0
  94. data/samples/punch.wav +0 -0
  95. data/samples/ruby.png +0 -0
  96. data/samples/song.ogg +0 -0
  97. data/samples/term16.png +0 -0
  98. data/samples/whiff.wav +0 -0
  99. data/test/audio_spec.rb +236 -0
  100. data/test/color_spec.rb +544 -0
  101. data/test/image.png +0 -0
  102. data/test/music_spec.rb +727 -0
  103. data/test/named_resource_spec.rb +211 -0
  104. data/test/short.ogg +0 -0
  105. data/test/sound_spec.rb +564 -0
  106. data/test/surface_spec.rb +219 -0
  107. data/test/test_crop.rb +45 -0
  108. data/test/test_rect.rb +841 -0
  109. metadata +174 -0
@@ -0,0 +1,36 @@
1
+ /*
2
+ * Rubygame -- Ruby code and bindings to SDL to facilitate game creation
3
+ * Copyright (C) 2004-2007 John Croisant
4
+ *
5
+ * This library is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * This library is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with this library; if not, write to the Free Software
17
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+ *
19
+ */
20
+
21
+ #ifndef _RUBYGAME_H
22
+ #define _RUBYGAME_H
23
+
24
+ #ifndef RUBYGAME_MAJOR_VERSION
25
+
26
+ #define RUBYGAME_MAJOR_VERSION 0
27
+ #define RUBYGAME_MINOR_VERSION 0
28
+ #define RUBYGAME_PATCHLEVEL 0
29
+
30
+ #endif
31
+
32
+ extern VALUE rbgm_keyname(VALUE, VALUE);
33
+ extern VALUE rbgm_init(VALUE);
34
+ extern VALUE rbgm_quit(VALUE);
35
+
36
+ #endif
Binary file
@@ -0,0 +1,1024 @@
1
+ /*
2
+ * Interface to SDL_mixer, for audio playback and mixing.
3
+ *--
4
+ * Rubygame -- Ruby code and bindings to SDL to facilitate game creation
5
+ * Copyright (C) 2004-2007 John Croisant
6
+ *
7
+ * This library is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * This library is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with this library; if not, write to the Free Software
19
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
+ *++
21
+ */
22
+
23
+ #include "rubygame_shared.h"
24
+ #include "rubygame_mixer.h"
25
+ #include "rubygame_sound.h"
26
+ #include "rubygame_music.h"
27
+
28
+
29
+ VALUE mMixer;
30
+ VALUE cSample;
31
+ VALUE cOldMusic;
32
+
33
+
34
+ /* Return 1 if SDL_mixer audio is open, or 0 if it is not. */
35
+ int audio_is_open()
36
+ {
37
+ /* We don't actually care about these, but Mix_QuerySpec wants args. */
38
+ int frequency; Uint16 format; int channels;
39
+
40
+ int result = Mix_QuerySpec(&frequency, &format, &channels);
41
+
42
+ return ( (result > 0) ? 1 : 0 );
43
+ }
44
+
45
+ /*
46
+ * If SDL_mixer audio is not open, try to open it with the default
47
+ * arguments, and return the result. If it's already open, return 0
48
+ * without doing anything.
49
+ */
50
+ int ensure_open_audio()
51
+ {
52
+ if( audio_is_open() )
53
+ {
54
+ return 0;
55
+ }
56
+ else
57
+ {
58
+ return Mix_OpenAudio( MIX_DEFAULT_FREQUENCY,
59
+ MIX_DEFAULT_FORMAT,
60
+ 2,
61
+ 1024 );
62
+ }
63
+ }
64
+
65
+
66
+ /* --
67
+ * SETUP AND INITIALIZATION
68
+ * ++
69
+ */
70
+
71
+ /* call-seq:
72
+ * open_audio( frequency=nil, format=nil, channels=nil, buffer=nil)
73
+ *
74
+ * **NOTE:** This method is DEPRECATED and will be removed in
75
+ * Rubygame 3.0. Please use the Rubygame.open_audio instead.
76
+ *
77
+ * Initializes the audio device. You must call this before using the other
78
+ * mixer functions. See also #close_audio().
79
+ *
80
+ * Returns nil. May raise an SDLError if initialization fails.
81
+ *
82
+ * This method takes these arguments:
83
+ *
84
+ * frequency:: output sample rate in audio samples per second (Hz).
85
+ * Affects the quality of the sound output, at the expense of
86
+ * CPU usage. If nil, the default (22050) is used. 22050 is
87
+ * recommended for most games. For reference, 44100 is CD quality.
88
+ * The larger the value, the more processing required.
89
+ *
90
+ * format:: output sample format. If nil, the default recommended system
91
+ * format is used. It's _highly_ recommended you leave this nil!
92
+ *
93
+ * But if you're feeling reckless, you can use one of these
94
+ * constants located in the Rubygame::Mixer module:
95
+ *
96
+ * AUDIO_U16SYS:: unsigned 16-bit samples.
97
+ * AUDIO_S16SYS:: signed 16-bit samples.
98
+ * AUDIO_U8:: unsigned 8-bit samples.
99
+ * AUDIO_S8:: signed 8-bit samples.
100
+ *
101
+ * channels:: output sound channels. Use 2 for stereo, 1 for mono.
102
+ * If nil, the default (2) is used.
103
+ * This option is not related to mixing channels.
104
+ *
105
+ * buffer:: size of the sound buffer, in bytes. If nil, the default (1024)
106
+ * is used. Larger values have more delay before playing a
107
+ * sound, but require less CPU usage (and have less skipping
108
+ * on slow systems).
109
+ *
110
+ */
111
+ VALUE rbgm_mixer_openaudio(int argc, VALUE *argv, VALUE module)
112
+ {
113
+
114
+ /* This feature will be removed in Rubygame 3.0. */
115
+ rg_deprecated("Rubygame::Mixer", "3.0");
116
+
117
+ VALUE vfreq, vformat, vchannels, vbuffer;
118
+ int freq = MIX_DEFAULT_FREQUENCY;
119
+ Uint16 format = MIX_DEFAULT_FORMAT;
120
+ int channels = 2;
121
+ int buffer = 1024;
122
+
123
+ rb_scan_args(argc, argv, "04", &vfreq, &vformat, &vchannels, &vbuffer);
124
+
125
+ if( RTEST(vfreq) )
126
+ {
127
+ freq = NUM2INT(vfreq);
128
+ }
129
+
130
+ if( RTEST(vformat) )
131
+ {
132
+ format = NUM2UINT(vformat);
133
+ }
134
+
135
+ if( RTEST(vchannels) )
136
+ {
137
+ channels = NUM2INT(vchannels);
138
+ }
139
+
140
+ if( RTEST(vbuffer) )
141
+ {
142
+ buffer = NUM2INT(vbuffer);
143
+ }
144
+
145
+ if ( Mix_OpenAudio(freq, format, channels, buffer) < 0 )
146
+ {
147
+ rb_raise(eSDLError, "Error initializing SDL_mixer: %s", Mix_GetError());
148
+ }
149
+
150
+ return Qnil;
151
+ }
152
+
153
+ /* call-seq:
154
+ * close_audio()
155
+ *
156
+ * **NOTE:** This method is DEPRECATED and will be removed in
157
+ * Rubygame 3.0. Please use the Rubygame.close_audio instead.
158
+ *
159
+ * Close the audio device being used by the mixer. You should not use any
160
+ * mixer functions after this function, unless you use #open_audio() to
161
+ * re-open the audio device. See also #open_audio().
162
+ *
163
+ * Returns nil.
164
+ */
165
+ VALUE rbgm_mixer_closeaudio(VALUE module)
166
+ {
167
+ /* This feature will be removed in Rubygame 3.0. */
168
+ rg_deprecated("Rubygame::Mixer", "3.0");
169
+
170
+ Mix_CloseAudio();
171
+ return Qnil;
172
+ }
173
+
174
+
175
+
176
+ /* call-seq:
177
+ * open_audio( options={ :buffer => 1024, :channels => 2, :frequency => 22050 } ) -> true or false
178
+ *
179
+ * Initializes the audio device using the given settings.
180
+ *
181
+ * NOTE: Audio will be automatically opened when Rubygame::Sound or
182
+ * Rubygame::Music are first used. You only need to open audio
183
+ * manually if you want settings different from the default, or if
184
+ * you are using the older, deprecated Music and Sample classes from
185
+ * the Rubygame::Mixer module.
186
+ *
187
+ * If audio is already open, this method has no effect, and returns false.
188
+ * If you want to change audio settings, you must #close_audio() and
189
+ * then open it again.
190
+ *
191
+ * options:: A Hash of any of the following options. (Hash, optional)
192
+ *
193
+ * :frequency:: output sample rate in audio samples per second
194
+ * (Hz). Affects the quality of the sound output, at
195
+ * the expense of CPU usage. If omitted, the default
196
+ * (22050) is used. The default is recommended for
197
+ * most games.
198
+ *
199
+ * :channels:: output sound channels. Use 2 for stereo, 1 for mono.
200
+ * If omitted, the default (2) is used.
201
+ *
202
+ * :buffer:: size of the sound buffer, in bytes. Must be a
203
+ * power of 2 (e.g. 512, 1024, 2048). If omitted,
204
+ * the default (1024) is used. If your game is
205
+ * fast-paced, you may want to use a smaller value
206
+ * to reduce audio delay, the time between when you
207
+ * play a sound and when it is heard.
208
+ *
209
+ * Returns:: true if the audio was newly opened by this action, or
210
+ * false if it was already open before this action.
211
+ *
212
+ * May raise:: SDLError, if initialization fails.
213
+ * ArgumentError, if an invalid value is given for any option.
214
+ *
215
+ *
216
+ */
217
+ VALUE rbgm_mixer_openaudio2(int argc, VALUE *argv, VALUE module)
218
+ {
219
+ VALUE options;
220
+
221
+ int buffer = 1024;
222
+ int channels = 2;
223
+ int frequency = MIX_DEFAULT_FREQUENCY;
224
+
225
+ /* In general, format should always be the default. */
226
+ Uint16 format = MIX_DEFAULT_FORMAT;
227
+
228
+
229
+ /* Does nothing if audio is already open. */
230
+ if( audio_is_open() )
231
+ {
232
+ return Qfalse;
233
+ }
234
+
235
+
236
+ rb_scan_args(argc, argv, "01", &options);
237
+
238
+
239
+ if( RTEST(options) )
240
+ {
241
+ /* Make sure options is a Hash table */
242
+ if( TYPE(options) != T_HASH )
243
+ {
244
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected Hash)",
245
+ rb_obj_classname(options));
246
+ }
247
+
248
+ VALUE temp;
249
+
250
+ /* Buffer size */
251
+ temp = rb_hash_aref(options, make_symbol("buffer"));
252
+ if( RTEST(temp) )
253
+ {
254
+ buffer = NUM2INT(temp);
255
+
256
+ if( buffer <= 0 )
257
+ {
258
+ rb_raise(rb_eArgError, "buffer size must be positive (got %d)", buffer);
259
+ }
260
+
261
+ /* Check to see if it's not a power of two */
262
+ if( buffer & (buffer - 1) != 0 )
263
+ {
264
+ rb_raise(rb_eArgError, "buffer size must be a power of 2 (e.g. 512, 1024) (got %d)", buffer);
265
+ }
266
+ }
267
+
268
+ /* Channels */
269
+ temp = rb_hash_aref(options, make_symbol("channels"));
270
+ if( RTEST(temp) )
271
+ {
272
+ channels = NUM2INT(temp);
273
+
274
+ if( channels != 1 && channels != 2 )
275
+ {
276
+ rb_raise(rb_eArgError, "channels must be 1 (mono) or 2 (stereo) (got %d)", channels);
277
+ }
278
+ }
279
+
280
+ /* Frequency */
281
+ temp = rb_hash_aref(options, make_symbol("frequency"));
282
+ if( RTEST(temp) )
283
+ {
284
+ frequency = NUM2INT(temp);
285
+
286
+ if( frequency <= 0 )
287
+ {
288
+ rb_raise(rb_eArgError, "frequency must be positive (got %d)", frequency);
289
+ }
290
+ }
291
+ }
292
+
293
+ int result = Mix_OpenAudio(frequency, format, channels, buffer);
294
+
295
+ if( result < 0 )
296
+ {
297
+ rb_raise(eSDLError, "Could not open audio: %s", Mix_GetError());
298
+ }
299
+
300
+ return Qtrue;
301
+ }
302
+
303
+
304
+ /* call-seq:
305
+ * close_audio() -> true or false
306
+ *
307
+ * Deinitializes and closes the audio device. If audio was not open,
308
+ * this method does nothing, and returns false. See also #open_audio().
309
+ *
310
+ * NOTE: The audio will be automatically closed when the program
311
+ * exits. You only need to close audio manually if you want to
312
+ * call #open_audio with different settings.
313
+ *
314
+ * Returns:: true if the audio changed from open to closed, or
315
+ * false if the audio was not open before this action.
316
+ */
317
+ VALUE rbgm_mixer_closeaudio2(VALUE module)
318
+ {
319
+ if( audio_is_open() )
320
+ {
321
+ Mix_CloseAudio();
322
+ return Qtrue;
323
+ }
324
+ else
325
+ {
326
+ return Qfalse;
327
+ }
328
+ }
329
+
330
+
331
+
332
+ /* call-seq:
333
+ * #mix_channels() -> integer
334
+ *
335
+ * **NOTE:** This method is DEPRECATED and will be removed in
336
+ * Rubygame 3.0. Please use the Rubygame::Sound class instead.
337
+ *
338
+ * Returns the number of mixing channels currently allocated.
339
+ * See also #mix_channels=().
340
+ */
341
+ VALUE rbgm_mixer_getmixchans(VALUE module)
342
+ {
343
+ /* This feature will be removed in Rubygame 3.0. */
344
+ rg_deprecated("Rubygame::Mixer", "3.0");
345
+
346
+ int result;
347
+ result = Mix_AllocateChannels(-1);
348
+
349
+ return INT2NUM(result);
350
+ }
351
+
352
+ /* call-seq:
353
+ * #mix_channels = num_channels
354
+ *
355
+ * **NOTE:** This method is DEPRECATED and will be removed in
356
+ * Rubygame 3.0. Please use the Rubygame::Sound class instead.
357
+ *
358
+ * Set the number of mixer channels, allocating or deallocating channels as
359
+ * needed. This can be called many times, even during audio playback. If this
360
+ * call reduces the number of channels allocated, the excess channels will
361
+ * be stopped automatically. See also #mix_channels()
362
+ *
363
+ * Returns the number of mixing channels allocated.
364
+ *
365
+ * Note that 8 mixing channels are allocated when #open_audio() is called.
366
+ * This method only needs to be called if you want a different number (either
367
+ * greater or fewer) of mixing channels.
368
+ *
369
+ * This method takes this argument:
370
+ * num_channels:: desired number of mixing channels, an integer.
371
+ * Negative values will cause this method to behave as
372
+ * #mix_channels(), returning the number of channels currently
373
+ * allocated, without changing it.
374
+ */
375
+ VALUE rbgm_mixer_setmixchans(VALUE module, VALUE channelsv)
376
+ {
377
+ /* This feature will be removed in Rubygame 3.0. */
378
+ rg_deprecated("Rubygame::Mixer", "3.0");
379
+
380
+ int desired;
381
+ int allocated;
382
+
383
+ desired = NUM2INT(channelsv);
384
+ allocated = Mix_AllocateChannels(desired);
385
+
386
+ return INT2NUM(allocated);
387
+ }
388
+
389
+ /* call-seq:
390
+ * driver_name -> string
391
+ *
392
+ * **NOTE:** This method is DEPRECATED and will be removed in
393
+ * Rubygame 3.0. Please use the Rubygame.audio_driver instead.
394
+ *
395
+ * Returns the name of the audio driver that SDL is using.
396
+ *
397
+ * May raise an SDLError if initialization fails.
398
+ */
399
+
400
+ VALUE rbgm_mixer_getdrivername(VALUE module)
401
+ {
402
+ /* This feature will be removed in Rubygame 3.0. */
403
+ rg_deprecated("Rubygame::Mixer", "3.0");
404
+
405
+ char driver_name[1024];
406
+ if(SDL_AudioDriverName(driver_name, sizeof(driver_name)) == NULL)
407
+ {
408
+ rb_raise(eSDLError, "Error fetching audio driver name: %s", SDL_GetError());
409
+ }
410
+ return rb_str_new2(driver_name);
411
+ }
412
+
413
+ /* call-seq:
414
+ * audio_driver -> string
415
+ *
416
+ * Returns the name of the audio driver that SDL is using.
417
+ *
418
+ * May raise an SDLError if initialization fails.
419
+ */
420
+
421
+ VALUE rbgm_mixer_audiodriver(VALUE module)
422
+ {
423
+ if( ensure_open_audio() != 0 )
424
+ {
425
+ rb_raise(eSDLError, "Could not initialize audio: %s", Mix_GetError());
426
+ }
427
+
428
+ char driver_name[1024];
429
+ if(SDL_AudioDriverName(driver_name, sizeof(driver_name)) == NULL)
430
+ {
431
+ rb_raise(eSDLError, "Could not get audio driver name: %s", Mix_GetError());
432
+ }
433
+ return rb_str_new2(driver_name);
434
+ }
435
+
436
+
437
+
438
+ /* --
439
+ * SAMPLES
440
+ * ++
441
+ */
442
+
443
+ /* call-seq:
444
+ * load_audio( filename ) -> Sample
445
+ *
446
+ * **NOTE:** Rubygame::Mixer::Sample is DEPRECATED and will be removed in
447
+ * Rubygame 3.0. Please use the Rubygame::Sound class instead.
448
+ *
449
+ * Load an audio sample (a "chunk", to use SDL_mixer's term) from a file.
450
+ * Only WAV files are supported at this time.
451
+ *
452
+ * Raises SDLError if the sample could not be loaded.
453
+ */
454
+ VALUE rbgm_sample_new(VALUE class, VALUE filev)
455
+ {
456
+ /* This feature will be removed in Rubygame 3.0. */
457
+ rg_deprecated("Rubygame::Mixer::Sample", "3.0");
458
+
459
+ VALUE self;
460
+ Mix_Chunk* sample;
461
+
462
+ sample = Mix_LoadWAV( StringValuePtr(filev) );
463
+
464
+ if( sample == NULL )
465
+ {
466
+ rb_raise(eSDLError, "Error loading audio Sample from file `%s': %s",
467
+ StringValuePtr(filev), Mix_GetError());
468
+ }
469
+ self = Data_Wrap_Struct( cSample, 0, Mix_FreeChunk, sample );
470
+
471
+ //rb_obj_call_init(self,argc,argv);
472
+
473
+ return self;
474
+ }
475
+
476
+ /* call-seq:
477
+ * play(sample, channel_num, repeats ) -> integer
478
+ *
479
+ * **NOTE:** This method is DEPRECATED and will be removed in
480
+ * Rubygame 3.0. Please use the Rubygame::Sound class instead.
481
+ *
482
+ * Play an audio Sample on a mixing channel, repeating a certain number
483
+ * of extra times. Returns the number of the channel that the sample
484
+ * is being played on.
485
+ *
486
+ * Raises SDLError if something goes wrong.
487
+ *
488
+ * This method takes these arguments:
489
+ * sample:: what Sample to play
490
+ * channel_num:: which mixing channel to play the sample on.
491
+ * Use -1 to play on the first unreserved channel.
492
+ * repeats:: how many extra times to repeat the sample.
493
+ * Can be -1 to repeat forever until it is stopped.
494
+ */
495
+ VALUE rbgm_mixchan_play( VALUE self, VALUE samplev, VALUE chanv, VALUE loopsv )
496
+ {
497
+
498
+ /* This feature will be removed in Rubygame 3.0. */
499
+ rg_deprecated("Rubygame::Mixer", "3.0");
500
+
501
+ Mix_Chunk* sample;
502
+ int loops, channel, result;
503
+
504
+ channel = NUM2INT(chanv);
505
+ Data_Get_Struct( samplev, Mix_Chunk, sample );
506
+ loops = NUM2INT(loopsv);
507
+
508
+ result = Mix_PlayChannel(channel, sample, loops);
509
+
510
+ if ( result < 0 )
511
+ {
512
+ rb_raise(eSDLError, "Error playing sample on channel %d: %s",
513
+ channel, Mix_GetError());
514
+ }
515
+
516
+ return INT2NUM( result );
517
+ }
518
+
519
+ /* call-seq:
520
+ * stop( channel_num )
521
+ *
522
+ * Stop playback of a playing or paused mixing channel.
523
+ * Unlike #pause, playback cannot be resumed from the current point.
524
+ * See also #play.
525
+ */
526
+ VALUE rbgm_mixchan_stop( VALUE self, VALUE chanv )
527
+ {
528
+ Mix_HaltChannel(NUM2INT(chanv));
529
+ return Qnil;
530
+ }
531
+
532
+ /* call-seq:
533
+ * pause( channel_num )
534
+ *
535
+ * Pause playback of a currently-playing mixing channel.
536
+ * Playback can be resumed from the current point with #resume.
537
+ * See also #stop.
538
+ */
539
+ VALUE rbgm_mixchan_pause( VALUE self, VALUE chanv )
540
+ {
541
+ Mix_Pause(NUM2INT(chanv));
542
+ return Qnil;
543
+ }
544
+
545
+ /* call-seq:
546
+ * resume( channel_num )
547
+ *
548
+ * Resume playback of a paused mixing channel. The channel must have been
549
+ * paused (via the #pause method) for this to have any effect. Playback will
550
+ * resume from the point where the channel was paused.
551
+ *
552
+ */
553
+ VALUE rbgm_mixchan_resume( VALUE self, VALUE chanv )
554
+ {
555
+ Mix_Resume(NUM2INT(chanv));
556
+ return Qnil;
557
+ }
558
+
559
+
560
+ /* --
561
+ * MUSIC
562
+ * ++
563
+ */
564
+
565
+ /* call-seq:
566
+ * set_music_command(command) -> integer
567
+ *
568
+ * Sets the external command used to play music.
569
+ *
570
+ * Raises SDLError if something goes wrong.
571
+ *
572
+ * This method takes these arguments:
573
+ * command:: what command to use to play the music.
574
+ *
575
+ */
576
+ VALUE rbgm_mixmusic_setcommand(VALUE class, VALUE commandv)
577
+ {
578
+ int result;
579
+ result = Mix_SetMusicCMD(StringValuePtr(commandv));
580
+ if( result < 0 )
581
+ {
582
+ rb_raise(eSDLError, "Error setting music player commando to `%s': %s",
583
+ StringValuePtr(commandv), Mix_GetError());
584
+ }
585
+ return INT2NUM( result );
586
+ }
587
+
588
+ /* call-seq:
589
+ * load_audio( filename ) -> Music
590
+ *
591
+ * **NOTE:** Rubygame::Mixer::Music is DEPRECATED and will be removed
592
+ * in Rubygame 3.0. Please use the Rubygame::Music class instead.
593
+ *
594
+ * Load music from a file. Supports WAVE, MOD, MIDI, OGG, and MP3 formats.
595
+ *
596
+ * Raises SDLError if the music could not be loaded.
597
+ */
598
+ VALUE rbgm_mixmusic_new(VALUE class, VALUE filev)
599
+ {
600
+
601
+ /* This feature will be removed in Rubygame 3.0. */
602
+ rg_deprecated("Rubygame::Mixer::Music", "3.0");
603
+
604
+ VALUE self;
605
+ Mix_Music* music;
606
+
607
+ music = Mix_LoadMUS( StringValuePtr(filev) );
608
+
609
+ if( music == NULL )
610
+ {
611
+ rb_raise(eSDLError, "Error loading audio music from file `%s': %s",
612
+ StringValuePtr(filev), Mix_GetError());
613
+ }
614
+ self = Data_Wrap_Struct( cOldMusic, 0, Mix_FreeMusic, music );
615
+
616
+ //rb_obj_call_init(self,argc,argv);
617
+
618
+ return self;
619
+ }
620
+
621
+ /* call-seq:
622
+ * play( repeats = 0 ) -> self
623
+ *
624
+ * Play music, repeating a certain number of extra times. If
625
+ * any music was already playing, that music will be stopped
626
+ * first, and this music will start.
627
+ *
628
+ * Raises SDLError if something goes wrong.
629
+ *
630
+ * This method takes these arguments:
631
+ * repeats:: how many extra times to play the music.
632
+ * Can be -1 to repeat forever until it is stopped.
633
+ */
634
+ VALUE rbgm_mixmusic_play(int argc, VALUE *argv, VALUE self)
635
+ {
636
+ Mix_Music* music;
637
+ int reps, result;
638
+ VALUE repsv;
639
+
640
+ Data_Get_Struct( self, Mix_Music, music );
641
+
642
+ rb_scan_args(argc, argv, "01", &repsv);
643
+
644
+ if( RTEST(repsv) )
645
+ {
646
+ reps = NUM2INT(repsv);
647
+ }
648
+ else
649
+ {
650
+ reps = 0;
651
+ }
652
+
653
+ if( reps > -1 )
654
+ {
655
+ /* Adjust so repeats means the same as it does for Samples */
656
+ reps += 1;
657
+ }
658
+
659
+ result = Mix_PlayMusic(music, reps);
660
+
661
+ if ( result < 0 )
662
+ {
663
+ rb_raise(eSDLError, "Error playing music: %s",
664
+ Mix_GetError());
665
+ }
666
+
667
+ return self;
668
+ }
669
+
670
+ /* call-seq:
671
+ * stop() -> self
672
+ *
673
+ * Stop playback of music.
674
+ * See also #play
675
+ */
676
+ VALUE rbgm_mixmusic_stop(VALUE self)
677
+ {
678
+ Mix_HaltMusic();
679
+ return self;
680
+ }
681
+
682
+ /* call-seq:
683
+ * pause() -> self
684
+ *
685
+ * Pause playback of the playing music. You can later #resume
686
+ * playback from the point where you paused.
687
+ * Safe to use on already-paused music.
688
+ * See also #play_music.
689
+ */
690
+ VALUE rbgm_mixmusic_pause(VALUE self)
691
+ {
692
+ Mix_PauseMusic();
693
+ return self;
694
+ }
695
+
696
+ /* call-seq:
697
+ * resume() -> self
698
+ *
699
+ * Resume playback of paused music from the point it was paused.
700
+ * Safe to use on already-playing music.
701
+ * See also #play.
702
+ */
703
+ VALUE rbgm_mixmusic_resume(VALUE self)
704
+ {
705
+ Mix_ResumeMusic();
706
+ return self;
707
+ }
708
+
709
+ /* call-seq:
710
+ * rewind() -> self
711
+ *
712
+ * Rewind the music to the start. This is safe to use on stopped, paused, and playing music.
713
+ * Only works for MOD, OGG, MP3, and MIDI (but not WAV).
714
+ *
715
+ */
716
+ VALUE rbgm_mixmusic_rewind(VALUE self)
717
+ {
718
+ Mix_RewindMusic();
719
+ return self;
720
+ }
721
+
722
+ /* call-seq:
723
+ * jump( time ) -> self
724
+ *
725
+ * Jump to a certain time in the music.
726
+ * Only works when music is playing or paused (but not stopped).
727
+ * Only works for OGG and MP3 files.
728
+ *
729
+ * Raises SDLError if something goes wrong, or if the music type does not
730
+ * support setting the position.
731
+ *
732
+ * time:: Time to jump to, in seconds from the beginning.
733
+ *
734
+ */
735
+ VALUE rbgm_mixmusic_jump(VALUE self, VALUE vtime)
736
+ {
737
+ double time = NUM2DBL(vtime);
738
+
739
+ switch( Mix_GetMusicType(NULL) )
740
+ {
741
+ case MUS_OGG:
742
+ case MUS_MP3:
743
+ Mix_RewindMusic(); // Needed for MP3, and OK with OGG
744
+
745
+ int result = Mix_SetMusicPosition(time);
746
+ if( result < 0 )
747
+ {
748
+ rb_raise(eSDLError, "Error jumping to time in music: %s", Mix_GetError());
749
+ }
750
+
751
+ return self;
752
+
753
+ case MUS_NONE:
754
+ rb_raise(eSDLError, "Cannot jump when no music is playing.");
755
+
756
+ default:
757
+ rb_raise(eSDLError, "Music type does not support jumping.");
758
+ }
759
+ }
760
+
761
+ /* call-seq:
762
+ * playing? -> true or false
763
+ *
764
+ * Returns true if the music is currently playing.
765
+ *
766
+ */
767
+ VALUE rbgm_mixmusic_playing(VALUE self)
768
+ {
769
+ return Mix_PlayingMusic() ? Qtrue : Qfalse;
770
+ }
771
+
772
+ /* call-seq:
773
+ * paused? -> true or false
774
+ *
775
+ * Returns true if the music is currently paused.
776
+ *
777
+ */
778
+ VALUE rbgm_mixmusic_paused(VALUE self)
779
+ {
780
+ return Mix_PausedMusic() ? Qtrue : Qfalse;
781
+ }
782
+
783
+ /* call-seq:
784
+ * volume
785
+ *
786
+ * Returns the current volume level of the music.
787
+ * 0.0 is total silence, 1.0 is maximum volume.
788
+ */
789
+ VALUE rbgm_mixmusic_getvolume(VALUE self)
790
+ {
791
+ return rb_float_new( (double)(Mix_VolumeMusic(-1)) / MIX_MAX_VOLUME );
792
+ }
793
+
794
+ /* call-seq:
795
+ * volume = new_volume
796
+ *
797
+ * Sets the volume level of the music.
798
+ * 0.0 is total silence, 1.0 is maximum volume.
799
+ */
800
+ VALUE rbgm_mixmusic_setvolume(VALUE self, VALUE volumev)
801
+ {
802
+ double volume = NUM2DBL(volumev);
803
+ Mix_VolumeMusic( (int)(volume * MIX_MAX_VOLUME) );
804
+ return volumev;
805
+ }
806
+
807
+ /* call-seq:
808
+ * fade_in( fade_time, repeats=0, start=0 ) -> self
809
+ *
810
+ * Play the music, fading in and repeating a certain number of times.
811
+ * See also #play.
812
+ *
813
+ * Raises SDLError if something goes wrong.
814
+ *
815
+ * fade_time:: Time in seconds for the fade-in effect to complete.
816
+ * repeats:: Number of extra times to play through the music.
817
+ * -1 plays the music forever until it is stopped.
818
+ * Defaults to 0, play only once (no repeats).
819
+ * start:: Time to start from, in seconds since the beginning.
820
+ * Defaults to 0, the beginning of the song.
821
+ * Non-zero values only work for OGG and MP3; other
822
+ * music types will raise SDLError.
823
+ */
824
+ VALUE rbgm_mixmusic_fadein(int argc, VALUE *argv, VALUE self)
825
+ {
826
+ VALUE fadev, repsv, startv;
827
+ rb_scan_args(argc, argv, "12", &fadev, &repsv, &startv);
828
+
829
+ Mix_Music* music;
830
+ Data_Get_Struct( self, Mix_Music, music );
831
+
832
+ int fade, reps, result;
833
+ fade = (int)(NUM2DBL(fadev) * 1000); /* convert to milliseconds */
834
+
835
+ if( RTEST(repsv) )
836
+ {
837
+ reps = NUM2INT(repsv);
838
+ }
839
+ else
840
+ {
841
+ reps = 0;
842
+ }
843
+
844
+ if( reps > -1 )
845
+ {
846
+ /* Adjust so repeats means the same as it does for Samples */
847
+ reps += 1;
848
+ }
849
+
850
+ if( !RTEST(startv) || NUM2DBL(startv) == 0.0 )
851
+ {
852
+ result = Mix_FadeInMusic(music, reps, fade);
853
+ }
854
+ else
855
+ {
856
+ result = Mix_FadeInMusicPos(music, reps, fade, NUM2DBL(startv));
857
+ }
858
+
859
+ if( result < 0 )
860
+ {
861
+ rb_raise(eSDLError, "Error fading in music: %s", Mix_GetError());
862
+ }
863
+
864
+ return self;
865
+ }
866
+
867
+ /* call-seq:
868
+ * fade_out( fade_time ) -> self
869
+ *
870
+ * Gradually fade the music to silence over +fade_length+ seconds.
871
+ * After the fade is complete, the music will be automatically stopped.
872
+ *
873
+ * Raises SDLError if something goes wrong.
874
+ *
875
+ * fade_time:: Time until the music is totally silent, in seconds.
876
+ */
877
+ VALUE rbgm_mixmusic_fadeout(VALUE self, VALUE fadev)
878
+ {
879
+ int fade = (int)(NUM2DBL(fadev) * 1000);
880
+ int result = Mix_FadeOutMusic(fade);
881
+
882
+ if ( result < 0 )
883
+ {
884
+ rb_raise(eSDLError, "Error fading out music: %s", Mix_GetError());
885
+ }
886
+ return self;
887
+ }
888
+
889
+ /* call-seq:
890
+ * fading?( direction = nil ) -> true or false
891
+ *
892
+ * True if the music is fading in or out (or either). You can
893
+ * specify +direction+ as :in/:out to check only fading in/out;
894
+ * otherwise, it will return true if it's fading either way.
895
+ *
896
+ * direction:: :in, :out, or nil if you don't care which.
897
+ * Returns:: true if the music is fading in the given direction.
898
+ */
899
+ VALUE rbgm_mixmusic_fading(int argc, VALUE *argv, VALUE self)
900
+ {
901
+ VALUE dirv;
902
+ rb_scan_args(argc, argv, "01", &dirv);
903
+
904
+ if( dirv == make_symbol("in") )
905
+ {
906
+ return ( (Mix_FadingMusic() == MIX_FADING_IN) ? Qtrue : Qfalse );
907
+ }
908
+ else if( dirv == make_symbol("out") )
909
+ {
910
+ return ( (Mix_FadingMusic() == MIX_FADING_OUT) ? Qtrue : Qfalse );
911
+ }
912
+ else
913
+ {
914
+ return ( (Mix_FadingMusic() != MIX_NO_FADING) ? Qtrue : Qfalse );
915
+ }
916
+ }
917
+
918
+
919
+ void Init_rubygame_mixer()
920
+ {
921
+
922
+ #if 0
923
+ mRubygame = rb_define_module("Rubygame");
924
+ #endif
925
+
926
+ Init_rubygame_shared();
927
+
928
+ Rubygame_Init_Sound();
929
+ Rubygame_Init_Music();
930
+
931
+ rb_hash_aset(rb_ivar_get(mRubygame,rb_intern("VERSIONS")),
932
+ ID2SYM(rb_intern("sdl_mixer")),
933
+ rb_ary_new3(3,
934
+ INT2NUM(SDL_MIXER_MAJOR_VERSION),
935
+ INT2NUM(SDL_MIXER_MINOR_VERSION),
936
+ INT2NUM(SDL_MIXER_PATCHLEVEL)));
937
+
938
+ /*
939
+ * Moving these to be under Rubygame module instead of Mixer.
940
+ */
941
+ rb_define_module_function(mRubygame,"open_audio", rbgm_mixer_openaudio2, -1);
942
+ rb_define_module_function(mRubygame,"close_audio", rbgm_mixer_closeaudio2, 0);
943
+ rb_define_module_function(mRubygame,"audio_driver",rbgm_mixer_audiodriver, 0);
944
+
945
+
946
+
947
+ /*
948
+ * **NOTE:** This module is DEPRECATED and will be removed in Rubygame 3.0.
949
+ * Please use Rubygame.open_audio, Rubygame.close_audio, Rubygame::Sound,
950
+ * and Rubygame::Music instead.
951
+ *
952
+ * The Mixer module provides access to the SDL_mixer library for audio
953
+ * playback and mixing. This module is still very basic, but it is
954
+ * good enough to load and play WAV files on multiple mix channels.
955
+ *
956
+ * See the Sample class for loading audio files.
957
+ * See the Music class for streaming music from a file.
958
+ */
959
+ mMixer = rb_define_module_under(mRubygame, "Mixer");
960
+
961
+ rb_define_const(mMixer,"AUDIO_U8", INT2NUM(AUDIO_U8));
962
+ rb_define_const(mMixer,"AUDIO_S8", INT2NUM(AUDIO_S8));
963
+ rb_define_const(mMixer,"AUDIO_U16SYS", INT2NUM(AUDIO_U16SYS));
964
+ rb_define_const(mMixer,"AUDIO_S16SYS", INT2NUM(AUDIO_S16SYS));
965
+
966
+ rb_define_module_function(mMixer,"open_audio",rbgm_mixer_openaudio, -1);
967
+ rb_define_module_function(mMixer,"close_audio",rbgm_mixer_closeaudio, 0);
968
+ rb_define_module_function(mMixer,"mix_channels",rbgm_mixer_getmixchans, 0);
969
+ rb_define_module_function(mMixer,"mix_channels=",rbgm_mixer_setmixchans, 1);
970
+ rb_define_module_function(mMixer,"driver_name", rbgm_mixer_getdrivername, 0);
971
+
972
+
973
+ /*
974
+ * **NOTE:** This class is DEPRECATED and will be removed in Rubygame 3.0.
975
+ * Please use the Rubygame::Sound class instead.
976
+ *
977
+ * Stores audio data to play with Mixer.play()
978
+ */
979
+ cSample = rb_define_class_under(mMixer, "Sample", rb_cObject);
980
+ rb_define_singleton_method(cSample, "load_audio", rbgm_sample_new, 1);
981
+ rb_define_module_function(mMixer,"play", rbgm_mixchan_play, 3);
982
+ rb_define_module_function(mMixer,"stop", rbgm_mixchan_stop, 1);
983
+ rb_define_module_function(mMixer,"pause", rbgm_mixchan_pause, 1);
984
+ rb_define_module_function(mMixer,"resume", rbgm_mixchan_resume, 1);
985
+
986
+
987
+ /*
988
+ * **NOTE:** This class is DEPRECATED and will be removed in Rubygame 3.0.
989
+ * Please use the Rubygame::Music class instead.
990
+ *
991
+ * The Music class is used for playing music from a file. It supports
992
+ * WAVE, MOD, MIDI, OGG, and MP3 files. There are two important differences
993
+ * between Music and Sample:
994
+ *
995
+ * 1. Music streams the music from disk, which means it can start faster and
996
+ * uses less memory than Sample, which loads the entire file into memory.
997
+ * This is especially important for music files, which are often several
998
+ * minutes long.
999
+ * 2. There can only be one Music instance playing at once, while there can
1000
+ * be many Samples playing at once. If you play a second Music while one
1001
+ * is already playing, the first one will be stopped. See #play.
1002
+ */
1003
+ cOldMusic = rb_define_class_under(mMixer, "Music", rb_cObject);
1004
+ rb_define_singleton_method(cOldMusic, "load_audio", rbgm_mixmusic_new, 1);
1005
+
1006
+ //rb_define_singleton_method(cOldMusic, "set_command", rbgm_mixmusic_setcommand, 1);
1007
+
1008
+ rb_define_method(cOldMusic, "play", rbgm_mixmusic_play, -1);
1009
+ rb_define_method(cOldMusic, "stop", rbgm_mixmusic_stop, 0);
1010
+ rb_define_method(cOldMusic, "pause", rbgm_mixmusic_pause, 0);
1011
+ rb_define_method(cOldMusic, "resume", rbgm_mixmusic_resume, 0);
1012
+ rb_define_method(cOldMusic, "rewind", rbgm_mixmusic_rewind, 0);
1013
+ rb_define_method(cOldMusic, "jump", rbgm_mixmusic_jump, 1);
1014
+ rb_define_method(cOldMusic, "paused?", rbgm_mixmusic_paused, 0);
1015
+ rb_define_method(cOldMusic, "playing?", rbgm_mixmusic_playing, 0);
1016
+
1017
+ rb_define_method(cOldMusic, "volume", rbgm_mixmusic_getvolume, 0);
1018
+ rb_define_method(cOldMusic, "volume=", rbgm_mixmusic_setvolume, 1);
1019
+ rb_define_method(cOldMusic, "fade_in", rbgm_mixmusic_fadein, -1);
1020
+ rb_define_method(cOldMusic, "fade_out", rbgm_mixmusic_fadeout, 1);
1021
+ rb_define_method(cOldMusic, "fading?", rbgm_mixmusic_fading, -1);
1022
+ }
1023
+
1024
+