Browse Source

Cleaned up some graphics code.

Alex Szpakowski 10 years ago
parent
commit
b876255442

+ 18 - 0
src/modules/graphics/Graphics.h

@@ -148,6 +148,24 @@ public:
 		size_t textureMemory;
 		size_t textureMemory;
 	};
 	};
 
 
+	struct ColorMask
+	{
+		bool r;
+		bool g;
+		bool b;
+		bool a;
+
+		bool operator == (const ColorMask &m) const
+		{
+			return r == m.r && g == m.g && b == m.b && a == m.a;
+		}
+
+		bool operator != (const ColorMask &m) const
+		{
+			return !(operator == (m));
+		}
+	};
+
 	virtual ~Graphics();
 	virtual ~Graphics();
 
 
 	// Implements Module.
 	// Implements Module.

+ 9 - 22
src/modules/graphics/opengl/Graphics.cpp

@@ -153,14 +153,8 @@ void Graphics::restoreStateChecked(const DisplayState &s)
 		}
 		}
 	}
 	}
 
 
-	for (int i = 0; i < 4; i++)
-	{
-		if (s.colorMask[i] != cur.colorMask[i])
-		{
-			setColorMask(s.colorMask);
-			break;
-		}
-	}
+	if (s.colorMask != cur.colorMask)
+		setColorMask(s.colorMask);
 
 
 	if (s.wireframe != cur.wireframe)
 	if (s.wireframe != cur.wireframe)
 		setWireframe(s.wireframe);
 		setWireframe(s.wireframe);
@@ -216,7 +210,7 @@ bool Graphics::setMode(int width, int height, bool &sRGB)
 	glEnable(GL_BLEND);
 	glEnable(GL_BLEND);
 
 
 	// Enable all color component writes.
 	// Enable all color component writes.
-	bool colormask[] = {true, true, true, true};
+	ColorMask colormask = {true, true, true, true};
 	setColorMask(colormask);
 	setColorMask(colormask);
 
 
 	// Enable line/point smoothing.
 	// Enable line/point smoothing.
@@ -727,15 +721,12 @@ std::vector<Canvas *> Graphics::getCanvas() const
 	return canvases;
 	return canvases;
 }
 }
 
 
-void Graphics::setColorMask(const bool mask[4])
+void Graphics::setColorMask(ColorMask mask)
 {
 {
-	for (int i = 0; i < 4; i++)
-		states.back().colorMask[i] = mask[i];
-
-	glColorMask(mask[0], mask[1], mask[2], mask[3]);
+	glColorMask(mask.r, mask.g, mask.b, mask.a);
 }
 }
 
 
-const bool *Graphics::getColorMask() const
+Graphics::ColorMask Graphics::getColorMask() const
 {
 {
 	return states.back().colorMask;
 	return states.back().colorMask;
 }
 }
@@ -1347,14 +1338,12 @@ Graphics::DisplayState::DisplayState()
 	, scissorBox()
 	, scissorBox()
 	, font(nullptr)
 	, font(nullptr)
 	, shader(nullptr)
 	, shader(nullptr)
+	, colorMask{true, true, true, true}
 	, wireframe(false)
 	, wireframe(false)
 	, defaultFilter()
 	, defaultFilter()
 	, defaultMipmapFilter(Texture::FILTER_NONE)
 	, defaultMipmapFilter(Texture::FILTER_NONE)
 	, defaultMipmapSharpness(0.0f)
 	, defaultMipmapSharpness(0.0f)
 {
 {
-	// We should just directly initialize the array in the initializer list, but
-	// that feature of C++11 is broken in Visual Studio 2013...
-	colorMask[0] = colorMask[1] = colorMask[2] = colorMask[3] = true;
 }
 }
 
 
 Graphics::DisplayState::DisplayState(const DisplayState &other)
 Graphics::DisplayState::DisplayState(const DisplayState &other)
@@ -1371,13 +1360,12 @@ Graphics::DisplayState::DisplayState(const DisplayState &other)
 	, font(other.font)
 	, font(other.font)
 	, shader(other.shader)
 	, shader(other.shader)
 	, canvases(other.canvases)
 	, canvases(other.canvases)
