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

Author Topic: Pressing Enter causes two screen switches instead of 1!  (Read 6707 times)

0 Members and 2 Guests are viewing this topic.

supdawg

  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: Pressing Enter causes two screen switches instead of 1!
« Reply #15 on: December 01, 2012, 08:36:16 pm »
Thanks so much expl0it3r. I got my fade to work by calling the update function of my ScreenManager and Animation classes which update the alpha values and screens. But i still have questions because some things still seem a bit strange.

First heres my main loop right now.

bool enableKeyRepeat = false;
  sf::RenderWindow Window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32),"Hulq", sf::Style::None);
  Window.clear(sf::Color(0, 0, 0));
  Window.setFramerateLimit(60);
  Window.setKeyRepeatEnabled(enableKeyRepeat);

  ScreenManager::getInstance().initialize();
  ScreenManager::getInstance().loadContent();

  sf::Clock clock;
while(Window.isOpen()){
    while(Window.pollEvent(ScreenManager::getInstance().event)){
      switch (ScreenManager::getInstance().event.type){
        case sf::Event::Closed:
          Window.close();
          break;

        case sf::Event::KeyPressed:
          if(ScreenManager::getInstance().event.key.code == sf::Keyboard::Escape) {
            Window.close();
          }//if escape is pressed close window and skip everything in loop
          else
            ScreenManager::getInstance().update(ScreenManager::getInstance().event, Window);
          break;

        default:
          ScreenManager::getInstance().update(ScreenManager::getInstance().event, Window);
      }//end of switch(event.type)*/


    }//end of while Poll event
    Window.clear(sf::Color(0, 0, 0));
    /****/ScreenManager::getInstance().update(ScreenManager::getInstance().event, Window);
    ScreenManager::getInstance().draw(Window);
    Window.display();
    ScreenManager::getInstance().timeBetwFrame = clock.restart(); //records time between frame
  }//end of while isOpen

The fade in and out only works if i put the line with /****/ in. if i take that line out it doesnt work. basically if i take that line out, i have to continuously press the return key (which switches screens) in order to fade in and out. if i put the line in, then i can press the return key once and it will fade out, switch screens, and fade the new screen in. I have read the events tutorial. From what i deduced, if no event is happening it wont even go inside the while(pollEvent()) and it will just keep looping the while(isOpen()) loop. And my update() function is called inside the while(pollEvent()). So i made a call to update outside (the /****/ line). is this a good way to do things?

Let me try to give u the jist of what im doing. my update function updates everything. screen and alpha too. once the Return key is pressed itll start the fade animation. A bool value called "inTransition" will become true and while thats true it will keep doing the fade animation as long as update() is called. basically to keep changing the alpha value (make it go from 0 to 255 and back to 0 to effectively fade out and then fade in) update() must be called which checks the bool inTransition to see if its true and if true, it proceeds to change the alpha value. now, if i press Return and then dont press anything else, update() wont be called unless i bring it outside to the while(isOpen()) loop. however it makes more sense to keep it only inside the while(pollEvent()) loop because you update() based on whether there are events or not. and yes i did take your advice, and my update function draws various things to the window but i only call Window.display() once. i looked at your SmallGameEngine and it seems your using a stack to push and pop states. it seems kind of advanced and i dont quite understand how you fade in your IntroState. You reduce the alpha value everytime it loops irrespective of keypresses. Its really clean code but i havent really used stacks. How exactly do you use stacks in a game? Doesnt while(pollEvent()) push and pop events for you? And does C++ only support .hpp headers? i used .h headers for my headers.



also sorry i keep bombarding with questions. for my titlescreen i just draw sf::Text to the screen. is there any easy way to center the text in the center of the window? right now im doing this.

    text.setPosition(SCREEN_WIDTH/2 - ( (stringLength/3) * text.getCharacterSize()), SCREEN_HEIGHT/2 - text.getCharacterSize());
 

as you can see, its completely random guessing with numbers. i understand i could load an image with a sprite, but i think id run into the same problem centering a sprite.

btw how do you change a thread to [SOLVED]?

