From 881a31512d07093c20876be266d55535b2ee1a81 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sun, 29 Dec 2019 21:20:47 -0400 Subject: [PATCH] Remove the variant of SpriteBatch:setColor() which disables all previously set colors (resolves issue #1090). --HG-- branch : minor --- src/modules/graphics/SpriteBatch.cpp | 29 +++++++---------------- src/modules/graphics/SpriteBatch.h | 18 ++++---------- src/modules/graphics/wrap_SpriteBatch.cpp | 15 ++---------- 3 files changed, 15 insertions(+), 47 deletions(-) diff --git a/src/modules/graphics/SpriteBatch.cpp b/src/modules/graphics/SpriteBatch.cpp index 75fbc71d1..54edb13e0 100644 --- a/src/modules/graphics/SpriteBatch.cpp +++ b/src/modules/graphics/SpriteBatch.cpp @@ -45,7 +45,7 @@ SpriteBatch::SpriteBatch(Graphics *gfx, Texture *texture, int size, vertex::Usag , size(size) , next(0) , color(255, 255, 255, 255) - , color_active(false) + , colorf(1.0f, 1.0f, 1.0f, 1.0f) , array_buf(nullptr) , range_start(-1) , range_count(-1) @@ -188,27 +188,17 @@ Texture *SpriteBatch::getTexture() const void SpriteBatch::setColor(const Colorf &c) { - color_active = true; + colorf.r = std::min(std::max(c.r, 0.0f), 1.0f); + colorf.g = std::min(std::max(c.g, 0.0f), 1.0f); + colorf.b = std::min(std::max(c.b, 0.0f), 1.0f); + colorf.a = std::min(std::max(c.a, 0.0f), 1.0f); - Colorf cclamped; - cclamped.r = std::min(std::max(c.r, 0.0f), 1.0f); - cclamped.g = std::min(std::max(c.g, 0.0f), 1.0f); - cclamped.b = std::min(std::max(c.b, 0.0f), 1.0f); - cclamped.a = std::min(std::max(c.a, 0.0f), 1.0f); - - this->color = toColor32(cclamped); + color = toColor32(colorf); } -void SpriteBatch::setColor() +Colorf SpriteBatch::getColor() const { - color_active = false; - color = Color32(255, 255, 255, 255); -} - -Colorf SpriteBatch::getColor(bool &active) const -{ - active = color_active; - return toColorf(color); + return colorf; } int SpriteBatch::getCount() const @@ -337,9 +327,6 @@ void SpriteBatch::draw(Graphics *gfx, const Matrix4 &m) { buffers.set(0, array_buf, 0); attributes.setCommonFormat(vertex_format, 0); - - if (!color_active) - attributes.disable(ATTRIB_COLOR); } int activebuffers = 1; diff --git a/src/modules/graphics/SpriteBatch.h b/src/modules/graphics/SpriteBatch.h index fa8738f76..30929c30b 100644 --- a/src/modules/graphics/SpriteBatch.h +++ b/src/modules/graphics/SpriteBatch.h @@ -67,24 +67,17 @@ public: Texture *getTexture() const; /** - * Set the current color for this SpriteBatch. The sprites added - * after this call will use this color. Note that global color - * will not longer apply to the SpriteBatch if this is used. + * Set the current color for this SpriteBatch. The sprites added after this + * call will use this color. * * @param color The color to use for the following sprites. */ void setColor(const Colorf &color); - /** - * Disable per-sprite colors for this SpriteBatch. The next call to - * draw will use the global color for all sprites. - */ - void setColor(); - /** * Get the current color for this SpriteBatch. **/ - Colorf getColor(bool &active) const; + Colorf getColor() const; /** * Get the number of sprites currently in this SpriteBatch. @@ -131,10 +124,9 @@ private: // The next free element. int next; - // Current color. This color, if present, will be applied to the next - // added sprite. + // Current color. This color will be applied to the next added sprite. Color32 color; - bool color_active; + Colorf colorf; vertex::CommonFormat vertex_format; size_t vertex_stride; diff --git a/src/modules/graphics/wrap_SpriteBatch.cpp b/src/modules/graphics/wrap_SpriteBatch.cpp index 6d829bb0b..791463c37 100644 --- a/src/modules/graphics/wrap_SpriteBatch.cpp +++ b/src/modules/graphics/wrap_SpriteBatch.cpp @@ -171,12 +171,7 @@ int w_SpriteBatch_setColor(lua_State *L) SpriteBatch *t = luax_checkspritebatch(L, 1); Colorf c; - if (lua_gettop(L) <= 1) - { - t->setColor(); - return 0; - } - else if (lua_istable(L, 2)) + if (lua_istable(L, 2)) { for (int i = 1; i <= 4; i++) lua_rawgeti(L, 2, i); @@ -204,18 +199,12 @@ int w_SpriteBatch_setColor(lua_State *L) int w_SpriteBatch_getColor(lua_State *L) { SpriteBatch *t = luax_checkspritebatch(L, 1); - bool active = false; - Colorf color = t->getColor(active); - - // getColor returns null if no color is set. - if (!active) - return 0; + Colorf color = t->getColor(); lua_pushnumber(L, color.r); lua_pushnumber(L, color.g); lua_pushnumber(L, color.b); lua_pushnumber(L, color.a); - return 4; }