Sign in to follow this  
aleisterbukowski

SDL Input problem OOP

Recommended Posts

Hello, I'm trying to write a small input class, so once I finish the input class it'll be much like my graphics class where I don't have to go inside and change how it works to load, and draw any type of images, fonts, draw text, format image, etc.. for example to draw an image onto the screen i simply gfx->draw_img(source, x, y); or gfx->draw_img(source, &offset); depending on what type of image and what the use of the image will be for. well here is the code
//testing input
int Input::return_input()
{
	return Event.key.keysym.sym;
}

here is me handling the events after I've initialized input and all that jazz
switch(lvl_Flag)
	{
		case 0:
			if(input->return_input() == SDLK_ESCAPE) input->m_windowClosed = true;
			if(input->return_input() == SDLK_SPACE)  
			{
				init_game();
				gfx->clr_screen();
				lvl_Flag += 1;
			}
			break;
		case 1:
			if(input->return_input() == SDLK_UP) player.offset.y -= player.speed;
			if(input->return_input() == SDLK_DOWN)player.offset.y += player.speed;
			if(input->return_input() == SDLK_ESCAPE) lvl -= 1;
		default:
			break;
	}

lvl_Flag is just a flag to let me know at what screen we're dealing with menu, the game, credits, game over, etc... The problem with this code is if I press the up, or down button the player will continue to move in that direction until I press another key... How come? I'm more interested in knowing why this is occuring then simply getting it fixed. I did some testing with it and I'm guessing that since I can press the right or left key and the player stops is because the return value for my input->return_input() becomes the SDLK_RIGHT or SDLK_LEFT key integer and not that previous state which it is in, and since I don't press another key to make the player change direction that player will continue to move in that direction until otherwise. How do I change this? How do I get it to say hey player only move when the SDLK_LEFT, SDLK_RIGHT, SDLK_UP, SDLK_DOWN key is pressed, but when it's pressed only move once at the given speed that you can move... And I decided to throw it in that class, and I know quite well I could work specifically inside that class, and have global boolean values, or have the player global and have it moved anyways I desire KEYDOWN, KEYUP, or anything there of, and even use Joystick, and mouse to move him inside the function below; however, that doesn't make the code very organize I don't believe, and sloppy on top of that. And I was just wondering if I could have it much like the idea I presented before, without resorting to putting the entire input movement in the function I have given to you below.
//read input from keyboard if something is pressed do something
void Input::read_input()
{
	 if (SDL_PollEvent(&Event))
	{
	   //If a button on the keyboard is pressed!
	   switch(Event.type)
	   {
			case SDL_KEYDOWN:
			///if(Event.key.keysym.sym == SDLK_ESCAPE) m_windowClosed = true;
			 break;
	   }

	   //If the X button on the window is pressed!!!!	
	   if(Event.type == SDL_QUIT) {m_windowClosed = true;}
	 }

}

Thanks for the replies in advance,

Share this post


Link to post
Share on other sites
Quote:
Original post by aleisterbukowski
The problem with this code is if I press the up, or down button the player will continue to move in that direction until I press another key...

How come? I'm more interested in knowing why this is occuring then simply getting it fixed.


Probably because you're using some horrible global or shared 'Event' variable which may or may not be getting reset at any point. Don't do that. Instead, pass a specific event to wherever it needs to be handled and use it there.

Quote:
And I was just wondering if I could have it much like the idea I presented before, without resorting to putting the entire input movement in the function I have given to you below.


You need to read each event on the queue and handle each event in turn, not read a single one, store it, and then later read it again. What are you going to do if there are 2 events to handle on the queue? What if the event that comes in is not a key event, but alters some of the data that key events use? You can't safely do it the way you currently do.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this