A way to make keyboard event queue both responsive and not take whole CPU power

前端 未结 4 638
迷失自我
迷失自我 2021-01-06 03:56

I am making an Sdl game, it\'s 2d shooter. I am using SDL to import surfaces and OpenGL to draw them on the screen (doing so because it works way faster than just SDL). I\'v

4条回答
  •  逝去的感伤
    2021-01-06 04:04

    Have you tried using something like usleep(50000) instead of delay(1)?

    This would make your thread sleep for 50 msecs between polling the queue, or equivalently, you would check the queue 20 times per second.

    Also, what platform is this on: Linux, Windows?

    On Windows you may not have usleep(), but you can try select() as follows:

    struct timeval tv;
    tv.tv_sec = 0;
    tv.tv_usec = 50000;
    select(0, NULL, NULL, NULL, &tv);
    

    Another suggestion is to try polling in a tight loop until it stops returning events. Once no events are pending, proceed with sleeping for 50 msecs between polls until it starts returning events again.

提交回复
热议问题