Making a game with OpenGL/GLUT and C++.
I've heard the easiest way to manage keyboard stuff in GLUT is to use an array of bools.
So basically, here's what I have:
bool* keys = new bool[256];
...
void keyDown(unsigned char ch, int x, int y)
{
keys[ch] = true;
}
void keyUp(unsigned char ch, int x, int y)
{
keys[ch] = false;
}
Then if I want to know the state of a key, I just return keys[key].
I have a similar setup for the special keys. Now, in my game when you die, it resets all the values that need to be reset and starts the game again. Until just now, that included
keys = new bool[256];
and a similar declaration for the special keys array. What makes no sense to me is that occasionally, this supposed resetting of the arrays would cause some or all of the values in keys to become two- or three-digit integers (therefore always evaluating true and making the player character move uncontrollably until one or more of the keys that were apparently down were depressed). I have a suspicion that this happened because at time of endgame one of the keys was pressed down, thereby confusing C++ when it tried to make the whole array false and found that one thing was stuck true.
Man, that's confusing. The problem has been solved by eliminating all the extra initialisations of keys, as far as I can see. But I don't get it. Anyone got any ideas on how I managed to turn some bools into ints?