From 9addd3d476245415244e59333a36a8fc0eae42bf Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 9 Dec 2010 20:57:30 +0000 Subject: [PATCH] Change Scene into an abstract base class and add a few subclasses --- source/{scene.cpp => instancescene.cpp} | 10 ++--- source/instancescene.h | 45 ++++++++++++++++++++++ source/orderedscene.cpp | 51 +++++++++++++++++++++++++ source/orderedscene.h | 41 ++++++++++++++++++++ source/scene.h | 28 ++++++-------- source/simplescene.cpp | 30 +++++++++++++++ source/simplescene.h | 37 ++++++++++++++++++ 7 files changed, 220 insertions(+), 22 deletions(-) rename source/{scene.cpp => instancescene.cpp} (78%) create mode 100644 source/instancescene.h create mode 100644 source/orderedscene.cpp create mode 100644 source/orderedscene.h create mode 100644 source/simplescene.cpp create mode 100644 source/simplescene.h diff --git a/source/scene.cpp b/source/instancescene.cpp similarity index 78% rename from source/scene.cpp rename to source/instancescene.cpp index 1e72d5dc..e3e63767 100644 --- a/source/scene.cpp +++ b/source/instancescene.cpp @@ -1,18 +1,18 @@ /* $Id$ This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2007-2008, 2010 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ #include "object.h" #include "objectinstance.h" -#include "scene.h" +#include "instancescene.h" namespace Msp { namespace GL { -void Scene::add(const Renderable &r) +void InstanceScene::add(const Renderable &r) { if(const ObjectInstance *oi = dynamic_cast(&r)) objects[&oi->get_object()].insert(oi); @@ -20,7 +20,7 @@ void Scene::add(const Renderable &r) renderables.insert(&r); } -void Scene::remove(const Renderable &r) +void InstanceScene::remove(const Renderable &r) { if(const ObjectInstance *oi = dynamic_cast(&r)) { @@ -36,7 +36,7 @@ void Scene::remove(const Renderable &r) renderables.erase(&r); } -void Scene::render(const Tag &tag) const +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); 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.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 -- 2.43.0