Jump to content
  • Advertisement
Sign in to follow this  

SDL Mixer stopping after eight plays

This topic is 2087 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone,


So I've got a Piano that I'd like to be able to have the player use when they're near it.  The goal is to have them play a certain melody that unlocks the next piece of the puzzle.  Here's the code:


/*in c_Stereo.cpp*/

void c_Stereo::loadSFX(const char* fileName, std::string SFXname)
	Mix_Chunk *SFX = Mix_LoadWAV( fileName );
	SFX_list.insert( std::pair<std::string, Mix_Chunk*>(SFXname, SFX) );

void c_Stereo::play_loadedSFX(std::string SFXname)
	Mix_Chunk *SFX;

	std::map<std::string, Mix_Chunk*>::const_iterator results = SFX_list.find(SFXname);
	if ( results == SFX_list.end() ){
		SFX = results->second;

	Mix_PlayChannel(-1, SFX, 0);

/*in c_Piano.cpp*/
c_Piano::c_Piano(int x, int y){
/*piano is the name of the c_Stereo object inside c_Piano, sorry for any confusion! it just made sense to me*/
        piano.loadSFX("A.wav", "A");
	piano.loadSFX("B.wav", "B");
	piano.loadSFX("C.wav", "C");
	piano.loadSFX("D.wav", "D");
	piano.loadSFX("E.wav", "E");
	piano.loadSFX("F.wav", "F");
	piano.loadSFX("G.wav", "G");

void c_Piano::handleInput()
	if ( event.type == SDL_KEYDOWN )
		switch (event.key.keysym.sym)
		case SDLK_a: piano.play_loadedSFX( "A" ); break;
		case SDLK_b: piano.play_loadedSFX( "B" ); break;
		case SDLK_c: piano.play_loadedSFX( "C" ); break;
		case SDLK_d: piano.play_loadedSFX( "D" ); break;
		case SDLK_e: piano.play_loadedSFX( "E" ); break;
		case SDLK_f: piano.play_loadedSFX( "F" ); break;
		case SDLK_g: piano.play_loadedSFX( "G" ); break;

however, for some reason the program only allows any note to be played eight times.  for example, if I played "A" eight times, the piano wouldn't play any more notes.

I do know that c_Piano.handle_input() is still being performed after the 8 note limit  through some simple debugging.  Also, it seems like if I wait a few seconds, the piano will let me play another eight notes.

I have a suspicion it has something to do with the SDL_Mixer's channels, but I have no idea how to fix it.


Any insight would be appreciated, thanks!

Edited by johnmarinelli

Share this post

Link to post
Share on other sites

A few things look odd, though I don't know if they are related to your issue.
1) I don't see where you pass in 'event' to handleInput(). That either means that 'event' is a member variable of 'c_Piano' which would be odd, or that 'event' is global, which would be odd.

2) You play a sound, even when your map lookup fails - but you play a null sound. Wouldn't that generate an error message for Mix_PlayChannel? I haven't used it in ages, so I might be wrong, but why even bother calling Mix_PlayChannel() with a null pointer?

3) You never check for the return result of Mix_PlayChannel, so you don't know if it's telling you if there is a problem or not.

I think the play_loadedSFX() should be rewritten as:

void Stereo::play_SFX(const std::string &name)
	//Check if the map contains that name.
	//Return if it doesn't have the sound.
	if(SFX_list.count(name) == 0)

	Mix_Chunk *sound = SFX_list[name];

	if(Mix_PlayChannel(-1, SFX, 0) == -1)
		std::cout << "An error occured in Stereo::PlaySound() with 'name' = \"" << name << "\".\n"
			<< "Mix_GetError(): \"" << Mix_GetError() << "\"" << std::endl;

Further, you don't check if 'Mix_LoadWAV()' succeeded or not either! smile.png

Programming is alot easier when you have proper error checking. I like it when my program tells me what the problem is, rather than me having to spend time hunting for the problem. wink.png


You can Mix_GetError() to get the error message if MIX_LoadWAV() returns NULL.


And "handleInput()" should have 'event' passed into it, instead of global. Globals cause more problems than they fix, though they innocently deceive you into thinking that things are easier, they can cause all kinds of headaches if you aren't careful.

Share this post

Link to post
Share on other sites

Hello again! 


'event' is indeed a global - however, I don't know where exactly I would put it if it weren't a global - I suppose the gameStates object? 

Admittedly I am bad with error-checking; I'll try doing that and see where it gets me.

Also, I didn't know you could use return; in a void function, how cool!


EDIT: I fixed it; it was a channel problem.  However, I do believe some good error checking would have saved me some time - I think next week my goal will to be to refactor/error check my code :) thanks again!

Edited by johnmarinelli

Share this post

Link to post
Share on other sites

You don't want to 'store' it anywhere, because it's something you use and then discard. It's not supposed to persist for longer than the event-handling code persists. 


Perhaps something like this:


//Your main loop:
while(the game is running)
    //Handle all the events.
    SDL_Event event;
        //Hands the event (temporarily!) to the GameState. GameState doesn't store it anywhere, it
        //just uses it then returns. GameState also hands the event (temporarily!) to other class' HandeEvent() functions so they also have a chance to deal with events.
    //Handle all the updating.
    //Hand the current time to the gameState to update itself. It'll pass the time along to other class' Update() functions.
    //I personally prefer converting the SDL_GetTicks() result to a float first and dividing by 1000, I find it easier to work with. (1.0 equals one second. 0.5 equals half a second, and so on).
    //Handle all the drawing.
    //Clear the screen.
    SDL_FillRect(screen, ...);
    //Hand the GameState the SDL_Surface, temporarily!
    //GameState uses it and hands it (temporarily!) to other classes that need it.
    //Apply the changes by swapping the double buffer.
It doesn't have to be exactly like that, but that's the general idea. SDL_Event is only valid from one call of SDL_PollEvent() to the next, so it should be held onto indefinitely, just used and discarded. Actually, we use and reuse and reuse it until there are no more events to process, then it gets discarded at the end of the loop, then created at the start of it again, but that's just semantics. happy.png
Mentally, we use it once then we discard it and get a new one for the next event, though in actuality it is reused.
SDL_Event is only valid from call to call of SDL_PollEvent(), and is invalid after the last SDL_PollEvent() call for that frame. By keeping SDL_Event local to your main loop, you enforce that it is only used inside of HandleInput() calls, which is good, because any usage outside of those calls will give buggy results. Instead of monitoring your own code usage, "Am I even able to use my global SDL_Event during this function or is it invalid?", you make your compiler work for you, and give a compiler error in situations were you can't use it. smile.png
In my previous post I said, "I like it when my program tells me what the problem is, rather than me having to spend time hunting for the problem".
I like it even better when my compiler won't even let me compile certain kinds of mistakes, when I am able to enforce safety in code (which isn't always possible).
Edited by Servant of the Lord

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!