Advertisement Jump to content
Sign in to follow this  

Silly input question

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

If you intended to correct an error in the post then please contact us.

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)

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

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


Share this post

Link to post
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...

A few comments, though:

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 this post

Link to post
Share on other sites
Thanks for the reply.

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?


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!