Código
#include <stdio.h> #include <time.h> enum { TIMEOUT_RUNNING, TIMEOUT_DONE, TIMEOUT_ERROR = -1 }; int timeout(unsigned segundos) { static time_t t; static unsigned s; if(segundos) { s = segundos; } if(!s) return TIMEOUT_ERROR; s = 0; return TIMEOUT_DONE; } return TIMEOUT_RUNNING; } int main() { int i = 0; if(timeout(1) == TIMEOUT_ERROR) return 1; while(i < 5) { if(timeout(0) == TIMEOUT_DONE) { timeout(1); ++i; } } }
Funciona pasándole, en una primera llamada un valor en segundos que quieres que la rutina espere hasta que indique que ha pasado el tiempo. Sólo comprueba, así que no clava el programa para esperar a que pase el tiempo, tu juego puede seguir ejecutándose.
Una vez que termine el conteo se debe iniciar pasándole de nuevo un valor, sino devolverá un error de que no hay tiempo que temporizar, el TIMEOUT_ERROR.