I'd like to make a simple game as console app. I want the game keeps on going with the sprite and animation (using emojis) while there's no keyboard input; and it will react accordingly if there are ones such as arrow keys to direct the actor path, etc. However, I don't know how to do it using Swift (v5).
I knew there's termios() in Darwin module but I found it incompatible with many C examples out there. Any hints, please?
I don't think I would need Dispatch. There's some configurations in Terminal via termios() to make it non-blocking. But I don't know how to do it in Swift.
Would you mind linking one of these non-working C examples? Unless they utilize weird macros or variadic functions, you should be able to call all the C stuff you need.
It appears FD_SET and FD_ZERO are implemented as macros, which is why they aren't importing properly.
To get around this, add a C header to your swift project with the following declarations and them import it into your swift project using a method like this:
There are some variables and methods that don't exist in Darwin
module, such as the FD_ZERO.
The select you’re doing on STDIN_FILENO in kbhit [1] is equivalent to using a Dispatch source, and Dispatch sources have a much nicer projection into Swift.
Share and Enjoy
Quinn “The Eskimo!” @ DTS @ Apple
[1] Well, kbhit isn’t using STDIN_FILENO, but it should be (-: