Jump to content
  • Advertisement
Sign in to follow this  
rocktribute

OpenGL Untitled

This topic is 3406 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, im creating a game in OpenGl linux... and right now there is problem with my input function, im moving a sphere, here it is void Input( float elapsed ) { static SDL_Event event; SDL_PollEvent( &event ); if( event.type == SDL_KEYUP ) { if( event.key.keysym.sym == SDLK_ESCAPE ) g_ExitProgram = GL_TRUE; } if( event.type == SDL_KEYDOWN ) { if( event.key.keysym.sym == SDLK_RIGHT ) spherePos.x += 20.0f * elapsed; if( event.key.keysym.sym == SDLK_LEFT ) spherePos.x -= 20.0f * elapsed; if( event.key.keysym.sym == SDLK_UP ) spherePos.z -= 20.0f * elapsed; if( event.key.keysym.sym == SDLK_DOWN ) spherePos.z += 20.0f * elapsed; } memset( &event, 0, sizeof(SDL_Event) ); } +++++++++++++++ I want to be able to move in a diagonal way, i mean be able to press the key UP and DOWN at the same time without a problem, thank you all

Share this post


Link to post
Share on other sites
Advertisement
Well, pressing UP and DOWN wouldn't make the sphere to be still? I guess you meant UP / DOWN and LEFT / RIGHT to move in diagonal, didn't you?

You don't state the problem you currently have (i.e., what happens when you press said buttons) but I guess, from the way you handle SDL's input, that you must press and release the key and press again and so on in order to move the sphere at all.

If that's the problem, then you didn't read SDL's documentation :-). In the documentation says that a SDL_KEYDOWN event is only sent when the key is pressed, but not while the key remains pressed. That is, you one receive a single SDL_KEYDOWN when you press the key, and then SDL_PollEvent() doens't say a thing about that key until a next SDL_KEYUP. In your Input function, any of the if (event.type.key.keysym.sym == SDLK_XXX) will only be true once, when at key press, so the sphere will move just once in that direction.

I can think two ways of solving this situation. One is to use SDL_EnableKeyRepeat() in your main to tell SDL to send SDL_KEYDOWN every internal (in ms) as long as the key is pressed.

The other method is to separate input and sphere movement. For instance, we could have four separate booleans that tell whether a key is being pressed and then use those four booleans to move the sphere, such as:


struct KeyState
{
bool up;
bool down;
bool left;
bool right;
};

int main()
{
// Other stuff
SDL_Event event;
SDL_PollEvent (&event);

if (event.type == SDL_KEYUP )
{
switch (event.key.keysym.sym)
{
case SDLK_UP:
input.up = false;
break;

case SDLK_DOWN:
input.down = false;
break;

case SDLK_LEFT:
input.left = false;
break;

case SDLK_RIGHT:
input.right = false;
break;
}
}
elseif (event.type == SDL_KEYDOWN )
{
switch (event.key.keysym.sym)
{
case SDLK_UP:
input.up = true;
break;

case SDLK_DOWN:
input.down = true;
break;

case SDLK_LEFT:
input.left = true;
break;

case SDLK_RIGHT:
input.right = true;
break;
}
}

HandleInput (elapsed, input);

// other stuff.
}



Of course, this is just an example. I don't know how to best integrate such a method in your system.

I hope this is what you were looking for. If not, please post additional details about the problem you are facing: what is the application doing right know, what should it do, etc.

Share this post


Link to post
Share on other sites
You need to use the return value of SDL_PollEvent() to determine if an event was actually available. If you want to wait for an event to happen, there is an aptly named SDL_WaitEvent() function.

The canonical SDL event polling loop looks like this:

void Input( float elapsed )
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
// ...
}
}


There is no need to make the SDL_Event here static. Nor is there reason to call memset() on it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!