Audio Module
#2
I've come up with an idea of how one could manually create some Ruby class from C code.
So in few words this means we are using CRuby code here.

As I had commented in my sample code, it definitely ignores how a C string is converted to a sf::String object. Thus it won't let you compile anything unless that gets resolved first.

According to SMFL C strings should be automatically converted to sf::Strings.

Update 2

Code:
/*=============================================================================*/
// ** rgl::Audio's Ruby Binding as Audio module
/*-----------------------------------------------------------------------------*/
/*
  Simply call the following:
    Init_rgl_audio();
  from any main function for any binary executable or create a Ruby gem.

  C strings should be automatically converted to sf::Strings according to SMFL.
*/

#include "rgl_audio.hpp"
#include <ruby.h>

#define RMF(func) ((VALUE (*)(ANYARGS))(func))

static VALUE rgl_audio_play_bgm(int size, VALUE* args, VALUE self)
{
  if (size == 0)
    return rgl::Audio.playBGM() ? Qtrue : Qfalse;
  const char* fn = StringValueCStr(args[0]);
  if (size == 1)
    return rgl::Audio.playBGM(fn) ? Qtrue : Qfalse;
  bool result;
  float volume = NUM2DBL(args[1]);
  if (size == 2) {
    result = rgl::Audio.playBGM(fn, volume);
  } else if (size == 3) {
    float pitch = NUM2DBL(args[2]);
    result = rgl::Audio.playBGM(fn, volume, pitch);
  }
  return result ? Qtrue : Qfalse;
}

static VALUE rgl_audio_replay_bgm(VALUE self)
{
  return rgl::Audio.replayBGM() ? Qtrue : Qfalse;
}

static VALUE rgl_audio_pause_bgm(VALUE self)
{
  rgl::Audio.pauseBGM();
  return Qnil;
}

static VALUE rgl_audio_stop_bgm(VALUE self)
{
  rgl::Audio.stopBGM();
  return Qnil;
}

static VALUE rgl_audio_fade_bgm(VALUE self, VALUE rvolume, VALUE rduration)
{
  float volume = NUM2DBL(rvolume), duration = NUM2DBL(rduration);
  rgl::Audio.fadeBGM(volume, duration);
  return Qnil;
}

static VALUE rgl_audio_play_bgs(int size, VALUE* args, VALUE self)
{
  if (size == 0)
    return rgl::Audio.playBGS() ? Qtrue : Qfalse;
  const char* fn = StringValueCStr(args[0]);
  if (size == 1)
    return rgl::Audio.playBGS(fn) ? Qtrue : Qfalse;
  bool result;
  float volume = NUM2DBL(args[1]);
  if (size == 2) {
    result = rgl::Audio.playBGS(fn, volume);
  } else if (size == 3) {
    float pitch = NUM2DBL(args[2]);
    result = rgl::Audio.playBGS(fn, volume, pitch);
  }
  return result ? Qtrue : Qfalse;
}

static VALUE rgl_audio_replay_bgs(VALUE self)
{
  return rgl::Audio.replayBGS() ? Qtrue : Qfalse;
}

static VALUE rgl_audio_pause_bgs(VALUE self)
{
  rgl::Audio.pauseBGS();
  return Qnil;
}

static VALUE rgl_audio_stop_bgs(VALUE self)
{
  rgl::Audio.stopBGS();
  return Qnil;
}

static VALUE rgl_audio_fade_bgs(VALUE self, VALUE rvolume, VALUE rduration)
{
  float volume = NUM2DBL(rvolume), duration = NUM2DBL(rduration);
  rgl::Audio.fadeBGS(volume, duration);
  return Qnil;
}

static VALUE rgl_audio_play_me(int size, VALUE* args, VALUE self)
{
  if (size == 0)
    return rgl::Audio.playME() ? Qtrue : Qfalse;
  const char* fn = StringValueCStr(args[0]);
  if (size == 1)
    return rgl::Audio.playME(fn) ? Qtrue : Qfalse;
  bool result;
  float volume = NUM2DBL(args[1]);
  if (size == 2) {
    result = rgl::Audio.playME(fn, volume);
  } else if (size == 3) {
    float pitch = NUM2DBL(args[2]);
    result = rgl::Audio.playME(fn, volume, pitch);
  }
  return result ? Qtrue : Qfalse;
}

static VALUE rgl_audio_replay_me(VALUE self)
{
  return rgl::Audio.replayME() ? Qtrue : Qfalse;
}

static VALUE rgl_audio_pause_me(VALUE self)
{
  rgl::Audio.pauseME();
  return Qnil;
}

static VALUE rgl_audio_stop_me(VALUE self)
{
  rgl::Audio.stopME();
  return Qnil;
}

static VALUE rgl_audio_fade_me(VALUE self, VALUE rvolume, VALUE rduration)
{
  float volume = NUM2DBL(rvolume), duration = NUM2DBL(rduration);
  rgl::Audio.fadeME(volume, duration);
  return Qnil;
}

static VALUE rgl_audio_play_se(int size, VALUE* args, VALUE self)
{
  if (size == 0)
    return rgl::Audio.playSE() ? Qtrue : Qfalse;
  const char* fn = StringValueCStr(args[0]);
  if (size == 1)
    return rgl::Audio.playSE(fn) ? Qtrue : Qfalse;
  bool result;
  float volume = NUM2DBL(args[1]);
  if (size == 2) {
    result = rgl::Audio.playSE(fn, volume);
  } else if (size == 3) {
    float pitch = NUM2DBL(args[2]);
    result = rgl::Audio.playSE(fn, volume, pitch);
  }
  return result ? Qtrue : Qfalse;
}

