Commits

Bart van Strien committed 72ca3a6

Prevent overflows and underflows in love.graphics.push and love.graphics.pop (bug #118)

Maximum is hardware maximum - 5, so the engine itself has a few matrices.

  • Participants
  • Parent commits 26ed36a

Comments (0)

Files changed (3)

src/modules/graphics/opengl/Graphics.cpp

 {
 
 	Graphics::Graphics()
-		: currentFont(0), lineWidth(1)
+		: currentFont(0), lineWidth(1), matrixLimit(0), userMatrices(0)
 	{
 		// Indicates that there is no screen
 		// created yet.
 
 		// Restore the display state.
 		restoreState(tempState);
+		
+		// Get the maximum number of matrices
+		// subtract a few to give the engine some room.
+		glGetIntegerv(GL_MAX_MODELVIEW_STACK_DEPTH, &matrixLimit);
+		matrixLimit -= 5;
 
 		return true;
 	}
 
 	void Graphics::push()
 	{
+		if (userMatrices == matrixLimit)
+			throw Exception("Maximum stack depth reached.");
 		glPushMatrix();
+		++userMatrices;
 	}
 
 	void Graphics::pop()
 	{
+		if (userMatrices < 1)
+			throw Exception("Minimum stack depth reached. (More pops than pushes?)");
 		glPopMatrix();
+		--userMatrices;
 	}
 
 	void Graphics::rotate(float r)

src/modules/graphics/opengl/Graphics.h

 		DisplayMode currentMode;
 
 		float lineWidth;
+		GLint matrixLimit;
+		GLint userMatrices;
 
 	public:
 

src/modules/graphics/opengl/wrap_Graphics.cpp

 		return 0;
 	}
 
-	int w_push(lua_State *)
+	int w_push(lua_State *L)
 	{
-		instance->push();
+		try
+		{
+			instance->push();
+		}
+		catch (love::Exception e)
+		{
+			return luaL_error(L, e.what());
+		}
 		return 0;
 	}
 
-	int w_pop(lua_State *)
+	int w_pop(lua_State *L)
 	{
-		instance->pop();
+		try
+		{
+			instance->pop();
+		}
+		catch (love::Exception e)
+		{
+			return luaL_error(L, e.what());
+		}
 		return 0;
 	}