Jump to content

  • Log In with Google      Sign In   
  • Create Account

SDL input manager


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Agreon   Members   -  Reputation: 161

Like
0Likes
Like

Posted 15 August 2013 - 10:13 AM

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?

 

 



Sponsor:

#2 MarekKnows.com   Members   -  Reputation: 739

Like
0Likes
Like

Posted 15 August 2013 - 11:08 AM

how did you declare m_keys?

 

what value does SDLK_ESCAPE have?


---
Free C++, OpenGL, and Game Development Video Tutorials @
www.MarekKnows.com
Play my free games: Ghost Toast, Zing, Jewel Thief


#3 BeerNutts   Crossbones+   -  Reputation: 2999

Like
0Likes
Like

Posted 15 August 2013 - 11:19 AM

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)

#4 Agreon   Members   -  Reputation: 161

Like
0Likes
Like

Posted 15 August 2013 - 01:59 PM

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.
 

 



#5 Dave Hunt   Crossbones+   -  Reputation: 2478

Like
0Likes
Like

Posted 15 August 2013 - 02:09 PM

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.



#6 Agreon   Members   -  Reputation: 161

Like
-2Likes
Like

Posted 15 August 2013 - 02:57 PM

I solved my problem thanks



#7 Dave Hunt   Crossbones+   -  Reputation: 2478

Like
1Likes
Like

Posted 15 August 2013 - 05:35 PM

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.



#8 Agreon   Members   -  Reputation: 161

Like
0Likes
Like

Posted 16 August 2013 - 07:09 AM

 

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS