Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 每次按键时从零开始计数_C++_Algorithm_Events - Fatal编程技术网

C++ 每次按键时从零开始计数

C++ 每次按键时从零开始计数,c++,algorithm,events,C++,Algorithm,Events,我有一个在屏幕上绘制图像的程序。这里的draw函数是按帧调用的,其中包含了我所有的绘图代码 我编写了一个图像序列器,它从图像索引返回相应的图像 void draw() { sequence.getFrameForTime(getCurrentElapsedTime()).draw(0,0); //get current time returns time in float and startson application start } 按键时,我从第一张图像[0]开始序列,然后继续。因此,每

我有一个在屏幕上绘制图像的程序。这里的draw函数是按帧调用的,其中包含了我所有的绘图代码

我编写了一个图像序列器,它从图像索引返回相应的图像

void draw()
{
sequence.getFrameForTime(getCurrentElapsedTime()).draw(0,0); //get current time returns time in float and startson application start
}
按键时,我从第一张图像[0]开始序列,然后继续。因此,每次我按下一个键,它必须从[0]开始,不像上面的代码,它基本上使用currentTime%numImages来获得帧(这不是图像的起始0位置)

我想写一个自己的计时器,基本上每次按键都可以触发,所以时间总是从0开始。但在做这件事之前,我想问一下,是否有人有更好/更简单的实现方案

编辑
为什么我不用柜台? 我的ImageSequence中也有帧速率调整

Image getFrameAtPercent(float rate)
{
float totalTime = sequence.size() / frameRate;
float percent = time / totalTime;
return setFrameAtPercent(percent);
}

int getFrameIndexAtPercent(float percent){
if (percent < 0.0 || percent > 1.0) percent -= floor(percent);
    return MIN((int)(percent*sequence.size()), sequence.size()-1);
}
Image getFrameAtPercent(浮动速率)
{
float totalTime=sequence.size()/frameRate;
浮动百分比=时间/总时间;
返回setFrameAtPercent(百分比);
}
int getFrameIndexAtPercent(浮动百分比){
如果(百分比<0.0 | |百分比>1.0)百分比-=地板(百分比);
返回最小值((int)(百分比*sequence.size()),sequence.size()-1);
}

是否有理由这样做不够?

您应该做的是将“currentFrame”增加为
float
,并将其转换为
int
以索引您的帧:

void draw()
{
    currentFrame += deltaTime * framesPerSecond; // delta time being the time between the current frame and your last frame
    if(currentFrame >= numImages)
        currentFrame -= numImages;
    sequence.getFrameAt((int)currentFrame).draw(0,0);
}

void OnKeyPress() { currentFrame = 0; }
这将优雅地处理具有不同帧速率的机器,甚至可以在一台机器上更改帧速率


此外,在循环时不会跳过部分帧,因为保留了减法的其余部分。

编辑了问题并添加了有关此问题的详细信息。如果我使用计数器,计数器将根据应用程序的全局帧速率递增。我无法控制递增的帧速率以使sequencer工作。@user1240679在这种情况下,您可以按照emartel的建议以不同的方式递增,或者在类似于
getFrameForTime
的函数中将计数器计数转换为帧,或者按照您所说的实现自己的计数器类来填充这些细节。
void draw()
{
    currentFrame += deltaTime * framesPerSecond; // delta time being the time between the current frame and your last frame
    if(currentFrame >= numImages)
        currentFrame -= numImages;
    sequence.getFrameAt((int)currentFrame).draw(0,0);
}

void OnKeyPress() { currentFrame = 0; }