13#include "nlua_canvas.h"
15#include "nlua_colour.h"
20static int nlua_canvas_counter = 0;
21static GLuint previous_fbo = 0;
22static int previous_fbo_set = 0;
23static int was_scissored = 0;
26static int canvasL_gc( lua_State *L );
27static int canvasL_eq( lua_State *L );
28static int canvasL_new( lua_State *L );
29static int canvasL_set( lua_State *L );
30static int canvasL_dims( lua_State *L );
31static int canvasL_getTex( lua_State *L );
32static int canvasL_clear( lua_State *L );
34static const luaL_Reg canvasL_methods[] = {
35 {
"__gc", canvasL_gc }, {
"__eq", canvasL_eq },
36 {
"new", canvasL_new }, {
"set", canvasL_set },
37 {
"dims", canvasL_dims }, {
"getTex", canvasL_getTex },
38 {
"clear", canvasL_clear }, { 0, 0 } };
46int nlua_loadCanvas( nlua_env env )
48 nlua_register( env, CANVAS_METATABLE, canvasL_methods, 1 );
77LuaCanvas_t *luaL_checkcanvas( lua_State *L,
int ind )
79 if ( lua_iscanvas( L, ind ) )
80 return lua_tocanvas( L, ind );
81 luaL_typerror( L, ind, CANVAS_METATABLE );
95 luaL_getmetatable( L, CANVAS_METATABLE );
96 lua_setmetatable( L, -2 );
106int lua_iscanvas( lua_State *L,
int ind )
110 if ( lua_getmetatable( L, ind ) == 0 )
112 lua_getfield( L, LUA_REGISTRYINDEX, CANVAS_METATABLE );
115 if ( lua_rawequal( L, -1, -2 ) )
128static int canvasL_gc( lua_State *L )
131 glDeleteFramebuffers( 1, &lc->
fbo );
133 if ( lc->
depth != 0 )
134 glDeleteTextures( 1, &lc->
depth );
136 NLUA_ERROR( L, _(
"OpenGL Error!" ) );
148static int canvasL_eq( lua_State *L )
152 lua_pushboolean( L, ( memcmp( c1, c2,
sizeof(
LuaCanvas_t ) ) == 0 ) );
170 SDL_asprintf( &name,
"nlua_canvas_%03d", ++nlua_canvas_counter );
171 lc->
tex = gl_loadImageData( NULL, w, h, 1, 1, name );
194static int canvasL_new( lua_State *L )
198 int w = luaL_checkint( L, 1 );
199 int h = luaL_checkint( L, 2 );
200 int depth = lua_toboolean( L, 3 );
202 if ( canvas_new( &lc, w, h ) )
203 return NLUA_ERROR( L, _(
"Error setting up framebuffer!" ) );
206 lua_pushcanvas( L, lc );
218static int canvasL_set( lua_State *L )
220 if ( !lua_isnoneornil( L, 1 ) ) {
222 if ( !previous_fbo_set ) {
224 previous_fbo_set = 1;
225 was_scissored = glIsEnabled( GL_SCISSOR_TEST );
228 glDisable( GL_SCISSOR_TEST );
229 glViewport( 0, 0, lc->
tex->
w, lc->
tex->
h );
230 glBindFramebuffer( GL_FRAMEBUFFER,
gl_screen.current_fbo );
245static int canvasL_dims( lua_State *L )
248 lua_pushnumber( L, lc->
tex->
w );
249 lua_pushnumber( L, lc->
tex->
h );
260static int canvasL_getTex( lua_State *L )
274static int canvasL_clear( lua_State *L )
278 const glColour *
c = luaL_optcolour( L, 2, &cBlack );
279 glClearColor(
c->r,
c->g,
c->b,
c->a );
280 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
281 glClearColor( 0.0, 0.0, 0.0, 0.0 );
288void canvas_reset(
void )
290 if ( !previous_fbo_set )
293 previous_fbo_set = 0;
295 glEnable( GL_SCISSOR_TEST );
297 glBindFramebuffer( GL_FRAMEBUFFER,
gl_screen.current_fbo );
glTexture ** lua_pushtex(lua_State *L, glTexture *texture)
Pushes a texture on the stack.
glTexture * gl_dupTexture(const glTexture *texture)
Duplicates a texture.
int gl_fboCreate(GLuint *fbo, GLuint *tex, GLsizei width, GLsizei height)
Creates a framebuffer and its associated texture.
int gl_fboAddDepth(GLuint fbo, GLuint *tex, GLsizei width, GLsizei height)
Adds a depth attachment to an FBO.
void gl_freeTexture(glTexture *texture)
Frees a texture.