From 927a1aa0a3a27e463ec0efc08bd08e7c4e969909 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 4 Feb 2010 17:59:06 +0000 Subject: [PATCH] Inherit Loaders from the ObjectLoader classes --- source/batch.cpp | 4 ++-- source/batch.h | 6 ++---- source/font.cpp | 30 ++++++++++----------------- source/font.h | 19 ++++------------- source/material.cpp | 10 ++++----- source/material.h | 8 ++----- source/mesh.cpp | 10 ++++----- source/mesh.h | 6 ++---- source/object.cpp | 16 +++++++------- source/object.h | 12 +---------- source/objectpass.cpp | 14 ++++++------- source/objectpass.h | 13 ++---------- source/program.cpp | 12 +++++------ source/program.h | 7 ++----- source/programdata.cpp | 12 +++++------ source/programdata.h | 5 ++--- source/technique.cpp | 47 +++++++++++++++++++++--------------------- source/technique.h | 11 +--------- source/texture.cpp | 8 +++---- source/texture.h | 7 ++----- source/texture2d.cpp | 6 +++--- source/texture2d.h | 1 - 22 files changed, 101 insertions(+), 163 deletions(-) diff --git a/source/batch.cpp b/source/batch.cpp index e5224911..33481a1c 100644 --- a/source/batch.cpp +++ b/source/batch.cpp @@ -53,14 +53,14 @@ void Batch::draw_with_buffer(unsigned offset) const Batch::Loader::Loader(Batch &b): - batch(b) + DataFile::ObjectLoader(b) { add("indices", &Loader::indices); } void Batch::Loader::indices(const vector &ind) { - batch.append(ind); + obj.append(ind); } } // namespace GL diff --git a/source/batch.h b/source/batch.h index 6950af89..bca54139 100644 --- a/source/batch.h +++ b/source/batch.h @@ -9,7 +9,7 @@ Distributed under the LGPL #define MSP_GL_BATCH_H_ #include -#include +#include #include "primitivetype.h" #include "types.h" @@ -19,13 +19,11 @@ namespace GL { class Batch { public: - class Loader: public DataFile::Loader + class Loader: public DataFile::ObjectLoader { public: Loader(Batch &); private: - Batch &batch; - void indices(const std::vector &); }; diff --git a/source/font.cpp b/source/font.cpp index f82b45d8..8320374e 100644 --- a/source/font.cpp +++ b/source/font.cpp @@ -5,6 +5,7 @@ Copyright © 2007 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ +#include #include "gl.h" #include "font.h" #include "immediate.h" @@ -125,26 +126,17 @@ float Font::get_glyph_advance(unsigned code) const Font::Loader::Loader(Font &f): - font(f), - coll(0) + DataFile::CollectionObjectLoader(f, 0) { init(); } Font::Loader::Loader(Font &f, Collection &c): - font(f), - coll(&c) + DataFile::CollectionObjectLoader(f, &c) { init(); } -DataFile::Collection &Font::Loader::get_collection() -{ - if(!coll) - throw InvalidState("No collection"); - return *coll; -} - void Font::Loader::init() { add("default_size", &Font::default_size); @@ -160,20 +152,20 @@ void Font::Loader::glyph(unsigned c) Glyph gl; gl.code=c; load_sub(gl); - font.glyphs.insert(GlyphMap::value_type(c, gl)); + obj.glyphs.insert(GlyphMap::value_type(c, gl)); } void Font::Loader::texture_inline() { RefPtr tex=new Texture2D; load_sub(*tex); - font.tex=tex.release(); - font.own_tex=true; + obj.tex=tex.release(); + obj.own_tex=true; } Font::Glyph::Loader::Loader(Glyph &g): - glyph(g) + DataFile::ObjectLoader(g) { add("texcoords", &Loader::texcoords); add("size", &Glyph::w, &Glyph::h); @@ -183,10 +175,10 @@ Font::Glyph::Loader::Loader(Glyph &g): void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2) { - glyph.x1=x1; - glyph.y1=y1; - glyph.x2=x2; - glyph.y2=y2; + obj.x1=x1; + obj.y1=y1; + obj.x2=x2; + obj.y2=y2; } } // namespace GL diff --git a/source/font.h b/source/font.h index a2406b9e..dd40ec0b 100644 --- a/source/font.h +++ b/source/font.h @@ -10,7 +10,7 @@ Distributed under the LGPL #include #include -#include +#include #include #include "vertexarray.h" @@ -23,19 +23,11 @@ class Texture2D; class Font { public: - class Loader: public DataFile::Loader + class Loader: public DataFile::CollectionObjectLoader { - private: - Font &font; - DataFile::Collection *coll; - public: - typedef DataFile::Collection Collection; - Loader(Font &); - Loader(Font &, DataFile::Collection &); - Font &get_object() { return font; } - DataFile::Collection &get_collection(); + Loader(Font &, Collection &); private: void init(); void glyph(unsigned); @@ -89,14 +81,11 @@ public: private: struct Glyph { - class Loader: public Msp::DataFile::Loader + class Loader: public Msp::DataFile::ObjectLoader { public: Loader(Glyph &); - Glyph &get_object() { return glyph; } private: - Glyph &glyph; - void texcoords(float, float, float, float); }; diff --git a/source/material.cpp b/source/material.cpp index 310834f3..b5ec6a5c 100644 --- a/source/material.cpp +++ b/source/material.cpp @@ -66,7 +66,7 @@ const Material *Material::current=0; Material::Loader::Loader(Material &m): - mat(m) + DataFile::ObjectLoader(m) { add("ambient", &Loader::ambient); add("diffuse", &Loader::diffuse); @@ -77,22 +77,22 @@ Material::Loader::Loader(Material &m): void Material::Loader::ambient(float r, float g, float b, float a) { - mat.ambient=GL::Color(r, g, b, a); + obj.ambient=GL::Color(r, g, b, a); } void Material::Loader::diffuse(float r, float g, float b, float a) { - mat.diffuse=GL::Color(r, g, b, a); + obj.diffuse=GL::Color(r, g, b, a); } void Material::Loader::specular(float r, float g, float b, float a) { - mat.specular=GL::Color(r, g, b, a); + obj.specular=GL::Color(r, g, b, a); } void Material::Loader::emission(float r, float g, float b, float a) { - mat.emission=GL::Color(r, g, b, a); + obj.emission=GL::Color(r, g, b, a); } } // namespace GL diff --git a/source/material.h b/source/material.h index 1da27199..6614aeea 100644 --- a/source/material.h +++ b/source/material.h @@ -8,7 +8,7 @@ Distributed under the LGPL #ifndef MSP_GL_MATERIAL_H_ #define MSP_GL_MATERIAL_H_ -#include +#include #include "color.h" namespace Msp { @@ -21,14 +21,10 @@ objects, application of material is done with several calls to glMaterial. class Material { public: - class Loader: public DataFile::Loader + class Loader: public DataFile::ObjectLoader { - private: - Material &mat; - public: Loader(Material &); - Material &get_object() const { return mat; } private: void ambient(float, float, float, float); diff --git a/source/mesh.cpp b/source/mesh.cpp index d43e0d0e..2ca040e6 100644 --- a/source/mesh.cpp +++ b/source/mesh.cpp @@ -99,7 +99,7 @@ void Mesh::update_index_buffer() Mesh::Loader::Loader(Mesh &m): - mesh(m) + DataFile::ObjectLoader(m) { add("vertices", &Loader::vertices); add("batch", &Loader::batch); @@ -107,14 +107,14 @@ Mesh::Loader::Loader(Mesh &m): void Mesh::Loader::vertices(VertexFormat f) { - mesh.vertices.reset(f); - load_sub(mesh.vertices); + obj.vertices.reset(f); + load_sub(obj.vertices); } void Mesh::Loader::batch(PrimitiveType p) { - mesh.batches.push_back(Batch(p)); - load_sub(mesh.batches.back()); + obj.batches.push_back(Batch(p)); + load_sub(obj.batches.back()); } } // namespace GL diff --git a/source/mesh.h b/source/mesh.h index 0a4142fe..044e7084 100644 --- a/source/mesh.h +++ b/source/mesh.h @@ -8,7 +8,7 @@ Distributed under the LGPL #ifndef MSP_GL_MESH_H_ #define MSP_GL_MESH_H_ -#include +#include #include "batch.h" #include "vertexarray.h" @@ -22,13 +22,11 @@ class Mesh friend class MeshBuilder; public: - class Loader: public DataFile::Loader + class Loader: public DataFile::ObjectLoader { public: Loader(Mesh &); private: - Mesh &mesh; - void vertices(VertexFormat); void batch(PrimitiveType); }; diff --git a/source/object.cpp b/source/object.cpp index d5d5231b..f2d1f8bf 100644 --- a/source/object.cpp +++ b/source/object.cpp @@ -5,6 +5,7 @@ Copyright © 2007 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ +#include #include #include "except.h" #include "material.h" @@ -124,8 +125,7 @@ void Object::render_instance(const ObjectInstance &inst, const Tag &tag) const Object::Loader::Loader(Object &o, Collection &c): - obj(o), - coll(c) + DataFile::CollectionObjectLoader(o, &c) { add("lod_mesh", &Loader::lod_mesh); add("material", &Object::material); @@ -154,20 +154,20 @@ void Object::Loader::finish() void Object::Loader::lod_mesh(unsigned l, const string &n) { obj.meshes.resize(l+1, 0); - obj.meshes[l]=coll.get(n); + obj.meshes[l]=coll->get(n); } void Object::Loader::material_inline() { RefPtr mat=new Material; load_sub(*mat); - coll.add(format("_%p", mat.get()), mat.get()); + coll->add(format("_%p", mat.get()), mat.get()); obj.material=mat.release(); } void Object::Loader::mesh(const string &n) { - obj.meshes[0]=coll.get(n); + obj.meshes[0]=coll->get(n); } void Object::Loader::shader_texture(const string &n) @@ -179,12 +179,12 @@ void Object::Loader::shader_texture(const string &n) if(eqsign==string::npos) throw InvalidParameterValue("Must specify texture slot name"); - obj.textures[obj.technique->get_texture_index(n.substr(0, eqsign))]=coll.get(n.substr(eqsign+1)); + obj.textures[obj.technique->get_texture_index(n.substr(0, eqsign))]=coll->get(n.substr(eqsign+1)); } void Object::Loader::technique(const string &n) { - obj.technique=coll.get(n); + obj.technique=coll->get(n); obj.textures.resize(obj.technique->get_n_textures()); obj.material=obj.technique->get_material(); } @@ -194,7 +194,7 @@ void Object::Loader::texture(const string &n) if(obj.main_texture) throw Exception("Only one main texture may be specified"); - Texture *tex=coll.get(n); + Texture *tex=coll->get(n); if(obj.technique) obj.textures[obj.technique->get_texture_index("texture")]=tex; obj.main_texture=tex; diff --git a/source/object.h b/source/object.h index fff1e4cf..bf4a0524 100644 --- a/source/object.h +++ b/source/object.h @@ -9,7 +9,6 @@ Distributed under the LGPL #define MSP_GL_OBJECT_H_ #include -#include #include "objectpass.h" #include "renderable.h" @@ -41,20 +40,11 @@ private: const Material *material; public: - class Loader: public DataFile::Loader + class Loader: public DataFile::CollectionObjectLoader { - public: - typedef DataFile::Collection Collection; - - protected: - Object &obj; - Collection &coll; - public: Loader(Object &, Collection &); - Object &get_object() const { return obj; } - Collection &get_collection() const { return coll; } private: virtual void finish(); void lod_mesh(unsigned, const std::string &); diff --git a/source/objectpass.cpp b/source/objectpass.cpp index afb9f45d..01a6c2eb 100644 --- a/source/objectpass.cpp +++ b/source/objectpass.cpp @@ -5,6 +5,7 @@ Copyright © 2007 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ +#include #include "objectpass.h" #include "program.h" #include "programdata.h" @@ -26,8 +27,7 @@ ObjectPass::~ObjectPass() ObjectPass::Loader::Loader(ObjectPass &p, Collection &c): - pass(p), - coll(c) + DataFile::CollectionObjectLoader(p, &c) { add("shader", &Loader::shader); add("use_textures", &ObjectPass::use_textures); @@ -35,16 +35,16 @@ ObjectPass::Loader::Loader(ObjectPass &p, Collection &c): void ObjectPass::Loader::shader(const string &n) { - Program *shprog=coll.get(n); + Program *shprog=coll->get(n); if(shprog) // Allow for unsupported shaders { RefPtr shdata=new ProgramData; load_sub(*shdata, *shprog); - pass.shprog=shprog; - if(pass.shdata) - delete pass.shdata; - pass.shdata=shdata.release(); + obj.shprog=shprog; + if(obj.shdata) + delete obj.shdata; + obj.shdata=shdata.release(); } } diff --git a/source/objectpass.h b/source/objectpass.h index e8190fa7..e3cac702 100644 --- a/source/objectpass.h +++ b/source/objectpass.h @@ -8,7 +8,7 @@ Distributed under the LGPL #ifndef MSP_GL_OBJECTPASS_H_ #define MSP_GL_OBJECTPASS_H_ -#include +#include namespace Msp { namespace GL { @@ -18,19 +18,10 @@ class ProgramData; struct ObjectPass { - class Loader: public DataFile::Loader + class Loader: public DataFile::CollectionObjectLoader { - public: - typedef DataFile::Collection Collection; - - private: - ObjectPass &pass; - Collection &coll; - public: Loader(ObjectPass &, Collection &); - ObjectPass &get_object() const { return pass; } - Collection &get_collection() const { return coll; } private: void shader(const std::string &); }; diff --git a/source/program.cpp b/source/program.cpp index 75f982d7..01644c02 100644 --- a/source/program.cpp +++ b/source/program.cpp @@ -141,9 +141,9 @@ const Program *Program::cur_prog=0; Program::Loader::Loader(Program &p): - prog(p) + DataFile::ObjectLoader(p) { - prog.set_del_shaders(true); + obj.set_del_shaders(true); add("vertex_shader", &Loader::vertex_shader); add("fragment_shader", &Loader::fragment_shader); @@ -152,22 +152,22 @@ Program::Loader::Loader(Program &p): void Program::Loader::vertex_shader(const string &src) { - prog.attach_shader(*new Shader(VERTEX_SHADER, src)); + obj.attach_shader(*new Shader(VERTEX_SHADER, src)); } void Program::Loader::fragment_shader(const string &src) { - prog.attach_shader(*new Shader(FRAGMENT_SHADER, src)); + obj.attach_shader(*new Shader(FRAGMENT_SHADER, src)); } void Program::Loader::attribute(uint i, const string &n) { - prog.bind_attribute(i, n); + obj.bind_attribute(i, n); } void Program::Loader::finish() { - prog.link(); + obj.link(); } } // namespace GL diff --git a/source/program.h b/source/program.h index ad37fb65..818e72be 100644 --- a/source/program.h +++ b/source/program.h @@ -10,7 +10,7 @@ Distributed under the LGPL #include #include -#include +#include #include "gl.h" #include "types.h" @@ -30,11 +30,8 @@ private: static const Program *cur_prog; public: - class Loader: public DataFile::Loader + class Loader: public DataFile::ObjectLoader { - private: - Program &prog; - public: Loader(Program &); diff --git a/source/programdata.cpp b/source/programdata.cpp index a7a9b037..9acb93bc 100644 --- a/source/programdata.cpp +++ b/source/programdata.cpp @@ -92,7 +92,7 @@ void ProgramData::apply() const ProgramData::Loader::Loader(ProgramData &pd, Program &pr): - pdata(pd), + DataFile::ObjectLoader(pd), prog(pr) { add("uniform1i", &Loader::uniform1i); @@ -104,27 +104,27 @@ ProgramData::Loader::Loader(ProgramData &pd, Program &pr): void ProgramData::Loader::uniform1i(const string &n, int v) { - pdata.uniform(prog.get_uniform_location(n), v); + obj.uniform(prog.get_uniform_location(n), v); } void ProgramData::Loader::uniform1f(const string &n, float v) { - pdata.uniform(prog.get_uniform_location(n), v); + obj.uniform(prog.get_uniform_location(n), v); } void ProgramData::Loader::uniform2f(const string &n, float v0, float v1) { - pdata.uniform(prog.get_uniform_location(n), v0, v1); + obj.uniform(prog.get_uniform_location(n), v0, v1); } void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2) { - pdata.uniform(prog.get_uniform_location(n), v0, v1, v2); + obj.uniform(prog.get_uniform_location(n), v0, v1, v2); } void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3) { - pdata.uniform(prog.get_uniform_location(n), v0, v1, v2, v3); + obj.uniform(prog.get_uniform_location(n), v0, v1, v2, v3); } } // namespace GL diff --git a/source/programdata.h b/source/programdata.h index 4d7938bf..591ffee3 100644 --- a/source/programdata.h +++ b/source/programdata.h @@ -9,7 +9,7 @@ Distributed under the LGPL #define MSP_GL_PROGRAMDATA_H_ #include -#include +#include namespace Msp { namespace GL { @@ -23,10 +23,9 @@ Stores uniform variables for a shader program. class ProgramData { public: - class Loader: public DataFile::Loader + class Loader: public DataFile::ObjectLoader { private: - ProgramData &pdata; Program &prog; public: diff --git a/source/technique.cpp b/source/technique.cpp index c7bd25ed..26837b61 100644 --- a/source/technique.cpp +++ b/source/technique.cpp @@ -5,6 +5,8 @@ Copyright © 2007 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ +#include +#include #include #include "material.h" #include "program.h" @@ -62,8 +64,7 @@ const Texture *Technique::get_texture(unsigned i) const Technique::Loader::Loader(Technique &t, Collection &c): - tech(t), - coll(c) + DataFile::CollectionObjectLoader(t, &c) { add("material", &Technique::material); add("material_inline", &Loader::material_inline); @@ -76,12 +77,12 @@ Technique::Loader::Loader(Technique &t, Collection &c): void Technique::Loader::finish() { - for(PassMap::iterator i=tech.passes.begin(); i!=tech.passes.end(); ++i) + for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i) if(i->second.shdata) { - for(unsigned j=0; jsecond.shprog->get_uniform_location(tech.textures[j].name); + unsigned loc=i->second.shprog->get_uniform_location(obj.textures[j].name); i->second.shdata->uniform(loc, static_cast(j)); } } @@ -91,32 +92,32 @@ void Technique::Loader::material_inline() { RefPtr mat=new Material; load_sub(*mat); - coll.add(format("_%p", mat.get()), mat.get()); - tech.material=mat.release(); + coll->add(format("_%p", mat.get()), mat.get()); + obj.material=mat.release(); } void Technique::Loader::pass(const string &n) { Tag tag(n); - if(tech.passes.count(tag)) + if(obj.passes.count(tag)) throw KeyError("Duplicate pass name", n); ObjectPass p; - load_sub(p, coll); - tech.passes[tag]=p; + load_sub(p, *coll); + obj.passes[tag]=p; } void Technique::Loader::shader(const string &n) { - Program *shprog=coll.get(n); + Program *shprog=coll->get(n); if(shprog) // Allow for unsupported shaders { RefPtr shdata=new ProgramData; load_sub(*shdata, *shprog); - tech.normal_pass->shprog=shprog; - if(tech.normal_pass->shdata) - delete tech.normal_pass->shdata; - tech.normal_pass->shdata=shdata.release(); + obj.normal_pass->shprog=shprog; + if(obj.normal_pass->shdata) + delete obj.normal_pass->shdata; + obj.normal_pass->shdata=shdata.release(); } } @@ -127,37 +128,37 @@ void Technique::Loader::shader_texture(const string &n) if(eqsign!=string::npos) { tex.name=n.substr(0, eqsign); - tex.texture=coll.get(n.substr(eqsign+1)); + tex.texture=coll->get(n.substr(eqsign+1)); } else { string::size_type dot=n.rfind('.'); tex.name=n.substr(0, dot); - tex.texture = coll.get(n); + tex.texture = coll->get(n); } for(string::iterator i=tex.name.begin(); i!=tex.name.end(); ++i) if(!isalnum(*i)) *i='_'; - tech.textures.push_back(tex); + obj.textures.push_back(tex); } void Technique::Loader::texture(const string &n) { - if(tech.main_texture) + if(obj.main_texture) throw Exception("Only one main texture may be specified"); - tech.main_texture=coll.get(n); + obj.main_texture=coll->get(n); TextureSlot tex; tex.name="texture"; - tex.texture=tech.main_texture; - tech.textures.push_back(tex); + tex.texture=obj.main_texture; + obj.textures.push_back(tex); } void Technique::Loader::texture_slot(const string &n) { TextureSlot tex; tex.name=n; - tech.textures.push_back(tex); + obj.textures.push_back(tex); } } // namespace GL diff --git a/source/technique.h b/source/technique.h index 468131f7..3bb347ea 100644 --- a/source/technique.h +++ b/source/technique.h @@ -25,20 +25,11 @@ slots which Objects must override. class Technique { public: - class Loader: public Msp::DataFile::Loader + class Loader: public Msp::DataFile::CollectionObjectLoader { - public: - typedef DataFile::Collection Collection; - - protected: - Technique &tech; - Collection &coll; - public: Loader(Technique &, Collection &); - Technique &get_object() const { return tech; } - Collection &get_collection() const { return coll; } private: virtual void finish(); void material_inline(); diff --git a/source/texture.cpp b/source/texture.cpp index 47b6ba37..e897ba92 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -111,7 +111,7 @@ void Texture::maybe_bind() const Texture::Loader::Loader(Texture &t): - tex(t) + DataFile::ObjectLoader(t) { add("min_filter", &Loader::min_filter); add("mag_filter", &Loader::mag_filter); @@ -120,17 +120,17 @@ Texture::Loader::Loader(Texture &t): void Texture::Loader::min_filter(TextureFilter f) { - tex.set_min_filter(f); + obj.set_min_filter(f); } void Texture::Loader::mag_filter(TextureFilter f) { - tex.set_mag_filter(f); + obj.set_mag_filter(f); } void Texture::Loader::generate_mipmap(bool gm) { - tex.parameter(GL_GENERATE_MIPMAP_SGIS, gm); + obj.parameter(GL_GENERATE_MIPMAP_SGIS, gm); } } // namespace GL diff --git a/source/texture.h b/source/texture.h index 48f551f0..21edb81a 100644 --- a/source/texture.h +++ b/source/texture.h @@ -9,7 +9,7 @@ Distributed under the LGPL #define MSP_GL_TEXTURE_H_ #include -#include +#include #include "gl.h" #include "types.h" @@ -37,11 +37,8 @@ one of the dimensioned texture classes. class Texture { protected: - class Loader: public DataFile::Loader + class Loader: public DataFile::ObjectLoader { - protected: - Texture &tex; - public: Loader(Texture &); void min_filter(TextureFilter); diff --git a/source/texture2d.cpp b/source/texture2d.cpp index b8f2f26b..c1f6e0e9 100644 --- a/source/texture2d.cpp +++ b/source/texture2d.cpp @@ -89,18 +89,18 @@ void Texture2D::Loader::image_data(const string &data) Graphics::Image img; img.load_memory(data.data(), data.size()); - static_cast(tex).image(img); + static_cast(obj).image(img); } void Texture2D::Loader::raw_data(const string &data) { - Texture2D &t2d=static_cast(tex);; + Texture2D &t2d=static_cast(obj); t2d.image(0, t2d.ifmt, UNSIGNED_BYTE, data.data()); } void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned b) { - static_cast(tex).storage(fmt, w, h, b); + static_cast(obj).storage(fmt, w, h, b); } } // namespace GL diff --git a/source/texture2d.h b/source/texture2d.h index f1b76783..5784645b 100644 --- a/source/texture2d.h +++ b/source/texture2d.h @@ -9,7 +9,6 @@ Distributed under the LGPL #define MSP_GL_TEXTURE2D_H_ #include -#include #include #include "pixelformat.h" #include "texture.h" -- 2.43.0