Jump to content

  • Log In with Google      Sign In   
  • Create Account

SDL Key press help- NEED BAD HELP


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
26 replies to this topic

#1 bennettbugs   Members   -  Reputation: 493

Like
0Likes
Like

Posted 03 June 2011 - 06:02 PM

So im making my game, right? and in my void KeyPress() function, it creates a local SDL Event, then changes up,down,left and right to true/false depending on the key state. but in my game, when you press a button, it quickly presses then releases the button. Does anyone know what is happening?!?

Sponsor:

#2 scgames   Members   -  Reputation: 1977

Like
0Likes
Like

Posted 03 June 2011 - 07:40 PM

Does anyone know what is happening?!?

No, no one knows what is happening ;) But, if you could post the relevant code, I'm sure someone will be able to help out :)

#3 bennettbugs   Members   -  Reputation: 493

Like
0Likes
Like

Posted 03 June 2011 - 10:46 PM

Key presses:


while (SDL_PollEvent(&keyevent))
{
if (keyevent.type == SDL_KEYDOWN)
{
switch(keyevent.key.keysym.sym)
{
case SDLK_LEFT:
left = true;
Log.Write("LEFT_PRESSED\n");
break;
case SDLK_RIGHT:
right = true;
Log.Write("RIGHT_PRESSED\n");
break;
case SDLK_UP:
up = true;
Log.Write("UP_PRESSED\n");
break;
case SDLK_DOWN:
down = true;
Log.Write("DOWN_PRESSED\n");
break;
default:
break;
}
}
if (keyevent.type == SDL_KEYUP)
{
switch(keyevent.key.keysym.sym)
{
case SDLK_LEFT:
left = false;
Log.Write("LEFT_RELEASED\n");
break;
case SDLK_RIGHT:
right = false;
Log.Write("RIGHT_RELEASED\n");
break;
case SDLK_UP:
up = false;
Log.Write("UP_RELEASED\n");
break;
case SDLK_DOWN:
down = false;
Log.Write("DOWN_RELEASED\n");
break;
default:
break;
}
}
if(keyevent.type==SDL_QUIT)
{
isRunning=false;
Log.Write("UNKOWN_QUIT_COMMAND\n");
}
if (keyevent.key.keysym.sym==SDLK_ESCAPE)
{
isRunning=false;
Log.Write("ESC_PRESSED\n");
Log.Write("Saving Map...\n");
myMap.Save("stuff/save.txt");
Log.Write("Saved!\n");
}
if (keyevent.key.keysym.sym==SDLK_q)
{
myMap.Load("stuff/save.txt");
}
if (keyevent.key.keysym.sym==SDLK_p)
{
if(Playing && Paused.Done){Game.Pause();}
else if(Playing==false && Paused.Done){Game.Resume();}
Log.Write("P_PRESSED\n");
}
}

#4 Wooh   Members   -  Reputation: 652

Like
0Likes
Like

Posted 03 June 2011 - 11:15 PM

Nothing wrong with the code you posted as far as I can see.

#5 scgames   Members   -  Reputation: 1977

Like
0Likes
Like

Posted 04 June 2011 - 06:19 AM

The code's a little hard to follow because of the formatting (you can use 'code' tags to fix that), but here's a couple of things I noticed:

1. For checking the event type, a switch statement or series of if-else's would be more appropriate (since an event can't be of multiple types).

2. It looks like you're checking for some key press events even when the event type may not be 'key down'. This may end up working anyway, but those checks should really be inside the 'key down' block.

3. Since the event can be any of several types (key press or release, 'quit', mouse, etc.), 'keyevent' is a misleading variable name; I'd just call it 'event'.

I'm not clear on what the problem behavior described in your original post is, but generally these sorts of problems can be solved be using the debugger and/or adding appropriate debug output (like the 'log' statements that you have already).

#6 bennettbugs   Members   -  Reputation: 493

Like
0Likes
Like

Posted 05 June 2011 - 01:39 PM

What happens is when you were to say press up, in the first loop you would have the up key pressed. but in the next loop, it automatically says in the keypress check that up has been released even though it is still being pushed. I have no idea what is happening.

#7 bennettbugs   Members   -  Reputation: 493

Like
0Likes
Like

Posted 05 June 2011 - 01:39 PM

and as in loop i mean each time it is called or used.

#8 scgames   Members   -  Reputation: 1977

Like
0Likes
Like

Posted 05 June 2011 - 01:46 PM

What version of SDL are you using?

Can you post a complete, minimal example that demonstrates the behavior? For example, a single main.cpp file that does nothing more than create a window, run an event loop, and indicate somehow (e.g. console output, or perhaps setting the window title) when a specific key has been pressed and when it's been released.

(When posting the code, be sure to use 'code' tags. Also, I suggest converting all tabs to spaces before posting in order to have a better chance of preserving the formatting.)

#9 bennettbugs   Members   -  Reputation: 493

Like
0Likes
Like

Posted 05 June 2011 - 06:06 PM

SDL 1.2.14

Log.txt :

