Browse Source

Add string mapping for spritebatch usage parameter (issue #264)

Bart van Strien 14 years ago
parent
commit
b14ca78109

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

@@ -243,6 +243,24 @@ namespace opengl
 		v[3].r = color.r; v[3].g = color.g; v[3].b = color.b; v[3].a = color.a;
 	}
 
+	bool SpriteBatch::getConstant(const char * in, UsageHint & out)
+	{
+		return usageHints.find(in, out);
+	}
+
+	bool SpriteBatch::getConstant(UsageHint in, const char *& out)
+	{
+		return usageHints.find(in, out);
+	}
+
+	StringMap<SpriteBatch::UsageHint, SpriteBatch::USAGE_MAX_ENUM>::Entry SpriteBatch::usageHintEntries[] =
+	{
+		{"dynamic", SpriteBatch::USAGE_DYNAMIC},
+		{"static", SpriteBatch::USAGE_STATIC},
+		{"stream", SpriteBatch::USAGE_STREAM},
+	};
+
+	StringMap<SpriteBatch::UsageHint, SpriteBatch::USAGE_MAX_ENUM> SpriteBatch::usageHints(usageHintEntries, sizeof(usageHintEntries));
 } // opengl
 } // graphics
 } // love

+ 10 - 2
src/modules/graphics/opengl/SpriteBatch.h

@@ -29,6 +29,7 @@
 #include <common/Object.h>
 #include <common/Vector.h>
 #include <common/Matrix.h>
+#include <common/StringMap.h>
 #include <graphics/Drawable.h>
 #include <graphics/Volatile.h>
 #include <graphics/Color.h>
@@ -73,9 +74,10 @@ namespace opengl
 
 		enum UsageHint
 		{
-			USAGE_DYNAMIC,
+			USAGE_DYNAMIC = 1,
 			USAGE_STATIC,
-			USAGE_STREAM
+			USAGE_STREAM,
+			USAGE_MAX_ENUM
 		};
 
 		SpriteBatch(Image * image, int size, int usage);
@@ -108,6 +110,9 @@ namespace opengl
 		// Implements Drawable.
 		void draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const;
 
+		static bool getConstant(const char * in, UsageHint & out);
+		static bool getConstant(UsageHint in, const char *& out);
+
 	private:
 
 		void addv(const vertex * v);
@@ -121,6 +126,9 @@ namespace opengl
 		 */
 		void setColorv(vertex * v, const Color & color);
 
+		static StringMap<UsageHint, USAGE_MAX_ENUM>::Entry usageHintEntries[];
+		static StringMap<UsageHint, USAGE_MAX_ENUM> usageHints;
+
 	}; // SpriteBatch
 
 } // opengl

+ 6 - 1
src/modules/graphics/opengl/wrap_Graphics.cpp

@@ -323,7 +323,12 @@ namespace opengl
 	{
 		Image * image = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T);
 		int size = luaL_optint(L, 2, 1000);
-		int usage = luaL_optint(L, 3, SpriteBatch::USAGE_DYNAMIC);
+		SpriteBatch::UsageHint usage = SpriteBatch::USAGE_DYNAMIC;
+		if (lua_gettop(L) > 2)
+		{
+			if (!SpriteBatch::getConstant(luaL_checkstring(L, 3), usage))
+				usage = SpriteBatch::USAGE_DYNAMIC;
+		}
 		SpriteBatch * t = NULL;
 		try {
 			t = instance->newSpriteBatch(image, size, usage);