Problem with SDL input using keystates.

Started by
4 comments, last by Saul 15 years, 8 months ago
Hi! I'm new at this forum and i'm quite new to game programming to. I'm currently making a 2d two player shooting game and so far everything has gone great. I have builded the whole game engine by my self and it is based on SDL. But so far i have only been testing with one character on the screen and i have already run in to some input problems. I'm only using the keystates func for input and it sems to me that this method have some limitations. This is the basic input code:

	//Makes the player release the jump button before jumping again
	if(!jumpBegin && !keystates[SDLK_SPACE])
		jumpBegin = true;

	//makes the first jump
	if(keystates[SDLK_SPACE] && (cords.yPos == (groundLevel - spriteClips[currentClip].h)) && (jumpState == 0) && jumpBegin){
		cords.yVel -= (20  -weight);
		jumpState = 1;
		jumpBegin = false;
	}
	//pulls the char down if jump is not pressed down
	if(((!keystates[SDLK_SPACE]) && (jumpState == 1)) && cords.yVel < 0){
		cords.yVel += 2;
	}
	//pulls the char up if jump i pressed
	else if(((keystates[SDLK_SPACE]) && (jumpState == 1)) && (cords.yVel < 0) && timeSpace(4)){
		cords.yVel -= 1;
	}
	//makes the second jump
	else if((jumpState == 1 && keystates[SDLK_SPACE]) && jumpBegin){
		cords.yVel =  ((-17) + weight);
		jumpState = 2;
	}

	//pulls the char down when down is pressed
	if(keystates[SDLK_DOWN] && cords.yPos != (groundLevel - spriteClips[currentClip].h))
		cords.yVel += 3;

	//Makes the char go left of right depending on it's pos
	if(keystates[SDLK_LEFT] && cords.yPos == (groundLevel - spriteClips[currentClip].h)){
		charPointRight = false;
		if(cords.xVel > -walkingSpeed)	
			cords.xVel -= acceleration;
	}
	
	if(keystates[SDLK_RIGHT] && cords.yPos == (groundLevel - spriteClips[currentClip].h)){
		charPointRight = true;
		if(cords.xVel < walkingSpeed)	
			cords.xVel += acceleration;
	}

	if(keystates[SDLK_LEFT] && cords.yPos != (groundLevel - spriteClips[currentClip].h)){
		charPointRight = false;
		if(cords.xVel > -walkingSpeed)	
			cords.xVel -= acceleration;
	}
	
	if(keystates[SDLK_RIGHT] && cords.yPos != (groundLevel - spriteClips[currentClip].h)){
		charPointRight = true;
		if(cords.xVel < walkingSpeed)	
			cords.xVel += acceleration;
	}
	//end of sida movements



As you can see, the code is not clean at all and you all might laugh at this, but this is my first time and i have figured out everything by my own. I hope that you can make some sence out of it :S The problem is that when i'm holding down the shooting button (with by the way is SDLK_c), moving right and try to jump, the character doesn't jump. But when im shooting, moving left and try to jump, it works... What is wrong? I think this is wery confusing! Another problem: When i'm gonna implement the other player, with way is the best way of getting the character class to use other keys? I meen how to a change "keystates[SDLK_RIGHT]" to "keystates[SDLK_d]" in the best and moast effective way? Edit: a simple question, is the keystates func limited to a sertant number of keypresses simultaneously? If so, how meny and what other option to a got to make this two player game work?
Advertisement
try to group every keystates[SDLK_SPACE] check to one if statement, like:
if(keystates[SDLK_SPACE]){    if(...)    {    }    else if(...)    {    }}

Quote:Original post by hlsl
try to group every keystates[SDLK_SPACE] check to one if statement, like:
*** Source Snippet Removed ***


I have grooped everything ass much as i can! There is now only one of eath keystate in the code (like kestate[SDLK_RIGHT] only exist in 1 place).
But the problem is not gone.

The problem is a BIG mystery. I putted the following code right in the beggining of the finc:

if(keystates[SDLK_c] && keystates[SDLK_RIGHT] && keystates[SDLK_SPACE])	 cords.yPos += 50;


I can spam all the three buttons and the yPos is changing a bit randomly. I can press one of the three buttons and spam the other two buttons back and forth and the yPos will change everytime i change key. I guess I'm holding them all dowm for moment while spaming. But if a stop spamming when yPos is changing and starts to hold all fo them dowm, nothing happens. This is so weird i'm starting to get really angry at the S**T!

Anyone with ANY idéa?

PLZZZZ HELP!!!
Sorry for double post...
This is almost certainly not a problem with SDL and is quite likely not even a problem with your code but a fundamental limitation of your hardware.

Keyboards can not reliably determine the state of every key in every possible state because of the way that the work internally. In general the keys are connected in a grid where every key, when pressed, connects a 'horizontal' wire with a 'vertical' wire. This means that for a 120ish key keyboard the microcontroller in it requires only 15 lines (7 * 8 > 120) rather than one for each key.

When a key is pressed the microcontroller detects the short and tells your PC that a key has been pressed. With this arrangement it is possible to correctly identify when two keys are pressed but if a third key is pressed and it share a line with either of the two other keys then it can not be determined uniquely. The chances are that your 'jump', 'fire' and 'left' keys aren't in a problem configuration where as you 'jump', 'fire' and 'right' keys are.

I would think that most keyboards have additional stuff to improve key detection for modifiers (shift, ctrl, alt, etc) so you might have more luck if you changed one of the functions ('fire' perhaps) to one of these.
If what RobotBoy says is true (which most definately is), I would make your JUMP key Shift instead.

This topic is closed to new replies.

Advertisement