C Snake clone:计时器功能忽略给定的停止时间并在其自己的固定时间停止

我正在尝试使用C和OpenGL / GLUT制作Snake克隆.但是,我一直无法编程允许在运动之间输入的短时间间隔.我已经尝试了几种计时方法,最后我为它做了一个课程(你将在下面看到).这似乎是编程输入延迟的最佳方式(而不是glutTimerFunc()或sleep()),因为计时器与游戏循环分开运行,而不是将整个程序置于保持状态.这很重要,因为我希望玩家能够随时暂停.不幸的是,我现在也遇到了这种方法的问题.我的计时器类似乎忽略了我给它的时间限制的双倍(简单地表示为双“限制”).

为了测试该类,我设置了一个简单的循环控制台程序,在计时器达到时间限制时显示用户的方向输入.它应该每0.33秒显示一次输入.相反,它以固定的间隔显示输入,看起来相隔约0.8秒,无论时间限制给出了什么值.为什么它不会在给定的时间间隔显示输入,为什么它会使它自己的时间限制?

这也恰好是我没有教程的第一个主要的C / OpenGL项目,因此对我的代码/方法的任何评论或建议都表示赞赏!

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

using namespace std;

Timer timer;

// Insert necessary OpenGL/GLUT code for display/looping here.

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
#include <time.h>

class Timer
{
public:
    Timer();
    bool    checkTime(double limit = 0.33);
private:
    double  getElapsed();
    time_t  start;
    time_t  now;
    double  elapsed;
    bool    running;
};

timer.cpp

#include "timer.h"

Timer::Timer()
{
    running = false;
}

bool Timer::checkTime(double limit)
{
    elapsed = getElapsed();

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

double Timer::getElapsed()
{
    if (! running)
    {
        time(&start);
        running = true;
        return 0.00;
    }
    else
    {
        time(&now);
        return difftime(now, start);
    }
}

最佳答案 glutTimer每触发1000/60毫秒,你就会检查调用getElapsed的Timer :: checkTime.你有你所有的时间函数定义为double,但你使用time_t,其分辨率只有1秒.

因此,你得到的东西看起来像这样(模拟数字)

start time: 1234.5 seconds
glutTimer: 1234.516 seconds  (getElapsed = 0) // checkTime returns false
glutTimer: 1234.532 seconds  (getElapsed = 0)
...
glutTimer: 1234.596 seconds (getElapsed = 0)
glutTimer: 1235.012 seconds (getElapsed = 1)  // condition finally returns true
...

因此,实际延迟取决于您何时设置相对于time()使用的Epoch的实际开始时间的开始时间;

我怀疑如果统计测量它平均接近0.5秒.

回答有关“决议”的问题:

返回当前时间的不同功能返回不同的准确度.例如,现在,我屏幕右下角的时钟显示为“12:14 PM”.这恰好是12:14而没有秒,或12:14和59秒?我无法分辨,因为时钟显示的“分辨率”是一分钟.同样地,我可能会说,如果我以“一刻钟”的分辨率报告时间,那么它是在12小时后的一个季度,当时真的是14分钟.作为人类,我们一直这样做而不考虑它.在软件中,您必须了解所调用的任何功能的这些细节.

如果您使用的是Windows,则可通过QueryPerformanceCounter API获得高分辨率计时器.在大多数平台上,性能计数器是基于硬件的,具有微秒级的分辨率.

以下是调用它的示例:http://msdn.microsoft.com/en-us/library/windows/desktop/dn553408(v=vs.85).aspx#examples_for_acquiring_time_stamps

LARGE_INTEGER StartingTime, EndingTime, ElapsedMicroseconds;
LARGE_INTEGER Frequency;

QueryPerformanceFrequency(&Frequency); // get number of ticks per second
QueryPerformanceCounter(&StartingTime); // get starting # of ticks

// Activity to be timed

QueryPerformanceCounter(&EndingTime);  // get ending # of ticks
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;


//
// We now have the elapsed number of ticks, along with the
// number of ticks-per-second. We use these values
// to convert to the number of elapsed microseconds.
// To guard against loss-of-precision, we convert
// to microseconds *before* dividing by ticks-per-second.
//

ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;

在Linux上可能有类似的工具,但我不熟悉它.

试试这个:

void update(int value)
{
    if (timer.hasTicked())
    {
        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;
    }
    else if (!timer.isRunning())
    {
       timer.start();
    }

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

timer.h

// this class provides a timer that can be polled and will allow the user to tell if a period has elapsed.
// note that this timer does NOT throw any events for timeout.
class PollTimer
{
public:
    PollTimer();

    // assuming time limit is a number of msec that and fits within a normal integer rather than the 64 bit  
    // variant (that would be a LONG LONG delay).
    void    setTimeout(int msDelay);

    // Timers generally expose start/stop and it’s not generally a good idea to make a single function 
    // that overloads complex combinations of behaviors as here both the start & elapsed operations.
    // admit this is a simple case, but generally it’s a bad design pattern that leads to “evil”.
    void    start();
    void    stop();
    bool    isRunning();

    // Has the timer expired since the last poll
    bool    hasTicked();

private:
    LARGE_INTEGER startTime;
    LARGE_INTEGER frequency; // per second
    int     delay; // in milliseconds
    bool    running;
};

timer.cpp

#include "timer.h"

PollTimer::PollTimer()
{
    // omitting error check for hardware that doesn’t support this.
    QueryPerformanceFrequency(& frequency); // get number of ticks per second
    running = false;
}

void PollTimer::setTimeout(int msDelay)
{
     delay = msDelay;
}

void PollTimer::start()
{
    QueryPerformanceCounter(&startTime);
    running = true;
}

void PollTimer::stop()
{
    running = false;
}

bool PollTimer::isRunning()
{
    return running;
}

bool PollTimer::hasTicked()
{
    if (!running)
        return false;

    LARGE_INTEGER now;
    QueryPerformanceCounter(&now);

    LARGE_INTEGER ElapsedMilliseconds;
    ElapsedMilliseconds.QuadPart = now.QuadPart - startTime.QuadPart;

    ElapsedMilliseconds.QuadPart *= 1000000;
    ElapsedMilliseconds.QuadPart /= frequency.QuadPart; // now microseconds
    ElapsedMilliseconds.QuadPart /= 1000; // milliseconds

    bool fExpired = ( ElapsedMilliseconds.HighPart > 0 || ElapsedMilliseconds.LowPart >= delay ) ; 
    if (fExpired)
    {
        // reset start time
        start(); // don’t copy/paste code you can call.
    }
    return fExpired;
}
点赞