Commits

Alex Szpakowski  committed 6dd4a2c

Added wrapper for getting/setting the current constant color

  • Participants
  • Parent commits 7140379
  • Branches GLES2-compatibility

Comments (0)

Files changed (7)

File src/modules/graphics/opengl/Context.cpp

 	initCapabilityState();
 	initTextureState();
 
-	// we can't query enabled vertex attributes, so we'll just make sure they're all disabled
+	// making sure all vertex attributes are disabled is much easier than querying them...
 	state.enabledVertexAttribs.clear();
 	setActiveVertexAttribs(ATTRIB_NONE);
+
+	// get the current color
+	GLfloat color[4];
+	glGetFloatv(GL_CURRENT_COLOR, color);
+	state.color = Color(color[0] * 255, color[1] * 255, color[2] * 255, color[3] * 255);
 }
 
 void Context::deInitState()
 	}
 }
 
+void Context::setColor(const Color &color)
+{
+	glColor4ub(color.r, color.g, color.b, color.a);
+	state.color = color;
+}
+
+const Color &Context::getColor()
+{
+	return state.color;
+}
+
 void Context::setActiveTextureUnit(int textureunit)
 {
 	if (textureunit < 0 || (size_t) textureunit >= state.textureUnits.size())

File src/modules/graphics/opengl/Context.h

 #define LOVE_GRAPHICS_OPENGL_CONTEXT_H
 
 #include "GLee.h"
+#include "graphics/Color.h"
 #include "graphics/Image.h"
 
 #include <vector>
 	void vertexAttribPointer(VertexAttribType attrib, GLint size, GLenum type, GLsizei stride, const GLvoid *pointer);
 
 	/**
+	 * Wrapper for setting the current constant color (glColor4ub).
+	 **/
+	void setColor(const Color &color);
+
+	/**
+	 * Gets the current constant color.
+	 **/
+	const Color &getColor();
+
+	/**
 	 * Sets the active texture unit.
 	 *
 	 * @param textureunit Index in the range of [0, maxtextureunits-1]
 	void bindTextureToUnit(GLuint texture, int textureunit, bool restoreprev);
 
 	/**
+	 * Returns the number of texture units supported on the current system.
+	 **/
+	int getNumTextureUnits() { return state.textureUnits.size(); };
+
+	/**
 	 * Deletes an OpenGL texture.
 	 * Cleans up if the texture is currently bound.
 	 **/
 		std::map<unsigned int, bool> enabledVertexAttribs;
 
 		std::map<GLenum, bool> enabledCapabilities; // tracks glEnable/Disable.
-		
+
+		Color color;
+
 	} state;
 
 	// Pointer to the currently active context.

File src/modules/graphics/opengl/Graphics.cpp

 	s.color = getColor();
 	s.backgroundColor = getBackgroundColor();
 
+	Context *ctx = getContext();
+
 	s.blendMode = getBlendMode();
 	s.colorMode = getColorMode();
 	//get line style
 	//get the point size
 	glGetFloatv(GL_POINT_SIZE, &s.pointSize);
 	//get point style
-	s.pointStyle = (glIsEnabled(GL_POINT_SMOOTH) == GL_TRUE) ? Graphics::POINT_SMOOTH : Graphics::POINT_ROUGH;
+	s.pointStyle = ctx->getCapability(GL_POINT_SMOOTH) ? Graphics::POINT_SMOOTH : Graphics::POINT_ROUGH;
 	//get scissor status
