1
0
Fork 0

Add basic adapter

This commit is contained in:
Meoweg 2015-11-16 13:05:04 +00:00
parent 008b4ef12e
commit 2c303581fe
10 changed files with 73 additions and 35 deletions

View file

@ -1,4 +1,4 @@
#include "../src/store.hpp"
#include "../src/adapters/adapter.hpp"
#include "../src/scene.hpp"
#include "../src/camera.hpp"
#include "../src/lights/sun.hpp"
@ -15,6 +15,7 @@
#include <emscripten/html5.h>
static Store store;
static const Adapter adapter(store);
static void iterate();
@ -78,12 +79,12 @@ int main()
camera.projection = glm::perspective(45.0f, (float)640 / (float)480, 0.1f, 100.0f);
protagonist = store.load<Models::Static>("protagonist.obj");
car = store.load<Models::Static>("car.obj");
suzanne = store.load<Models::Static>("suzanne.obj");
teapot = store.load<Models::Static>("teapot.obj");
bunny = store.load<Models::Static>("bunny.obj");
untitled = store.load<Models::Raw>("untitled.raw");
protagonist = adapter.load<Models::Static>("protagonist.obj");
car = adapter.load<Models::Static>("car.obj");
suzanne = adapter.load<Models::Static>("suzanne.obj");
teapot = adapter.load<Models::Static>("teapot.obj");
bunny = adapter.load<Models::Static>("bunny.obj");
untitled = adapter.load<Models::Raw>("untitled.raw");
protagonist1 = new Objects::WithModel(*protagonist);
protagonist1->position.z = 4;

36
src/adapters/adapter.hpp Normal file
View file

@ -0,0 +1,36 @@
#ifndef _ADAPTER_HPP_
#define _ADAPTER_HPP_
#include "../store.hpp"
struct Adapter
{
Adapter(Store &store): _store(store) {};
template <class T>
const T *load(const std::string &name) const;
template <class T>
const std::string filename(const std::string &name) const;
private:
Store &_store;
};
template <class T>
const T *Adapter::load(const std::string &name) const
{
static_assert(std::is_convertible<T, Resource>::value
&& !std::is_same<T, Resource>::value,
"Adapter can deal with only Resource's children");
return _store.load<T>(*this, name);
}
template <class T>
const std::string Adapter::filename(const std::string &name) const
{
return "/data" + T::filename(name);
}
#endif // _ADAPTER_HPP_

View file

