3

I've just a strange problem. I'm looking to put sounds in my game. It's a game with two phases. One of rpg and one of fight. In rpg you can launch a fight when you walk on a monster. In my code i use 3 times SDL_mixer to play music (in the menu and in the rpg and in the fight). It works in the first two cases, but when i launch a fight there is no music. The music is loaded, and Mix_playingMusic returns true, but I can't hear any music when a fight is launched. I use the same music than in the rpg and menu. The code about SDL_Mixer is the same everywhere. Part of the code of the fight :

{
SDL_Event events;
Mix_Music *fightMusic;

enum actual_player actual = PLAYER;
int go = 1;
int action;
int monsterId = 0;

Uint32  t = SDL_GetTicks(), nt;

fightMusic = Mix_LoadMUS("data/Music/rpg.mp3"); // we loading the music

if(fightMusic == NULL){
    printf("error loading fightMusic\n");
}

Mix_VolumeMusic(MIX_MAX_VOLUME);

if(Mix_PlayMusic(fightMusic, -1)<0){
    printf("error playing fightMusic \n");
}
if (Mix_PlayingMusic() ){
    printf("Music it's playing \n");
}

while(go){
    if(isFinished(fightSdl->fight))
        go = 0;
    nt = SDL_GetTicks();


    while (SDL_PollEvent(&events)){

        action = inputKeyboard(events);
        if(action == QUIT){
            return -1;
            go = 0;
        }

        if(action == -2){
            clearBufferGame(fightSdl->buffer, CHARACTER);
        }

        if(actual == PLAYER) {

            switch(action){

                case MOVE_DOWNWARD:                 
                    addActionToBufferGame(fightSdl->buffer, DOWN);
                break;

                case MOVE_UPWARD:
                    addActionToBufferGame(fightSdl->buffer, UP);
                break;

                case MOVE_LEFT:
                    addActionToBufferGame(fightSdl->buffer, LEFT);
                break;

                case MOVE_RIGHT:
                    addActionToBufferGame(fightSdl->buffer, RIGHT);

                break;

                case ATTACK_1:
                    actionAttackSdl(fightSdl, ATTACK_1);
                break;

                case ATTACK_2:
                    if(Mix_PlayChannel(-1,fireSound,0)<0){
                            printf("error playing fire sound");
                        }
                    actionAttackSdl(fightSdl, ATTACK_2);
                break;

                case ATTACK_3:
                    actionAttackSdl(fightSdl, ATTACK_3);
                break;

                case NOTHING:
                    actual = ENNEMY;
                break;

                case -1:
                    setIsIdleCharacSdl(fightSdl->characSdl,1);
            }   
        }           
    }
    if (nt-t>300){

        if(actual == ENNEMY){
            hitNRunSdl(fightSdl, monsterId);

            if(getMpEnnemyFight(fightSdl->fight) == 3 && getMpEnnemyFight(fightSdl->fight) == 3)
                monsterId++;
            if(monsterId >= fightSdl->nbMstrSdl){
                actual = PLAYER;
                setApPlayerFight(fightSdl->fight, 3);
                setMpPlayerFight(fightSdl->fight, 3);
                monsterId = 0;
            }

        }
        t = nt;
    }


    // updatePlayerDisplay(fightSdl);
    // updateMonsterDisplay(fightSdl);
    updatePlayerActionFight(fightSdl);
    updateSpellDisplay(fightSdl);
    updateDisplay(fightSdl);

    drawGame(fightSdl);

    // SDL_RenderPresent(fightSdl->renderer);
}

if(getLifePointsCharacter(getCharacterFight(fightSdl->fight)) >= 0)
    return 1;
return 0;

}

EDIT : i've added a printf("%s", Mix_GetError) and there is no error in the menu and rpg but in the fight it was print : Invalid audio device ID

1 Answers1

1

So, I found a solution to solve my problem. Apparently the return value of SDL_OpenAudio is always success or failure, and not a device ID, which means you can only have one device open at a time with this function. I just closed the audio before re-opning it when launching fight, and it works.