i apologize for the huge essay post.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Pressing Enter causes two screen switches instead of 1!
« Reply #16 on: December 01, 2012, 09:40:38 pm »
The fade in and out only works if i put the line with /****/ in. if i take that line out it doesnt work. basically if i take that line out, i have to continuously press the return key (which switches screens) in order to fade in and out. if i put the line in, then i can press the return key once and it will fade out, switch screens, and fade the new screen in. I have read the events tutorial. From what i deduced, if no event is happening it wont even go inside the while(pollEvent()) and it will just keep looping the while(isOpen()) loop. And my update() function is called inside the while(pollEvent()). So i made a call to update outside (the /****/ line). is this a good way to do things?
As I already said if you do it with your strange event/input handling then you have to make sure that the event is filled with the correct values otherwise the behavior is undefined...

i looked at your SmallGameEngine and it seems your using a stack to push and pop states. it seems kind of advanced and i dont quite understand how you fade in your IntroState. You reduce the alpha value everytime it loops irrespective of keypresses. Its really clean code but i havent really used stacks.
Well as I said, the framerate is limited to 60fps thus I can rely on the fact the update function is getting called 60 times every second and thus reducing the alpha value by 1 every iteration will need ~4.25 seconds to go from 255 to 0. I'm drawing a rectangle over the whole screen and simply reduce it's alpha channel until it's fully transparent.
But you're right I don't do anything with keys, it just happens when you start that state until it reaches 0. ;)

How exactly do you use stacks in a game?
It's simply the FILO principle (First In Last Out) and you've got the two operations push() to put a new object onto the stack and pop to remove the top object.

Doesnt while(pollEvent()) push and pop events for you?
Yes it pushes the new events into the queue and pops one event every call and puts it into the referenced event object.

And does C++ only support .hpp headers? i used .h headers for my headers.
C++ (the standard) doesn't really say anything about this, but it's the compilers that define what works and what not and to my knowledge all C++ compiler support the .hpp extention and I advise everyone to use it, when they write C++ code, so you'd already see in what language a library or similar is written in: .c/.h = C; .cpp/.hpp = C++

is there any easy way to center the text in the center of the window? right now im doing this.
You should really read the documentation...
sf::Text text("Hello", font, 20);
text.setOrigin(text.getGlobalBounds().width/2.f, text.getGlobalBounds().height()/2.f);
text.setPosition(static_cast<sf::Vector2f>(window.getSize()/2.f));

btw how do you change a thread to [SOLVED]?
By editing your first post and changing the title there. ;)

FYI: You really shouldn't be using global variables, that is your singletons are global variables too! ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

supdawg

  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: Pressing Enter causes two screen switches instead of 1!
« Reply #17 on: December 02, 2012, 12:50:41 am »
Well as I said, the framerate is limited to 60fps thus I can rely on the fact the update function is getting called 60 times every second and thus reducing the alpha value by 1 every iteration will need ~4.25 seconds to go from 255 to 0. I'm drawing a rectangle over the whole screen and simply reduce it's alpha channel until it's fully transparent.
But you're right I don't do anything with keys, it just happens when you start that state until it reaches 0.

So youre update does get called every frame then. Ah i see. If i do that then my fade works. Before i display i call update everytime even if the user doesnt press any keys. The thing i was worried about is, if there is no event happening, what exactly happens? like say theres no event happening, what does event contain? i couldnt find this in the documentation for events. if the event stack is empty then what happens when u try to access an sf::Event member?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Pressing Enter causes two screen switches instead of 1!
« Reply #18 on: December 02, 2012, 01:37:32 am »
The thing i was worried about is, if there is no event happening, what exactly happens? like say theres no event happening, what does event contain? i couldnt find this in the documentation for events. if the event stack is empty then what happens when u try to access an sf::Event member?
sf::Event is just a data class, it holds some structs but they never get initialized with any 'default' values, thus if you just instantiate a sf::Event object and try to access a variable you'll get anything that could get casted to that type.
That's why I've been telling you now already a few times, that it's a bad idea to separate the event processing from the event 'capturing'. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

supdawg

  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: Pressing Enter causes two screen switches instead of 1!
« Reply #19 on: December 02, 2012, 02:29:18 am »
alrite i get it.

i searched places and couldnt find the difference between global coordinates, screen coordinates and local coordinates. whats the difference between them?

is global your whole monitor and screen just your window?