【发布时间】:2013-02-27 21:19:15
【问题描述】:
我正在为一个游戏编写一个“回放系统”,我想知道我应该如何存储记录的帧?
至于现在我有这个代码/结构(注意:这个代码被缩短了):
struct Point4D { float x, y, z, w; };
struct Point3D { float x, y, z; };
struct Point2D { float x, y; };
struct QuatRot { Point4D Front,Right,Up; };
struct VehicleInfo
{
Point3D Pos,Velocity,TurnSpeed;
QuatRot Rotation;
};
namespace Recorder
{
struct FrameInfo
//~380 bytes / frame| max 45600 bytes @ 120 fps
//max 3.7 GB raw data for 24 hours of recording, not bad..
{
std::chrono::high_resolution_clock::duration time;
VehicleInfo Vehicle;
int Nitro;
float RPM;
int CurrentGear;
};
std::deque<FrameInfo> frames;
FrameInfo g_Temp;
void ProcessRecord()
{
//record vehicle data here to g_Temp
frames.push_back(g_Temp);
return;
}
//other recording code.......
};
我的想法是,制作一个原始字节数组,将其分配给双端队列容器的大小,用 memcpy 将它们从双端队列复制到数组中,然后将所有数组字节写入文件..
然后,如果我想读取我的记录数据,我只需读取文件的字节并将它们分配给一个新数组,然后使用 memcpy 将数组内容复制到一个双端队列..
这很像..好吧.. C方式? 必须有其他方法来做到这一点,将数据存储在文件中,然后将其读回双端队列(也许使用一些 C++11 功能?)。
我将如何做到这一点?
您推荐哪种方法?
如果这很重要,我正在使用 Windows。
【问题讨论】:
-
std::deque不需要:1)将其内存存储在连续存储中,因此不会有“来自/到双端队列的memcpy”。 2) 将其内存存储在std::deque对象中。所以你的计划是行不通的,期间。你为什么还要使用std::deque? -
这种 memcpy 的事情甚至不可能使用 deque,因为在内部元素不一定存储在一个连续的数组中。
-
@NicolBolas 因为我需要在录制时 push_back 并在回放时获取 front() 和 pop_front()。
-
@GamErix:重播时为什么需要
pop_front?只需推进一个迭代器;取出所有数据后,删除整个缓冲区。 -
这是一个想法,但我将最后的记录存储到历史记录中,所以只是为了以不同的帧速率进行同步,我删除了“太旧”的帧。当它开始播放时,将来不再需要所有其他帧。所以我可以删除它.. ?
标签: c++ file memory-management c++11 deque