@ -21,17 +21,17 @@ const GLchar *const Raw::uniforms[] = {
const std::string Raw::filename(const std::string &name)
{
return "/data/models/" + name;
return "/models/" + name;
}
Raw::Raw(__attribute__((unused)) Store &store, const std::string &name)
Raw::Raw(const Adapter &adapter, const std::string &name)
{
exe = store.load<Program>("normal")->build(
exe = adapter.load<Program>("normal")->build(
__attrib_count, attribs,
__uniform_count, uniforms
);
FILE *file = fopen(filename(name).c_str(), "r");
FILE *file = fopen(adapter.filename<Raw>(name).c_str(), "r");
while (!feof(file))
{

View file

@ -27,17 +27,17 @@ const GLchar *const Static::uniforms[] = {
const std::string Static::filename(const std::string &name)
{
return "/data/models/" + name;
return "/models/" + name;
}
Static::Static(Store &store, const std::string &name)
Static::Static(const Adapter &adapter, const std::string &name)
{
exe = store.load<Program>("textured")->build(
exe = adapter.load<Program>("textured")->build(
__attrib_count, attribs,
__uniform_count, uniforms
);
std::ifstream file(filename(name), std::ios::in);
std::ifstream file(adapter.filename<Static>(name), std::ios::in);
const Mtllib *mtllib = nullptr;
@ -104,7 +104,7 @@ Static::Static(Store &store, const std::string &name)
else
if (line.substr(0, 7) == "mtllib ")
{
mtllib = (Mtllib*)store.load<Mtllib>(line.substr(7));
mtllib = adapter.load<Mtllib>(line.substr(7));
}
else
if (line.substr(0, 7) == "usemtl ")

View file

@ -5,12 +5,12 @@
const std::string Mtllib::filename(const std::string &name)
{
return "/data/materials/" + name;
return "/materials/" + name;
}
Mtllib::Mtllib(Store &store, const std::string &name)
Mtllib::Mtllib(const Adapter &adapter, const std::string &name)
{
std::ifstream file(filename(name), std::ios::in);
std::ifstream file(adapter.filename<Mtllib>(name), std::ios::in);
Material *material = nullptr;
@ -28,7 +28,7 @@ Mtllib::Mtllib(Store &store, const std::string &name)
else
if (line.substr(0, 7) == "map_Kd ")
{
material->texture = store.load<Texture>(line.substr(7));
material->texture = adapter.load<Texture>(line.substr(7));
}
}
}

View file

@ -6,12 +6,12 @@
const std::string Program::filename(const std::string &name)
{
return "/data/programs/" + name;
return "/programs/" + name;
}
Program::Program(Store &store, const std::string &name):
vertex_shader(store.load<Shader>(name + ".vert")),
fragment_shader(store.load<Shader>(name + ".frag"))
Program::Program(const Adapter &adapter, const std::string &name):
vertex_shader(adapter.load<Shader>(name + ".vert")),
fragment_shader(adapter.load<Shader>(name + ".frag"))
{}
const Executable *Program::build(

View file

@ -1,7 +1,7 @@
#ifndef _RESOURCE_HPP_
#define _RESOURCE_HPP_
#include "store.hpp"
#include "adapters/adapter.hpp"
struct Resource
{
@ -14,7 +14,7 @@ private:
#define RESOURCE(T) \
friend struct ::Store; \
T(Store &store, const std::string &name); \
T(const Adapter &adapter, const std::string &name); \
static const std::string filename(const std::string &name);
#endif // _RESOURCE_HPP_

View file

@ -5,14 +5,14 @@
const std::string Shader::filename(const std::string &name)
{
return "/data/shaders/" + name;
return "/shaders/" + name;
}
Shader::Shader(__attribute__((unused)) Store &store, const std::string &name)
Shader::Shader(const Adapter &adapter, const std::string &name)
{
const GLuint type = name.substr(name.size() - 5) == ".vert" ? GL_VERTEX_SHADER : GL_FRAGMENT_SHADER;
FILE *file = fopen(filename(name).c_str(), "r");
FILE *file = fopen(adapter.filename<Shader>(name).c_str(), "r");
fseek(file, 0, SEEK_END);
const long size = ftell(file);

View file

@ -5,26 +5,27 @@
#include <string>
#include <map>
struct Adapter;
struct Resource;
struct Store
{
template <class T>
const T *load(const std::string &name);
const T *load(const Adapter &adapter, const std::string &name);
private:
std::map<std::string, Resource*> _resources;
};
template <class T>
const T *Store::load(const std::string &name)
const T *Store::load(const Adapter &adapter, const std::string &name)
{
static_assert(std::is_convertible<T, Resource>::value
&& !std::is_same<T, Resource>::value,
"Store can load only Resource's children");
if (_resources.find(T::filename(name)) == _resources.end())
_resources[T::filename(name)] = new T(*this, name);
_resources[T::filename(name)] = new T(adapter, name);
_resources[T::filename(name)]->_ref_count++;
return (T*)_resources[T::filename(name)];

View file

@ -6,12 +6,12 @@
const std::string Texture::filename(const std::string &name)
{
return "/data/textures/" + name;
return "/textures/" + name;
}
Texture::Texture(__attribute__((unused)) Store &store, const std::string &name)
Texture::Texture(const Adapter &adapter, const std::string &name)
{
SDL_Surface *surface = IMG_Load(filename(name).c_str());
SDL_Surface *surface = IMG_Load(adapter.filename<Texture>(name).c_str());
glGenTextures(1, &_id);
glBindTexture(GL_TEXTURE_2D, _id);