Browse Source

Remove the variant of SpriteBatch:setColor() which disables all previously set colors (resolves issue #1090).

--HG--
branch : minor
Alex Szpakowski 5 years ago
parent
commit
881a31512d

+ 8 - 21
src/modules/graphics/SpriteBatch.cpp

@@ -45,7 +45,7 @@ SpriteBatch::SpriteBatch(Graphics *gfx, Texture *texture, int size, vertex::Usag
 	, size(size)
 	, size(size)
 	, next(0)
 	, next(0)
 	, color(255, 255, 255, 255)
 	, color(255, 255, 255, 255)
-	, color_active(false)
+	, colorf(1.0f, 1.0f, 1.0f, 1.0f)
 	, array_buf(nullptr)
 	, array_buf(nullptr)
 	, range_start(-1)
 	, range_start(-1)
 	, range_count(-1)
 	, range_count(-1)
@@ -188,27 +188,17 @@ Texture *SpriteBatch::getTexture() const
 
 
 void SpriteBatch::setColor(const Colorf &c)
 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
 int SpriteBatch::getCount() const
@@ -337,9 +327,6 @@ void SpriteBatch::draw(Graphics *gfx, const Matrix4 &m)
 	{
 	{
 		buffers.set(0, array_buf, 0);
 		buffers.set(0, array_buf, 0);
 		attributes.setCommonFormat(vertex_format, 0);
 		attributes.setCommonFormat(vertex_format, 0);
-
-		if (!color_active)
-			attributes.disable(ATTRIB_COLOR);
 	}
 	}
 
 
 	int activebuffers = 1;
 	int activebuffers = 1;

+ 5 - 13
src/modules/graphics/SpriteBatch.h

@@ -67,24 +67,17 @@ public:
 	Texture *getTexture() const;
 	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.
 	 * @param color The color to use for the following sprites.
 	 */
 	 */
 	void setColor(const Colorf &color);
 	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.
 	 * Get the current color for this SpriteBatch.
 	 **/
 	 **/
-	Colorf getColor(bool &active) const;
+	Colorf getColor() const;
 
 
 	/**
 	/**
 	 * Get the number of sprites currently in this SpriteBatch.
 	 * Get the number of sprites currently in this SpriteBatch.
@@ -131,10 +124,9 @@ private:
 	// The next free element.
 	// The next free element.
 	int next;
 	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;
 	Color32 color;
-	bool color_active;
+	Colorf colorf;
 
 
 	vertex::CommonFormat vertex_format;
 	vertex::CommonFormat vertex_format;
 	size_t vertex_stride;
 	size_t vertex_stride;

+ 2 - 13
src/modules/graphics/wrap_SpriteBatch.cpp

@@ -171,12 +171,7 @@ int w_SpriteBatch_setColor(lua_State *L)
 	SpriteBatch *t = luax_checkspritebatch(L, 1);
 	SpriteBatch *t = luax_checkspritebatch(L, 1);
 	Colorf c;
 	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++)
 		for (int i = 1; i <= 4; i++)
 			lua_rawgeti(L, 2, i);
 			lua_rawgeti(L, 2, i);
@@ -204,18 +199,12 @@ int w_SpriteBatch_setColor(lua_State *L)
 int w_SpriteBatch_getColor(lua_State *L)
 int w_SpriteBatch_getColor(lua_State *L)
 {
 {
 	SpriteBatch *t = luax_checkspritebatch(L, 1);
 	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.r);
 	lua_pushnumber(L, color.g);
 	lua_pushnumber(L, color.g);
 	lua_pushnumber(L, color.b);
 	lua_pushnumber(L, color.b);
 	lua_pushnumber(L, color.a);
 	lua_pushnumber(L, color.a);
-
 	return 4;
 	return 4;
 }
 }