Cleaned up some graphics code.

This commit is contained in:
Alex Szpakowski
2015-01-24 03:39:05 -04:00
parent e71c15e650
commit b876255442
4 changed files with 41 additions and 33 deletions
+18
View File
@@ -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.
+9 -22
View File
@@ -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<Canvas *> 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;
+3 -4
View File
@@ -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<Object::StrongRef<Canvas>> canvases;
// Color mask.
bool colorMask[4];
ColorMask colorMask;
bool wireframe;
+11 -7
View File
@@ -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;
}