Asynchronous event handling in c++

How would I go about making an asynchronous event handler. I am working on making a terminal game and I need to asynchronously handle the keyboard input events. Thanks in advance!
What exactly do you mean by "asynchronously handle the keyboard input events"? When the user presses a key, what do you think your program should do, and how would it differ if it was handling the event "synchronously"?
I need to be able to move the character around the screen while changing health. The problem with using one function is that it’s waiting on keyboard input so it can’t update the health at the same time
You don't need asynchronous events for that, you just need a normal game loop and a function that lets you poll the state of the keyboard.
1
2
3
4
5
6
7
8
9
while (game_is_running){
    while (!event_queue.empty()){
        auto event = event_queue.pop();
        handle_event(event); //handle keys being pressed, released, etc.
    }
    
    update_game_state();
    draw_screen();
}

If you're only using <iostream> or <cstdio>/<stdio.h>, that's not enough to do this. Those headers are designed to work this streams but you need to treat the console as an actual console, not just as a linear stream of characters.
What platform are you using?
I’m using A Debian based Linux distribution and code blocks.
Take a look at ncurses. Here's a guide I found after five seconds of googling, so I make no assurances about its quality: http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/
Where in here is polling for events? I thought you said I need to poll for the state of the keyboard?
Ncurses doesn't seems like it has polling, but it does allow you to have a timer for how long to wait for input. I would doubt this can be used for asynchronous input, it is designed for timeouts.

If your game is turn based, you should probably look at example rougelike games out there and see how they handle their logic, because there is no reason for async input for a turn based game.

If you are trying to make a real time game (as in you want the play to slowly loose HP while standing still), not turn based, you could look into libtcod or a full blown graphical API like SDL2 or SFML which does have non blocking input.

I believe the way how ncurses could have async IO is by using low level OS dependent functions, like getting the stdin file descriptor and polling it on linux, and probably something different but similar on windows, but it isn't worth the hassle.
How do you get libtcod for Linux?
Where in here is polling for events?
ncurses' getch() will return the key that is currently being pressed, or ERR when no key is being pressed.
Sorry for being late, you will have to compile it from source (unless you can find libtcod in your package manager), and for the autotools build section it seems like it explains instructions very clearly.

https://github.com/libtcod/libtcod
Topic archived. No new replies allowed.