Sign in to follow this  
KHearts

SDL Key Problem

Recommended Posts

I've been working on this all day and I've reached my limit before throwing my unit out of the window.

I require a boolean function to only return true if the next step is valid. It runs nicely so far, however, when other keys are pressed or the mouse is moved, it seems to destroy my intentions and probably returns false.

Is there a way I can "ignore" the other events?



bool Level::isMovable(SDL_Event* Event, SDL_Surface* Screen, Player *player)
{
//if(Event->type == SDL_KEYUP)
//{
if(Event->type == SDL_KEYUP && Event->key.keysym.sym == SDLK_LEFT)
{
xVel = 0;
}
if(Event->type == SDL_KEYUP && Event->key.keysym.sym == SDLK_UP)
{
yVel = 0;
}
if(Event->type == SDL_KEYUP && Event->key.keysym.sym == SDLK_DOWN)
{
yVel = 0;
}
if(Event->type == SDL_KEYUP && Event->key.keysym.sym == SDLK_RIGHT)
{
xVel = 0;
}
//}
if(Event->type == SDL_KEYDOWN)
{
if( Event->key.keysym.sym == SDLK_LEFT && yVel==0)
{
xVel = 9;
if(xVel + LevelBG.BG.rect.x > 0) return false;
else return true;
}
if(Event->key.keysym.sym == SDLK_UP && xVel==0)
{
yVel = 9;
if(yVel + LevelBG.BG.rect.y > 0) return false;
else return true;
}
if(Event->key.keysym.sym == SDLK_DOWN && xVel==0)
{
yVel = -9;
if((yVel + LevelBG.BG.rect.y+LevelBG.BG.rect.h) < Screen->h) return false;
else return true;
}
if(Event->key.keysym.sym == SDLK_RIGHT && yVel==0)
{
xVel = -9;
if((xVel + LevelBG.BG.rect.x+LevelBG.BG.rect.w) < Screen->w) return false;
else return true;
}
return false;
}
return false;




}



Share this post


Link to post
Share on other sites
I don't understand what "next step is valid" means. What's the next step?

If out-of-spec keys or events (such as mouse movement) is fed into this method, what should come out? True or false?

You might test to confirm that the event is within spec before actually calling this method.

Share this post


Link to post
Share on other sites
Quote:
Original post by JoeCooper
I don't understand what "next step is valid" means. What's the next step?

If out-of-spec keys or events (such as mouse movement) is fed into this method, what should come out? True or false?

You might test to confirm that the event is within spec before actually calling this method.


1. It's for a 2D RPG, I'm checking to see if the next move I can make will not be intrusive to any hard objects or the screen

2. Neither. I don't want any OOS keys or events to even be considered.

Share this post


Link to post
Share on other sites
have you tried this approach?


//this happens every frame

velocity = (0,0);

if(left key-down event)
leftPressed = true;
if(left key-up event)
leftPressed = false;
//same for the other key events

if(leftPressed)
velocity += (-9,0);
if(rightPressed)
velocity += (9,0);
//same for the others


Share this post


Link to post
Share on other sites
After the code Xytor pitched, you would do the intrusion checking as part of the physics step. Actually move the entity, do a check, then if collision is directed, reverse it appropriately.

Share this post


Link to post
Share on other sites

while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
GameDone = true;
break;

case SDL_KEYDOWN:
KeysHeld[event.key.keysym.sym] = true;
if(event.key.keysym.sym == SDLK_ESCAPE) {
GameDone = true;
}
break;

case SDL_KEYUP:
KeysHeld[event.key.keysym.sym] = false;
break;

default:
break;
}
}


Where KeysHeld is defined as bool KeysHeld[SDLK_LAST];

That should allow you to hold down multiple keys at the same time.

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