PROGRAM_START
UP_PRESSED
UP_RELEASED
PROGRAM_END

#10 bennettbugs   Members   -  Reputation: 493

Like
0Likes
Like

Posted 05 June 2011 - 06:07 PM

and that would be what would happen if you hold down the up key.

#11 scgames   Members   -  Reputation: 1977

Like
0Likes
Like

Posted 05 June 2011 - 06:24 PM

Log.txt :

PROGRAM_START
UP_PRESSED
UP_RELEASED
PROGRAM_END

We need to see the code.

#12 bennettbugs   Members   -  Reputation: 493

Like
0Likes
Like

Posted 06 June 2011 - 05:50 PM

What part? i have a lot of code. if its the main loop:
(and PS: if its good for learning c++ in 3 months, tell me)

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


#include "SDL.h"
#include "SDL_opengl.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include "include/ApplySurface.h"
#include "include/IntToChar.h"
#include "include/CharToInt.h"
#include "include/File.h"
#include "include/InitImages.h"
#include "include/Animation.h"
#include "include/Animations.h"
//DEFINE VARIABLES
bool left,right,up,down;//we save in which state the button is
int Timer;
const int ZOMBIES=3;
bool isRunning=true,Playing=true;
#include "include/TextToDraw.h"
Fire burn;
Pointer select;
//INCLUDE THINGS DEPENDANT ON VARIABLES

File Log("stuff/log.txt");
#include "include/ScreenClass.h"
Screen Paused;
#include "include/GameStats.h"
GameStats Game;//NEEDS GAMESTATS
#include "include/Map.h"


Map myMap;
#include "include/Zombie.h"
newZombie Zombies[ZOMBIES];



#include "include/IntToImage.h"
#include "include/GameDraw.h"
#include "include/ScreenDraw.h"
#include "include/Movement.h"
#include "include/GameLogic.h"
#include "include/ScreenLogic.h"
//#include "include/KeyPresses.h"
#include "include/ReadFromSource.h"


char Version[7]="abc/";


int main( int argc, char* args[] )
{

Log.Write("PROGRAM_START\n");

myMap.Init();
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("Survivor Pre-Alpha",NULL);
SDL_SetVideoMode(600,480,32, SDL_OPENGL );
InitImages();
SDL_WM_SetIcon(icon,NULL);




burn.Init(Game.myX,Game.myY);
select.Init(0,0);




for(int n=0;n<ZOMBIES;n++)
{
float x=(float)rand()/(float)RAND_MAX;
float y=(float)rand()/(float)RAND_MAX;
Zombies[n].Init(x*5+10,y*5+10);
}


std::string NewName=ReadFromScreen("name");
int newType=atoi(NewName.c_str());
myMap.Box(15,15,3,3,newType);
SDL_Event keyevent;
while(isRunning)
{
while (SDL_PollEvent(&keyevent))
{
if (keyevent.type == SDL_KEYDOWN)
{
switch(keyevent.key.keysym.sym)
{
case SDLK_LEFT:
left = true;
Log.Write("LEFT_PRESSED\n");
break;
case SDLK_RIGHT:
right = true;
Log.Write("RIGHT_PRESSED\n");
break;
case SDLK_UP:
up = true;
Log.Write("UP_PRESSED\n");
break;
case SDLK_DOWN:
down = true;
Log.Write("DOWN_PRESSED\n");
break;
default:
break;
}
}
if (keyevent.type == SDL_KEYUP)
{
switch(keyevent.key.keysym.sym)
{
case SDLK_LEFT:
left = false;
Log.Write("LEFT_RELEASED\n");
break;
case SDLK_RIGHT:
right = false;
Log.Write("RIGHT_RELEASED\n");
break;
case SDLK_UP:
up = false;
Log.Write("UP_RELEASED\n");
break;
case SDLK_DOWN:
down = false;
Log.Write("DOWN_RELEASED\n");
break;
default:
break;
}
}
if(keyevent.type==SDL_QUIT)
{
isRunning=false;
Log.Write("UNKOWN_QUIT_COMMAND\n");
}
if (keyevent.key.keysym.sym==SDLK_ESCAPE)
{
isRunning=false;
Log.Write("ESC_PRESSED\n");
Log.Write("Saving Map...\n");
myMap.Save("stuff/save.txt");
Log.Write("Saved!\n");
}
if (keyevent.key.keysym.sym==SDLK_q)
{
myMap.Load("stuff/save.txt");
}
if (keyevent.key.keysym.sym==SDLK_p)
{
if(Playing && Paused.Done){Game.Pause();}
else if(Playing==false && Paused.Done){Game.Resume();}
Log.Write("P_PRESSED\n");
}
}


if(Playing){GameLogic();GameDraw();lastscreen=screen;}//IN GAME LOGIC AND DRAWING
if(Playing==false){ScreenLogic();ScreenDraw();}//SCREEN LOGIC AND DRAWING

SDL_Delay(10);
}
Log.Write("PROGRAM_END");
SDL_Quit();
return 0;
}

