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

Author Topic: window.pollEvent() or real-time ?  (Read 1593 times)

0 Members and 1 Guest are viewing this topic.

CINEfilHD77

  • Newbie
  • *
  • Posts: 38
    • View Profile
window.pollEvent() or real-time ?
« on: August 17, 2016, 11:22:27 pm »
Hi everybody !

To begin with, sorry for my bad english, especially if it sounds so cold and devoid of enthusiasm (cause the truth is that it's the absolute contrary).

11pm, i'm a bit tired, so I will be brief.
So ! I'm actually programming a 2D game with SFML (online action shooter), but I'm now stuck on the problematic of events. I know that there are 2 types of events : the ones we can retrieve with window.pollEvent(sf::Event), and the real-time ones (sf::Keyboard::[...]). What are the best practices for a videogame ? I have explored Google a couple of days without any conclusion. My system should work according to this "diagram" :

- Main loop :
   - Inputs handling
   - Game logic
   - Rendering

So, the "Inputs handling" can handle events in two ways :
1. Call window.pollEvent() and send each event to associated listeners (game entities, etc...)
2. Browse each entity and call a handleInputs() method that will check for real-time inputs

I'm asking myself also about a thing, what is the best way to handle player movements for example ?
Update the position when a corresponding keyboard event is received, or set a flag (for example : action = MOVE_UP | MOVE_RIGHT, when up and right arrows have been pressed) and update the position during the process of "Game logic" ?

I apologize if I'm not as clear as I hoped.

Have a good day (or night) !  ;D

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: window.pollEvent() or real-time ?
« Reply #1 on: August 18, 2016, 01:09:46 pm »
Hi! :)

Just to be clear, checking real-time states is not a type of event. The 'first type' you mentioned are events and the 'second type' are states. I only mention it because if you mention events when meaning testing real-time states, there can be confusion.

So, the "Inputs handling" can handle events in two ways :
1. Call window.pollEvent() and send each event to associated listeners (game entities, etc...)
2. Browse each entity and call a handleInputs() method that will check for real-time inputs
Actually, it's more likely that you would do both.

You must process events for the window anyway since an operating system may complain or close your application if it doesn't, plus you probably want to use events for mouse clicks, single key presses etc..

Checking real-time states for input would then be used for things that are pressed (or held or whatever) or not. For example, you may hold a key to continually move right. Use real-time checks to see if it's (still) pressed. In this case, it doesn't matter when it was pressed, only that it is 'right now'.

- Main loop :
   - Inputs handling
   - Game logic
   - Rendering
With the above said, you can process real-time states inside the game logic directly or use real-time states inside the input handling block to set internal application states and use these internal states for use in the game logic. Note, though, that if you do this, the game logic will have the same input for each update per frame and may not be what you want if you are updating logic (possibly) multiple times per frame.

Mentioning updating logic multiple times per frame, I would recommend game logic to be updated by a fixed time step, with multiple updates occuring to 'catch' up with the amount of actual time passed. For more detailed information, you can read Fix Your Timestep and/or use the small timing library - Kairos - and use its Timestep class to simplify things.

Rendering can be taken care of if a separate function, method (if application itself is a class), directly in the loop, or in a thread. Rendering in a thread is very likely to not worth it. Rendering in the main thread is highly recommended. Remember that if you're rendering in a separate function, you may need to pass references to everything that it needs. If your game logic updates graphical objects directly, you'll need to pass those. If your game logic updates 'frames', 'skeletons' or 'proxies', you pass those and the renderer would create the graphical output from that information. This can have the added benefit that none of the graphics are actually involved in the game logic.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything