SDL input manager

Started by
6 comments, last by Agreon 10 years, 8 months ago

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?

Advertisement

how did you declare m_keys?

what value does SDLK_ESCAPE have?

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?

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

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.

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.

I solved my problem thanks

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.

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 -.-.

This topic is closed to new replies.

Advertisement