Game event loop using ncurses library

Started by
7 comments, last by Wooh 9 years, 4 months ago

Hello,

I am writing console RPG game. Mainly is prototype, cause I will remake it using SDL2 and OpenGL.

But for now I am planning to stick to ncurses library, for input and rendering in console.

I am using getch() function for input, but it is blocking, so If I have something like bullet objects, it moves only then some input is entered. So I need something like in SDL2 polling events.

Know all I have in input function:


void PlayState::input(Game* game)
{
    game->currentInput = getch(); //blocking input
}

Any help is appreciated. But I prefer to use ncurses for input.

Deltron Zero and Automator.

Advertisement

You can use "select()" or (preferably) "epoll()" to see if stdin has data on it before calling "getch()" to process it.

You can use "select()" or (preferably) "epoll()" to see if stdin has data on it before calling "getch()" to process it.

Firstly, thanks for reply. Just one question - will it work only on linux? I am preffering cross-platform solutions, but it may work for now.

Deltron Zero and Automator.

The timeout function can be used to set how long getch will wait for input. timeout(0) will make getch() return immediately, and if there was no input the return value is ERR.

The timeout function can be used to set how long getch will wait for input. timeout(0) will make getch() return immediately, and if there was no input the return value is ERR.

This works. I set 100ms timeout, but still rendering looks awful - it clears screen on every call to render(), So I get this flickering, flashing effect.

Deltron Zero and Automator.

Do you use clear(); to clear the screen?

For ncurses you should use the nodelay function to tell the api to not block on input reads from a given terminal.

http://hughm.cs.ukzn.ac.za/~murrellh/os/notes/ncurses.html#input

[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

Do you use clear(); to clear the screen?

Yes. Thats why its flashing.

Deltron Zero and Automator.

Well, I don't know what is the best way. Maybe you should not clear the screen but instead output the new graphics on top of the old graphics. If your rendering function is complex and contain many layers you might want to do some double buffering.

This topic is closed to new replies.

Advertisement