static VALUE rgl_audio_replay_se(VALUE self)
{
  return rgl::Audio.replaySE() ? Qtrue : Qfalse;
}

static VALUE rgl_audio_pause_se(VALUE self)
{
  rgl::Audio.pauseSE();
  return Qnil;
}

static VALUE rgl_audio_stop_se(VALUE self)
{
  rgl::Audio.stopSE();
  return Qnil;
}

static VALUE rgl_audio_fade_se(VALUE self, VALUE rvolume, VALUE rduration)
{
  float volume = NUM2DBL(rvolume), duration = NUM2DBL(rduration);
  rgl::Audio.fadeSE(volume, duration);
  return Qnil;
}

static VALUE rgl_audio_update(VALUE self)
{
  rgl::Audio.update();
  return Qnil;
}

static VALUE rgl_audio_reset(VALUE self)
{
  rgl::Audio.reset();
  return Qnil;
}

static VALUE rgl_audio_fade_all(VALUE self, VALUE rvolume, VALUE rduration)
{
  float volume = NUM2DBL(rvolume), duration = NUM2DBL(rduration);
  rgl::Audio.fadeAll(volume, duration);
  return Qnil;
}

void Init_rgl_audio()
{
  VALUE module = rb_define_module("Audio");
  rb_define_module_function(module, "play_bgm", RMF(rgl_audio_play_bgm), -1);
  rb_define_module_function(module, "replay_bgm", RMF(rgl_audio_replay_bgm), 0);
  rb_define_module_function(module, "pause_bgm", RMF(rgl_audio_pause_bgm), 0);
  rb_define_module_function(module, "stop_bgm", RMF(rgl_audio_stop_bgm), 0);
  rb_define_module_function(module, "fade_bgm", RMF(rgl_audio_fade_bgm), 2);
  rb_define_module_function(module, "play_bgs", RMF(rgl_audio_play_bgs), -1);
  rb_define_module_function(module, "replay_bgs", RMF(rgl_audio_replay_bgs), 0);
  rb_define_module_function(module, "pause_bgs", RMF(rgl_audio_pause_bgs), 0);
  rb_define_module_function(module, "stop_bgs", RMF(rgl_audio_stop_bgs), 0);
  rb_define_module_function(module, "fade_bgs", RMF(rgl_audio_fade_bgs), 2);
  rb_define_module_function(module, "play_me", RMF(rgl_audio_play_me), -1);
  rb_define_module_function(module, "replay_me", RMF(rgl_audio_replay_me), 0);
  rb_define_module_function(module, "pause_me", RMF(rgl_audio_pause_me), 0);
  rb_define_module_function(module, "stop_me", RMF(rgl_audio_stop_me), 0);
  rb_define_module_function(module, "fade_me", RMF(rgl_audio_fade_me), 2);
  rb_define_module_function(module, "play_se", RMF(rgl_audio_play_se), -1);
  rb_define_module_function(module, "replay_se", RMF(rgl_audio_replay_se), 0);
  rb_define_module_function(module, "pause_se", RMF(rgl_audio_pause_se), 0);
  rb_define_module_function(module, "stop_se", RMF(rgl_audio_stop_se), 0);
  rb_define_module_function(module, "fade_se", RMF(rgl_audio_fade_se), 2);
  rb_define_module_function(module, "update", RMF(rgl_audio_update), 0);
  rb_define_module_function(module, "reset", RMF(rgl_audio_reset), 0);
  rb_define_module_function(module, "fade_all", RMF(rgl_audio_fade_all), 2);
}

Yeah, I've gained some experience after compiling this kind of rubygem or library while working on my HiddenChest project. Laughing
So in theory I might be capable of building it on Linux... if I had the full RGL source code, of course. Winking
"For God has not destined us for wrath, but for obtaining salvation through our Lord Jesus Christ," 1 Thessalonians 5:9

Maranatha!

The Internet might be either your friend or enemy. It just depends on whether or not she has a bad hair day.

[Image: SP1-Scripter.png]
[Image: SP1-Writer.png]
[Image: SP1-Poet.png]
[Image: SP1-PixelArtist.png]
[Image: SP1-Reporter.png]

My Original Stories (available in English and Spanish)

List of Compiled Binary Executables I have published...
HiddenChest & Roole

Give me a free copy of your completed game if you include at least 3 of my scripts! Laughing + Tongue sticking out

Just some scripts I've already published on the board...
KyoGemBoost XP VX & ACE, RandomEnkounters XP, KSkillShop XP, Kolloseum States XP, KEvents XP, KScenario XP & Gosu, KyoPrizeShop XP Mangostan, Kuests XP, KyoDiscounts XP VX, ACE & MV, KChest XP VX & ACE 2016, KTelePort XP, KSkillMax XP & VX & ACE, Gem Roulette XP VX & VX Ace, KRespawnPoint XP, VX & VX Ace, GiveAway XP VX & ACE, Klearance XP VX & ACE, KUnits XP VX, ACE & Gosu 2017, KLevel XP, KRumors XP & ACE, KMonsterPals XP VX & ACE, KStatsRefill XP VX & ACE, KLotto XP VX & ACE, KItemDesc XP & VX, KPocket XP & VX, OpenChest XP VX & ACE
Reply }


Messages In This Thread
Audio Module - by Kain Nobel - 01-23-2022, 02:47 PM
RE: Audio Module (C++ / SFML) - by kyonides - 01-24-2022, 02:09 AM
RE: Audio Module - by Kain Nobel - 01-25-2022, 11:39 AM



Users browsing this thread: 1 Guest(s)