Internally keep track of the value of the current color (issue #564)

This commit is contained in:
Alex Szpakowski
2013-04-12 15:24:21 -03:00
parent ba3a363fd3
commit 3edf033498
3 changed files with 41 additions and 19 deletions
+8 -19
View File
@@ -576,21 +576,12 @@ Shader *Graphics::newShader(const Shader::ShaderSources &sources)
void Graphics::setColor(const Color &c) void Graphics::setColor(const Color &c)
{ {
glColor4ubv(&c.r); opengl::setCurrentColor(c);
} }
Color Graphics::getColor() const Color Graphics::getColor() const
{ {
float c[4]; return opengl::getCurrentColor();
glGetFloatv(GL_CURRENT_COLOR, c);
Color t;
t.r = (unsigned char)(255.0f*c[0]);
t.g = (unsigned char)(255.0f*c[1]);
t.b = (unsigned char)(255.0f*c[2]);
t.a = (unsigned char)(255.0f*c[3]);
return t;
} }
void Graphics::setBackgroundColor(const Color &c) void Graphics::setBackgroundColor(const Color &c)
@@ -1051,17 +1042,14 @@ static void draw_overdraw(Vector *overdraw, size_t count, float pixel_size, bool
// prepare colors: // prepare colors:
// even indices in overdraw* point to inner vertices => alpha = current-alpha, // even indices in overdraw* point to inner vertices => alpha = current-alpha,
// odd indices point to outer vertices => alpha = 0. // odd indices point to outer vertices => alpha = 0.
GLfloat c[4]; Color c = opengl::getCurrentColor();
glGetFloatv(GL_CURRENT_COLOR, c);
Color *colors = new Color[2*count+2]; Color *colors = new Color[2*count+2];
for (size_t i = 0; i < 2*count+2; ++i) for (size_t i = 0; i < 2*count+2; ++i)
{ {
colors[i] = Color(GLubyte(c[0] * 255.f), colors[i] = c;
GLubyte(c[1] * 255.f), // avoids branching. equiv to if (i%2 == 1) colors[i].a = 0;
GLubyte(c[2] * 255.f), colors[i].a *= GLubyte(i % 2 == 0);
// avoids branching. equiv to if (i%2 == 1) colors[i].a = 0;
GLubyte(c[3] * 255.f) * GLubyte(i%2 == 0));
} }
// draw faded out line halos // draw faded out line halos
@@ -1072,7 +1060,8 @@ static void draw_overdraw(Vector *overdraw, size_t count, float pixel_size, bool
glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_COLOR_ARRAY);
// "if GL_COLOR_ARRAY is enabled, the value of the current color is // "if GL_COLOR_ARRAY is enabled, the value of the current color is
// undefined after glDrawArrays executes" // undefined after glDrawArrays executes"
glColor4fv(c);
opengl::setCurrentColor(c);
delete[] colors; delete[] colors;
} }
+21
View File
@@ -36,6 +36,8 @@ namespace opengl
static bool contextInitialized = false; static bool contextInitialized = false;
static Color curColor;
static int curTextureUnit = 0; static int curTextureUnit = 0;
static std::vector<GLuint> textureUnits; static std::vector<GLuint> textureUnits;
@@ -48,6 +50,14 @@ void initializeContext()
contextInitialized = true; contextInitialized = true;
// Store the current color so we don't have to get it through GL later
GLfloat glcolor[4];
glGetFloatv(GL_CURRENT_COLOR, glcolor);
curColor.r = glcolor[0];
curColor.g = glcolor[1];
curColor.b = glcolor[2];
curColor.a = glcolor[3];
// initialize multiple texture unit support for shaders, if available // initialize multiple texture unit support for shaders, if available
textureUnits.clear(); textureUnits.clear();
if (Shader::isSupported()) if (Shader::isSupported())
@@ -134,6 +144,17 @@ void uninitializeContext()
contextInitialized = false; contextInitialized = false;
} }
void setCurrentColor(const Color &c)
{
glColor4ubv(&c.r);
curColor = c;
}
Color getCurrentColor()
{
return curColor;
}
void setActiveTextureUnit(int textureunit) void setActiveTextureUnit(int textureunit)
{ {
if (textureunit < 0 || (size_t) textureunit >= textureUnits.size()) if (textureunit < 0 || (size_t) textureunit >= textureUnits.size())
+12
View File
@@ -22,6 +22,8 @@
#define LOVE_GRAPHICS_OPENGL_OPENGL_H #define LOVE_GRAPHICS_OPENGL_OPENGL_H
#include "GLee.h" #include "GLee.h"
#include "graphics/Color.h"
#include "graphics/Image.h" #include "graphics/Image.h"
namespace love namespace love
@@ -42,6 +44,16 @@ void initializeContext();
**/ **/
void uninitializeContext(); void uninitializeContext();
/**
* Sets the current constant color.
**/
void setCurrentColor(const Color &c);
/**
* Gets the current constant color.
**/
Color getCurrentColor();
/** /**
* Helper for setting the active texture unit. * Helper for setting the active texture unit.
* *