Use single store for resource tree
This commit is contained in:
parent
913d14642f
commit
ea811ab340
12
src/main.cpp
12
src/main.cpp
|
@ -12,6 +12,8 @@
|
|||
#include <emscripten/emscripten.h>
|
||||
#include <emscripten/html5.h>
|
||||
|
||||
static Store store;
|
||||
|
||||
static void iterate();
|
||||
|
||||
static GLFWCALL void on_key(int key, int action);
|
||||
|
@ -60,7 +62,7 @@ int main()
|
|||
glfwSetKeyCallback(on_key);
|
||||
emscripten_set_mousemove_callback(nullptr, nullptr, false, on_em_mousemove);
|
||||
|
||||
const Program *program = Store().load<Program>("textured");
|
||||
const Program *program = store.load<Program>("textured");
|
||||
program->use();
|
||||
|
||||
mvp_uniform = program->get_uniform_location("mvp");
|
||||
|
@ -72,10 +74,10 @@ int main()
|
|||
camera.projection = glm::perspective(45.0f, (float)640 / (float)480, 0.1f, 10.0f);
|
||||
camera.position.z = 2;
|
||||
|
||||
protagonist = Store().load<Model>("protagonist.obj");
|
||||
suzanne = Store().load<Model>("suzanne.obj");
|
||||
teapot = Store().load<Model>("teapot.obj");
|
||||
bunny = Store().load<Model>("bunny.obj");
|
||||
protagonist = store.load<Model>("protagonist.obj");
|
||||
suzanne = store.load<Model>("suzanne.obj");
|
||||
teapot = store.load<Model>("teapot.obj");
|
||||
bunny = store.load<Model>("bunny.obj");
|
||||
|
||||
protagonist1 = new Object(*protagonist);
|
||||
protagonist1->position.z = 4;
|
||||
|
|
|
@ -10,7 +10,7 @@ const std::string Model::filename(const std::string &name)
|
|||
return "/data/models/" + name;
|
||||
}
|
||||
|
||||
Model::Model(const std::string &name)
|
||||
Model::Model(Store &store, const std::string &name)
|
||||
{
|
||||
std::ifstream file(filename(name), std::ios::in);
|
||||
|
||||
|
@ -81,7 +81,7 @@ Model::Model(const std::string &name)
|
|||
else
|
||||
if (line.substr(0, 7) == "mtllib ")
|
||||
{
|
||||
mtllib = (Mtllib*)Store().load<Mtllib>(line.substr(7));
|
||||
mtllib = (Mtllib*)store.load<Mtllib>(line.substr(7));
|
||||
}
|
||||
else
|
||||
if (line.substr(0, 7) == "usemtl ")
|
||||
|
|
|
@ -8,7 +8,7 @@ const std::string Mtllib::filename(const std::string &name)
|
|||
return "/data/materials/" + name;
|
||||
}
|
||||
|
||||
Mtllib::Mtllib(const std::string &name)
|
||||
Mtllib::Mtllib(Store &store, const std::string &name)
|
||||
{
|
||||
std::ifstream file(filename(name), std::ios::in);
|
||||
|
||||
|
@ -28,7 +28,7 @@ Mtllib::Mtllib(const std::string &name)
|
|||
else
|
||||
if (line.substr(0, 7) == "map_Kd ")
|
||||
{
|
||||
material->texture = Store().load<Texture>(line.substr(7));
|
||||
material->texture = store.load<Texture>(line.substr(7));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ const std::string Program::filename(const std::string &name)
|
|||
return "/data/shaders/" + name;
|
||||
}
|
||||
|
||||
Program::Program(const std::string &name)
|
||||
Program::Program(__attribute__((unused)) Store &store, const std::string &name)
|
||||
{
|
||||
const std::string path = filename(name) + '/';
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ class Resource
|
|||
#define RESOURCE(T) \
|
||||
private: \
|
||||
friend class Store; \
|
||||
T(const std::string &name); \
|
||||
T(Store &store, const std::string &name); \
|
||||
static const std::string filename(const std::string &name);
|
||||
|
||||
#endif // _RESOURCE_HPP_
|
||||
|
|
|
@ -25,7 +25,7 @@ const T *Store::load(const std::string &name)
|
|||
"Store can load only Resource's children");
|
||||
|
||||
if (_resources.find(name) == _resources.end())
|
||||
_resources[T::filename(name)] = new T(name);
|
||||
_resources[T::filename(name)] = new T(*this, name);
|
||||
|
||||
_resources[T::filename(name)]->_ref_count++;
|
||||
return (T*)_resources[T::filename(name)];
|
||||
|
|
|
@ -9,7 +9,7 @@ const std::string Texture::filename(const std::string &name)
|
|||
return "/data/textures/" + name;
|
||||
}
|
||||
|
||||
Texture::Texture(const std::string &name)
|
||||
Texture::Texture(__attribute__((unused)) Store &store, const std::string &name)
|
||||
{
|
||||
SDL_Surface *surface = IMG_Load(filename(name).c_str());
|
||||
|
||||
|
|
Reference in New Issue