I know I should be using version 2.0, but I made this game when I didn't know any better and it's a learning experience. Also, I don't know if the library is the problem; I am having a freaky bug that is only happening in Release mode, though I am starting to wonder because I don't know what else it could be. If that is the problem, I guess I'll have to change libraries. I have checked all my variables, and haven't found any uninitialized variables so I don't THINK that's it.
I have reduced my code down to the following and am looking for any ideas... I have tried everything I could think of and even flung some mud to see if anything might stick, all to no avail. It works in Debug Mode.
I am opening a menu and when I click 'return to game' it is supposed to return to game and close the menu. If I press 'q' with the menu open, it works and returns to game. But if I click 'return to game', it doesn't work. The menu closes for a split second (so the game::update code is being called and the screen is redrawn) but then the menu immediately opens again.
If I click any of the other buttons (restart game, level, quit, goto SplashScreen) those work. But those also enact major code changes behind the scene for different states.
The fact that pressing q works has me so confused, it's as if the currentEvent in game is holding onto the Escape and entering that block again, but I don't see how that is possible because the window is repolled at the top of the loop and there are buttons pressed and the window polled inside of the menu code so it should be a non-issue. Also, if I change the type of currentEvent inside the gameLoop code after it returns from the ShowGameMenu() so that it is an unhandled event it still happens.
here is the minimal code:
Game::GameLoop
{ //etc code &Update and Draw Functions
if(currentEvent.Type == sf::Event::KeyPressed && currentEvent.Key.Code == sf::Key::Escape)
{
ShowGameMenu();
}
//Code
//Display() - clear buffer to the screen
}
void Game::ShowGameMenu()
{
int bMenu = menu.Show(m_MainWindow);
switch (bMenu) {
case 1: //gameOn
m_GameState = dlb::Playing;
break;//etc cases that aren't the problem
}
}
int Menu::Show(sf::RenderWindow& window)
{//Various menuState code that isn't the problem removed - This passes the returnCode back to game
int code = MainMenu(window);
return code;
}
//returns 1-gameOn, 2-SplashScreen, 0-close, 3-restartLvl, 4-restartGame, 5-option,
int Menu::MainMenu(sf::RenderWindow& window)
{
int returnCode = -1;
bool running = true;
sprite.SetPosition(center);
sf::Event currEvent;
while (running)
{
window.GetEvent(currEvent);
Draw(window); //Displays the menu on top of game window
if(currEvent.Type == sf::Event::KeyPressed && currEvent.Key.Code == sf::Key::Q) //This works too
{
returnCode = 1;
running = false;
}
if (currEvent.Type == sf::Event::MouseButtonReleased && currEvent.MouseButton.Button == sf::Mouse::Left)
{
sf::Vector2f mouse = sf::Vector2f((float)currEvent.MouseButton.X, (float)currEvent.MouseButton.Y);
while ((*currItr)->IsEnd() != true)
{
if ((*currItr)->isClicked(mouse))
{
//returns 1-gameOn, 2-SplashScreen, 0-close, 3-restartLvl, 4-restartGame, 5-option,
switch ((*currItr)->GetType())
{
case dlb::ReturnGame:
returnCode = 1;
break;
//various cases removed here because they matter not
}
running = false;
break;
}
else
{
++currItr;
}
}
}
}
return returnCode;
}
Any thoughts would be welcome. I have tried everything I could think of.
Edit:
Also, if I hold down an arrow key before clicking (which moves the player in-game) the menu stays closed and the player moves.