A ridiculouly easy to use Cinder block for loading common resources.
Contact: [email protected]
A ridiculouly easy to use Cinder block for loading common resources.
Contact: [email protected]
#include "AssetManager.h" | |
#include "cinder/app/App.h" | |
#include "cinder/ImageIo.h" | |
#include "cinder/Function.h" | |
#include <map> | |
using namespace std; | |
using namespace ci; | |
using namespace ci::app; | |
namespace | |
{ | |
template <typename T> | |
T& getAssetResource(const string& relativeName, function<T(const string&)> loadFunc) | |
{ | |
typedef map<string, typename T> MapType; | |
static MapType sMap; | |
MapType::iterator it = sMap.find(relativeName); | |
if (it != sMap.end()) | |
{ | |
return it->second; | |
} | |
console() << "Loading " << relativeName << endl; | |
try | |
{ | |
fs::path aPath = getAssetPath(".") / relativeName; | |
return sMap[relativeName] = loadFunc(aPath.string()); | |
} | |
catch (Exception& e) | |
{ | |
console() << "getAssetResource: " << e.what() << endl; | |
throw; | |
} | |
} | |
} | |
static Surface loadSurface(const string& absoluteName) | |
{ | |
return loadImage(absoluteName); | |
} | |
Surface& getSurface(const string& relativeName) | |
{ | |
return getAssetResource<Surface>(relativeName, loadSurface); | |
} | |
static gl::Texture loadTexture(const string& absoluteName) | |
{ | |
return loadImage(absoluteName); | |
} | |
gl::Texture& getTexture(const string& relativeName) | |
{ | |
return getAssetResource<gl::Texture>(relativeName, loadTexture); | |
} | |
static TriMesh loadTriMesh(const string& absoluteName) | |
{ | |
TriMesh mesh; | |
mesh.read(DataSourcePath::create(absoluteName)); | |
return mesh; | |
} | |
TriMesh& getTriMesh(const string& relativeName) | |
{ | |
return getAssetResource<TriMesh>(relativeName, loadTriMesh); | |
} | |
static gl::VboMesh loadVboMesh(const string& absoluteName) | |
{ | |
gl::VboMesh::Layout layout; | |
gl::VboMesh mesh(loadTriMesh(absoluteName), layout); | |
return mesh; | |
} | |
gl::VboMesh& getVboMesh(const string& relativeName) | |
{ | |
return getAssetResource<gl::VboMesh>(relativeName, loadVboMesh); | |
} | |
static gl::GlslProg loadGlslProg(const string& absoluteName) | |
{ | |
fs::path vertexShader = absoluteName + ".vs"; | |
fs::path fragmentShader = absoluteName + ".fs"; | |
gl::GlslProg glsl(DataSourcePath::create(vertexShader), DataSourcePath::create(fragmentShader)); | |
return glsl; | |
} | |
gl::GlslProg& getGlslProg(const string& relativeName) | |
{ | |
return getAssetResource<gl::GlslProg>(relativeName, loadGlslProg); | |
} |
#pragma once | |
#include "cinder/gl/Texture.h" | |
#include "cinder/gl/Vbo.h" | |
#include "cinder/gl/GlslProg.h" | |
ci::Surface& getSurface(const std::string& relativeName); | |
ci::gl::Texture& getTexture(const std::string& relativeName); | |
ci::TriMesh& getTriMesh(const std::string& relativeName); | |
ci::gl::VboMesh& getVboMesh(const std::string& relativeName); | |
// Will load vertex shader from /assets/<relativeName>.vs | |
// and fragment shader from /assets/<relativeName>.fs | |
ci::gl::GlslProg& getGlslProg(const std::string& relativeName); |
#include "AssetManager.h" | |
void SomeApp::draw() | |
{ | |
getGlslProg("shader/toon").bind(); | |
gl::draw(getTexture("image/background.png")); | |
getGlslProg("shader/toon").unbind(); | |
} |