class keyboard
{
public:
keyboard();
~keyboard();
bool getState(SDLKey sym) {return m_keys[sym];}
void setState(SDLKey sym, bool state) {m_keys[sym] = state;}
void handleInput();
protected:
bool m_keys[256];
};
And now I'm experiencing the same problem as I was at first. I've tried to implement my previous "hack" but without luck. I really hope anyone can shed light on this for me. Any ideas are welcome. If I've been too vague in some areas, please feel free to ask questions.
Thanks in advance,
nto.
Strange problem concerning bool arrays
Hey everybody!
I've been having some trouble lately after having implemented an array
bool keys[256] = {false};
that represents which keys are pressed (this is handled through the event loop). However, I have a very weird problem occuring. When using this type of declaration, my program begins to act unpredictably, it will just crash on windows, on linux it will display some of my pictures at random, move in random directions and other weird behavior. At first I declared this as a global variable, and it wouldn't work. However! If I added another variable, like this:
bool randomvariablename = false;
bool keys[256] = {false};
It all of a sudden works flawlessly. Now, I could live with this hack. But today I tried redesigning my game, so that events are handled by different classes. Keyboard class is as such:
Generally bugs of this type are due to writing outside the bounds of memory. You are probably accessing the array with a negative index or an index greater than 255. Adding the additional variable sometimes "solves" the problem because it causes the program to be laid out differently in memory. This means that instead of overwriting something critical and crashing, instead your invalid memory accesses write someplace where they are unnoticed
You most likely have a bug somewhere in your program that's accessing memory that shouldn't be accessed (sometimes adding or removing variable declarations can have an indirect effect on the behavior of these sorts of bugs, which can be confusing [Edit: as noted above]).
Here's where I'd start: use either std::vector (the usual caveats regarding vector<bool> apply here), boost::array<bool>, or std::bitset (I'd recommend the latter), and use the range-checking accessors (vector/array::at() and bitset::test()). Any bad indexing will then result in an exception, which should help you track the problem down. (The next step would probably be to make sure you're using range-checked containers everywhere - which you really should be doing anyway :).
Here's where I'd start: use either std::vector (the usual caveats regarding vector<bool> apply here), boost::array<bool>, or std::bitset (I'd recommend the latter), and use the range-checking accessors (vector/array::at() and bitset::test()). Any bad indexing will then result in an exception, which should help you track the problem down. (The next step would probably be to make sure you're using range-checked containers everywhere - which you really should be doing anyway :).
[edit]continuing on from cshowe[/edit]Yep SDLKey has more than 256 keys and this will be the reason see SDL_keysym.h for the enum values and range.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement