• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
johnmarinelli

SDL Mixer stopping after eight plays

3 posts in this topic

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 = NULL;
	}
	else
		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
0

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)
		return;

	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.

2

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
0

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;
    while(SDL_PollEvent(&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.
        gameState.HandleEvent(event);
    }
    
    //-------------------------------------
    //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).
    gameState.Update(SDL_GetTicks());
 
    //-------------------------------------
    //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.
    gameState.Draw(screen);
 
    //Apply the changes by swapping the double buffer.
    SDL_Flip(screen);
}
 
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
2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0