The greatest achievement of the human spirit is to live up to one’s opportunities and make the most of one’s resources. — Vauvenargues
We start our lesson with the ResourceSystem. A singleton designed to handle any external resources to our world. Meshes or Heightfields are commonly used with the ResourceSystem, but other less complicated files such as text files can be used – if you so wish.
ResourceSystem* sparky = ResourceSystem::getSingleton();
The ResourceSystem is typically a very lazy creature, it never went to school and read about different file types, operating system structures or the exact placement of a block header should be. Instead the ResourceSystem has lots of friends called ResourceProtocols which the ResourceSystem has bribed them to handle any resources for him.
Two examples are the FileResourceProtocol and MemoryResourceProtocol which you’ll never meet because they are invisible and really shy. We talk to these ResourceProtocols using an ancient tribal dance called a UniformResourceIdentifier to quickly reveal themselves, open their magic secret bags full of goodies or if your a traditionalist Archives.
We start our dance by writing down the name of the archive we want to call it to a green ribbon, then shouting out aloud the protocol we want, then write an ancient symbol the colon in the air then finally we write down the location of the Archive and bury it in the ground.
sparky->openArchive("myFishesArchive", "file:C:/Program Files/Fishes/");
Some Protocols don’t use Archives at all. When we want something – it will be there regardless of where it came from; MemoryResourceProtocol subscribes to this new age thinking. Of course MemoryResourceProtocol doesn’t even name it’s resources – it’s that new age.
After we’ve gotten hold of our magic bag or archive if you prefer. We can now open it and play with the things inside. We do that by another dance called the ArchiveResourceIdentifier. It’s similar to the UniformResourceIdentifier dance, but now we whisper the name of the archive, trace another colon in the air, and start singing out the name of the resource we want, and finish of with a long burp indicating how much access we want to it.
Resource* mr_trout = sparky->open("myFishesArchive:fish.nxs", Enums::ResourceAccess_ReadOnly);
With our Resource now opened we can now play with it, with the assurance that our new friends will handle the nasty bits with Operating Systems, Memory, or even really knowing what type of file or archive we are using.
int x = mr_trout->readInt();
if (size_t size = mr_trout->getSize() > 140) {
mr_trout->seek(140);
short y = mr_trout->readUShort();
}
unsigned int readOps = mr_trout->getNbReadOperations();
mr_trout->seekBeginning();
Next time on the Singletons, we explore the easier side of Resources; Meshes and Heightfields.