Hi there,
I’m a beginner to both C++ and SDL2.0. To warm up, I’m making a slot machine game, and I’ve been stuck for a week on the problem of preventing multiple spins from being queued up. The spinReelsRandom() function is what starts a series of loops that generate random numbers in here.
The game operates perfectly right now except for the fact that a player who repeatedly hits the Space button during a spin (common practice for slot players) is queuing up spins that play out immediately after the user is done. I want to ignore input starting right after the first time the user presses the spacebar to start the spin until the spinReelsRandom() function is complete.
What I’ve already tried:
I’m able to get the desired result by using SDL_SetEventFilter (commented out below) in the main loop. My eventFilter is simply returning 1. However, for some reason, this prevents SDL_Quit from working. My guess as tp why this works is because the filter is only returning 1 while there are no events waiting to be polled since it’s outside the event polling loop, preventing users from queuing spins until the spinReelsRandom function is complete. If there’s an explanation, and a way to re-enable SDL_Quit, this could be it!
Moving the filter into the procedure right after pressing the spacebar doesn’t seem to work, and I’ve tried following up with a SDL_SetEventFilter(NULL, NULL) to reset the event filter after the spin is complete but it doesn’t seem to work.
I’ve also tried using an “isSpinning” flag that flips true while the reel is spinning and using a check on initiating a spin, but as soon as the flag flips back to false once the spin is complete, the additional polled spins begin.
Is there something I can be doing with SDL_PeepEvents? Any advice is greatly appreciated!
Here’s the main loop after initialization, with my event being “e”:
while (!quit)
{
while (SDL_PollEvent(&e) != 0)
{
// When an event is polled, this sets a filter to stop polling for more events until that action is completed.
// Note: This is what’s stopping the repeated spins, however it has disabled the quit functionality.
// SDL_SetEventFilter(eventFilter, &e);
switch (e.type)
{
case SDL_QUIT:
quit = true;
break;
case SDL_KEYDOWN:
switch (e.key.keysym.sym)
{
case SDLK_SPACE:
//Function that continuously spins the reels until they reach a random destination
spinReelsRandom();
SDL_Delay(25);
break;
case SDLK_0:
cout << "This works";
break;
case SDLK_ESCAPE:
quit = true;
break;
}
}
//Final Spin Cleanup
spinCleanup();
}
}