+	, colorMask(other.colorMask)
 	, wireframe(other.wireframe)
 	, wireframe(other.wireframe)
 	, defaultFilter(other.defaultFilter)
 	, defaultFilter(other.defaultFilter)
 	, defaultMipmapFilter(other.defaultMipmapFilter)
 	, defaultMipmapFilter(other.defaultMipmapFilter)
 	, defaultMipmapSharpness(other.defaultMipmapSharpness)
 	, defaultMipmapSharpness(other.defaultMipmapSharpness)
 {
 {
-	for (int i = 0; i < 4; i++)
-		colorMask[i] = other.colorMask[i];
 }
 }
 
 
 Graphics::DisplayState::~DisplayState()
 Graphics::DisplayState::~DisplayState()
@@ -1401,8 +1389,7 @@ Graphics::DisplayState &Graphics::DisplayState::operator = (const DisplayState &
 	shader = other.shader;
 	shader = other.shader;
 	canvases = other.canvases;
 	canvases = other.canvases;
 
 
-	for (int i = 0; i < 4; i++)
-		colorMask[i] = other.colorMask[i];
+	colorMask = other.colorMask;
 
 
 	wireframe = other.wireframe;
 	wireframe = other.wireframe;
 
 

+ 3 - 4
src/modules/graphics/opengl/Graphics.h

@@ -215,13 +215,12 @@ public:
 	/**
 	/**
 	 * Sets the enabled color components when rendering.
 	 * Sets the enabled color components when rendering.
 	 **/
 	 **/
-	void setColorMask(const bool mask[4]);
+	void setColorMask(ColorMask mask);
 
 
 	/**
 	/**
 	 * Gets the current color mask.
 	 * Gets the current color mask.
-	 * Returns an array of 4 booleans representing the mask.
 	 **/
 	 **/
-	const bool *getColorMask() const;
+	ColorMask getColorMask() const;
 
 
 	/**
 	/**
 	 * Sets the current blend mode.
 	 * Sets the current blend mode.
@@ -470,7 +469,7 @@ private:
 		std::vector<Object::StrongRef<Canvas>> canvases;
 		std::vector<Object::StrongRef<Canvas>> canvases;
 
 
 		// Color mask.
 		// Color mask.
-		bool colorMask[4];
+		ColorMask colorMask;
 
 
 		bool wireframe;
 		bool wireframe;
 
 

+ 11 - 7
src/modules/graphics/opengl/wrap_Graphics.cpp

@@ -654,17 +654,19 @@ int w_getFont(lua_State *L)
 
 
 int w_setColorMask(lua_State *L)
 int w_setColorMask(lua_State *L)
 {
 {
-	bool mask[4];
+	Graphics::ColorMask mask;
 
 
 	if (lua_gettop(L) <= 1 && lua_isnoneornil(L, 1))
 	if (lua_gettop(L) <= 1 && lua_isnoneornil(L, 1))
 	{
 	{
 		// Enable all color components if no argument is given.
 		// Enable all color components if no argument is given.
-		mask[0] = mask[1] = mask[2] = mask[3] = true;
+		mask.r = mask.g = mask.b = mask.a = true;
 	}
 	}
 	else
 	else
 	{
 	{
-		for (int i = 0; i < 4; i++)
-			mask[i] = luax_toboolean(L, i + 1);
+		mask.r = luax_toboolean(L, 1);
+		mask.g = luax_toboolean(L, 2);
+		mask.b = luax_toboolean(L, 3);
+		mask.a = luax_toboolean(L, 4);
 	}
 	}
 
 
 	instance()->setColorMask(mask);
 	instance()->setColorMask(mask);
@@ -674,10 +676,12 @@ int w_setColorMask(lua_State *L)
 
 
 int w_getColorMask(lua_State *L)
 int w_getColorMask(lua_State *L)
 {
 {
-	const bool *mask = instance()->getColorMask();
+	Graphics::ColorMask mask = instance()->getColorMask();
 
 
-	for (int i = 0; i < 4; i++)
-		luax_pushboolean(L, mask[i]);
+	luax_pushboolean(L, mask.r);
+	luax_pushboolean(L, mask.g);
+	luax_pushboolean(L, mask.b);
+	luax_pushboolean(L, mask.a);
 
 
 	return 4;
 	return 4;
 }
 }