C++ 蛇克隆 : timer function ignores given stop time and stops at it's own fixed time

标签 c++ opengl timer delay 2d-games

我正在尝试使用 C++ 和 OpenGL/GLUT 制作一个 Snake 克隆。然而,我一直在编程允许输入 Action 之间的短时间间隔时遇到问题。我已经尝试了一些计时方法,最后我为它创建了一个类(如下所示)。这似乎是对输入延迟进行编程的最佳方式(而不是 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();
}

定时器.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;
};

定时器.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 毫秒触发一次,您检查 Timer::checkTime,它调用 getElapsed。您的所有时间函数都以 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() 使用的纪元开始的一秒的实际开始时间;

我怀疑如果您对其进行统计测量,平均时间接近 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();
}

定时器.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;
};

定时器.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;
}

关于C++ 蛇克隆 : timer function ignores given stop time and stops at it's own fixed time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24846663/

相关文章:

c++ - 为什么 strlen() 返回 64 位整数?我错过了什么吗?

c++ - 我可以将 openGL 缓冲区设置为按照与 OBJ 文件类似的原理工作吗?

java - 如何使用 javax swing 计时器的整数数组来改变速度

c# - 如何防止 Global.asax 中的计时器加倍?

c++ - 在函数模板中,如何根据另一个参数确定一个参数的类型

c++ - 向自定义 directshow 转换过滤器添加接口(interface)

c++ - MinGW隐藏dll中的方法名称

cocoa - OpenGL 纹理绑定(bind)失败

opengl - NVIDIA Optimus 卡无法在 OpenGL 下切换

sharepoint - 来自 SPWeb 的计时器作业