The Journey  
Intro
The Game
Download
Forum
Paper
Contact
Help  


The Paper - Page 12 - File Access Part 2

The Workflow of C++ Game-Development on a Series 60 Platform device

Andreas Jakl, Revision 1.0, July 2004


4.3.3 Saving files

The current state of the game will, most of the time, be managed in RAM memory. For this case, using streams and stores is a good approach for easily writing it to a file. This method does not require taking care of file management directly. First, a file store is created, which is equivalent to the file itself. A stream can be put into it; it would also be possible to have more than one of them in one store. This example uses a direct file store , which does not allow modifying the data once it has been written – it can only be replaced by a new file. This behavior is fine for the needs of saving the current state of the game.

Once the store and the stream are ready, data can be written into the stream. Several functions are available to write variables of different data types. It is also possible to save the data of objects to a file store, the process is called externalizing . You have to implement two functions called ExternalizeL(RWriteStream& aStream) for writing to the stream and InternalizeL(RReadStream& aStream) to read the saved data from the stream again and then save it in the member variables of the class. In those, you have to take care of writing/reading all necessary data to/from the stream. Externalizing the object works by using stream << myObj; , Internalizing is equally simple by using stream >> myObj; .

When everything has been written to the stream, the changes have to be committed first to the stream and then to the store, where the stream is set as the root stream.

    void SaveGameProgressL() {
        TFileName fullName;
        AppendFullPath(KFileStore, fullName);

        // Create a direct file store that
        // will contain the game progress
        CFileStore* store = CDirectFileStore::ReplaceLC
            (iFileServerSession, fullName, EFileWrite);
        store->SetTypeL(KDirectFileStoreLayoutUid);

        // Create the stream
        RStoreWriteStream stream;
        TStreamId id = stream.CreateLC(*store);

        // Write game progress
        stream.WriteInt16L(iVar);
        // ...

        // Commit the changes to the stream
        stream.CommitL();
        CleanupStack::PopAndDestroy();  // stream

        // Set the stream in the store and commit it
        store->SetRootL(id);
        store->CommitL();
        CleanupStack::PopAndDestroy();  // store
    }

4.3.4 Loading files

Loading files works like saving files. The store has to be opened and then the root stream has to be found. From it, the variables can be read in exactly the same order as they were saved.

    void LoadGameProgressL() {
        TFileName fullName;
        AppendFullPath(KFileStore, fullName);

        CFileStore* store = CDirectFileStore::OpenLC
            (iFileServerSession, fullName, EFileRead);

        // Open the data stream inside the store
        RStoreReadStream stream;
        stream.OpenLC(*store, store->Root());

        // Read all the data
        iVar = stream.ReadInt16L();
        // ...

        // Remove from cleanup stack (store, stream)
        CleanupStack::PopAndDestroy(2);
    }
 
< File Access Part 1 Contents Localization >
 
       
© 2004 Andreas Jakl. All Rights Reserved.
Privacy Statement | Contact and Imprint
The Journey