Gameboy Development Forum

Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)

You are not logged in.

Ads

#1 2019-12-15 14:47:56

jchristof
Member
Registered: 2019-12-01
Posts: 17

Forwarding varargs in GBDK

I've tried several implementation to get var args forwarding to work in GBDK - none seem to work. Has anyone experienced the same?

Compiles fine but PlayFx doesn't product audio now

Code:

void PlaySound(SOUND_CHANNEL channel, UINT8 mute_frames, ...){
    va_list args;
    va_start(args, mute_frames);

    PlayFx(channel, mute_frames, args);
    
    va_end(args);
}

Doesn't compile because caller arguments don't match the function signature:

Code:

void PlaySound(SOUND_CHANNEL channel, UINT8 mute_frames, va_list args){
    PlayFx(channel, mute_frames, args);
}

Offline

 

#2 2019-12-17 23:31:17

bbbbbr
Member
Registered: 2019-03-04
Posts: 124

Re: Forwarding varargs in GBDK

You can find examples of it's use in both Print.c and Sound.c in the ZGB:

https://github.com/Zal0/ZGB/blob/master … rc/Print.c
https://github.com/Zal0/ZGB/blob/master … rc/Sound.c

Code:

void PlayFx(SOUND_CHANNEL channel, UINT8 mute_frames, ...) {
    UINT8 i;
    UINT8* reg = (UINT8*)FX_ADDRESS[channel];
    va_list list;

    va_start(list, mute_frames);
    for(i = 0; i < FX_REG_SIZES[channel]; ++i, ++reg) {
        *reg = va_arg(list, INT16);
    }
    va_end(list);
    
    if(channel != CHANNEL_5) {
        gbt_enable_channels(~(0xF & (1 << channel)));
    }

    music_mute_frames = mute_frames;
}

Last edited by bbbbbr (2019-12-17 23:32:08)

Offline

 

#3 2019-12-19 10:54:47

jchristof
Member
Registered: 2019-12-01
Posts: 17

Re: Forwarding varargs in GBDK

Thanks for the response on that - my use case is one where I'm just forwarding the argument list. My first example matches examples from SO etc... but doesn't seem to work correctly. I'll have to dig a little deeper to see where it's failing - I just wanted to poll the group to see if this might be a known sdcc issue.

Offline

 

#4 2019-12-19 11:19:03

Zalo
Member
From: Spain
Registered: 2016-07-26
Posts: 103
Website

Re: Forwarding varargs in GBDK

Probably not answering your question but any reason why you want to do a function that just calls another function?

Offline

 

#5 2019-12-19 16:59:09

jchristof
Member
Registered: 2019-12-01
Posts: 17

Re: Forwarding varargs in GBDK

The example's simplified. I was trying to mute  sound effects by not forwarding the args to your PlayFx(). The forwarding doesn't seem to work they way I expected. I ended just muting the sfx directly in PlayFx() as a workaround for the moment.

BTW - thanks for creating ZGB. Having the tool chain work out-of-the-box is priceless.

Offline

 

#6 2019-12-20 02:32:12

Zalo
Member
From: Spain
Registered: 2016-07-26
Posts: 103
Website

Re: Forwarding varargs in GBDK

I was guessing so...
But are you sure you want to mute sound effects? What I do is exactly the opposite, muting the channel for the music player so fx are played

Anyways, I am not sure if this an specific GBDK issue or is something related to C itself
Maybe another approach is using variadic macros which let you reuse the params using __VA_ARGS__

I am glad you liked the engine smile

Offline

 

#7 2019-12-20 10:07:22

jchristof
Member
Registered: 2019-12-01
Posts: 17

Re: Forwarding varargs in GBDK

Muting sfx was just an options menu item I threw in thinking it would take a second to do wink - to answer your question, it's not super useful for this game.

I am making use of the mute frames for sfx + music - that's nice.

I'll take a look at the variadic macros ref.

Offline

 

#8 2019-12-25 19:06:14

bbbbbr
Member
Registered: 2019-03-04
Posts: 124

Re: Forwarding varargs in GBDK

Ohh, I see what you were asking.

You may need to change the argument type for PlayFx() to accept a va_args list. For example, making an alternate version of PlayFx. Maybe that defeats the purpose for you though?

Code:

void PlaySound(SOUND_CHANNEL channel, UINT8 mute_frames, ...){
    va_list args;

    va_start(args, mute_frames);
        PlayFxArgs(channel, mute_frames, args);
    va_end(args);
}

void PlayFxArgs(SOUND_CHANNEL channel, UINT8 mute_frames, va_list list) {
    UINT8 i;
    UINT8* reg = (UINT8*)FX_ADDRESS[channel];

    for(i = 0; i < FX_REG_SIZES[channel]; ++i, ++reg) {
        *reg = va_arg(list, INT16);
    }

    if(channel != CHANNEL_5) {
        gbt_enable_channels(~(0xF & (1 << channel)));
    }

    music_mute_frames = mute_frames;
}

You might also reference this part of SDCC for another example:
https://github.com/darconeous/sdcc/blob … asprintf.c

Offline

 

#9 2019-12-26 14:16:22

jchristof
Member
Registered: 2019-12-01
Posts: 17

Re: Forwarding varargs in GBDK

I will give it a try - thanks!

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson