Browse Source

Replace love.graphics.pop("all") with love.graphics.getStackDepth (resolves #1248 again)

The pop("all") api was a bit awkward, especially considering there's a
push("all") that does something else. I think any use case that benefited from
pop("all") benefits from getStackDepth just as much if not more. There is also
additional functionality, like being able to find the source of a mismatched
push/pop.

--HG--
branch : minor
Bart van Strien 8 years ago
parent
commit
77663ed847

+ 17 - 35
src/modules/graphics/Graphics.cpp

@@ -1288,6 +1288,11 @@ Graphics::Stats Graphics::getStats() const
 	return stats;
 }
 
+size_t Graphics::getStackDepth() const
+{
+	return stackTypeStack.size();
+}
+
 void Graphics::push(StackType type)
 {
 	if (stackTypeStack.size() == MAX_USER_STACK_DEPTH)
@@ -1303,30 +1308,25 @@ void Graphics::push(StackType type)
 	stackTypeStack.push_back(type);
 }
 
-void Graphics::pop(PopMode mode)
+void Graphics::pop()
 {
-	size_t amount = mode == POP_ALL ? stackTypeStack.size() : 1;
-
-	if (stackTypeStack.size() < amount)
+	if (stackTypeStack.size() < 1)
 		throw Exception("Minimum stack depth reached (more pops than pushes?)");
 
-	for (size_t i = 0; i < amount; ++i)
-	{
-		popTransform();
-		pixelScaleStack.pop_back();
-
-		if (stackTypeStack.back() == STACK_ALL)
-		{
-			DisplayState &newstate = states[states.size() - 2];
+	popTransform();
+	pixelScaleStack.pop_back();
 
-			restoreStateChecked(newstate);
+	if (stackTypeStack.back() == STACK_ALL)
+	{
+		DisplayState &newstate = states[states.size() - 2];
 
-			// The last two states in the stack should be equal now.
-			states.pop_back();
-		}
+		restoreStateChecked(newstate);
 
-		stackTypeStack.pop_back();
+		// The last two states in the stack should be equal now.
+		states.pop_back();
 	}
+
+	stackTypeStack.pop_back();
 }
 
 /**
@@ -1520,16 +1520,6 @@ bool Graphics::getConstant(StackType in, const char *&out)
 	return stackTypes.find(in, out);
 }
 
-bool Graphics::getConstant(const char *in, PopMode &out)
-{
-	return popModes.find(in, out);
-}
-
-bool Graphics::getConstant(PopMode in, const char *&out)
-{
-	return popModes.find(in, out);
-}
-
 StringMap<Graphics::DrawMode, Graphics::DRAW_MAX_ENUM>::Entry Graphics::drawModeEntries[] =
 {
 	{ "line", DRAW_LINE },
@@ -1623,13 +1613,5 @@ 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::PopMode, Graphics::POP_MAX_ENUM>::Entry Graphics::popModeEntries[] =
-{
-	{ "one", POP_ONE },
-	{ "all", POP_ALL },
-};
-
-StringMap<Graphics::PopMode, Graphics::POP_MAX_ENUM> Graphics::popModes(Graphics::popModeEntries, sizeof(Graphics::popModeEntries));
-
 } // graphics
 } // love

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

@@ -194,13 +194,6 @@ public:
 		STACK_MAX_ENUM
 	};
 
-	enum PopMode
-	{
-		POP_ONE,
-		POP_ALL,
-		POP_MAX_ENUM
-	};
-
 	enum TemporaryRenderTargetFlags
 	{
 		TEMPORARY_RT_DEPTH   = (1 << 0),
@@ -741,8 +734,9 @@ public:
 	 **/
 	Stats getStats() const;
 
+	size_t getStackDepth() const;
 	void push(StackType type = STACK_TRANSFORM);
-	void pop(PopMode mode = POP_ONE);
+	void pop();
 
 	const Matrix4 &getTransform() const;
 	const Matrix4 &getProjection() const;
@@ -805,9 +799,6 @@ public:
 	static bool getConstant(const char *in, StackType &out);
 	static bool getConstant(StackType in, const char *&out);
 
-	static bool getConstant(const char *in, PopMode &out);
-	static bool getConstant(PopMode in, const char *&out);
-
 	// Default shader code (a shader is always required internally.)
 	static Shader::ShaderSource defaultShaderCode[Shader::STANDARD_MAX_ENUM][Shader::LANGUAGE_MAX_ENUM][2];
 
@@ -956,9 +947,6 @@ private:
 	static StringMap<StackType, STACK_MAX_ENUM>::Entry stackTypeEntries[];
 	static StringMap<StackType, STACK_MAX_ENUM> stackTypes;
 
-	static StringMap<PopMode, POP_MAX_ENUM>::Entry popModeEntries[];
-	static StringMap<PopMode, POP_MAX_ENUM> popModes;
-
 }; // Graphics
 
 } // graphics

+ 8 - 6
src/modules/graphics/wrap_Graphics.cpp

@@ -2648,6 +2648,12 @@ int w_flushBatch(lua_State *)
 	return 0;
 }
 
+int w_getStackDepth(lua_State *L)
+{
+	lua_pushnumber(L, instance()->getStackDepth());
+	return 1;
+}
+
 int w_push(lua_State *L)
 {
 	Graphics::StackType stype = Graphics::STACK_TRANSFORM;
@@ -2668,12 +2674,7 @@ int w_push(lua_State *L)
 
 int w_pop(lua_State *L)
 {
-	Graphics::PopMode mode = Graphics::POP_ONE;
-	const char *modestring = lua_isnoneornil(L, 1) ? nullptr : luaL_checkstring(L, 1);
-	if (modestring && !Graphics::getConstant(modestring, mode))
-		return luaL_error(L, "Invalid pop mode: %s", modestring);
-
-	luax_catchexcept(L, [&](){ instance()->pop(mode); });
+	luax_catchexcept(L, [&](){ instance()->pop(); });
 	return 0;
 }
 
@@ -2857,6 +2858,7 @@ static const luaL_Reg functions[] =
 
 	{ "flushBatch", w_flushBatch },
 
+	{ "getStackDepth", w_getStackDepth },
 	{ "push", w_push },
 	{ "pop", w_pop },
 	{ "rotate", w_rotate },