From: Mikko Rasa Date: Thu, 9 Dec 2010 20:57:30 +0000 (+0000) Subject: Change Scene into an abstract base class and add a few subclasses X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=9addd3d476245415244e59333a36a8fc0eae42bf;p=libs%2Fgl.git Change Scene into an abstract base class and add a few subclasses --- diff --git a/source/instancescene.cpp b/source/instancescene.cpp new file mode 100644 index 00000000..e3e63767 --- /dev/null +++ b/source/instancescene.cpp @@ -0,0 +1,49 @@ +/* $Id$ + +This file is part of libmspgl +Copyright © 2007-2008, 2010 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#include "object.h" +#include "objectinstance.h" +#include "instancescene.h" + +namespace Msp { +namespace GL { + +void InstanceScene::add(const Renderable &r) +{ + if(const ObjectInstance *oi = dynamic_cast(&r)) + objects[&oi->get_object()].insert(oi); + else + renderables.insert(&r); +} + +void InstanceScene::remove(const Renderable &r) +{ + if(const ObjectInstance *oi = dynamic_cast(&r)) + { + ObjectMap::iterator i = objects.find(&oi->get_object()); + if(i!=objects.end()) + { + i->second.erase(oi); + if(i->second.empty()) + objects.erase(i); + } + } + else + renderables.erase(&r); +} + +void InstanceScene::render(const Tag &tag) const +{ + for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i) + i->first->render(i->second.begin(), i->second.end(), tag); + + for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i) + (*i)->render(tag); +} + +} // namespace GL +} // namespace Msp diff --git a/source/instancescene.h b/source/instancescene.h new file mode 100644 index 00000000..1f79d767 --- /dev/null +++ b/source/instancescene.h @@ -0,0 +1,45 @@ +/* $Id$ + +This file is part of libmspgl +Copyright © 2007-2008, 2010 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#ifndef MSP_GL_INSTANCESCENE_H_ +#define MSP_GL_INSTANCESCENE_H_ + +#include +#include +#include "scene.h" + +namespace Msp { +namespace GL { + +class Object; +class ObjectInstance; + +/** +A Scene optimized for rendering ObjectInstances. All instances of the same +Object are rendered in one go; otherwise the rendering order is unspecified. +*/ +class InstanceScene: public Scene +{ +private: + typedef std::set InstanceSet; + typedef std::map ObjectMap; + typedef std::set RenderableSet; + + ObjectMap objects; + RenderableSet renderables; + +public: + virtual void add(const Renderable &); + virtual void remove(const Renderable &); + + virtual void render(const Tag &tag = Tag()) const; +}; + +} // namespace GL +} // namespace Msp + +#endif diff --git a/source/orderedscene.cpp b/source/orderedscene.cpp new file mode 100644 index 00000000..38edf696 --- /dev/null +++ b/source/orderedscene.cpp @@ -0,0 +1,51 @@ +/* $Id$ + +This file is part of libmspgl +Copyright © 2010 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#include +#include "orderedscene.h" + +namespace Msp { +namespace GL { + +void OrderedScene::add(const Renderable &r) +{ + renderables.push_back(&r); +} + +void OrderedScene::remove(const Renderable &r) +{ + RenderableList::iterator end = std::remove(renderables.begin(), renderables.end(), &r); + renderables.erase(end, renderables.end()); +} + +void OrderedScene::prepend(const Renderable &r) +{ + renderables.push_front(&r); +} + +void OrderedScene::insert(unsigned index, const Renderable &r) +{ + RenderableList::iterator i = renderables.begin(); + for(; (i!=renderables.end() && index); ++i, --index) ; + renderables.insert(i, &r); +} + +void OrderedScene::insert_after(const Renderable &after, const Renderable &r) +{ + RenderableList::iterator i = renderables.begin(); + for(; (i!=renderables.end() && *i!=&after); ++i) ; + renderables.insert(i, &r); +} + +void OrderedScene::render(const Tag &tag) const +{ + for(RenderableList::const_iterator i=renderables.begin(); i!=renderables.end(); ++i) + (*i)->render(tag); +} + +} // namespace GL +} // namespace Msp diff --git a/source/orderedscene.h b/source/orderedscene.h new file mode 100644 index 00000000..6bb12192 --- /dev/null +++ b/source/orderedscene.h @@ -0,0 +1,41 @@ +/* $Id$ + +This file is part of libmspgl +Copyright © 2010 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#ifndef MSP_GL_ORDEREDSCENE_H_ +#define MSP_GL_ORDEREDSCENE_H_ + +#include +#include + +namespace Msp { +namespace GL { + +/** +A scene that renders its contents in a specific order. Inserting Renderables +in the middle and removing them are O(N) operations. +*/ +class OrderedScene: public Scene +{ +private: + typedef std::list RenderableList; + + RenderableList renderables; + +public: + virtual void add(const Renderable &); + virtual void remove(const Renderable &); + void prepend(const Renderable &); + void insert(unsigned, const Renderable &); + void insert_after(const Renderable &, const Renderable &); + + virtual void render(const Tag & = Tag()) const; +}; + +} // namespace GL +} // namespace Msp + +#endif diff --git a/source/scene.cpp b/source/scene.cpp deleted file mode 100644 index 1e72d5dc..00000000 --- a/source/scene.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - -#include "object.h" -#include "objectinstance.h" -#include "scene.h" - -namespace Msp { -namespace GL { - -void Scene::add(const Renderable &r) -{ - if(const ObjectInstance *oi = dynamic_cast(&r)) - objects[&oi->get_object()].insert(oi); - else - renderables.insert(&r); -} - -void Scene::remove(const Renderable &r) -{ - if(const ObjectInstance *oi = dynamic_cast(&r)) - { - ObjectMap::iterator i = objects.find(&oi->get_object()); - if(i!=objects.end()) - { - i->second.erase(oi); - if(i->second.empty()) - objects.erase(i); - } - } - else - renderables.erase(&r); -} - -void Scene::render(const Tag &tag) const -{ - for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i) - i->first->render(i->second.begin(), i->second.end(), tag); - - for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i) - (*i)->render(tag); -} - -} // namespace GL -} // namespace Msp diff --git a/source/scene.h b/source/scene.h index 329099ea..92a4a171 100644 --- a/source/scene.h +++ b/source/scene.h @@ -1,38 +1,32 @@ /* $Id$ This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2010 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ #ifndef MSP_GL_SCENE_H_ #define MSP_GL_SCENE_H_ -#include -#include #include "renderable.h" namespace Msp { namespace GL { -class Object; -class ObjectInstance; - +/** +Scenes are containers for other Renderables. This is a base class that can't +be instantiated. At the moment the available Scene types are SimpleScene, +InstancedScene and OrderedScene. +*/ class Scene: public Renderable { -private: - typedef std::set InstanceSet; - typedef std::map ObjectMap; - typedef std::set RenderableSet; - - ObjectMap objects; - RenderableSet renderables; - +protected: + Scene() { } public: - void add(const Renderable &); - void remove(const Renderable &); + virtual ~Scene() { } - virtual void render(const Tag &tag = Tag()) const; + virtual void add(const Renderable &) = 0; + virtual void remove(const Renderable &) = 0; }; } // namespace GL diff --git a/source/simplescene.cpp b/source/simplescene.cpp new file mode 100644 index 00000000..b410374e --- /dev/null +++ b/source/simplescene.cpp @@ -0,0 +1,30 @@ +/* $Id$ + +This file is part of libmspgl +Copyright © 2010 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#include "simplescene.h" + +namespace Msp { +namespace GL { + +void SimpleScene::add(const Renderable &r) +{ + renderables.insert(&r); +} + +void SimpleScene::remove(const Renderable &r) +{ + renderables.erase(&r); +} + +void SimpleScene::render(const Tag &tag) const +{ + for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i) + (*i)->render(tag); +} + +} // namespace GL +} // namespace Msp diff --git a/source/simplescene.h b/source/simplescene.h new file mode 100644 index 00000000..b28b5c0c --- /dev/null +++ b/source/simplescene.h @@ -0,0 +1,37 @@ +/* $Id$ + +This file is part of libmspgl +Copyright © 2010 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#ifndef MSP_GL_SIMPLESCENE_H_ +#define MSP_GL_SIMPLESCENE_H_ + +#include +#include "scene.h" + +namespace Msp { +namespace GL { + +/** +The simplest possible Scene. Rendering order is unspecified. +*/ +class SimpleScene: public Scene +{ +private: + typedef std::set RenderableSet; + + RenderableSet renderables; + +public: + virtual void add(const Renderable &); + virtual void remove(const Renderable &); + + virtual void render(const Tag &tag = Tag()) const; +}; + +} // namespace GL +} // namespace Msp + +#endif