Commits

Alex Szpakowski  committed 0c1b7c0

Added wrapper functions for glEnable/Disable and glIsEnabled

  • Participants
  • Parent commits 9448187
  • Branches GLES2-compatibility

Comments (0)

Files changed (3)

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

 	deInitState();
 }
 
-void Context::initState()
+void Context::initCapabilityState()
+{
+	state.enabledCapabilities.clear();
+
+	// getCapability starts tracking the state if the capability isn't in the map yet.
+	getCapability(GL_BLEND);
+	getCapability(GL_TEXTURE_2D);
+	getCapability(GL_SCISSOR_TEST);
+	getCapability(GL_STENCIL_TEST);
+	getCapability(GL_SCISSOR_TEST);
+	getCapability(GL_POINT_SMOOTH);
+	getCapability(GL_MULTISAMPLE);
+}
+
+void Context::initTextureState()
 {
 	state.textureUnits.clear();
 
 
 		glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint *) &state.textureUnits[0]);
 	}
+}
+
+void Context::initState()
+{
+	initCapabilityState();
+	initTextureState();
 
 	// we can't query enabled vertex attributes, so we'll just make sure they're all disabled
 	state.enabledVertexAttribs.clear();
 	bindTexture(curtexture);
 }
 
+void Context::setCapability(GLenum capability, bool enable)
+{
+	// is this capability already set to the state we want?
+	std::map<GLenum, bool>::const_iterator cap = state.enabledCapabilities.find(capability);
+	if (cap != state.enabledCapabilities.end() && cap->second == enable)
+		return;
+
+	state.enabledCapabilities[capability] = enable;
+
+	// TODO: ignore certain capabilities on ES2?
+
+	if (enable)
+		glEnable(capability);
+	else
+		glDisable(capability);
+}
+
+bool Context::getCapability(GLenum capability)
+{
+	std::map<GLenum, bool>::const_iterator cap = state.enabledCapabilities.find(capability);
+	if (cap != state.enabledCapabilities.end())
+		return cap->second;
+
+	bool isenabled = glIsEnabled(capability) == GL_TRUE;
+	state.enabledCapabilities[capability] = isenabled;
+
+	return isenabled;
+}
+
 void Context::setVertexAttribState(unsigned int attrib, bool enable)
 {
 	if (attrib == ATTRIB_NONE)

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

 	void deInitState();
 
 	/**
+	 * Wrapper for glEnable/glDisable, enables or disables the specified OpenGL
+	 * capability.
+	 **/
+	void setCapability(GLenum capability, bool enable);
+
+	/**
+	 * Wrapper for glIsEnabled, returns whether the specified OpenGL capability
+	 * is enabled.
+	 **/
+	bool getCapability(GLenum capability);
+
+	/**
 	 * Sets the currently active vertex attributes used when rendering a vertex
 	 * array.
 	 *
 	 **/
 	void setVertexAttribState(unsigned int attrib, bool enable);
 
+	/**
+	 * Define an array of vertex attribute data for a specific vertex attribute.
+	 * See http://www.opengl.org/sdk/docs/man2/xhtml/glVertexAttribPointer.xml
+	 *
+	 * @param attrib The attribute type to define the array for.
+	 * @param size The number of vector components per attribute.
+	 * @param type The data type of each component in the array, e.g. GL_FLOAT.
+	 * @param stride Offset in bytes between consecutive vertex attributes.
+	 * @param pointer Pointer to the attribute array, or byte offset into the
+	 * currently bound vertex buffer if applicable.
+	 **/
 	void vertexAttribPointer(VertexAttribType attrib, GLint size, GLenum type, GLsizei stride, const GLvoid *pointer);
 
 	/**
 
 private:
 
+	void initCapabilityState();
+	void initTextureState();
 	void createDefaultTexture();
 
-	// map of standard vertex attributes to internal OpenGL attribute indices
+	// Map of standard vertex attributes to internal OpenGL attribute indices.
 	std::map<unsigned int, GLenum> vertexAttribMap;
 
-	// Currently tracked OpenGL state
-	struct State
+	// Currently tracked OpenGL state.
+	struct
 	{
 		int curTextureUnit;
-		std::vector<GLuint> textureUnits; // tracks bound texture for each unit
+		std::vector<GLuint> textureUnits; // Tracks bound texture for each unit.
 
 		std::map<unsigned int, bool> enabledVertexAttribs;
+
+		std::map<GLenum, bool> enabledCapabilities; // tracks glEnable/Disable.
 		
 	} state;
 
-	// Pointer to the currently active context
+	// Pointer to the currently active context.
 	static Context *current;
 };
 

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

 	Context *ctx = resetContext();
 
 	// Make sure antialiasing works when set elsewhere
-	glEnable(GL_MULTISAMPLE);
+	ctx->setCapability(GL_MULTISAMPLE, true);
 
 	// Enable blending
-	glEnable(GL_BLEND);
+	ctx->setCapability(GL_BLEND, true);
 
 	// "Normal" blending
 	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
 	// Enable line/point smoothing.
 	setLineStyle(LINE_SMOOTH);
-	glEnable(GL_POINT_SMOOTH);
+	ctx->setCapability(GL_POINT_SMOOTH, true);
 	glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
 
 	// Auto-generated mipmaps should be the best quality possible
 		glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST);
 
 	// Enable textures
-	glEnable(GL_TEXTURE_2D);
+	ctx->setCapability(GL_TEXTURE_2D, true);
 	ctx->setActiveTextureUnit(0);
 
 	// Set the viewport to top-left corner
 
 void Graphics::setScissor(int x, int y, int width, int height)
 {
-	glEnable(GL_SCISSOR_TEST);
+	getContext()->setCapability(GL_SCISSOR_TEST, true);
 	glScissor(x, getRenderHeight() - (y + height), width, height); // Compensates for the fact that our y-coordinate is reverse of OpenGLs.
 }
 
 void Graphics::setScissor()
 {
-	glDisable(GL_SCISSOR_TEST);
+	getContext()->setCapability(GL_SCISSOR_TEST, false);
 }
 
 int Graphics::getScissor(lua_State *L)
 void Graphics::defineStencil()
 {
 	glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
-	glEnable(GL_STENCIL_TEST);
+	getContext()->setCapability(GL_STENCIL_TEST, true);
 	glClear(GL_STENCIL_BUFFER_BIT);
 	glStencilFunc(GL_ALWAYS, 1, 1);
 	glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
 void Graphics::discardStencil()
 {
 	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
-	glDisable(GL_STENCIL_TEST);
+	getContext()->setCapability(GL_STENCIL_TEST, false);
 }
 
 Image *Graphics::newImage(love::image::ImageData *data)
 
 void Graphics::setPointStyle(Graphics::PointStyle style)
 {
-	if (style == POINT_SMOOTH)
-		glEnable(GL_POINT_SMOOTH);
-	else // love::POINT_ROUGH
-		glDisable(GL_POINT_SMOOTH);
+	getContext()->setCapability(GL_POINT_SMOOTH, style == POINT_SMOOTH);
 }
 
 void Graphics::setPoint(float size, Graphics::PointStyle style)
 {
-	if (style == POINT_SMOOTH)
-		glEnable(GL_POINT_SMOOTH);
-	else // POINT_ROUGH
-		glDisable(GL_POINT_SMOOTH);
+	Context *ctx = getContext();
 
+	ctx->setCapability(GL_POINT_SMOOTH, style == POINT_SMOOTH);
 	glPointSize((GLfloat)size);
 }