#13 scgames   Members   -  Reputation: 1977

Like
0Likes
Like

Posted 06 June 2011 - 06:04 PM

What part? i have a lot of code.

It'd probably be easier if you could post a complete, minimal example that demonstrates the problem (as described earlier). Creating such an example can often be an effective way of finding the cause of problems such as this one.

You can also just try posting some relevant code from your project (as you did above), but be sure to use 'code' tags so that we can read it easily :)

#14 Roots   Members   -  Reputation: 725

Like
0Likes
Like

Posted 06 June 2011 - 06:18 PM

Are you enabling joysticks anywhere? We've recently encountered an issue with our input system (which is about 4-5 years old and completely stable) where some players are unable to hold down a direction key to perform movement because the input engine is registering key releases unexpectedly. We haven't fixed the problem, but we think we found where the issue lies. On one user's system, disabling joystick input fixed the issue. Turns out, even though they do not have a joystick plugged into their system. SDL still registered some kind of virtual joystick and that was somehow causing joystick release events to occur. Its really odd and we've only had reports on it by two people so far. You can read more details in our thread:

http://www.allacrost.org/forum/viewtopic.php?f=3&t=5376
Hero of Allacrost --- http://www.allacrost.org
A free, open-source 2D RPG in development.

Latest release Oct. 10th, 2010.

#15 bennettbugs   Members   -  Reputation: 493

Like
0Likes
Like

Posted 07 June 2011 - 04:20 PM

Im not enabling joysticks. What you said sounds like what is happening. When you press a key, it will in the next call assume you have let go and release it.

#16 rip-off   Moderators   -  Reputation: 8721

Like
1Likes
Like

Posted 07 June 2011 - 04:34 PM

Here is a minimal program:
#include <iostream>
#include <cstdlib>

#include "SDL.h"

int main(int argc, char **argv)
{
	if(SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		std::cerr << SDL_GetError() << '\n';
		return 1;
	}
	std::atexit(&SDL_Quit);

	SDL_Surface *screen = SDL_SetVideoMode(800, 600, 0, 0);
	if(!screen)
	{
		std::cerr << SDL_GetError() << '\n';
		return 2;
	}

	int vertical = 0;
	int horizontal = 0;

	bool running = true;
	while(running)
	{
		
		SDL_Event event;
		while(SDL_PollEvent(&event))
		{
			if(event.type == SDL_KEYDOWN)
			{
				switch(event.key.keysym.sym)
				{
				case SDLK_UP: vertical -= 1; break;
				case SDLK_DOWN: vertical += 1; break;
				case SDLK_LEFT: horizontal -= 1; break;
				case SDLK_RIGHT: horizontal += 1; break;
				}
			}
			else if(event.type == SDL_KEYUP)
			{
				switch(event.key.keysym.sym)
				{
				case SDLK_UP: vertical -= -1; break;
				case SDLK_DOWN: vertical += -1; break;
				case SDLK_LEFT: horizontal -= -1; break;
				case SDLK_RIGHT: horizontal += -1; break;
				}
			}
			else if(event.type == SDL_QUIT)
			{
				running = false;
			}
		}

		// Clear screen
		SDL_FillRect(screen, 0, 0);

		// Draw center
		SDL_Rect rect;
		rect.w = 10;
		rect.h = 10;
		rect.x = screen->w / 2;
		rect.y = screen->h / 2;
		SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0x00, 0xff, 0x00));

		// Draw offset
		rect.x += rect.w * horizontal;
		rect.y += rect.h * vertical;
		SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0xff, 0x00, 0x00));
		SDL_Flip(screen);
	}
}
Does the behaviour occur in this?

#17 bennettbugs   Members   -  Reputation: 493

Like
0Likes
Like

Posted 08 June 2011 - 04:46 PM

When i ran it, i could only go 1 block in any direction. is that supposed to happen? then when i let go it went back to the center.

#18 rip-off   Moderators   -  Reputation: 8721

Like
0Likes
Like

Posted 09 June 2011 - 02:37 AM

That was what I wrote it to do. It is designed to see if the SDK_KEYUP is being spuriously sent. It seems you're getting the expected behaviour, which indicates that there is something else in your program that is causing this issue.

I think you should take a copy of your program, and start commenting stuff out until the problem disappears. Do it bit by bit, comment out the Zombies and run it, comment out the Fire and see what happens, and so on. The point at which the problem disappears should then be uncommented, and you should dive into its internals and start commenting out smaller chunks. This process can be repeated until you isolate a line or number of lines which cause the issue.

Alternatively, you could take my working program and add your code to it piece by piece until it breaks.

#19 bennettbugs   Members   -  Reputation: 493

Like
0Likes
Like

Posted 10 June 2011 - 05:21 PM

Add your code until it breaks? But i will try to comment out stuff.

#20 bennettbugs   Members   -  Reputation: 493

Like
0Likes
Like

Posted 10 June 2011 - 05:40 PM

Ok, so adding your code still does not fix it. I do know that it is something with the key presses. :(




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