Quesa provides support for reading and writing arbitrary hierarchies of Quesa objects to and from files or memory buffers.
To read objects from a buffer or file, first create a TQ3FileObject that represents the input stream. Then, call Q3File_OpenRead to open the file for reading. While Q3File_IsEndOfFile is not true, call Q3File_ReadObject to read each object from the stream. What you do with the TQ3Object returned is up to you; but a typical application is to store this in a display group, so that the entire file contents can be treated elsewhere as a single object.
The C++ sample code below illustrates reading the contents of a memory buffer into a TQ3DisplayGroup.
// Create a place to store the geometry.
TQ3GeometryObject geom = Q3DisplayGroup_New();
// create a File object
TQ3FileObject fileObj = Q3File_New();
// create a Storage object, and associate it with the File
TQ3StorageObject storageObj = Q3MemoryStorage_NewBuffer(buffer, bytes, bytes);
Q3File_SetStorage(fileObj, storageObj);
Q3Object_Dispose(storageObj);
// open the file, read the model
TQ3FileMode fileMode = 0;
Q3File_OpenRead(fileObj, &fileMode);
while (not Q3File_IsEndOfFile(fileObj)) {
TQ3Object tempObj = Q3File_ReadObject(fileObj);
if (tempObj) {
Q3Group_AddObject(geom, tempObj);
Q3Object_Dispose(tempObj);
}
}
The following example shows C code for writing an object (which would typically represent a model or a scene) to an arbitrary storage object. Note that while the submit loop in this example submits just the one object, additional objects could be submitted as well, just as when rendering.
TQ3Status SaveToStorage( TQ3Object object, TQ3ViewObject view, TQ3StorageObject storage )
{
TQ3Status status;
TQ3ViewStatus viewStatus;
TQ3FileObject file;
// check parameters
if (NULL == object || NULL == storage) return kQ3Failure;
// create a file object, and attach it to the given storage
file = Q3File_New();
if (NULL == file) return kQ3Failure;
status = Q3File_SetStorage(file, storage);
if (kQ3Success == status) {
// open the file for writing
status = Q3File_OpenWrite(file, kQ3FileModeNormal);
// start writing, then use a submit loop to write data
if (kQ3Success == status) {
status = Q3View_StartWriting(view, file);
viewStatus = kQ3ViewStatusRetraverse;
while (kQ3Success == status && kQ3ViewStatusRetraverse == viewStatus) {
Q3Object_Submit(object, view);
viewStatus = Q3View_EndWriting(view);
}
}
Q3File_Close(file);
}
Q3Object_Dispose(file);
return status;
}