Public Group

# Silly input question

This topic is 4426 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to write code that separates the sdl event stuff from the rest of my code. I made a input manager class that uses an array of bools to store which keys are pressed or not. I was wondering if thats the right way to go about it because it seems quite clean but now that I am trying to use it I'm not sure. I used it to exit the gameloop when escape is pressed ok but for somthing like controling your characters movement maybe it wouldn't be usefull. Perhaps each loop I would check if each direction key had been pressed seperately and if one of them had change the velocity of the character, but then how would I make sure if you held the left button for example the character didn't keep going faster and faster to the left. Also I am trying to make my game quite efficient because I am not making it for the PC so I was wondering if this is maybe a unnecessarily expensive way to go about it. (With all the different keys being checked each loop and all.) This is the sort of code I would use to exit if escape is pressed:
input_manager input_man;
while (running == true)
{
input_man.update();

if (input_man.check(KEY_ESCAPE) == true)
running = false;
}


Here is the input_manager class:
class input_manager
{
public:
input_manager();
void update();
bool check(unsigned char button) {return key[button];};

private:
SDL_Event event;
bool key[18];
};


Thanks

##### Share on other sites
I won't repeat my performance rant here - I've done that in recent other posts, so go ahead and search if you want to see one of those...

Putting hardcoded numbers like bool key[18] is a bad idea. What does 18 mean/stand for? You might know now, you might forget later, someone else reading the code almost certainly won't know. Your code would potentially crash if the check() function were called with 'q' as a parameter. Use a symbolic constant instead, and typedef your key values to something besides unsigned char.

Having the word "manager" in a class name is usually redundant. Most code "manages" something or another.

If you want to avoid the performance penalty of polling, use the events - it's the difference between the kid in the back repeatedly asking "are we there yet" versus taking a nap, and you waking him up when you arrive at the destination.

##### Share on other sites

The 18 stands for the 18 different buttons on the console (gp2x) I am making the game for. I just used defines to set each number to a easier to use name. Good point about the crash though, I will check to make sure the button is in range to stop that now.

I still am unsure about how to do this.. I don't really want to hardcode to the events because its kind of sloppy I suppose but then again I don't want it to be slower than it needs to be.

I also am unsure how I would make the arrow keys move a circle say.. I am using 8 different direction keys. If I checked each key in the loop and changed the circle's velocity it would speed up and up if I held a direction. Also maybe if I pressed two directions at once it would mess up.

I am starting to think that my code just adds a problem rather than helping. What do you think?

Thanks

1. 1
2. 2
frob
16
3. 3
Rutin
12
4. 4
5. 5

• 13
• 12
• 59
• 14
• 15
• ### Forum Statistics

• Total Topics
632124
• Total Posts
3004243

×