SuperDisk / hUGEDriver

An easy-to-use, fast, tracker-based, public domain sound driver for Game Boy homebrew
https://nickfa.ro/index.php/HUGETracker
52 stars 10 forks source link

Starting a song in an arbitrary position. #40

Open RodrigoCard opened 9 months ago

RodrigoCard commented 9 months ago

In my game, I have one music for the stage and one for the pause menu. I just reload the corresponding music on (un)pause.

I want to restart the stage music where it was on pause, but the current music position is lost when loading another song. So how can I do that?

Two things:

  1. I see there is no hUGE_Get_position or something similiar, but I suppose you can calculate that, but how?

    also, is it possible to implement a hUGE_get_position()?

  2. I see there is a hUGE_set_position(unsigned char pattern); in the header, but how to use that? what pattern refers to?

Thanks!

SuperDisk commented 9 months ago

I just made a modified version you can try: https://github.com/SuperDisk/hUGEDriver/actions/runs/6444707503

This adds hUGE_current_order which is the current order that is being played. hUGE_set_position takes a number which represents what order to change to, I guess pattern is a misnomer here.

image

RodrigoCard commented 9 months ago

Great!!! But I run into a linking problem with the new symbol when trying to use it ?ASlink-Warning-Undefined Global '_hUGE_current_order' referenced by module 'gameplay' make: *** [Makefile:41: out/mygame.gb] Error 1

SuperDisk commented 9 months ago

https://github.com/SuperDisk/hUGEDriver/actions/runs/6489024190

Sorry about that, try this.

RodrigoCard commented 9 months ago

I finally got some time to test this properly. The code compiles and links without any problems now. And it ALMOST works... :)

I'm using it like this: declarations:

extern const hUGESong_t test_music;
extern const hUGESong_t pause_music;
unsigned char test_music_position = 0;

inside my main game loop:

if(JUST_PRESSED(J_START) && can_pause && gamestate_time>60) {
        paused=!paused;
        if (paused)
        {
            test_music_position = hUGE_current_order;
            hUGE_init(&pause_music);
        } else {
            hUGE_init(&test_music);
            hUGE_set_position(test_music_position);
        }
    }

hUGE_dosound() is being called on vblank

So, I just store the hUGE_current_order in test_music_position and use hUGE_set_position with that. Is that right?

Anyway, it kinda works, but the position start drifting after a few unpauses, and skips waaaay out of bounds eventually, producing garbage audio and after some time, making the game crash:

Captura de tela 2023-10-12 174120

Suspecting some kind of misalignment, I shifted a few bits to see what happened and this was what worked best for me: hUGE_set_position((test_music_position>>1)+1); (which is not correct either but doesnt crash.)

Any idea?