Commits

rude committed 7187cf4

Added Canvas:getPixel(x, y). (pull request #17)

Comments (0)

Files changed (4)

src/modules/graphics/opengl/Canvas.cpp

 	return img;
 }
 
+void Canvas::getPixel(unsigned char* pixel_rgba, int x, int y)
+{
+	strategy->bindFBO( fbo );
+
+	glReadPixels(x, height - y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel_rgba);
+
+	if (current)
+		strategy->bindFBO( current->fbo );
+	else
+		strategy->bindFBO( 0 );
+}
+
 void Canvas::setFilter(const Image::Filter &f)
 {
 	bindTexture(img);

src/modules/graphics/opengl/Canvas.h

 
 	love::image::ImageData *getImageData(love::image::Image *image);
 
+	void getPixel(unsigned char* pixel_rgba, int x, int y);
+
 	void setFilter(const Image::Filter &f);
 	Image::Filter getFilter() const;
 

src/modules/graphics/opengl/wrap_Canvas.cpp

 	return 1;
 }
 
+int w_Canvas_getPixel(lua_State * L)
+{
+	Canvas * canvas = luax_checkcanvas(L, 1);
+	int x = luaL_checkint(L, 2);
+	int y = luaL_checkint(L, 3);
+	unsigned char c[4];
+	try
+	{
+		canvas->getPixel(c, x, y);
+	}
+	catch (love::Exception & e)
+	{
+		return luaL_error(L, "%s", e.what());
+	}
+	lua_pushnumber(L, c[0]);
+	lua_pushnumber(L, c[1]);
+	lua_pushnumber(L, c[2]);
+	lua_pushnumber(L, c[3]);
+	return 4;
+}
+
 int w_Canvas_setFilter(lua_State *L)
 {
 	Canvas *canvas = luax_checkcanvas(L, 1);
 {
 	{ "renderTo", w_Canvas_renderTo },
 	{ "getImageData", w_Canvas_getImageData },
+	{ "getPixel", w_Canvas_getPixel },
 	{ "setFilter", w_Canvas_setFilter },
 	{ "getFilter", w_Canvas_getFilter },
 	{ "setWrap", w_Canvas_setWrap },

src/modules/graphics/opengl/wrap_Canvas.h

 Canvas *luax_checkcanvas(lua_State *L, int idx);
 int w_Canvas_renderTo(lua_State *L);
 int w_Canvas_getImageData(lua_State *L);
+int w_Canvas_getPixel(lua_State * L);
 int w_Canvas_setFilter(lua_State *L);
 int w_Canvas_getFilter(lua_State *L);
 int w_Canvas_setWrap(lua_State *L);