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 trying to make a Snake clone with C++ and OpenGL/GLUT, but I've been having trouble with programming the timed input. I made a timer class, as you'll see below, to be used for the short time intervals allowed for input and movement. It's function checkTime() is called to start the timer. Then on following calls, it checks if the time limit (represented by the int variable limit) has been reached.

To test it, I set up a simple, looping console program, that displays keyboard input. When working properly, it displays what key is being held precisely at the point when the timer finds the time limit has been reached (it returns "true"). Given a limit of 1 second, it will only display current input every second. If it is given a value below 1 however, like 0.333, it will act as if the time limit has instantly been reached, and will display input instantly whenever a key is pressed. How can I get it to only display the input every 0.333 seconds?

Also - I realize my method of doing things here may not be the best, given the tools I'm using. However, I'm curious why this won't work properly.

#include <iostream>
#include "timer.h"
// Include all files necessary for OpenGL/GLUT here...

using namespace std;

Timer timer;

void update(int value)
{
    if (timer.checkTime())
    {
        if (GetAsyncKeyState(VK_LEFT))
            cout << "You pressed LEFT!" << endl;
        else if (GetAsyncKeyState(VK_RIGHT))
            cout << "You pressed RIGHT!" << endl;
        else if (GetAsyncKeyState(VK_UP))
            cout << "You pressed UP!" << endl;
        else if (GetAsyncKeyState(VK_DOWN))
            cout << "You pressed DOWN!" << endl;
    }

    glutTimerFunc(1000/60, update, 0);
    glutPostRedisplay();
}

timer.h

#pragma once

class Timer
{
public:
    Timer();
    bool            checkTime(int limit = 0.333);
private:
    unsigned long   getElapsed();
    bool            running;
    unsigned long   elapsed;
    unsigned long   start;
};

timer.cpp

#include "timer.h"
#include <ctime>

Timer::Timer()
{
    running = false;
    elapsed = 0.0;
    start = 0.0;
}

bool Timer::checkTime(int limit)
{
    (unsigned long) limit;

    elapsed = getElapsed();

    if (elapsed < limit)
    {
        return false;
    }
    else if (elapsed >= limit)
    {
        running = false;
        return true;
    }
}

unsigned long Timer::getElapsed()
{
    if (! running)
    {
        start = (unsigned long) clock();
        running = true;
        return 0.0;
    }
    else
    {
        return ((unsigned long) clock() - start) / CLOCKS_PER_SEC;
    }
}
share|improve this question

closed as off-topic by Byte56 Jul 14 at 14:05

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Byte56
If this question can be reworded to fit the rules in the help center, please edit the question.

2  
int limit = 0.333. ints are integers. You probably want float. –  Jason Jul 14 at 2:40
1  
A great reason to work with warnings turned up high, and to actually read the compiler output, both of which should have given a warning about truncating a float or some such. –  Patrick Hughes Jul 14 at 3:03
    
Thanks for your input! I had meant to make it a float, and I will definitely be more careful to check for compiler warnings. –  Warran Penner Jul 14 at 5:39

1 Answer 1

up vote 1 down vote accepted
int limit = 0.333;

is the same as

int limit = 0;

because int has no decimal places. So you ether work in fixed point where the number is divided by a fixed amount, or floating point.

so for example if doing fixed point hundredths, you would use,

const int FIXED = 100;

unsigned long Timer::getElapsed()
{
    if (! running)
    {
        start = (unsigned long) clock();
        running = true;
        return 0;
     }
     else
     {
         return ((unsigned long) clock() - start) / (CLOCKS_PER_SEC/FIXED);
     }
}

and fixed could be

int limit = (unsigned long)(0.333 * FIXED);

I wouldn't define it like the above, but leaving it in form you would reconise.

share|improve this answer
    
Embarassing! I can't believe I didn't notice it was an integer. Thanks for a prompt, informative answer! Didn't know it would literally change to 0. –  Warran Penner Jul 14 at 5:37

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