15#include "nlua_canvas.h"
20#include "nlua_colour.h"
22static int nlua_canvas_counter = 0;
23static GLuint previous_fbo = 0;
24static int previous_fbo_set = 0;
25static int was_scissored = 0;
28static int canvasL_gc( lua_State *L );
29static int canvasL_eq( lua_State *L );
30static int canvasL_new( lua_State *L );
31static int canvasL_set( lua_State *L );
32static int canvasL_dims( lua_State *L );
33static int canvasL_getTex( lua_State *L );
34static int canvasL_clear( lua_State *L );
35static const luaL_Reg canvasL_methods[] = {
36 {
"__gc", canvasL_gc },
37 {
"__eq", canvasL_eq },
38 {
"new", canvasL_new },
39 {
"set", canvasL_set },
40 {
"dims", canvasL_dims },
41 {
"getTex", canvasL_getTex },
42 {
"clear", canvasL_clear },
52int nlua_loadCanvas( nlua_env env )
54 nlua_register(env, CANVAS_METATABLE, canvasL_methods, 1);
83LuaCanvas_t* luaL_checkcanvas( lua_State *L,
int ind )
85 if (lua_iscanvas(L,ind))
86 return lua_tocanvas(L,ind);
87 luaL_typerror(L, ind, CANVAS_METATABLE);
101 luaL_getmetatable(L, CANVAS_METATABLE);
102 lua_setmetatable(L, -2);
112int lua_iscanvas( lua_State *L,
int ind )
116 if (lua_getmetatable(L,ind)==0)
118 lua_getfield(L, LUA_REGISTRYINDEX, CANVAS_METATABLE);
121 if (lua_rawequal(L, -1, -2))
134static int canvasL_gc( lua_State *L )
137 glDeleteFramebuffers( 1, &lc->
fbo );
151static int canvasL_eq( lua_State *L )
154 c1 = luaL_checkcanvas(L,1);
155 c2 = luaL_checkcanvas(L,2);
156 lua_pushboolean( L, (memcmp( c1, c2,
sizeof(
LuaCanvas_t) )==0) );
175 asprintf( &name,
"nlua_canvas_%03d", ++nlua_canvas_counter );
176 lc->
tex = gl_loadImageData( NULL, w, h, 1, 1, name );
181 glGenFramebuffers( 1, &lc->
fbo );
182 glBindFramebuffer(GL_FRAMEBUFFER, lc->
fbo);
185 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, lc->
tex->
texture, 0);
188 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
189 if (status != GL_FRAMEBUFFER_COMPLETE)
193 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
210static int canvasL_new( lua_State *L )
214 int w = luaL_checkint(L,1);
215 int h = luaL_checkint(L,2);
217 if (canvas_new( &lc, w, h ))
218 NLUA_ERROR( L, _(
"Error setting up framebuffer!"));
219 lua_pushcanvas( L, lc );
230static int canvasL_set( lua_State *L )
234 if (lua_iscanvas(L,1)) {
235 lc = luaL_checkcanvas(L,1);
236 if (!previous_fbo_set) {
238 previous_fbo_set = 1;
239 was_scissored = glIsEnabled(GL_SCISSOR_TEST);
242 glDisable(GL_SCISSOR_TEST);
243 glViewport( 0, 0, lc->
tex->
w, lc->
tex->
h );
246 else if ((lua_gettop(L)<=0) || lua_isnil(L,1)) {
248 previous_fbo_set = 0;
250 glEnable(GL_SCISSOR_TEST);
255 NLUA_ERROR(L,_(
"Unexpected parameter"));
267static int canvasL_dims( lua_State *L )
270 lua_pushnumber( L, lc->
tex->
w );
271 lua_pushnumber( L, lc->
tex->
h );
282static int canvasL_getTex( lua_State *L )
296static int canvasL_clear( lua_State *L )
300 const glColour *
c = luaL_optcolour(L,2,&cBlack);
301 glClearColor(
c->r,
c->g,
c->b,
c->a );
302 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
303 glClearColor( 0.0, 0.0, 0.0, 1.0 );
Header file with generic functions and naev-specifics.
glTexture ** lua_pushtex(lua_State *L, glTexture *texture)
Pushes a texture on the stack.
int asprintf(char **strp, const char *fmt,...)
Like sprintf(), but it allocates a large-enough string and returns the pointer in the first argument....
glTexture * gl_dupTexture(const glTexture *texture)
Duplicates a texture.
void gl_freeTexture(glTexture *texture)
Frees a texture.