From 19e4dea8e857dbe6f35103c5e5142ad8efa3eb2d Mon Sep 17 00:00:00 2001 From: vrld Date: Sat, 12 Mar 2011 15:43:00 +0100 Subject: [PATCH] Add framebuffer:clear(r,g,b,a) (issue #134). --HG-- branch : minor --- src/modules/graphics/opengl/Framebuffer.cpp | 17 +++++++++ src/modules/graphics/opengl/Framebuffer.h | 4 +++ .../graphics/opengl/wrap_Framebuffer.cpp | 36 +++++++++++++++++++ .../graphics/opengl/wrap_Framebuffer.h | 1 + 4 files changed, 58 insertions(+) diff --git a/src/modules/graphics/opengl/Framebuffer.cpp b/src/modules/graphics/opengl/Framebuffer.cpp index 7d6326509..abd5fbb9e 100644 --- a/src/modules/graphics/opengl/Framebuffer.cpp +++ b/src/modules/graphics/opengl/Framebuffer.cpp @@ -1,4 +1,5 @@ #include "Framebuffer.h" +#include "Graphics.h" #include #include // For memcpy @@ -233,6 +234,22 @@ namespace opengl current = NULL; } + + void Framebuffer::clear(const Color& c) + { + GLuint previous = 0; + if (current != NULL) + previous = current->fbo; + + strategy->bindFBO(fbo); + glPushAttrib(GL_COLOR_BUFFER_BIT); + glClearColor((float)c.r/255.0f, (float)c.g/255.0f, (float)c.b/255.0f, (float)c.a/255.0f); + glClear(GL_COLOR_BUFFER_BIT); + glPopAttrib(); + + strategy->bindFBO(previous); + } + void Framebuffer::draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const { static Matrix t; diff --git a/src/modules/graphics/opengl/Framebuffer.h b/src/modules/graphics/opengl/Framebuffer.h index c8faae295..3fd3a798d 100644 --- a/src/modules/graphics/opengl/Framebuffer.h +++ b/src/modules/graphics/opengl/Framebuffer.h @@ -15,6 +15,8 @@ namespace graphics { namespace opengl { + struct Color; // forward declaration for clear + class Framebuffer : public Drawable, public Volatile { public: @@ -29,6 +31,8 @@ namespace opengl void startGrab(); void stopGrab(); + void clear(const Color& c); + virtual void draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const; love::image::ImageData * getImageData(love::image::Image * image); diff --git a/src/modules/graphics/opengl/wrap_Framebuffer.cpp b/src/modules/graphics/opengl/wrap_Framebuffer.cpp index a15c18d41..e3fbcf547 100644 --- a/src/modules/graphics/opengl/wrap_Framebuffer.cpp +++ b/src/modules/graphics/opengl/wrap_Framebuffer.cpp @@ -1,3 +1,4 @@ +#include "Graphics.h" #include "wrap_Framebuffer.h" namespace love @@ -105,6 +106,40 @@ namespace opengl return 2; } + int w_Framebuffer_clear(lua_State * L) + { + Framebuffer * fbo = luax_checkfbo(L, 1); + Color c; + if (lua_isnoneornil(L, 2)) { + c.r = 0; + c.g = 0; + c.b = 0; + c.a = 0; + } else if (lua_istable(L, 2)) { + lua_pushinteger(L, 1); + lua_gettable(L, 2); + c.r = (unsigned char)luaL_checkint(L, -1); + lua_pushinteger(L, 2); + lua_gettable(L, 2); + c.g = (unsigned char)luaL_checkint(L, -1); + lua_pushinteger(L, 3); + lua_gettable(L, 2); + c.b = (unsigned char)luaL_checkint(L, -1); + lua_pushinteger(L, 4); + lua_gettable(L, 2); + c.g = (unsigned char)luaL_optint(L, -1, 255); + lua_pop(L, 4); + } else { + c.r = (unsigned char)luaL_checkint(L, 2); + c.g = (unsigned char)luaL_checkint(L, 3); + c.b = (unsigned char)luaL_checkint(L, 4); + c.a = (unsigned char)luaL_optint(L, 5, 255); + } + fbo->clear(c); + + return 0; + } + static const luaL_Reg functions[] = { { "renderTo", w_Framebuffer_renderTo }, { "getImageData", w_Framebuffer_getImageData }, @@ -112,6 +147,7 @@ namespace opengl { "getFilter", w_Framebuffer_getFilter }, { "setWrap", w_Framebuffer_setWrap }, { "getWrap", w_Framebuffer_getWrap }, + { "clear", w_Framebuffer_clear }, { 0, 0 } }; diff --git a/src/modules/graphics/opengl/wrap_Framebuffer.h b/src/modules/graphics/opengl/wrap_Framebuffer.h index 55bf99da4..1433c2ade 100644 --- a/src/modules/graphics/opengl/wrap_Framebuffer.h +++ b/src/modules/graphics/opengl/wrap_Framebuffer.h @@ -19,6 +19,7 @@ namespace opengl int w_Framebuffer_getFilter(lua_State * L); int w_Framebuffer_setWrap(lua_State * L); int w_Framebuffer_getWrap(lua_State * L); + int w_Framebuffer_clear(lua_State * L); int luaopen_framebuffer(lua_State * L); } // opengl