diff options
| author | Tom Black <[email protected]> | 2018-12-12 00:00:19 -0800 |
|---|---|---|
| committer | Tom Black <[email protected]> | 2018-12-12 00:00:19 -0800 |
| commit | 80a2c9b1e14b5ed6a68107db407568e63547ac8d (patch) | |
| tree | fb5b55176d47ce53849a6ef4c240e3466f40e530 | |
| parent | fd8b90bd1d5e874f0402af069fd7dd266b68532f (diff) | |
| download | ruby2d-80a2c9b1e14b5ed6a68107db407568e63547ac8d.tar.gz ruby2d-80a2c9b1e14b5ed6a68107db407568e63547ac8d.zip | |
Use Simple 2D API to get and set music volume
| -rw-r--r-- | ext/ruby2d/ruby2d.c | 33 | ||||
| -rw-r--r-- | lib/ruby2d/music.rb | 6 |
2 files changed, 27 insertions, 12 deletions
diff --git a/ext/ruby2d/ruby2d.c b/ext/ruby2d/ruby2d.c index 03caf3d..f9cf3b4 100644 --- a/ext/ruby2d/ruby2d.c +++ b/ext/ruby2d/ruby2d.c @@ -672,17 +672,29 @@ static R_VAL ruby2d_music_ext_stop(R_VAL self) { /* - * Ruby2D::Music#ext_volume + * Ruby2D::Music#ext_get_volume */ #if MRUBY -static R_VAL ruby2d_music_ext_volume(mrb_state* mrb, R_VAL self) { - mrb_value vol; - mrb_get_args(mrb, "o", &vol); +static R_VAL ruby2d_music_ext_get_volume(mrb_state* mrb, R_VAL self) { #else -static R_VAL ruby2d_music_ext_volume(R_VAL self, R_VAL vol) { +static R_VAL ruby2d_music_ext_get_volume(R_VAL self) { #endif - int mix_vol = NUM2INT(vol) == -1 ? -1 : MIX_MAX_VOLUME * NUM2INT(vol) / 100.0; - return INT2NUM(ceil(Mix_VolumeMusic(mix_vol) * 100.0 / MIX_MAX_VOLUME)); + return INT2NUM(S2D_GetMusicVolume()); +} + + +/* + * Ruby2D::Music#ext_set_volume + */ +#if MRUBY +static R_VAL ruby2d_music_ext_set_volume(mrb_state* mrb, R_VAL self) { + mrb_value volume; + mrb_get_args(mrb, "o", &volume); +#else +static R_VAL ruby2d_music_ext_set_volume(R_VAL self, R_VAL volume) { +#endif + S2D_SetMusicVolume(NUM2INT(volume)); + return R_NIL; } @@ -1150,8 +1162,11 @@ void Init_ruby2d() { // Ruby2D::Music#ext_stop r_define_method(ruby2d_music_class, "ext_stop", ruby2d_music_ext_stop, r_args_none); - // Ruby2D::Music#self.ext_volume - r_define_class_method(ruby2d_music_class, "ext_volume", ruby2d_music_ext_volume, r_args_req(1)); + // Ruby2D::Music#self.ext_get_volume + r_define_class_method(ruby2d_music_class, "ext_get_volume", ruby2d_music_ext_get_volume, r_args_none); + + // Ruby2D::Music#self.ext_set_volume + r_define_class_method(ruby2d_music_class, "ext_set_volume", ruby2d_music_ext_set_volume, r_args_req(1)); // Ruby2D::Music#ext_fadeout r_define_method(ruby2d_music_class, "ext_fadeout", ruby2d_music_ext_fadeout, r_args_req(1)); diff --git a/lib/ruby2d/music.rb b/lib/ruby2d/music.rb index e47131e..bc4ec5a 100644 --- a/lib/ruby2d/music.rb +++ b/lib/ruby2d/music.rb @@ -39,16 +39,16 @@ module Ruby2D ext_stop end - # Returns the previous volume setting, in percentage + # Returns the volume, in percentage def self.volume - self.ext_volume(-1) + self.ext_get_volume end # Set music volume, 0 to 100% def self.volume=(v) # If a negative value, volume will be 0 if v < 0 then v = 0 end - self.ext_volume(v) + self.ext_set_volume(v) end # Alias instance methods to class methods |
