Members - Reputation: 127
Posted 29 October 2012 - 06:21 AM
I just want to ask if can I check for "KEY"(keyboard) that is HOLD/PRESSED for a long time, because I am to create a clone of breakout with air hockey for 2 different human players.
Here's the list of my concern:
1. Do I need other/ 3rd party library for KEY HOLDS?
2. Is multi-threading needed? I don't know anything about this multi-threading stuff and
I don't think about using one(I'm just a NEWBIE).
3. One more thing, what if the two players pressed their respective key at the same time,
how can I program to avoid error or worse one player's key is prioritized first before
the the key of the other.
Player 1 = W for UP & S for DOWN
Player 2 = O for UP & L for DOWN
(example: W & L is pressed at the same time)
Great thanks for help.
PS: I use GLUT for the visuals of the game.
Crossbones+ - Reputation: 18519
Posted 29 October 2012 - 06:36 AM
Read a bit of documentation and maybe a tutorial on how to do keyboard input with SDL or SFML and post back if you can't figure it out.
Edited by alvaro, 29 October 2012 - 06:36 AM.
Members - Reputation: 1589
Posted 29 October 2012 - 02:10 PM
#3 Input should be just like any other task such as physics, or rendering, ie: it will be processed during a slice of the timestep, so collect your key ups key downs when they occur, process them all at once.
As Alvaro mentioned, each os will have it's own method of providing keyboard input. In windows, this would be the windows message pump, and the keyup/keydown messages. This is where a prebuilt library will help, but it is not necessary.
Senior Staff - Reputation: 24264
Posted 01 November 2012 - 01:51 AM
You can find a couple of tutorials on keyboard handling with GLUT here and here.
For your third question, certain keyboards can have trouble with missing key events if certain combinations of keys are pressed simultaneously, but this is a hardware problem that even effects commercial AAA titles and isn't something you should really worry about, and would only normally be a problem if you would expect a large number of keys to be held down simultaneously. As mentioned by others, the order of key-presses should not be a problem.
Let us know if you have trouble implementing your input and we'll give you some more tips, but hopefully you've got a good starting point. You should probably stick with a nice simple input system for your first couple of games, but once you've got a good handle on that you might consider more advanced functionality such as allowing your players to re-map the keys; there are some thoughts on how to do so in ApochPiQ's journal entry "designing a robust input handling system for games" and a link to a sample implementation of the code, although it may be a bit advanced till you've had some more practice.
Hope that's helpful!
- Jason Astle-Adams.