Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problem closing my menu using version 1.6 in Release Mode  (Read 2333 times)

0 Members and 1 Guest are viewing this topic.

bickinator

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Problem closing my menu using version 1.6 in Release Mode
« on: March 06, 2013, 05:43:43 am »
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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Problem closing my menu using version 1.6 in Release Mode
« Reply #1 on: March 06, 2013, 07:19:47 am »
You should take a close look at the event tutorial again. ;)
Calling window.GetEvent() doesn't always have to return an event, that's why one usually does it in another while loop. Try to use the event where none has been triggered can lead to undefined behavior.

Also your code is not complete and thus we can't say for sure. You should always provide a minimal and complete example. ;)

It's never 'too late' to switch to SFML 2, it's just a question of the will to redactor. Personally I think one should never fear the refactoring. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

bickinator

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Problem closing my menu using version 1.6 in Release Mode
« Reply #2 on: March 06, 2013, 05:31:22 pm »
That worked! kindof...
I wrapped my getEvent in while loops. However, now it takes two occurrences of each event before it does anything. e.g. I have to double click and press each key twice before anything happens. Is this code just doomed to fail from bad design?  :P

This game was started last summer as a learning experience, and I wanted to add a Menu recently just to see what would happen, if I could do it (obviously not  ;) ) I thought about refactoring, but decided instead to keep learning. This isn't really a game others would want to play. It's a clone of the Maze Tutorial that comes with GameMaker.

 I'll probably start a new game soon in SFML 2 that hopefully isn't written so poorly.

Thank you immeasurably for fixing this first problem though; I've been racking my brain for 3 days over it.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Re: Problem closing my menu using version 1.6 in Release Mode
« Reply #3 on: March 06, 2013, 06:11:35 pm »
Is this code just doomed to fail from bad design?  :P
Could be, if you don't manage to track down some logic problem it might very well be poor design.

Thank you immeasurably for fixing this first problem though; I've been racking my brain for 3 days over it.
You should get here earlier then nexttime. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

bickinator

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Problem closing my menu using version 1.6 in Release Mode
« Reply #4 on: March 07, 2013, 02:21:43 am »
Doh! I had changed some code for debugging purposes and didn't change it back when I put other code back in and it ended up a mess.

I think I am going to refactor to make everything nicer and more organized. Then I am going to switch the game to SFML 2.0. Now that I've gotten it figured out a little better, I'll keep it going until I think up a new game idea.

 

anything