Browse Source

Add pixel effects (love-glsl)

vrld 14 years ago
parent
commit
24a52f056b

+ 2 - 0
src/common/types.h

@@ -50,6 +50,7 @@ namespace love
 		GRAPHICS_PARTICLE_SYSTEM_ID,
 		GRAPHICS_SPRITE_BATCH_ID,
 		GRAPHICS_FRAMEBUFFER_ID,
+		GRAPHICS_PIXELEFFECT_ID,
 
 		// Image
 		IMAGE_IMAGE_DATA_ID,
@@ -112,6 +113,7 @@ namespace love
 	const bits GRAPHICS_PARTICLE_SYSTEM_T = (bits(1) << GRAPHICS_PARTICLE_SYSTEM_ID) | GRAPHICS_DRAWABLE_T;
 	const bits GRAPHICS_SPRITE_BATCH_T = (bits(1) << GRAPHICS_SPRITE_BATCH_ID) | GRAPHICS_DRAWABLE_T;
 	const bits GRAPHICS_FRAMEBUFFER_T = (bits(1) << GRAPHICS_FRAMEBUFFER_ID) | GRAPHICS_DRAWABLE_T;
+	const bits GRAPHICS_PIXELEFFECT_T = (bits(1) << GRAPHICS_PIXELEFFECT_ID) | OBJECT_T;
 
 	// Image.
 	const bits IMAGE_IMAGE_DATA_T = (bits(1) << IMAGE_IMAGE_DATA_ID) | DATA_T;

+ 3 - 0
src/modules/graphics/opengl/Framebuffer.h

@@ -48,6 +48,9 @@ namespace opengl
 		void unloadVolatile();
 
 	private:
+		friend class PixelEffect;
+		GLuint getTextureName() const { return img; }
+
 		GLsizei width;
 		GLsizei height;
 		GLuint fbo;

+ 14 - 0
src/modules/graphics/opengl/Graphics.cpp

@@ -325,6 +325,7 @@ namespace opengl
 	{
 		glClear(GL_COLOR_BUFFER_BIT);
 		glLoadIdentity();
+		PixelEffect::detach();
 	}
 
 	void Graphics::present()
@@ -541,6 +542,19 @@ namespace opengl
 		return new Framebuffer(width, height);
 	}
 
