SDL music and event que doesnt sync properly

currently i am creating a main menu for a game in which I want a background music played and a different one when the game actual starts, the game starts by pressing the "w" key, but the music doesn't changes, any help will be much appreciated.

the globals cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once
#include <iostream>
#include <string>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <SDL_mixer.h>
using namespace std;

SDL_Window* gwindow = NULL;
SDL_Renderer* grender = NULL;

Mix_Music* background_music = NULL;

enum Music_for { moves,special_moves };

const int window_height = 700;
const int window_width = 900;

bool is_running = true;
SDL_Event main_event;


main cpp code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "globals.h"

enum State { main_menu, fighting };
State state;

#include "Fighters.h"
Fighters game;

#include "music.h"
Music gb_music;

int main(int argc, char**argv){

    game.initialize();
    state = main_menu;
    while (is_running) {
        ticks_for_caping_fps = SDL_GetTicks();

        while (SDL_PollEvent(&main_event)) {
            if (main_event.type == SDL_QUIT) {
                is_running = false;
                break;
            }
            if (main_event.type == SDL_KEYDOWN) {
                switch (main_event.key.keysym.sym) {
                case SDLK_w:
                    state = fighting;
                    break;
                case SDLK_s:
                    state = main_menu;
                    break;
                }
            }
            
            //it behaves improperly here, but if i move it out of the que it doesnt even work unless i keep dragging the window arround
            gb_music.load_background_music();
        }

        SDL_SetRenderDrawColor(grender, 255, 255, 255, 255);
        SDL_RenderClear(grender);
        game.load_main_menu(main_event);
        
        SDL_RenderPresent(grender);
        capfps(ticks_for_caping_fps);
    }


    return 0;
}

the music header code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#pragma once
class Music {
	Mix_Chunk* sound_affect = NULL;
public:
	int load_background_music() {
		if (state == main_menu) {
			background_music = Mix_LoadMUS("sounds/main_menu_background_music.mp3");
			if (background_music == NULL) {
				cerr << "failed to load the background music " << Mix_GetError() << endl;
			}
			Mix_PlayMusic(background_music, -1);
		}
		if(state == fighting){
			background_music = Mix_LoadMUS("sounds/fighting_background_music.mp3");
			if (background_music == NULL) {
				cerr << "failed to load the fighting background music " << Mix_GetError() << endl;
			}
			Mix_PlayMusic(background_music, -1);
			
		}
		return 1;//for success
	}

};

Last edited on
Well in one case, you Mix_LoadMUS then Mix_HaltMusic
In the other, you Mix_HaltMusic then Mix_LoadMUS

Maybe, you have a whole mess of global variables.
well made some changes and removed that halt music ftn all together, my problem got better but it didnt do the trick
PS: i updated the code
Last edited on
Please don't update code in previous posts. Now @salem c's response makes no sense to someone coming along later to read it.

Just post your updated code in a new post.
Topic archived. No new replies allowed.