C ++,ECS和保存/加载 [英] C++, ECS and Saving / Loading

查看:126
本文介绍了C ++,ECS和保存/加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用实体组件系统框架的程序。从本质上讲,这意味着我具有一组实体,这些实体具有附加的各种组件。实体实际上只是整数ID号,并且通过将组件映射到实体的指定ID号将组件附加到它们。

I have a program that employs an entity-component-system framework. Essentially this means that I have a collection of entities that have various components attached to them. Entities are actually just integer ID numbers, and components are attached to them by mapping the component to the specified ID number of the entity.

现在,我需要存储实体和关联的组件到以后可以修改的文件中,因此基本上我需要保存和加载功能。但是,作为C ++的新手,我很难弄清楚该怎么做。

Now, I need to store collections of entities and the associated components to a file that can be modified later on, so basically I need a saving and loading functionality. However, being somewhat a newcomer to C++, I have hard time figuring out how to exactly do this.

来自Java和C#,我的第一选择是序列化C ++。对象放入JSON中,然后在加载JSON时反序列化它们。但是,C ++没有任何反射功能。因此,问题是:如何保存和加载C ++对象?我不是说实际的文件操作,而是要处理对象和结构的方式,以便在程序启动之间保留它们。

Coming from Java and C#, my first choice would be to serialize the objects into, say, JSON, and then deserialize them when the JSON is loaded. However, C++ does not have any reflection features. So, the question is: how do I save and load C++ objects? I don't mean the actual file operations, I mean the way the objects and structs should be handled in order to preserve them between program launches.

推荐答案

一种方法是创建使用C ++的持久对象,并存储您的数据。

One way of doing is to create Persistent Objects in C++, and store the your data.

检查以下链接:

类似于永恒的C ++对象持久性库

C++ object persistence library similar to eternity

http://sourceforge.net/projects/litesql/

http:// en .wikipedia.org / wiki / ODB_(C%2B%2B)

http://drdobbs.com/cpp/184408893

http://tools.devshed.com/c/a/Web-Development/C-Programming-Persistence/

C ++不直接支持持久性(有建议在将来向C ++添加持久性和反射)。持久性支持并不像乍看起来那样简单。同一对象的大小和内存布局可能因平台不同而异。不同的字节顺序或字节顺序会使问题更加复杂。为了使对象持久化,我们必须在非易失性存储设备中保留其状态。即:编写一个持久对象以将其状态保留在创建该对象的程序范围之外。

C++ doesn't support persistence directly (there are proposals for adding persistence and reflection to C++ in the future). Persistence support is not as trivial as it may seem at first. The size and memory layout of the same object may vary from one platform to another. Different byte ordering, or endian-ness, complicate matters even further. To make an object persistent, we have to reserve its state in a non-volatile storage device. ie: Write a persistent object to retain its state outside the scope of the program in which it was created.

另一种方法是将对象存储到数组中,然后将数组缓冲区推送到文件。
的优点是磁盘盘片不会浪费时间,而且可以连续执行写入操作。

Other Way, is to store the objects into an array, then push the array buffer to a file. The advantage are that the disk platters don't have waste time ramping up and also the writing can be performed contiguously.

您可以通过使用来提高性能线程。将对象转储到缓冲区中,一旦完成,就会触发一个线程来处理输出。

You can increase the performance by using threads. Dump the objects to a buffer, once done trigger a thread to handle the output.

示例:
以下代码尚未编译,仅用于说明目的。

Example: The following code has not been compiled and is for illustrative purposes only.

#include <fstream>
#include <algorithm>

using std::ofstream;
using std::fill;

#define MAX_DATA_LEN  1024 // Assuming max size of data be 1024

class stream_interface
{
    virtual void    load_from_buffer(const unsigned char *& buf_ptr) = 0;
    virtual size_t  size_on_stream(void) const = 0;
    virtual void    store_to_buffer(unsigned char *& buf_ptr) const = 0;
};

struct Component 
    : public stream_interface,
    data_length(MAX_DATA_LEN)
{
    unsigned int entity;
    std::string  data;
    const unsigned int  data_length;

    void load_from_buffer(const unsigned char *& buf_ptr)
    {
        entity = *((unsigned int *) buf_ptr);
        buf_ptr += sizeof(unsigned int);
        data = std::string((char *) buf_ptr);
        buf_ptr += data_length;
        return;
    }
    size_t size_on_stream(void) const
    {
        return sizeof(unsigned int) + data_length;
    }
    void store_to_buffer(unsigned char *& buf_ptr) const
    {
        *((unsigned int *) buf_ptr) = entity;
        buf_ptr += sizeof(unsigned int);
        std::fill(buf_ptr, 0, data_length);
        strncpy((char *) buf_ptr, data.c_str(), data_length);
        buf_ptr += data_length;
        return;
    }
};


int main(void)
{
    Component c1;
    c1.data = "Some Data";
    c1.entity = 5;
    ofstream    data_file("ComponentList.bin", std::ios::binary);

    // Determine size of buffer
    size_t  buffer_size = c1.size_on_stream();

    // Allocate the buffer
    unsigned char * buffer = new unsigned char [buffer_size];
    unsigned char * buf_ptr = buffer;

    // Write / store the object into the buffer.
    c1.store_to_buffer(buf_ptr);

    // Write the buffer to the file / stream.
    data_file.write((char *) buffer, buffer_size);

    data_file.close();
    delete [] buffer;
    return 0;
}

这篇关于C ++,ECS和保存/加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