Browse Source

Removed Canvas:clear. love.graphics.clear now accepts r,g,b,a values (and defaults to 0,0,0,0 with no arguments given.) love.graphics.clear clears the content of the currently active Canvas(es.)

--HG--
branch : minor
Alex Szpakowski 10 years ago
parent
commit
3efcf3901b

+ 0 - 1
changes.txt

@@ -10,7 +10,6 @@ Released: N/A
   * Added love.graphics.getSupported (replaces love.graphics.isSupported.)
   * Added love.graphics.getSystemLimits (replaces love.graphics.getSystemLimit.)
   * Added love.graphics.stencil and love.graphics.setStencilTest (replaces love.graphics.setStencil.)
-  * Added optional type argument to love.graphics.clear. Current type enums are "all" (the default) and "stencil".
   * Added optional x/y/width/height arguments to Image:refresh.
   * Added support for OpenGL ES.
   * Added support for loading ETC1/2, EAC, and PVRTC compressed textures.

+ 0 - 18
src/modules/graphics/Graphics.cpp

@@ -99,16 +99,6 @@ bool Graphics::getConstant(StackType in, const char *&out)
 	return stackTypes.find(in, out);
 }
 
-bool Graphics::getConstant(const char *in, ClearType &out)
-{
-	return clearTypes.find(in, out);
-}
-
-bool Graphics::getConstant(ClearType in, const char *&out)
-{
-	return clearTypes.find(in, out);
-}
-
 bool Graphics::getConstant(const char *in, StatType &out)
 {
 	return statTypes.find(in, out);
@@ -183,14 +173,6 @@ StringMap<Graphics::StackType, Graphics::STACK_MAX_ENUM>::Entry Graphics::stackT
 
 StringMap<Graphics::StackType, Graphics::STACK_MAX_ENUM> Graphics::stackTypes(Graphics::stackTypeEntries, sizeof(Graphics::stackTypeEntries));
 
-StringMap<Graphics::ClearType, Graphics::CLEAR_MAX_ENUM>::Entry Graphics::clearTypeEntries[] =
-{
-	{"all", Graphics::CLEAR_ALL},
-	{"stencil", Graphics::CLEAR_STENCIL},
-};
-
-StringMap<Graphics::ClearType, Graphics::CLEAR_MAX_ENUM> Graphics::clearTypes(Graphics::clearTypeEntries, sizeof(Graphics::clearTypeEntries));
-
 StringMap<Graphics::StatType, Graphics::STAT_MAX_ENUM>::Entry Graphics::statTypeEntries[] =
 {
 	{"drawcalls", Graphics::STAT_DRAW_CALLS},

+ 0 - 13
src/modules/graphics/Graphics.h

@@ -101,13 +101,6 @@ public:
 		STACK_MAX_ENUM
 	};
 
-	enum ClearType
-	{
-		CLEAR_ALL,
-		CLEAR_STENCIL,
-		CLEAR_MAX_ENUM
-	};
-
 	enum StatType
 	{
 		STAT_DRAW_CALLS,
@@ -213,9 +206,6 @@ public:
 	static bool getConstant(const char *in, StackType &out);
 	static bool getConstant(StackType in, const char *&out);
 
-	static bool getConstant(const char *in, ClearType &out);
-	static bool getConstant(ClearType in, const char *&out);
-
 	static bool getConstant(const char *in, StatType &out);
 	static bool getConstant(StatType in, const char *&out);
 
@@ -242,9 +232,6 @@ private:
 	static StringMap<StackType, STACK_MAX_ENUM>::Entry stackTypeEntries[];
 	static StringMap<StackType, STACK_MAX_ENUM> stackTypes;
 
-	static StringMap<ClearType, CLEAR_MAX_ENUM>::Entry clearTypeEntries[];
-	static StringMap<ClearType, CLEAR_MAX_ENUM> clearTypes;
-
 	static StringMap<StatType, STAT_MAX_ENUM>::Entry statTypeEntries[];
 	static StringMap<StatType, STAT_MAX_ENUM> statTypes;
 

+ 14 - 84
src/modules/graphics/opengl/Canvas.cpp

@@ -43,8 +43,14 @@ static GLenum createFBO(GLuint &framebuffer, GLuint texture)
 	gl.bindFramebuffer(GL_FRAMEBUFFER, framebuffer);
 
 	if (texture != 0)
+	{
 		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
 
+		// Initialize the texture to transparent black.
+		glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+		glClear(GL_COLOR_BUFFER_BIT);
+	}
+
 	GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
 
 	// unbind framebuffer
@@ -67,7 +73,13 @@ static GLenum createMSAABuffer(int width, int height, int &samples, GLenum iform
 
 	GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
 
-	if (status != GL_FRAMEBUFFER_COMPLETE)
+	if (status == GL_FRAMEBUFFER_COMPLETE)
+	{
+		// Initialize the buffer to transparent black.
+		glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+		glClear(GL_COLOR_BUFFER_BIT);
+	}
+	else
 	{
 		glDeleteRenderbuffers(1, &buffer);
 		buffer = 0;
@@ -246,8 +258,6 @@ bool Canvas::loadVolatile()
 		return false;
     }
 
-	clear(Color(0, 0, 0, 0));
-
 	size_t prevmemsize = texture_memory;
 
 	texture_memory = (getFormatBitsPerPixel(format) * width * height) / 8;
@@ -393,11 +403,6 @@ void Canvas::setupGrab()
 		else if (screenHasSRGB)
 			glDisable(GL_FRAMEBUFFER_SRGB);
 	}
-
-	// We discard the stencil buffer in stopGrab, so we should clear it here
-	// to prevent the discarded (invalid) contents from being accidentally used.
-	if (depth_stencil != 0 && (GLAD_ES_VERSION_3_0 || GLAD_EXT_discard_framebuffer))
-		glClear(GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 }
 
 void Canvas::startGrab(const std::vector<Canvas *> &canvases)
@@ -482,17 +487,6 @@ void Canvas::stopGrab(bool switchingToOtherCanvas)
 	if (current != this)
 		return;
 
-	if (depth_stencil != 0)
-	{
-		GLenum attachments[] = {GL_STENCIL_ATTACHMENT, GL_DEPTH_ATTACHMENT};
-
-		// Hint for the driver that it doesn't need to save these buffers.
-		if (GLAD_ES_VERSION_3_0)
-			glInvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments);
-		else if (GLAD_EXT_discard_framebuffer)
-			glDiscardFramebufferEXT(GL_FRAMEBUFFER, 2, attachments);
-	}
-
 	// Make sure the canvas texture is up to date if we're using MSAA.
 	resolveMSAA(false);
 
@@ -523,70 +517,6 @@ void Canvas::stopGrab(bool switchingToOtherCanvas)
 	}
 }
 
