Jump to content
  • Advertisement
Sign in to follow this  
knobby67

sdl key press problem

This topic is 2920 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,
for testing some code I'm using SDL key press to simulate some embedded IO (I'm running simulation on my PC). I'm running SDL for graphics and it works fine. However try as I might I can't get SDL to read a key.
I'm running a thread with key press release and quit. I know the event is working as the thread drops out when I close the video, so quit is seen. But it doesn't detect key presses or releases.

I start SDL with

SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO );

and have an IO thread running with


void *IO_Thread_Function(void *)
{
int running = 1;

do
{

SDL_PollEvent(&SDLevent);

switch(SDLevent.type)
{
case SDL_KEYDOWN:
if ( SDLevent.key.type == SDLK_RETURN ) printf("pressed \n");


printf("\n sdl key down event\n");
break;
case SDL_KEYUP:
if ( SDLevent.key.type == SDLK_RETURN ) printf("pressed \n");
break;
case SDL_QUIT:
running = 0;
break;
}



usleep(1000);

}while( running == 1 );


pthread_exit(NULL);
}


I'm unsure if there is something I'm doing wrong, something I've missed, or if there's some problem with my laptop ( it's a dell! and does some funny things with external USB devices)

Thanks in advace :)

Share this post


Link to post
Share on other sites
Advertisement
SDL_PollEvent() calls SDL_PumpEvents(). SDL_PumpEvents() can only be called from the thread that called SDL_SetVideoMode(). SDL_SetVideoMode() can only be called from the main() thread. Due to this, SDL_PollEvent() cannot be called from an auxiliary thread. This is mainly due to implementation details on the Windows platform, but to be cross platform you need to conform.

This isn't quite as bad as it seems. Instead of waiting for events in a separate thread, do that in the main thread and move whatever main() is doing (apart from rendering usually) into the auxiliary thread.

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!