Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm planning to use the Sleep() function to implement a countdown timer. But this freezes the input of the user at the same time. Thus I wanted to ask if it is possible to have the countdown still running in realtime, whether they're typing or not; or in other words not freeze everything.

Is it possible? If so, how?

Example : Pop Quiz with Timer

share|improve this question
4  
I don't understand the question. –  Ken Nov 24 '12 at 14:43
    
Please elaborate. Is your problem that the Sleep function halts the program, or that receiving input halts the program? –  ktodisco Nov 25 '12 at 23:10

3 Answers 3

If I understand your question correctly, you should be able to do this by having a few variables:

float countdownDuration = 1.0f;
float cooldown = coutdownDuration;

Each frame, you subtract deltaTime, or the time that has passed since the last frame, from cooldown. When cooldown reaches 0 or below, your countdown timer has 'ticked' down, and you can reset cooldown to countdownDuration minus the value below 0 that cooldown has reached. In this particular case, by the way I initialized the variables above, each tick will be one second.

In this way, you do not have to sleep the main thread and will still check for input every frame.

share|improve this answer

You have to code a non-blocking version of cin using kbhit() and getch() found in <conio.h>.

Try this code if you use windows:

#include <stdio.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#include <string>
#include <iostream>
using namespace std;

#define KB_UP 72
#define KB_DOWN 80
#define KB_LEFT 75
#define KB_RIGHT 77
#define KB_ESCAPE 27
#define KB_F8 66
#define KB_ENTER 13

unsigned int x_hours=0;
unsigned int x_minutes=0;
unsigned int x_seconds=0;
unsigned int x_milliseconds=0;
unsigned int totaltime=0,count_down_time_in_secs=0,time_left=0;
clock_t x_startTime,x_countTime;
int KeyBoard_code,input_count=0;
int caret_x=0,caret_y=0;
char keyboard_buffer[100]={' '};
char answer_buffer[100]={' '};
char correct_answer[ ]="foo bar";

int is_from_keyboard(int ch);
void  start_timer();
void  delta_time_update_timer();
void gotoxy(int x, int y);
void clrscr(void);
void setcolor(WORD color); 
void keyboard_input();

int main ()
{

    count_down_time_in_secs= 4;  // 1 minute is 60sec (60x1min), 1 hour is 3600 (60x60min)

    start_timer();
    delta_time_update_timer();

    gotoxy(1 , 2);  
    printf( "\nWhat is the answer to the  bla bla of bla bla?  ");
    gotoxy(1,5);printf( "Answer? >");


    while (time_left>0) 
    {
        keyboard_input();
        delta_time_update_timer();
    }

    gotoxy(1 , 12);
    printf( "\n\n\nTime's out\n\n\n");

return 0;
}



void  start_timer()
{
    x_startTime=clock();  // start clock
}

void  delta_time_update_timer()
{
    x_countTime=clock(); // update timer difference
    x_milliseconds=x_countTime-x_startTime;
    x_seconds=(x_milliseconds/(CLOCKS_PER_SEC))-(x_minutes*60);
    x_minutes=(x_milliseconds/(CLOCKS_PER_SEC))/60;
    x_hours=x_minutes/60;
    time_left=count_down_time_in_secs-x_seconds;   // update timer


        gotoxy(1 , 1);
        printf( "\nYou have %d seconds left  ",time_left,count_down_time_in_secs);
        gotoxy(1,5);

}


void keyboard_input()
{
    if (kbhit())
      {
            KeyBoard_code = getch();

            caret_x++;
            gotoxy(10+caret_x,5+caret_y);
            printf( "%c",KeyBoard_code);
            input_count++;
            keyboard_buffer[input_count]=(char)KeyBoard_code;


            switch (KeyBoard_code)
            {

                case KB_ESCAPE:


                break;

                case KB_ENTER:

                    memcpy(answer_buffer, keyboard_buffer,sizeof(keyboard_buffer));

                    gotoxy(1 ,7);
                    printf( "Your answer is  %s ",answer_buffer);   


                    gotoxy(1,5);printf( "Answer? >              ");
                    gotoxy(1 ,9);
                    printf( "The correct answer is %s ", correct_answer);
                    caret_x=0;
                    input_count=0;
                    start_timer();
                    count_down_time_in_secs= 7;
                    delta_time_update_timer();
                    memset(keyboard_buffer,32,sizeof(keyboard_buffer));
                    memset(answer_buffer,32,sizeof(answer_buffer));

                break;

                case KB_LEFT:

                break;



                case KB_RIGHT:

                  break;

                case KB_UP:

                break;

                case KB_DOWN:

                break;

            }        

      }

}


void setcolor(WORD color)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
    return;
}


void gotoxy(int x, int y)
{
  static HANDLE hStdout = NULL;
  COORD coord;

  coord.X = x;
  coord.Y = y;

  if(!hStdout)
  {
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  }

  SetConsoleCursorPosition(hStdout,coord);
}


void clrscr(void)
{
  static HANDLE hStdout = NULL;      
  static CONSOLE_SCREEN_BUFFER_INFO csbi;
  const COORD startCoords = {0,0};   
  DWORD dummy;

  if(!hStdout)               
  {
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hStdout,&csbi);
  }

  FillConsoleOutputCharacter(hStdout,
                             ' ',
                             csbi.dwSize.X * csbi.dwSize.Y,
                             startCoords,
                             &dummy);    
  gotoxy(0,0);
}



int is_from_keyboard(int ch)
{

    if ( ch>=31 && ch<128) return 1;
    else return -1;

}
share|improve this answer

For starters, use clock instead and find the time difference between each call.

http://www.cplusplus.com/reference/ctime/clock/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.