15#include "nlua_canvas.h"
20#include "nlua_colour.h"
23static int nlua_canvas_counter = 0;
24static GLuint previous_fbo = 0;
25static int previous_fbo_set = 0;
26static int was_scissored = 0;
29static int canvasL_gc( lua_State *L );
30static int canvasL_eq( lua_State *L );
31static int canvasL_new( lua_State *L );
32static int canvasL_set( lua_State *L );
33static int canvasL_dims( lua_State *L );
34static int canvasL_getTex( lua_State *L );
35static int canvasL_clear( lua_State *L );
36static const luaL_Reg canvasL_methods[] = {
37 {
"__gc", canvasL_gc },
38 {
"__eq", canvasL_eq },
39 {
"new", canvasL_new },
40 {
"set", canvasL_set },
41 {
"dims", canvasL_dims },
42 {
"getTex", canvasL_getTex },
43 {
"clear", canvasL_clear },
53int nlua_loadCanvas( nlua_env env )
55 nlua_register(env, CANVAS_METATABLE, canvasL_methods, 1);
84LuaCanvas_t* luaL_checkcanvas( lua_State *L,
int ind )
86 if (lua_iscanvas(L,ind))
87 return lua_tocanvas(L,ind);
88 luaL_typerror(L, ind, CANVAS_METATABLE);
102 luaL_getmetatable(L, CANVAS_METATABLE);
103 lua_setmetatable(L, -2);
113int lua_iscanvas( lua_State *L,
int ind )
117 if (lua_getmetatable(L,ind)==0)
119 lua_getfield(L, LUA_REGISTRYINDEX, CANVAS_METATABLE);
122 if (lua_rawequal(L, -1, -2))
135static int canvasL_gc( lua_State *L )
138 glDeleteFramebuffers( 1, &lc->
fbo );
152static int canvasL_eq( lua_State *L )
155 c1 = luaL_checkcanvas(L,1);
156 c2 = luaL_checkcanvas(L,2);
157 lua_pushboolean( L, (memcmp( c1, c2,
sizeof(
LuaCanvas_t) )==0) );
176 SDL_asprintf( &name,
"nlua_canvas_%03d", ++nlua_canvas_counter );
177 lc->
tex = gl_loadImageData( NULL, w, h, 1, 1, name );
182 glGenFramebuffers( 1, &lc->
fbo );
183 glBindFramebuffer(GL_FRAMEBUFFER, lc->
fbo);
186 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, lc->
tex->
texture, 0);
189 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
190 if (status != GL_FRAMEBUFFER_COMPLETE)
194 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
211static int canvasL_new( lua_State *L )
215 int w = luaL_checkint(L,1);
216 int h = luaL_checkint(L,2);
218 if (canvas_new( &lc, w, h ))
219 return NLUA_ERROR( L, _(
"Error setting up framebuffer!"));
220 lua_pushcanvas( L, lc );
231static int canvasL_set( lua_State *L )
233 if (!lua_isnoneornil(L,1)) {
235 if (!previous_fbo_set) {
237 previous_fbo_set = 1;
238 was_scissored = glIsEnabled(GL_SCISSOR_TEST);
241 glDisable(GL_SCISSOR_TEST);
242 glViewport( 0, 0, lc->
tex->
w, lc->
tex->
h );
259static int canvasL_dims( lua_State *L )
262 lua_pushnumber( L, lc->
tex->
w );
263 lua_pushnumber( L, lc->
tex->
h );
274static int canvasL_getTex( lua_State *L )
288static int canvasL_clear( lua_State *L )
292 const glColour *
c = luaL_optcolour(L,2,&cBlack);
293 glClearColor(
c->r,
c->g,
c->b,
c->a );
294 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
295 glClearColor( 0.0, 0.0, 0.0, 1.0 );
302void canvas_reset (
void)
304 if (!previous_fbo_set)
307 previous_fbo_set = 0;
309 glEnable(GL_SCISSOR_TEST);
Header file with generic functions and naev-specifics.
glTexture ** lua_pushtex(lua_State *L, glTexture *texture)
Pushes a texture on the stack.
glTexture * gl_dupTexture(const glTexture *texture)
Duplicates a texture.
void gl_freeTexture(glTexture *texture)
Frees a texture.