4 Replies - 772 Views - Last Post: 19 January 2012 - 08:24 AM Rate Topic: -----

Topic Sponsor:

#1 civicdude95  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 18-January 12

Global SDL Key Input

Posted 18 January 2012 - 11:28 PM

Hello all,

I've frequented this site now for some time and have found its content to be very helpful, but I've come across a problem that I can't seem to find the answer to (I've Googled and searched this site, unsuccessfully). The problem is that I'm trying to implement a global keyboard input system in SDL/C++ and I'm having trouble with my basic IsKeyPress() method. I've been able to successfully make the InputHandler class into a Singleton, so I know that's not the problem; it seems that perhaps the way I'm trying to check for key input is flawed. Here is my method so far:

bool InputHandler::IsKeyPress(SDLKey key) {
 	if (SDL_PollEvent(&event)) {
		switch (event.type) {
		case SDL_KEYDOWN:
			if (event.key.keysym.sym == key) { // I am aware that this line doesn't work
				return true;               // however, this is the general method I'm
			}                                  // trying to implement.
		}
	}
	
	Logger::Instance()->log(1, "Key Press Failure");
	false;
}


You can see that I want to send in a key from wherever in the game and return true or false based on whether or not that key is currently down. Is this possible, given the method I'm trying to use? Or is there an easier way to handle input on a global scale?

I'm not a seasoned professional with SDL so I may have simply made a n00b mistake. Thanks for taking a look at my question and for any help you can provide.

Edit* I realize that the last line should read 'return false;'

That was merely a typo.

Is This A Good Question/Topic? 0
  • +

Replies To: Global SDL Key Input

#2 stayscrisp  Icon User is offline

  • Lets-a play!
  • member icon

Reputation: 800
  • View blog
  • Posts: 3,691
  • Joined: 14-February 08

Re: Global SDL Key Input

Posted 19 January 2012 - 02:09 AM

Well for one thing you do not want to do the event polling inside this function. You want the polling to be done constantly.

Where are you calling this function?
Was This Post Helpful? 0
  • +
  • -

#3 civicdude95  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 18-January 12

Re: Global SDL Key Input

Posted 19 January 2012 - 07:41 AM

View Poststayscrisp, on 19 January 2012 - 02:09 AM, said:

Well for one thing you do not want to do the event polling inside this function. You want the polling to be done constantly.

Where are you calling this function?


Hey thanks for replying stayscrisp, I'm actually following your framework for the basic game setup, and I'm at this point just calling this function from playstate.cpp like so:

void PlayState::HandleEvents() {
     SDL_Event event;

     if (SDL_PollEvent(&event)) {
          switch(event.type) {
          case SDL_QUIT:
               GameInst::Instance()->Quit();
               break;
     }

     if (Input::Instance()->IsKeyPress(SDLK_SPACE)) {
          GameInst::Instance()->PushState(PauseState::Instance());
          break;
     }
}



I used your Singleton code snippet that you suggest in your tutorial to make the InputHandler class a singleton. Like I said, this is probably the completely wrong way to implement this, but this is how my C#/XNA brain is thinking at the moment.

Maybe I could have an SDL_Event in my game.cpp which would be polled every time game's HandleEvents method was called. Then I could pass in the event to the state's HandleEvents and do the key logic after that? Like this maybe:

In game.cpp
void Game::HandleEvents() {
     SDL_Event event;

     while (SDL_PollEvent(&event)) {
          states.back()->HandleEvents(event);  // The Game parameter is removed since we/you made the game class a singleton
     }
}



As you can see, I'm "coding out loud" here and hoping this makes sense to you.
Was This Post Helpful? 0
  • +
  • -

#4 stayscrisp  Icon User is offline

  • Lets-a play!
  • member icon

Reputation: 800
  • View blog
  • Posts: 3,691
  • Joined: 14-February 08

Re: Global SDL Key Input

Posted 19 January 2012 - 08:18 AM

To be honest, depending on what your doing it might be easier to just let each object handle their own events. Of course this may not be the best approach but it is certainly workable, especially in a small game.

Here is an example:

void PlayState::HandleEvents() 
{
     SDL_Event event;

     while (SDL_PollEvent(&event)) 
     {
          if(event.type == SDL_QUIT)
          {
                 // quit
          }
          
          // you could stick this in a loop and do it for all your objects
          m_pObject->HandleEvents(event);
     }
}



Now in an objects handle events function

void Player::HandleEvents(const SDL_Event& event)
{
	switch(event.type)
	{
	case SDL_KEYDOWN:

		switch(event.key.keysym.sym)
		{
		case SDLK_LEFT:
			// stuff
			break;

		case SDLK_RIGHT:
			// more stuff
			break;

		default:
			break;
		}

		break;

		// other input stuff
	}
}



Yes this could get unruly but for a small game it is more than adequate. I hope that helps some :)
Was This Post Helpful? 0
  • +
  • -

#5 civicdude95  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 18-January 12

Re: Global SDL Key Input

Posted 19 January 2012 - 08:24 AM

You're probably right, especially for a small game (which this one is, just a small shmup). I guess it goes back to the old saying "Make it work, then make it pretty".

I'll continue on with the individualistic handle events and hold off on the global input handler until it is definitely needed.

I will say though, off-topic as it is, that I do have a global Logger class and that has worked out great for dumping logs to a txt file.

Thanks for helping me out with this, stayscrisp.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1