wavelength 1.0.0
Loading...
Searching...
No Matches
rndmanager.h
Go to the documentation of this file.
1#pragma once
2
3#include <list>
4#include <map>
5#include <memory>
6#include <vector>
7
8#include "os/binstream.h"
9#include "os/file.h"
10#include "os/filepath.h"
11#include "os/strless.h"
12#include "rnd/rndloader.h"
13#include "rnd/rndobject.h"
14
23public:
30 typedef RndObject *(*ClassFactory)(const char *pszName);
31
38
46 public:
57 ResourceLoader(std::unique_ptr<File> pFile, const char *pszTag);
58
66
76 bool Poll();
77
89 int GetData(void **ppData, int *pnSize);
90
92 void AddRef() {
93 ++mRefs;
94 }
95
96 private:
97 std::unique_ptr<File> mFile;
98 const char *mTag;
99 char *mCompressed;
100 char *mBuffer;
101 int mRefs;
102 int mSize;
103 };
104
112
120
129 void Init();
130
141 void PollLoaders(float fBudgetMs);
142
152 void RemoveLoader(RndLoader *pLoader);
153
162 RndObject *Find(const char *pszName);
163
177 std::unique_ptr<RndLoader>
178 AddLoader(const char *pszFile, int nFlags, RndLoader::Callback *pCallback, BinStream *pStream);
179
189 void LoadFile(const char *pszFile);
190
201 void SaveFile(const char *pszFile);
202
214 RndObject *Create(const char *pszClass, const char *pszName);
215
232 void Clone(RndObject *pSource,
233 const char *pszPrefix,
234 std::list<RndObject *> *pClones,
235 int nFlags,
236 int bRecurse,
237 int bLink);
238
249 void SortObjects(std::list<RndObject *> &objects);
250
261 void Write(BinStream &stream);
262
273 static RndObject *NewTunnel(const char *pszName);
274
285 static const char *ResourceTag(const char *pszFile);
286
297 const char *ResourcePath(const char *pszFile);
298
309 bool AcquireTexture(const FilePath &path);
310
323 void GetResource(const FilePath &path, void **ppData, int *pnSize);
324
335 int IsTextureLoaded(const FilePath &path);
336
346
356 void ReleaseTexture(const FilePath &path);
357
367
369 const std::map<const char *, RndObject *, StrLess> &GetObjects() const {
370 return mObjects;
371 }
372
379 void SetObject(const char *pszName, RndObject *pObject) {
380 mObjects[pszName] = pObject;
381 }
382
388 void RemoveObject(const char *pszName) {
389 mObjects.erase(pszName);
390 }
391
398 void SetClassFactory(const char *pszClass, ClassFactory factory) {
399 mClasses[pszClass] = factory;
400 }
401
408 static int sRev;
409
410private:
411 std::map<const char *, RndObject *, StrLess> mObjects;
412 std::map<const char *, ClassFactory, StrLess> mClasses;
413 std::map<FilePath, std::unique_ptr<ResourceLoader>> mResources;
414 std::list<RndLoader *> mLoaders;
415};
416
423
443template <typename T>
444BinStream &operator<<(BinStream &stream, const std::list<T *> &list) {
445 const int nSize = static_cast<int>(list.size());
446 stream.WriteEndian(&nSize, sizeof(nSize));
447 for (T *pElement : list) {
448 const RndObject *pObject = pElement;
449 stream.WriteString(pObject != nullptr ? pObject->GetName() : "");
450 }
451 return stream;
452}
453
476template <typename T>
477BinStream &operator>>(BinStream &stream, std::list<T *> &list) {
478 int nSize;
479 stream.ReadEndian(&nSize, sizeof(nSize));
480 list.resize(nSize, nullptr);
481 for (T *&pElement : list) {
482 String name;
483 stream >> name;
484 if (name.GetLength() == 0) {
485 pElement = nullptr;
486 } else {
487 pElement = dynamic_cast<T *>(TheManager.Find(name.c_str()));
488 }
489 }
490 return stream;
491}
492
507template <typename T>
508BinStream &operator<<(BinStream &stream, const std::vector<T *> &objects) {
509 const int nSize = static_cast<int>(objects.size());
510 stream.WriteEndian(&nSize, sizeof(nSize));
511 for (T *pElement : objects) {
512 const RndObject *pObject = pElement;
513 stream.WriteString(pObject != nullptr ? pObject->GetName() : "");
514 }
515 return stream;
516}
517
532template <typename T>
533BinStream &operator>>(BinStream &stream, std::vector<T *> &objects) {
534 int nSize;
535 stream.ReadEndian(&nSize, sizeof(nSize));
536 objects.resize(nSize, nullptr);
537 for (T *&pElement : objects) {
538 String name;
539 stream >> name;
540 if (name.GetLength() == 0) {
541 pElement = nullptr;
542 } else {
543 pElement = dynamic_cast<T *>(TheManager.Find(name.c_str()));
544 }
545 }
546 return stream;
547}
Stream of raw bytes, the base of the memory, file, and network streams.
Definition binstream.h:22
void ReadEndian(void *pData, int nBytes)
Read a value of 2, 4, 8, or 16 bytes, reversing its bytes unless the stream is little endian.
BinStream & WriteString(const char *pszText)
Write a string with its length first.
void WriteEndian(const void *pData, int nBytes)
Write a value of 2, 4, 8, or 16 bytes, reversing its bytes unless the stream is little endian.
Path of a file, kept relative to a shared root directory.
Definition filepath.h:14
Interface a loader consults before it creates each object of its file.
Definition rndloader.h:35
Loader of one .rnd file, which reads the file over several frames.
Definition rndloader.h:21
ResourceLoader(std::unique_ptr< File > pFile, const char *pszTag)
Start reading a file.
void AddRef()
Take one more reference, for one more GetData().
Definition rndmanager.h:92
int GetData(void **ppData, int *pnSize)
Wait for the data and hand it out, dropping one reference.
bool Poll()
Finish the read when the file reports it complete.
~ResourceLoader()
Release the buffer and the file.
Registry of every live renderer object, of every class a .rnd file may instantiate,...
Definition rndmanager.h:22
void SetClassFactory(const char *pszClass, ClassFactory factory)
Register the factory that Create() calls for a class.
Definition rndmanager.h:398
void Write(BinStream &stream)
Write every object that is not internal as a .rnd file.
void PollLoaders(float fBudgetMs)
Advance the queued loaders until a time budget is spent.
const char * ResourcePath(const char *pszFile)
Report the file to read for a resource.
void Init()
Register the factory of every class a .rnd file may instantiate.
void SaveFile(const char *pszFile)
Write the objects to a .rnd file.
TextureState
Results of IsTextureLoaded().
Definition rndmanager.h:33
@ kTextureLoading
Definition rndmanager.h:34
@ kTextureLoaded
Definition rndmanager.h:35
@ kTextureUnknown
Definition rndmanager.h:36
void SortObjects(std::list< RndObject * > &objects)
Order a list of objects for writing, the textures first and sorted by file, followed by the animation...
void LoadFile(const char *pszFile)
Load a .rnd file before the call returns.
void GetResource(const FilePath &path, void **ppData, int *pnSize)
Wait for a resource file and hand out its data, dropping one reference.
void ReleaseTexture(const FilePath &path)
Release the read of a texture file whatever its references.
RndObject * Find(const char *pszName)
Resolve a live object by name.
RndObject *(* ClassFactory)(const char *pszName)
Builds one instance of a registered class.
Definition rndmanager.h:30
bool AcquireTexture(const FilePath &path)
Take a reference on a texture file, starting to read it on the first reference.
std::unique_ptr< RndLoader > AddLoader(const char *pszFile, int nFlags, RndLoader::Callback *pCallback, BinStream *pStream)
Create a loader for a .rnd file and queue it.
void DeleteLoadedObjects()
Delete every object a file created, leaving the internal objects.
RndManager()
Construct an empty registry.
void SetObject(const char *pszName, RndObject *pObject)
Register an object under a name, replacing any object registered under it.
Definition rndmanager.h:379
RndObject * Create(const char *pszClass, const char *pszName)
Create an instance of a registered class.
void Clone(RndObject *pSource, const char *pszPrefix, std::list< RndObject * > *pClones, int nFlags, int bRecurse, int bLink)
Clone an object under a prefixed name, optionally with its descendants and its parents.
static int sRev
The format version Write() writes and the newest a loader accepts.
Definition rndmanager.h:408
static const char * ResourceTag(const char *pszFile)
Report the tag a resource file's buffer is billed to, from the file's extension.
~RndManager()
Destroy the registry.
static RndObject * NewTunnel(const char *pszName)
Create a view in place of a tunnel, the factory registered for the class Tunnel.
int IsTextureLoaded(const FilePath &path)
Report whether a texture file finished reading.
void ClearResources()
Release every resource read.
void RemoveLoader(RndLoader *pLoader)
Remove a loader from the queue.
void RemoveObject(const char *pszName)
Remove the registration of a name.
Definition rndmanager.h:388
const std::map< const char *, RndObject *, StrLess > & GetObjects() const
Definition rndmanager.h:369
Base of every object the renderer can load, resolve by name, and serialise.
Definition rndobject.h:20
const char * GetName() const
Definition rndobject.h:133
Growable text buffer that is also a PrnStream.
Definition string.h:19
int GetLength() const
Definition string.h:374
const char * c_str() const
Report the text.
Definition string.h:369
RndManager TheManager
The renderer's registry.
BinStream & operator<<(BinStream &stream, const std::list< T * > &list)
Write a list of objects as its size followed by each object's name.
Definition rndmanager.h:444
BinStream & operator>>(BinStream &stream, std::list< T * > &list)
Read a list of objects the list writer wrote, resolving each name in TheManager.
Definition rndmanager.h:477