-	s.scissor = (glIsEnabled(GL_SCISSOR_TEST) == GL_TRUE);
+	s.scissor = ctx->getCapability(GL_SCISSOR_TEST);
 	//do we have scissor, if so, store the box
 	if (s.scissor)
 		glGetIntegerv(GL_SCISSOR_BOX, s.scissorBox);
 
 int Graphics::getScissor(lua_State *L)
 {
-	if (glIsEnabled(GL_SCISSOR_TEST) == GL_FALSE)
+	if (!getContext()->getCapability(GL_SCISSOR_TEST))
 		return 0;
 
 	GLint scissor[4];
 
 void Graphics::setColor(const Color &c)
 {
-	glColor4ubv(&c.r);
+	getContext()->setColor(c);
 }
 
 Color Graphics::getColor()
 {
-	float c[4];
-	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;
+	return getContext()->getColor();
 }
 
 void Graphics::setBackgroundColor(const Color &c)
 
 Graphics::PointStyle Graphics::getPointStyle()
 {
-	if (glIsEnabled(GL_POINT_SMOOTH) == GL_TRUE)
+	if (getContext()->getCapability(GL_POINT_SMOOTH))
 		return POINT_SMOOTH;
 	else
 		return POINT_ROUGH;
 	// prepare colors:
 	// even indices in overdraw* point to inner vertices => alpha = current-alpha,
 	// odd indices point to outer vertices => alpha = 0.
-	GLfloat c[4];
-	glGetFloatv(GL_CURRENT_COLOR, c);
+	const Color &c = ctx->getColor();
 
 	Color *colors = new Color[2*count+2];
 	for (size_t i = 0; i < 2*count+2; ++i)
 	{
-		colors[i] = Color(GLubyte(c[0] * 255.f),
-						  GLubyte(c[1] * 255.f),
-						  GLubyte(c[2] * 255.f),
+		colors[i] = Color(c.r,
+						  c.g,
+						  c.b,
 						  // avoids branching. equiv to if (i%2 == 1) colors[i].a = 0;
-						  GLubyte(c[3] * 255.f) * GLubyte(i%2 == 0));
+						  c.a * GLubyte(i%2 == 0));
 	}
 
 	// draw faded out line halos
 	ctx->setVertexAttribState(Context::ATTRIB_COLOR, true);
-	
+
 	ctx->vertexAttribPointer(Context::ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, 0, colors);
 	ctx->vertexAttribPointer(Context::ATTRIB_VERTEX, 2, GL_FLOAT, 0, (const GLvoid *)overdraw);
-	
+
 	glDrawArrays(GL_TRIANGLE_STRIP, 0, 2*count + 2 * int(!looping));
 
 	// "if GL_COLOR_ARRAY is enabled, the value of the current color is
 	// undefined after glDrawArrays executes"
-	glColor4fv(c);
+	ctx->setColor(ctx->getColor());
 
 	delete[] colors;
 }

File src/modules/graphics/opengl/Image.cpp

 	: width((float)(data->getWidth()))
 	, height((float)(data->getHeight()))
 	, texture(0)
-	, mipmapsharpness(0.0f)
+	, mipmapSharpness(0.0f)
 {
 	data->retain();
 	this->data = data;
 		return;
 
 	// LOD bias has the range (-maxbias, maxbias)
-	mipmapsharpness = std::min(std::max(sharpness, -maxmipmapsharpness + 0.01f), maxmipmapsharpness - 0.01f);
+	mipmapSharpness = std::min(std::max(sharpness, -maxMipmapSharpness + 0.01f), maxMipmapSharpness - 0.01f);
 
 	bind();
-	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, -mipmapsharpness); // negative bias is sharper
+	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, -mipmapSharpness); // negative bias is sharper
 }
 
 float Image::getMipmapSharpness() const
 {
-	return mipmapsharpness;
+	return mipmapSharpness;
 }
 
 void Image::bind() const
 bool Image::loadVolatile()
 {
 	if (hasMipmapSharpnessSupport())
-		glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS, &maxmipmapsharpness);
+		glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS, &maxMipmapSharpness);
 
 	if (hasNpot())
 		return loadVolatileNPOT();
 					GL_UNSIGNED_BYTE,
 					data->getData());
 
-	setMipmapSharpness(mipmapsharpness);
+	setMipmapSharpness(mipmapSharpness);
 	setFilter(filter);
 	setWrap(wrap);
 
 				 GL_UNSIGNED_BYTE,
 				 data->getData());
 
-	setMipmapSharpness(mipmapsharpness);
+	setMipmapSharpness(mipmapSharpness);
 	setFilter(filter);
 	setWrap(wrap);
 

File src/modules/graphics/opengl/Image.h

 	{
 		return texture;
 	}
+
+	bool loadVolatilePOT();
+	bool loadVolatileNPOT();
+
+	void checkMipmapsCreated() const;
+
 	// The ImageData from which the texture is created.
 	love::image::ImageData *data;
 
 	vertex vertices[4];
 
 	// Mipmap texture LOD bias value
-	float mipmapsharpness;
+	float mipmapSharpness;
 
 	// Implementation-dependent maximum/minimum mipmap sharpness values
-	float maxmipmapsharpness;
+	float maxMipmapSharpness;
 
 	// The image's filter mode
 	Image::Filter filter;
 	// The image's wrap mode
 	Image::Wrap wrap;
 
-	bool loadVolatilePOT();
-	bool loadVolatileNPOT();
-
-	void checkMipmapsCreated() const;
-
 }; // Image
 
 } // opengl

File src/modules/graphics/opengl/ParticleSystem.cpp

 	int numParticles = count();
 	if (numParticles == 0) return; // don't bother if there's nothing to do
 
-	glPushAttrib(GL_CURRENT_BIT);
-
 	spriteBatch->clear();
 	spriteBatch->lock();
 
 	}
 
 	spriteBatch->unlock();
-
-	sprite->bind();
 	spriteBatch->draw(x, y, angle, sx, sy, ox, oy, kx, ky);
-
-	glPopAttrib();
 }
 
 void ParticleSystem::update(float dt)

File src/modules/graphics/opengl/SpriteBatch.cpp

 
 	glDrawElements(GL_TRIANGLES, element_buf->getIndexCount(next), element_buf->getType(), element_buf->getPointer(0));
 
+	// Current color is undefined after drawing a vertex array with the color attribute.
+	if (color)
+		ctx->setColor(ctx->getColor());
+
 	glPopMatrix();
 }