Browse Source

Compile ResourceManager documentation snippets.

pull/317/head
Vladimír Vondruš 7 years ago
parent
commit
1bd3d270d8
  1. 84
      doc/snippets/Magnum.cpp
  2. 30
      src/Magnum/AbstractResourceLoader.h
  3. 28
      src/Magnum/ResourceManager.h

84
doc/snippets/Magnum.cpp

@ -26,6 +26,9 @@
#include "Magnum/Image.h" #include "Magnum/Image.h"
#include "Magnum/PixelFormat.h" #include "Magnum/PixelFormat.h"
#ifdef MAGNUM_TARGET_GL #ifdef MAGNUM_TARGET_GL
#include "Magnum/ResourceManager.h"
#include "Magnum/GL/AbstractShaderProgram.h"
#include "Magnum/GL/Mesh.h"
#include "Magnum/GL/PixelFormat.h" #include "Magnum/GL/PixelFormat.h"
#include "Magnum/GL/Texture.h" #include "Magnum/GL/Texture.h"
#endif #endif
@ -33,6 +36,28 @@
using namespace Magnum; using namespace Magnum;
using namespace Magnum::Math::Literals; using namespace Magnum::Math::Literals;
#ifdef MAGNUM_TARGET_GL
Containers::Pointer<GL::Mesh> mesh;
bool found = false;
/* [AbstractResourceLoader-implementation] */
class MeshResourceLoader: public AbstractResourceLoader<GL::Mesh> {
void doLoad(ResourceKey key) override {
// Load the mesh...
// Not found
if(!found) {
setNotFound(key);
return;
}
// Found, pass it to resource manager
set(key, std::move(mesh));
}
};
/* [AbstractResourceLoader-implementation] */
#endif
int main() { int main() {
{ {
@ -176,4 +201,63 @@ CompressedImage2D image = texture.compressedImage(0, {});
} }
#endif #endif
#ifdef MAGNUM_TARGET_GL
{
/* [ResourceManager-typedef] */
typedef ResourceManager<GL::Mesh, GL::Texture2D, GL::AbstractShaderProgram>
MyResourceManager;
MyResourceManager manager;
/* [ResourceManager-typedef] */
}
{
typedef ResourceManager<GL::Mesh, GL::Texture2D, GL::AbstractShaderProgram>
MyResourceManager;
struct MyShader: GL::AbstractShaderProgram {
void bindTexture(GL::Texture2D&) {}
};
/* [ResourceManager-fill] */
MyResourceManager& manager = MyResourceManager::instance();
Resource<GL::Texture2D> texture{manager.get<GL::Texture2D>("texture")};
Resource<GL::AbstractShaderProgram, MyShader> shader =
manager.get<GL::AbstractShaderProgram, MyShader>("shader");
Resource<GL::Mesh> cube = manager.get<GL::Mesh>("cube");
// The manager doesn't have data for the cube yet, add them
if(!cube) {
GL::Mesh mesh;
// ...
manager.set(cube.key(), std::move(mesh));
}
/* [ResourceManager-fill] */
/* [ResourceManager-use] */
shader->bindTexture(*texture);
cube->draw(*shader);
/* [ResourceManager-use] */
}
{
ResourceManager<GL::AbstractShaderProgram> manager;
struct MyShader: GL::AbstractShaderProgram {};
/* [ResourceManager-get-derived] */
Resource<GL::AbstractShaderProgram, MyShader> shader =
manager.get<GL::AbstractShaderProgram, MyShader>("shader");
/* [ResourceManager-get-derived] */
}
{
typedef ResourceManager<GL::Mesh> MyResourceManager;
/* [AbstractResourceLoader-use] */
MyResourceManager manager;
Containers::Pointer<MeshResourceLoader> loader;
manager.setLoader<GL::Mesh>(std::move(loader));
// This will now automatically request the mesh from loader by calling load()
Resource<GL::Mesh> myMesh = manager.get<GL::Mesh>("my-mesh");
/* [AbstractResourceLoader-use] */
}
#endif
} }

30
src/Magnum/AbstractResourceLoader.h

