Sign in to follow this  
Agreon

SDL input manager

Recommended Posts

Agreon    167

Hy,

I worked on an Input manager but it doesnt work.

This is, what it looks like:
 

#include "cInput.h"


cInput::cInput(void)
{
	m_MouseX = 0;
	m_MouseY = 0;

	for(int i = 0; i < 323; i++)
	{
		m_keys[i] = false;
	}
}

void cInput::handleInput(SDL_Event evt)
{
	while(SDL_PollEvent(&evt))
	{
		switch(evt.type)
		{
		case SDL_QUIT:
			running = false;
			break;
		case SDL_KEYDOWN:
			m_keys[evt.key.keysym.sym] = true;
                        //if i  add if(evt.key.keysym.sym == SDLK_ESACPE) running = false; it works.  
			break;
		case SDL_KEYUP:
			m_keys[evt.key.keysym.sym] = false;
			break;
		case SDL_MOUSEBUTTONDOWN:
			if(evt.button.button == SDL_BUTTON_LEFT)
			{
				m_leftMouse = true;
			}
			else
			{
				m_rightMouse = true;
			}
			m_MouseX = evt.button.x;
			m_MouseY = evt.button.y;
			break;
		case SDL_MOUSEBUTTONUP:
			if(evt.button.button == SDL_BUTTON_LEFT)
			{
				m_leftMouse = false;
			}
			else
			{
				m_rightMouse = false;
			}
			m_MouseX = evt.button.x;
			m_MouseY = evt.button.y;
			break;
		case SDL_MOUSEMOTION:
			m_MouseX = evt.button.x;
			m_MouseY = evt.button.y;
			break;
		}
	}
}


bool cInput::keyPressed(SDLKey key)
{
	return m_keys[key];
}

bool cInput::leftMousePressed()
{
	return m_leftMouse;
}


bool cInput::rightMousePressed()
{
	return m_rightMouse;
}


int cInput::getMousePositionX()
{
	return m_MouseX;
}


int cInput::getMousePositionY()
{
	return m_MouseY;
}

But now, when i try to use it, it doesnt work. The Function "keyPressed" returns false every time.

 

if(Input->keyPressed(SDLK_ESCAPE))
{
	running = false;
}

Does anybody have an Idea what im doing wrong?

 

 

Share this post


Link to post
Share on other sites
BeerNutts    4400

How are you calling cInput::handleInput() ?  It would seem you've already read the event before that call, but you then also call PollEvent.  So, you may be missing the event.

 

Otherwise, why pass in the Event anyway?

Share this post


Link to post
Share on other sites
Agreon    167

how did you declare m_keys?

 

what value does SDLK_ESCAPE have?

 

bool m_keys[323];

 

SDLK_ESCAPE has the value 27. But thats not the problem. Every other Key doesnet work.

 

How are you calling cInput::handleInput() ?  It would seem you've already read the event before that call, but you then also call PollEvent.  So, you may be missing the event.

 

Otherwise, why pass in the Event anyway?

 

With PollEvent i get all the events, so that i can handle them. I call it everytime in the loop, before update, where i check the m_keys.
 

 

Share this post


Link to post
Share on other sites
Dave Hunt    4872

You can potentially get more than one key event in a single frame, so it's possible that you are getting both the SDL_KEYDOWN and SDL_KEYUP events in the same frame. In that case you would first set the key to true, then on another iteration of the loop, set it back to false. That would explain why it works if you put your escape check inside the SDL_KEYDOWN case.

 

The easiest solution would be to reset all keys to false before the even polling loop and remove the SDL_KEYUP case from your switch.

Share this post


Link to post
Share on other sites
Dave Hunt    4872

I solved my problem thanks

Perhaps you could share your solution so others can benefit from this thread. Otherwise, it may as well not have been started.

Share this post


Link to post
Share on other sites
Agreon    167

 

I solved my problem thanks

Perhaps you could share your solution so others can benefit from this thread. Otherwise, it may as well not have been started.

 

It was my fault. I dont know why, but i forgot to call the event function -.-.

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