From b8762554421feb68f6be38680da8d897c46b1ba4 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 24 Jan 2015 03:39:05 -0400 Subject: [PATCH] Cleaned up some graphics code. --- src/modules/graphics/Graphics.h | 18 +++++++++++ src/modules/graphics/opengl/Graphics.cpp | 31 ++++++------------- src/modules/graphics/opengl/Graphics.h | 7 ++--- src/modules/graphics/opengl/wrap_Graphics.cpp | 18 ++++++----- 4 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src/modules/graphics/Graphics.h b/src/modules/graphics/Graphics.h index a34919583..a40bba16d 100644 --- a/src/modules/graphics/Graphics.h +++ b/src/modules/graphics/Graphics.h @@ -148,6 +148,24 @@ public: 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(); // Implements Module. diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index bc8524da3..df4acbd49 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/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) setWireframe(s.wireframe); @@ -216,7 +210,7 @@ bool Graphics::setMode(int width, int height, bool &sRGB) glEnable(GL_BLEND); // Enable all color component writes. - bool colormask[] = {true, true, true, true}; + ColorMask colormask = {true, true, true, true}; setColorMask(colormask); // Enable line/point smoothing. @@ -727,15 +721,12 @@ std::vector Graphics::getCanvas() const 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; } @@ -1347,14 +1338,12 @@ Graphics::DisplayState::DisplayState() , scissorBox() , font(nullptr) , shader(nullptr) + , colorMask{true, true, true, true} , wireframe(false) , defaultFilter() , defaultMipmapFilter(Texture::FILTER_NONE) , 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) @@ -1371,13 +1360,12 @@ Graphics::DisplayState::DisplayState(const DisplayState &other) , font(other.font) , shader(other.shader) , canvases(other.canvases) + , colorMask(other.colorMask) , wireframe(other.wireframe) , defaultFilter(other.defaultFilter) , defaultMipmapFilter(other.defaultMipmapFilter) , defaultMipmapSharpness(other.defaultMipmapSharpness) { - for (int i = 0; i < 4; i++) - colorMask[i] = other.colorMask[i]; } Graphics::DisplayState::~DisplayState() @@ -1401,8 +1389,7 @@ Graphics::DisplayState &Graphics::DisplayState::operator = (const DisplayState & shader = other.shader; canvases = other.canvases; - for (int i = 0; i < 4; i++) - colorMask[i] = other.colorMask[i]; + colorMask = other.colorMask; wireframe = other.wireframe; diff --git a/src/modules/graphics/opengl/Graphics.h b/src/modules/graphics/opengl/Graphics.h index befb5c68c..c0145439d 100644 --- a/src/modules/graphics/opengl/Graphics.h +++ b/src/modules/graphics/opengl/Graphics.h @@ -215,13 +215,12 @@ public: /** * Sets the enabled color components when rendering. **/ - void setColorMask(const bool mask[4]); + void setColorMask(ColorMask 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. @@ -470,7 +469,7 @@ private: std::vector> canvases; // Color mask. - bool colorMask[4]; + ColorMask colorMask; bool wireframe; diff --git a/src/modules/graphics/opengl/wrap_Graphics.cpp b/src/modules/graphics/opengl/wrap_Graphics.cpp index 2790a8e34..e6e80bd2f 100644 --- a/src/modules/graphics/opengl/wrap_Graphics.cpp +++ b/src/modules/graphics/opengl/wrap_Graphics.cpp @@ -654,17 +654,19 @@ int w_getFont(lua_State *L) int w_setColorMask(lua_State *L) { - bool mask[4]; + Graphics::ColorMask mask; if (lua_gettop(L) <= 1 && lua_isnoneornil(L, 1)) { // 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 { - 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); @@ -674,10 +676,12 @@ int w_setColorMask(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; }