我正在创建一个程序,它从 XML 文件中读取有关烟花的数据(例如类型、颜色、启动时间(以毫秒为单位)以及持续多长时间等)。然后在循环中使用 openGL 显示烟花汇演。
我的方法是有一个管理器类,它接收 xml 读取烟花的向量,并在 3 个不同的向量之间移动它们:inactiveFireworks(那些尚未发射的)、activeFireworks(那些需要动画的)和 depletedFireworks (显示运行时将被推回非活动烟花的那些)。
要检查是否需要点燃烟花,我需要计算调用烟花管理器的时间与程序的当前时间之间的差异,并将其乘以 1000 以获得毫秒。
例如:如果烟花是由于在 3000 时着火,它会在 3 秒内被调用到消防经理的存在。
不幸的是,此时我收到一个未处理的异常错误,我不知道为什么......
timeDifference = difftime(time(¤tTime),initTime) * 1000;
这是头文件:
#ifndef FWORKMANAGER_H
#define FWORKMANAGER_H
#include <Time.h>
#include <vector>
#include "firework.h"
class fworkManager
{
private:
time_t initTime;
time_t currentTime;
double timeDifference;
std::vector<firework*> inactiveFireworks;
std::vector<firework*> activeFireworks;
std::vector<firework*> depletedFireworks;
public:
fworkManager(std::vector<firework*> fListIn);
void drawAllFireworks();
void evolve();
void fireInactiveFireworks();
void moveActiveFireworks();
void moveDepletedFireworks();
void reset();
};
#endif
这是CPP。
#include <vector>
#include "LUtil.h"
#include "fworkManager.h"
fworkManager :: fworkManager(std::vector<firework*> fListIn){
inactiveFireworks = fListIn;
time (&initTime);
}
//animates fireworks
void fworkManager::evolve(){
//check time against inactiveFireworks
fireInactiveFireworks();
moveActiveFireworks();
moveDepletedFireworks();
reset();
}
//draws fireworks as they come up
void fworkManager::drawAllFireworks()
{
std::vector<firework*>::iterator i;
for(i=activeFireworks.begin(); i != activeFireworks.end(); i ++)
{
(*i) -> draw();
}
}
//if fireworks are ready to fire, push them to active list
void fworkManager::fireInactiveFireworks()
{
timeDifference = difftime(time(¤tTime),initTime) * 1000;
std::vector<firework*>::iterator i;
for(i = inactiveFireworks.begin(); i != inactiveFireworks.end();)
{
if((*i) -> getBegin() <= timeDifference)
{
activeFireworks.push_back(*i);
(*i) -> explode();
i = inactiveFireworks.erase(i);
}else{
++i;
}
}
}
//animate each firework in the active list
void fworkManager::moveActiveFireworks()
{
std::vector<firework*>::iterator i;
for(i = activeFireworks.begin(); i != activeFireworks.end(); i++)
{
(*i) -> evolve();
}
}
//move fireworks that have met their duration requirement to the depleted list.
void fworkManager::moveDepletedFireworks()
{
std::vector<firework*>::iterator i;
for(i = activeFireworks.begin(); i != activeFireworks.end();)
{
if((*i) -> getLifeSpan() >= (*i) -> getDuration() )
{
depletedFireworks.push_back(*i);
i = activeFireworks.erase(i);
}else{
++i;
}
}
}
//repopulates the inactive firework list and resets the time difference. Allows animation to loop.
void fworkManager::reset()
{
if(inactiveFireworks.empty() && activeFireworks.empty())
{
time (&initTime);
std::vector<firework*>::iterator i;
for(i=depletedFireworks.begin(); i != depletedFireworks.end();)
{
(*i) -> reset();
inactiveFireworks.push_back(*i);
i = depletedFireworks.erase(i);
}
}
}
非常感谢您提供的任何见解。