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

Author Topic: effective main loop event handling?  (Read 1037 times)

0 Members and 1 Guest are viewing this topic.

grim

  • Guest
effective main loop event handling?
« on: January 29, 2014, 06:08:52 am »
I am using a state engine for my game and currently my main loop is:
while(engine.isRunning()){
                while(engine.getWindow()->isOpen()){
                        while(engine.getWindow()->pollEvent(event)){
                                engine.Handling(event);
                        }

                        engine.Update();
                        engine.Paint();
                }
        }

But I am wondering if that is an effective way of doing this.  The problem I foresee is when the character has to move it won't happen until the person let's go of the movement and therefore the character will just jump across the screen. Would it be alright to copy and paste
engine.Update()
and
engine.Paint()
into the the loop so that it occurs right after the Handling call and outside the loop as well? or is that some form of bad coding practice? 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10998
    • View Profile
    • development blog
    • Email
AW: effective main loop event handling?
« Reply #1 on: January 29, 2014, 08:04:31 am »
Keyboard/mouse events are triggered in a certain interval, thus when pressing a key the event loop won't go infinitely in a loop. For example if your game runs at 60fps, thus you get to draw a new frame every 16.66ms. A new event might for example get fired every 250ms, thus there's a lot of time to finish processing an event, before the next one gets triggeted. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

MadMartin

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: effective main loop event handling?
« Reply #2 on: January 29, 2014, 12:33:50 pm »
All you need to know about main loops:
http://gafferongames.com/game-physics/fix-your-timestep/

The article explains the separation of update() and render() very thoroughly.
If you combine it with e.g. the Actions of Nexus' Thor library ( http://www.bromeon.ch/libraries/thor/v2.0/tutorial-actions.html ) you can easily write a flexible, efficient main loop.

 

anything