-void Canvas::clear(Color c)
-{
-	GLuint previous = gl.getDefaultFBO();
-
-	if (current != this)
-	{
-		if (current != nullptr)
-			previous = current->fbo;
-
-		gl.bindFramebuffer(GL_FRAMEBUFFER, fbo);
-	}
-
-	GLfloat glcolor[] = {c.r/255.f, c.g/255.f, c.b/255.f, c.a/255.f};
-
-	// We don't need to worry about multiple FBO attachments or global clear
-	// color state when OpenGL 3.0+ is supported.
-	if (GLAD_ES_VERSION_3_0 || GLAD_VERSION_3_0)
-	{
-		glClearBufferfv(GL_COLOR, 0, glcolor);
-
-		if (depth_stencil != 0)
-		{
-			GLint stencilvalue = 0;
-			glClearBufferiv(GL_STENCIL, 0, &stencilvalue);
-		}
-	}
-	else
-	{
-		// glClear will clear all active draw buffers, so should make sure only
-		// the draw buffer corresponding to our canvas' texture is active.
-		if (attachedCanvases.size() > 0)
-			glDrawBuffer(GL_COLOR_ATTACHMENT0);
-
-		// Don't use the state-shadowed gl.setClearColor because we want to save
-		// the previous clear color.
-		glClearColor(glcolor[0], glcolor[1], glcolor[2], glcolor[3]);
-		glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
-		// Restore the draw buffers for rendering to multiple textures.
-		if (attachedCanvases.size() > 0)
-		{
-			std::vector<GLenum> drawbuffers;
-			drawbuffers.reserve(attachedCanvases.size() + 1);
-
-			for (int i = 0; i < (int) attachedCanvases.size() + 1; i++)
-				drawbuffers.push_back(GL_COLOR_ATTACHMENT0 + i);
-
-			glDrawBuffers((GLsizei) drawbuffers.size(), &drawbuffers[0]);
-		}
-
-		// Restore the global clear color.
-		gl.setClearColor(gl.getClearColor());
-	}
-
-	if (current != this)
-	{
-		// If we're not the active canvas, we need to do a MSAA resolve after
-		// clearing the FBO since the resolve texture might be used afterward.
-		resolveMSAA(false);
-
-		gl.bindFramebuffer(GL_FRAMEBUFFER, previous);
-	}
-}
-
 bool Canvas::checkCreateStencil()
 {
 	// Do nothing if we've already created the stencil buffer.
@@ -624,7 +554,7 @@ bool Canvas::checkCreateStencil()
 
 	// We don't want the stencil buffer filled with garbage.
 	if (success)
-		glClear(GL_STENCIL_BUFFER_BIT);
+		glClear(GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 	else
 	{
 		glDeleteRenderbuffers(1, &depth_stencil);

+ 4 - 5
src/modules/graphics/opengl/Canvas.h

@@ -84,8 +84,6 @@ public:
 	void startGrab();
 	void stopGrab(bool switchingToOtherCanvas = false);
 
-	void clear(Color c);
-
 	/**
 	 * Create and attach a stencil buffer to this Canvas' framebuffer, if necessary.
 	 **/
@@ -132,9 +130,13 @@ public:
 
 private:
 
+	void setupGrab();
+
 	bool createMSAAFBO(GLenum internalformat);
 	bool resolveMSAA(bool restoreprev);
 
+	void drawv(const Matrix &t, const Vertex *v);
+
 	static Format getSizedFormat(Format format);
 	static void convertFormat(Format format, GLenum &internalformat, GLenum &externalformat, GLenum &type);
 	static size_t getFormatBitsPerPixel(Format format);
@@ -157,9 +159,6 @@ private:
 
 	size_t texture_memory;
 
-	void setupGrab();
-	void drawv(const Matrix &t, const Vertex *v);
-
 	static bool supportedFormats[FORMAT_MAX_ENUM];
 	static bool checkedFormats[FORMAT_MAX_ENUM];
 

+ 9 - 17
src/modules/graphics/opengl/Graphics.cpp

@@ -158,15 +158,20 @@ void Graphics::restoreStateChecked(const DisplayState &s)
 	setFont(s.font.get());
 	setShader(s.shader.get());
 
+	bool canvaseschanged = s.canvases.size() != cur.canvases.size();
+
 	for (size_t i = 0; i < s.canvases.size() && i < cur.canvases.size(); i++)
 	{
 		if (s.canvases[i].get() != cur.canvases[i].get())
 		{
-			setCanvas(s.canvases);
+			canvaseschanged = true;
 			break;
 		}
 	}
 
+	if (canvaseschanged)
+		setCanvas(s.canvases);
+
 	if (s.colorMask != cur.colorMask)
 		setColorMask(s.colorMask);
 
@@ -413,22 +418,10 @@ void Graphics::reset()
 	origin();
 }
 
-void Graphics::clear(ClearType type)
+void Graphics::clear(Color c)
 {
-	GLbitfield mask = 0;
-
-	switch (type)
-	{
-	case CLEAR_ALL:
-	default:
-		mask = GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
-		break;
-	case CLEAR_STENCIL:
-		mask = GL_STENCIL_BUFFER_BIT;
-		break;
-	}
-
-	glClear(mask);
+	glClearColor(c.r / 255.0f, c.g / 255.0f, c.b / 255.0f, c.a / 255.0f);
+	glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 }
 
 void Graphics::present()
@@ -751,7 +744,6 @@ Color Graphics::getColor() const
 
 void Graphics::setBackgroundColor(const Color &c)
 {
-	gl.setClearColor(c);
 	states.back().backgroundColor = c;
 }
 

+ 2 - 2
src/modules/graphics/opengl/Graphics.h

@@ -81,9 +81,9 @@ public:
 	void reset();
 
 	/**
-	 * Clears the screen.
+	 * Clears the screen to a specific color.
 	 **/
-	void clear(ClearType type = CLEAR_ALL);
+	void clear(Color c);
 
 	/**
 	 * Flips buffers. (Rendered geometry is presented on screen).

+ 0 - 18
src/modules/graphics/opengl/OpenGL.cpp

@@ -87,13 +87,6 @@ void OpenGL::setupContext()
 	GLfloat glcolor[4] = {1.0f, 1.0f, 1.0f, 1.0f};
 	glVertexAttrib4fv(ATTRIB_COLOR, glcolor);
 
-	// Get the current clear color so we don't have to get it through GL later.
-	glGetFloatv(GL_COLOR_CLEAR_VALUE, glcolor);
-	state.clearColor.r = glcolor[0] * 255;
-	state.clearColor.g = glcolor[1] * 255;
-	state.clearColor.b = glcolor[2] * 255;
-	state.clearColor.a = glcolor[3] * 255;
-
 	// Get the current viewport.
 	glGetIntegerv(GL_VIEWPORT, (GLint *) &state.viewport.x);
 
@@ -387,17 +380,6 @@ Color OpenGL::getColor() const
 	return state.color;
 }
 
-void OpenGL::setClearColor(const Color &c)
-{
-	glClearColor(c.r / 255.0f, c.g / 255.0f, c.b / 255.0f, c.a / 255.0f);
-	state.clearColor = c;
-}
-
-Color OpenGL::getClearColor() const
-{
-	return state.clearColor;
-}
-
 void OpenGL::setViewport(const OpenGL::Viewport &v)
 {
 	glViewport(v.x, v.y, v.w, v.h);

+ 0 - 12
src/modules/graphics/opengl/OpenGL.h

@@ -198,16 +198,6 @@ public:
 	 **/
 	Color getColor() const;
 
-	/**
-	 * Sets the current clear color for all framebuffer objects.
-	 **/
-	void setClearColor(const Color &c);
-
-	/**
-	 * Gets the current clear color.
-	 **/
-	Color getClearColor() const;
-
 	/**
 	 * Sets the OpenGL rendering viewport to the specified rectangle.
 	 * The y-coordinate starts at the top.
@@ -356,8 +346,6 @@ private:
 		// Current constant color.
 		Color color;
 
-		Color clearColor;
-
 		// Texture unit state (currently bound texture for each texture unit.)
 		std::vector<GLuint> boundTextures;
 

+ 0 - 33
src/modules/graphics/opengl/wrap_Canvas.cpp

@@ -77,38 +77,6 @@ int w_Canvas_newImageData(lua_State *L)
 	return 1;
 }
 
-int w_Canvas_clear(lua_State *L)
-{
-	Canvas *canvas = luax_checkcanvas(L, 1);
-	Color c;
-	if (lua_isnoneornil(L, 2))
-	{
-		c.set(0, 0, 0, 0);
-	}
-	else if (lua_istable(L, 2))
-	{
-		for (int i = 1; i <= 4; i++)
-			lua_rawgeti(L, 2, i);
-
-		c.r = (unsigned char)luaL_checkinteger(L, -4);
-		c.g = (unsigned char)luaL_checkinteger(L, -3);
-		c.b = (unsigned char)luaL_checkinteger(L, -2);
-		c.a = (unsigned char)luaL_optinteger(L, -1, 255);
-
-		lua_pop(L, 4);
-	}
-	else
-	{
-		c.r = (unsigned char)luaL_checkinteger(L, 2);
-		c.g = (unsigned char)luaL_checkinteger(L, 3);
-		c.b = (unsigned char)luaL_checkinteger(L, 4);
-		c.a = (unsigned char)luaL_optinteger(L, 5, 255);
-	}
-	canvas->clear(c);
-
-	return 0;
-}
-
 int w_Canvas_getFormat(lua_State *L)
 {
 	Canvas *canvas = luax_checkcanvas(L, 1);
@@ -141,7 +109,6 @@ static const luaL_Reg functions[] =
 
 	{ "renderTo", w_Canvas_renderTo },
 	{ "newImageData", w_Canvas_newImageData },
-	{ "clear", w_Canvas_clear },
 	{ "getFormat", w_Canvas_getFormat },
 	{ "getMSAA", w_Canvas_getMSAA },
 	{ 0, 0 }

+ 0 - 1
src/modules/graphics/opengl/wrap_Canvas.h

@@ -37,7 +37,6 @@ namespace opengl
 Canvas *luax_checkcanvas(lua_State *L, int idx);
 int w_Canvas_renderTo(lua_State *L);
 int w_Canvas_newImageData(lua_State *L);
-int w_Canvas_clear(lua_State *L);
 int w_Canvas_getFormat(lua_State *L);
 int w_Canvas_getMSAA(lua_State *L);
 extern "C" int luaopen_canvas(lua_State *L);

+ 9 - 5
src/modules/graphics/opengl/wrap_Graphics.cpp

@@ -47,13 +47,17 @@ int w_reset(lua_State *)
 
 int w_clear(lua_State *L)
 {
-	Graphics::ClearType type = Graphics::CLEAR_ALL;
+	Color color(0, 0, 0, 0);
 
-	const char *tname = lua_isnoneornil(L, 1) ? nullptr : luaL_checkstring(L, 1);
-	if (tname && !Graphics::getConstant(tname, type))
-		return luaL_error(L, "Invalid graphics clear type: %s", tname);
+	if (!lua_isnoneornil(L, 1))
+	{
+		color.r = (unsigned char) luaL_checkinteger(L, 1);
+		color.g = (unsigned char) luaL_checkinteger(L, 2);
+		color.b = (unsigned char) luaL_checkinteger(L, 3);
+		color.a = (unsigned char) luaL_optinteger(L, 4, 255);
+	}
 
-	instance()->clear(type);
+	instance()->clear(color);
 	return 0;
 }
 

+ 1 - 1
src/scripts/boot.lua

@@ -526,7 +526,7 @@ function love.run()
 		if love.update then love.update(dt) end -- will pass 0 if love.timer is disabled
 
 		if love.graphics and love.graphics.isActive() then
-			love.graphics.clear()
+			love.graphics.clear(love.graphics.getBackgroundColor())
 			love.graphics.origin()
 			if love.draw then love.draw() end
 			love.graphics.present()

+ 3 - 1
src/scripts/boot.lua.h

@@ -951,7 +951,9 @@ const unsigned char boot_lua[] =
 	0x20, 0x61, 0x6e, 0x64, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x67, 0x72, 0x61, 0x70, 0x68, 0x69, 0x63, 0x73, 
 	0x2e, 0x69, 0x73, 0x41, 0x63, 0x74, 0x69, 0x76, 0x65, 0x28, 0x29, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a,
 	0x09, 0x09, 0x09, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x67, 0x72, 0x61, 0x70, 0x68, 0x69, 0x63, 0x73, 0x2e, 0x63, 
-	0x6c, 0x65, 0x61, 0x72, 0x28, 0x29, 0x0a,
+	0x6c, 0x65, 0x61, 0x72, 0x28, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x67, 0x72, 0x61, 0x70, 0x68, 0x69, 0x63, 0x73, 
+	0x2e, 0x67, 0x65, 0x74, 0x42, 0x61, 0x63, 0x6b, 0x67, 0x72, 0x6f, 0x75, 0x6e, 0x64, 0x43, 0x6f, 0x6c, 0x6f, 
+	0x72, 0x28, 0x29, 0x29, 0x0a,
 	0x09, 0x09, 0x09, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x67, 0x72, 0x61, 0x70, 0x68, 0x69, 0x63, 0x73, 0x2e, 0x6f, 
 	0x72, 0x69, 0x67, 0x69, 0x6e, 0x28, 0x29, 0x0a,
 	0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x64, 0x72, 0x61, 0x77, 0x20, 0x74, 0x68,