From 84ffd0189e6c65ae6f109ef731e29d01a2a9a417 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 18 Jun 2016 13:25:58 +0200 Subject: [PATCH] Renderer2D: reverse the relationship between Renderer2D and Renderable. Pass Renderer2D to Renderable and have each subclass of Renderable decide what the Renderer should do (like drawText(), drawRect() etc). This makes the Renderable more flexible. right now, each renderable has a texture, vertices and indices. but what if some renderables does not use textures? or more than one? What if some renderables are just a group of other renderables? (Scene Graph's). If we instead just make Renderables implement render(Renderer2D& r) interface. we can make each renderable pass the data it holds to the renderer without a hard defined interface. --- include/Spectre/Graphics/BatchRenderer2D.h | 3 +++ include/Spectre/Graphics/Renderable.h | 3 +++ include/Spectre/Graphics/Renderer2D.h | 3 +++ include/Spectre/Graphics/Sprite.h | 2 ++ include/Spectre/Graphics/Text.h | 2 ++ source/Graphics/BatchRenderer2D.cpp | 9 +++++++-- source/Graphics/Sprite.cpp | 6 ++++++ source/Graphics/Text.cpp | 6 ++++++ 8 files changed, 32 insertions(+), 2 deletions(-) diff --git a/include/Spectre/Graphics/BatchRenderer2D.h b/include/Spectre/Graphics/BatchRenderer2D.h index 637a23f..ad49098 100644 --- a/include/Spectre/Graphics/BatchRenderer2D.h +++ b/include/Spectre/Graphics/BatchRenderer2D.h @@ -29,6 +29,9 @@ public : void submit(const Renderable2D& renderable); + // Drawing + virtual void draw(const Renderable2D* renderable); + void render(); protected : diff --git a/include/Spectre/Graphics/Renderable.h b/include/Spectre/Graphics/Renderable.h index 386c9e2..31425d7 100644 --- a/include/Spectre/Graphics/Renderable.h +++ b/include/Spectre/Graphics/Renderable.h @@ -8,6 +8,7 @@ #include #include +class Renderer2D; class ShaderProgram; class Texture; @@ -31,6 +32,8 @@ public : virtual const Texture* getTexture() const { return NULL; }; virtual const RenderType getRenderType() const = 0; + + virtual void render(Renderer2D& renderer) const = 0; }; #endif /* SPECTRE_GRAPHCIS_RENDERABLE_H */ diff --git a/include/Spectre/Graphics/Renderer2D.h b/include/Spectre/Graphics/Renderer2D.h index 2d1db4a..9690005 100644 --- a/include/Spectre/Graphics/Renderer2D.h +++ b/include/Spectre/Graphics/Renderer2D.h @@ -18,6 +18,9 @@ public : virtual void submit(const Renderable2D& renderable) = 0; + // Drawing. + virtual void draw(const Renderable2D* renderable) = 0; + virtual void render() = 0; protected : diff --git a/include/Spectre/Graphics/Sprite.h b/include/Spectre/Graphics/Sprite.h index f4c25dc..a786ec1 100644 --- a/include/Spectre/Graphics/Sprite.h +++ b/include/Spectre/Graphics/Sprite.h @@ -33,6 +33,8 @@ public : virtual const RenderType getRenderType() const { return RenderType_Scene; }; + virtual void render(Renderer2D& renderer) const; + protected : void updateGeometry(); diff --git a/include/Spectre/Graphics/Text.h b/include/Spectre/Graphics/Text.h index b02202f..bed3226 100644 --- a/include/Spectre/Graphics/Text.h +++ b/include/Spectre/Graphics/Text.h @@ -60,6 +60,8 @@ public : virtual const Texture* getTexture() const; + void render(Renderer2D& renderer) const; + private : void updateGeometry() const; diff --git a/source/Graphics/BatchRenderer2D.cpp b/source/Graphics/BatchRenderer2D.cpp index 7cf81f6..9c81a29 100644 --- a/source/Graphics/BatchRenderer2D.cpp +++ b/source/Graphics/BatchRenderer2D.cpp @@ -104,7 +104,12 @@ void BatchRenderer2D::end() void BatchRenderer2D::submit(const Renderable2D& renderable) { - m_queue.push_back(&renderable); + renderable.render(*this); +} + +void BatchRenderer2D::draw(const Renderable2D* renderable) +{ + m_queue.push_back(renderable); } void BatchRenderer2D::render() @@ -126,7 +131,7 @@ void BatchRenderer2D::render() // Setup position. glEnableVertexAttribArray(VertexAttribPosition); glVertexAttribPointer(VertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void*) 0); - + // Setup color glEnableVertexAttribArray(VertexAttribColor0); diff --git a/source/Graphics/Sprite.cpp b/source/Graphics/Sprite.cpp index 8ee6851..905dc49 100644 --- a/source/Graphics/Sprite.cpp +++ b/source/Graphics/Sprite.cpp @@ -1,4 +1,5 @@ +#include #include #include @@ -108,4 +109,9 @@ const std::vector& Sprite::getIndices() const void Sprite::updateGeometry() { +} + +void Sprite::render(Renderer2D& renderer) const +{ + renderer.draw(this); } \ No newline at end of file diff --git a/source/Graphics/Text.cpp b/source/Graphics/Text.cpp index e1ab6cd..7160c8e 100644 --- a/source/Graphics/Text.cpp +++ b/source/Graphics/Text.cpp @@ -1,5 +1,6 @@ #include +#include #include Text::Text() : @@ -167,4 +168,9 @@ void Text::updateGeometry() const int x = 0; +} + +void Text::render(Renderer2D& renderer) const +{ + renderer.draw(this); } \ No newline at end of file