@ -65,22 +65,7 @@ keys.
Example implementation for synchronous mesh loader: Example implementation for synchronous mesh loader:
@code{.cpp} @snippet Magnum.cpp AbstractResourceLoader-implementation
class MeshResourceLoader: public AbstractResourceLoader<Mesh> {
void doLoad(ResourceKey key) override {
// Load the mesh...
// Not found
if(!found) {
setNotFound(key);
return;
}
// Found, pass it to resource manager
set(key, mesh, state, policy);
}
};
@endcode
You can then add it to resource manager instance like this. Note that the You can then add it to resource manager instance like this. Note that the
manager automatically deletes the all loaders on destruction before unloading manager automatically deletes the all loaders on destruction before unloading
@ -89,18 +74,7 @@ having to delete the loader explicitly to ensure proper resource unloading. In
the following code, however, the loader destroys itself (and removes itself the following code, however, the loader destroys itself (and removes itself
from the manager) before the manager is destroyed. from the manager) before the manager is destroyed.
@code{.cpp} @snippet Magnum.cpp AbstractResourceLoader-use
MyResourceManager manager;
MeshResourceLoader loader;
manager->setLoader(&loader);
// This will now automatically request the mesh from loader by calling load()
Resource<Mesh> myMesh = manager->get<Mesh>("my-mesh");
@endcode
@todoc How about working with resources of different data types (i.e. mesh
buffers), should that be allowed?
*/ */
template<class T> class AbstractResourceLoader { template<class T> class AbstractResourceLoader {
public: public:

28
src/Magnum/ResourceManager.h

@ -218,10 +218,7 @@ Basic usage is:
<li> <li>
Typedef'ing manager of desired types, creating its instance. Typedef'ing manager of desired types, creating its instance.
@code{.cpp} @snippet Magnum.cpp ResourceManager-typedef
typedef ResourceManager<Mesh, Texture2D, AbstractShaderProgram> MyResourceManager;
MyResourceManager manager;
@endcode
</li> </li>
<li> <li>
Filling the manager with resource data and acquiring the resources. Note Filling the manager with resource data and acquiring the resources. Note
@ -229,27 +226,12 @@ Basic usage is:
contains the data for it, as long as the resource data are not accessed (or contains the data for it, as long as the resource data are not accessed (or
fallback is provided). fallback is provided).
@code{.cpp} @snippet Magnum.cpp ResourceManager-fill
MyResourceManager& manager = MyResourceManager::instance();
Resource<Texture2D> texture{manager.get<Texture2D>("texture")};
Resource<AbstractShaderProgram, MyShader> shader{manager.get<AbstractShaderProgram, MyShader>("shader")};
Resource<Mesh> cube{manager.get<Mesh>("cube")};
// The manager doesn't have data for the cube yet, add them
if(!cube) {
Mesh* mesh = new Mesh;
// ...
manager.set(cube.key(), mesh, ResourceDataState::Final, ResourcePolicy::Resident);
}
@endcode
</li> </li>
<li> <li>
Using the resource data. Using the resource data.
@code{.cpp} @snippet Magnum.cpp ResourceManager-use
shader->setTexture(*texture);
cube->draw(*shader);
@endcode
</li> </li>
<li> <li>
Destroying resource references and deleting manager instance when nothing Destroying resource references and deleting manager instance when nothing
@ -303,9 +285,7 @@ template<class... Types> class ResourceManager: private Implementation::Resource
* can be defined to cast the type automatically when accessing the * can be defined to cast the type automatically when accessing the
* data. This is commonly used for shaders, e.g.: * data. This is commonly used for shaders, e.g.:
* *
* @code{.cpp} * @snippet Magnum.cpp ResourceManager-get-derived
* Resource<AbstractShaderProgram, MyShader> shader = manager->get<AbstractShaderProgram, MyShader>("shader");
* @endcode
*/ */
template<class T, class U = T> Resource<T, U> get(ResourceKey key) { template<class T, class U = T> Resource<T, U> get(ResourceKey key) {
return this->Implementation::ResourceManagerData<T>::template get<U>(key); return this->Implementation::ResourceManagerData<T>::template get<U>(key);

Loading…
Cancel
Save