Menu

File_formats

James Legg

File formats used for the racer game are generally a bunch of numbers thrown together in text file. They are read sequentially from start to finish, making the objects listed in the file along the way.

There are two different file formats this applies to, one for themes and one for tracks.

A list of objects is simply the number of objects, a space, then the objects themselves. We store lists of objects that will be loaded and saved as a std::vector of boost::shared_ptr<Type>s. There are functions for this sort of thing in libtrack/stream_loader.h, including one that passes an additional argument to each object's constructor as it is loaded.

Strings should be read and written as lines. There are functions to read and write lists of strings in libtrack/stream_loader.h too, which will escape any newlines with \n and escape \ with \\.

To load and save, we just pass around references to streams. Each function then writes / reads what it needs and returns.

Example

MyClass.h:

#include <istream>
#include <ostream>
#include <vector>

#include <boost/shared_ptr.hpp>

#include "SingleObject.h"

class MyClass
{
public:
    // load a MyClass from a stream.
    MyClass(MyClass);
    // other functions
    // ...
protected:
    // Some data to load and save
    SingleObject single_object;
    std::vector<SingleObject> > multiple_objects;
}

// write it to a stream
ostream & operator

Then MyClass.cpp would look something like this:

#include "MyClass.h"
#include <libtrack/stream_loader.h>

MyClass::MyClass(std::istream & source)
    :   single_object(source)  // Load the singular objects
{
    // Load all the vector objects.
    // This is a templated function, give it a vector of boost::shared_ptr's of anything.
    fill_vector_from_stream(multiple_objects, source)
}

std::ostream & operator

[Category:Development]


Related

Wiki: Main_Page

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.