c++ - OpenGL/GLUT keyboard managed with bool*: all values appear to be set to ints at second initialisation. Any ideas? -
making game opengl/glut , c++.
i've heard easiest way manage keyboard stuff in glut use array of bools.
so basically, here's 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 want know state of key, return keys[key].
i have similar setup special keys. now, in game when die, resets values need reset , starts game again. until now, included
keys = new bool[256];
and similar declaration special keys array. makes no sense me occasionally, supposed resetting of arrays cause or of values in keys become two- or three-digit integers (therefore evaluating true , making player character move uncontrollably until 1 or more of keys apparently down depressed). have suspicion happened because @ time of endgame 1 of keys pressed down, thereby confusing c++ when tried make whole array false , found 1 thing stuck true.
man, that's confusing. problem has been solved eliminating initialisations of keys, far can see. don't it. got ideas on how managed turn bools ints?
this statement
keys = new bool[256];
allocates memory. not initialize memory particular value. that, memset(keys, 0, sizeof(bool) * 256)
zero.
however, shouldn't allocate memory (which new
does) reinitialize array. should actually reinitialize it, using memset
or whatever feature used initialize "false" values.
lastly, why allocating array new
begin with? make global array: bool keys[256]
. initialize (with memset
) before use it, , fine.
i have suggested std::vector<bool>
, sadly, not usable. use std::vector<char>
though, 0 meaning false. or std::bitset
256 bits. of these contain allocations internally (thus being safe), , have ways clear memory value.
making game opengl/glut , c++.
as aside, wouldn't advise that. glut isn't tool designed kinds of timings games need. glfw allows control main loop, rather using callbacks, more appropriate tool game.
Comments
Post a Comment