+	PixelEffect * Graphics::newPixelEffect(const std::string& code)
+	{
+		PixelEffect * effect = NULL;
+		try {
+			effect = new PixelEffect(code);
+		} catch (love::Exception& e) {
+			if (effect)
+				delete effect;
+			throw(e);
+		}
+		return effect;
+	}
+
 	void Graphics::setColor(const Color& c)
 	{
 		glColor4ubv(&c.r);

+ 3 - 0
src/modules/graphics/opengl/Graphics.h

@@ -42,6 +42,7 @@
 #include "SpriteBatch.h"
 #include "ParticleSystem.h"
 #include "Framebuffer.h"
+#include "PixelEffect.h"
 
 namespace love
 {
@@ -288,6 +289,8 @@ namespace opengl
 
 		Framebuffer * newFramebuffer(int width, int height);
 
+		PixelEffect * newPixelEffect(const std::string& code);
+
 		/**
 		* Sets the foreground color.
 		* @param c The new foreground color.

+ 3 - 0
src/modules/graphics/opengl/Image.h

@@ -147,6 +147,9 @@ namespace opengl
 
 		void drawv(const Matrix & t, const vertex * v) const;
 
+		friend class PixelEffect;
+		GLuint getTextureName() const { return texture; }
+
 	}; // Image
 
 } // opengl

+ 51 - 0
src/modules/graphics/opengl/wrap_Graphics.cpp

@@ -24,6 +24,7 @@
 #include <font/Rasterizer.h>
 
 #include <scripts/graphics.lua.h>
+#include "GLInfo.h"
 
 namespace love
 {
@@ -383,6 +384,33 @@ namespace opengl
 		return 1;
 	}
 
+	int w_newPixelEffect(lua_State * L)
+	{
+		if (!PixelEffect::isSupported())
+			return luaL_error(L, "Sorry, your graphics card does not support pixel effects.");
+
+		try {
+			luaL_checkstring(L, 1);
+
+			luax_getfunction(L, "graphics", "_effectCodeToGLSL");
+			lua_pushvalue(L, 1);
+			lua_pcall(L, 1, 1, 0);
+
+			const char* code = lua_tostring(L, -1);
+			PixelEffect * effect = instance->newPixelEffect(code);
+			luax_newtype(L, "PixelEffect", GRAPHICS_PIXELEFFECT_T, (void*)effect);
+		} catch (love::Exception& e) {
+			// memory is freed in Graphics::newPixelEffect
+			luax_getfunction(L, "graphics", "_transformGLSLErrorMessages");
+			lua_pushstring(L, e.what());
+			lua_pcall(L, 1,1, 0);
+			const char* err = lua_tostring(L, -1);
+			return luaL_error(L, err);
+		}
+
+		return 1;
+	}
+
 	int w_setColor(lua_State * L)
 	{
 		Color c;
@@ -702,6 +730,24 @@ namespace opengl
 		return 0;
 	}
 
+	int w_setPixelEffect(lua_State * L)
+	{
+		if (lua_isnoneornil(L,1)) {
+			PixelEffect::detach();
+			return 0;
+		}
+
+		PixelEffect * effect = luax_checkpixeleffect(L, 1);
+		effect->attach();
+		return 0;
+	}
+
+	int w_hasPixelEffects(lua_State * L)
+	{
+		lua_pushboolean(L, PixelEffect::isSupported());
+		return 1;
+	}
+
 	/**
 	* Draws an Image at the specified coordinates, with rotation and
 	* scaling along both axes.
@@ -1075,6 +1121,7 @@ namespace opengl
 		{ "newSpriteBatch", w_newSpriteBatch },
 		{ "newParticleSystem", w_newParticleSystem },
 		{ "newFramebuffer", w_newFramebuffer },
+		{ "newPixelEffect", w_newPixelEffect },
 
 		{ "setColor", w_setColor },
 		{ "getColor", w_getColor },
@@ -1102,6 +1149,9 @@ namespace opengl
 		{ "newScreenshot", w_newScreenshot },
 		{ "setRenderTarget", w_setRenderTarget },
 
+		{ "setPixelEffect", w_setPixelEffect },
+		{ "hasPixelEffects", w_hasPixelEffects },
+
 		{ "draw", w_draw },
 		{ "drawq", w_drawq },
 		{ "drawTest", w_drawTest },
@@ -1158,6 +1208,7 @@ namespace opengl
 		luaopen_spritebatch,
 		luaopen_particlesystem,
 		luaopen_framebuffer,
+		luaopen_pixeleffect,
 		0
 	};
 

+ 5 - 0
src/modules/graphics/opengl/wrap_Graphics.h

@@ -28,6 +28,7 @@
 #include "wrap_SpriteBatch.h"
 #include "wrap_ParticleSystem.h"
 #include "wrap_Framebuffer.h"
+#include "wrap_PixelEffect.h"
 #include "Graphics.h"
 
 namespace love
@@ -61,6 +62,7 @@ namespace opengl
 	int w_newSpriteBatch(lua_State * L);
 	int w_newParticleSystem(lua_State * L);
 	int w_newFramebuffer(lua_State * L); // comments in function
+	int w_newPixelEffect(lua_State * L);
 	int w_setColor(lua_State * L);
 	int w_getColor(lua_State * L);
 	int w_setBackgroundColor(lua_State * L);
@@ -86,6 +88,9 @@ namespace opengl
 	int w_getMaxPointSize(lua_State * L);
 	int w_newScreenshot(lua_State * L);
 	int w_setRenderTarget(lua_State * L);
+	int w_setPixelEffect(lua_State * L);
+	int w_hasPixelEffects(lua_State * L);
+	int w_getGLSLVersion(lua_State * L);
 	int w_draw(lua_State * L);
 	int w_drawq(lua_State * L);
 	int w_drawTest(lua_State * L);

+ 1 - 0
src/scripts/boot.lua

@@ -689,6 +689,7 @@ function love.releaseerrhand(msg)
 	end
 
 	love.graphics.setRenderTarget()
+	love.graphics.setPixelEffect()
 
 	-- Load.
 	love.graphics.reset()

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

@@ -1642,7 +1642,9 @@ const unsigned char boot_lua[] =
 	0x09, 0x09, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x0a,
 	0x09, 0x65, 0x6e, 0x64, 0x0a,0x0a,
 	0x09, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x67, 0x72, 0x61, 0x70, 0x68, 0x69, 0x63, 0x73, 0x2e, 0x73, 0x65, 0x74, 
-	0x52, 0x65, 0x6e, 0x64, 0x65, 0x72, 0x54, 0x61, 0x72, 0x67, 0x65, 0x74, 0x28, 0x29, 0x0a,0x0a,
+	0x52, 0x65, 0x6e, 0x64, 0x65, 0x72, 0x54, 0x61, 0x72, 0x67, 0x65, 0x74, 0x28, 0x29, 0x0a,
+	0x09, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x67, 0x72, 0x61, 0x70, 0x68, 0x69, 0x63, 0x73, 0x2e, 0x73, 0x65, 0x74, 
+	0x50, 0x69, 0x78, 0x65, 0x6c, 0x45, 0x66, 0x66, 0x65, 0x63, 0x74, 0x28, 0x29, 0x0a,0x0a,
 	0x09, 0x2d, 0x2d, 0x20, 0x4c, 0x6f, 0x61, 0x64, 0x2e, 0x0a,
 	0x09, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x67, 0x72, 0x61, 0x70, 0x68, 0x69, 0x63, 0x73, 0x2e, 0x72, 0x65, 0x73, 
 	0x65, 0x74, 0x28, 0x29, 0x0a,

File diff suppressed because it is too large
+ 1226 - 1226
src/scripts/graphics.lua


File diff suppressed because it is too large
+ 6119 - 4898
src/scripts/graphics.lua.h


Some files were not shown because too many files changed in this diff