/* xlsubr - define a builtin function */ LVAL xlsubr P4C(char *, sname, int, type, subrfun, fcn, int, offset) { LVAL sym; sym = xlenter(sname); #ifdef MULVALS setfunction(sym,cvsubr(fcn, type&TYPEFIELD, offset)); setmulvalp(getfunction(sym), (type & (TYPEFIELD+1)) ? TRUE : FALSE); #else setfunction(sym,cvsubr(fcn,type,offset)); #endif /* MULVALS */ return (sym); }
int FrameBuffer::_new(lua_State *l) { int width = luaL_checkinteger(l, -2); int height = luaL_checkinteger(l, -1); FrameBuffer *self = newuserdata(FrameBuffer); cout << "creating framebuffer " << $(width) << $(height) << endl; self->width = width; self->height = height; self->tex = Image::from_bytes(NULL, width, height); glGenFramebuffersEXT(1, &self->fbo); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, self->fbo); // attach texture to fbo glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, self->tex.texid, 0); // create depth buffer glGenRenderbuffersEXT(1, &self->depth); glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, self->depth); glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width, height); // attach depth to fbo glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, self->depth); // see if it worked GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); if (status != GL_FRAMEBUFFER_COMPLETE_EXT) { cout << "Failed to setup framebuffer" << endl; } glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); if (luaL_newmetatable(l, "FrameBuffer")) { lua_newtable(l); // the index table setfunction("bind", FrameBuffer::_bind); setfunction("release", FrameBuffer::_release); setfunction("render", FrameBuffer::_render); setfunction("bindTex", FrameBuffer::_bindTex); setfunction("draw", FrameBuffer::_draw); lua_setfield(l, -2, "__index"); } lua_setmetatable(l, -2); return 1; }
/* SHLIB-INIT funtab &optional (version -1) (oldest version) */ LVAL xshlibinit() { LVAL subr, val, sym; xlshlib_modinfo_t *info = getnpaddr(xlganatptr()); FUNDEF *p = info->funs; FIXCONSTDEF *pfix = info->fixconsts; FLOCONSTDEF *pflo = info->floconsts; STRCONSTDEF *pstr = info->strconsts; struct version_info defversion; defversion.current = moreargs()?getfixnum(xlgafixnum()):-1; defversion.oldest = moreargs()?getfixnum(xlgafixnum()):defversion.current; xllastarg(); if (! check_version(&defsysversion, &(info->sysversion))) xlfail("shared library not compatible with current system"); if (defversion.current >= 0 && ! check_version(&defversion, &(info->modversion))) xlfail("module not compatible with requested version"); xlsave1(val); val = NIL; if (p != NULL) for (val = NIL; (p->fd_subr) != (LVAL(*)(void)) NULL; p++) { subr = cvsubr(p->fd_subr, p->fd_type & TYPEFIELD, 0); setmulvalp(subr, (p->fd_type & (TYPEFIELD + 1)) ? TRUE : FALSE); val = cons(subr, val); if (p->fd_name != NULL) { sym = xlenter(p->fd_name); setfunction(sym, subr); } } if (pfix != NULL) for (; pfix->name != NULL; pfix++) { sym = xlenter(pfix->name); defconstant(sym, cvfixnum(pfix->val)); } if (pflo != NULL) for (; pflo->name != NULL; pflo++) { sym = xlenter(pflo->name); defconstant(sym, cvflonum(pflo->val)); } if (pstr != NULL) for (; pstr->name != NULL; pstr++) { sym = xlenter(pstr->name); defconstant(sym, cvstring(pstr->val)); } if (info->sysversion.current >= MAKEVERSION(0,1)) { ULONGCONSTDEF *pulong = info->ulongconsts; if (pulong != NULL) for (; pulong->name != NULL; pulong++) { sym = xlenter(pulong->name); defconstant(sym, ulong2lisp(pulong->val)); } } xlpop(); return xlnreverse(val); }
// Restore the symbol values to their original value and remove any added // symbols. LOCAL void nyx_restore_obarray() { LVAL obvec = getvalue(obarray); int i; // Scan all obarray vectors for (i = 0; i < HSIZE; i++) { LVAL last = NULL; LVAL dcon; // Scan all elements for (dcon = getelement(obvec, i); dcon; dcon = cdr(dcon)) { LVAL dsym = car(dcon); char *name = (char *)getstring(getpname(dsym)); LVAL scon; // Ignore *OBARRAY* since setting it causes the input array to be // truncated. if (strcmp(name, "*OBARRAY*") == 0) { continue; } // Ignore *SCRATCH* since it's allowed to be updated if (strcmp(name, "*SCRATCH*") == 0) { continue; } // Find the symbol in the original obarray. for (scon = getelement(nyx_obarray, hash(name, HSIZE)); scon; scon = cdr(scon)) { LVAL ssym = car(scon); // If found, then set the current symbols value to the original. if (strcmp(name, (char *)getstring(getpname(ssym))) == 0) { setvalue(dsym, nyx_dup_value(getvalue(ssym))); setplist(dsym, nyx_dup_value(getplist(ssym))); setfunction(dsym, nyx_dup_value(getfunction(ssym))); break; } } // If we didn't find the symbol in the original obarray, then it must've // been added and must be removed from the current obarray. if (scon == NULL) { if (last) { rplacd(last, cdr(dcon)); } else { setelement(obvec, i, cdr(dcon)); } } // Must track the last dcon for symbol removal last = dcon; } } }
/* cvsymbol - convert a string to a symbol */ LVAL cvsymbol(char *pname) { LVAL val; xlsave1(val); val = newvector(SYMSIZE); val->n_type = SYMBOL; setvalue(val,s_unbound); setfunction(val,s_unbound); setpname(val,cvstring(pname)); xlpop(); return (val); }
/* xlsetfunction - set the functional value of a symbol */ void xlsetfunction(LVAL sym, LVAL val) { register LVAL fp,ep; /* look for the symbol in the environment list */ for (fp = xlfenv; fp; fp = cdr(fp)) for (ep = car(fp); ep; ep = cdr(ep)) if (sym == car(car(ep))) { rplacd(car(ep),val); return; } /* store the global value */ setfunction(sym,val); }
// Make a copy of the original obarray, leaving the original in place LOCAL void nyx_save_obarray() { LVAL newarray; int i; // This provide permanent protection for nyx_obarray as we do not want it // to be garbage-collected. xlprot1(nyx_obarray); nyx_obarray = getvalue(obarray); // Create and set the new vector. This allows us to use xlenter() to // properly add the new symbol. Probably slower than adding directly, // but guarantees proper hashing. newarray = newvector(HSIZE); setvalue(obarray, newarray); // Scan all obarray vectors for (i = 0; i < HSIZE; i++) { LVAL sym; // Scan all elements for (sym = getelement(nyx_obarray, i); sym; sym = cdr(sym)) { LVAL syma = car(sym); char *name = (char *) getstring(getpname(syma)); LVAL nsym = xlenter(name); // Ignore *OBARRAY* since there's no need to copy it if (strcmp(name, "*OBARRAY*") == 0) { continue; } // Ignore *SCRATCH* since it's allowed to be updated if (strcmp(name, "*SCRATCH*") == 0) { continue; } // Duplicate the symbol's values setvalue(nsym, nyx_dup_value(getvalue(syma))); setplist(nsym, nyx_dup_value(getplist(syma))); setfunction(nsym, nyx_dup_value(getfunction(syma))); } } // Swap the obarrays, so that the original is put back into service setvalue(obarray, nyx_obarray); nyx_obarray = newarray; }
/* initwks - build an initial workspace */ LOCAL void initwks(void) { FUNDEF *p; int i; xlsinit(); /* initialize xlsym.c */ xlsymbols();/* enter all symbols used by the interpreter */ xlrinit(); /* initialize xlread.c */ xloinit(); /* initialize xlobj.c */ /* setup defaults */ setvalue(s_evalhook,NIL); /* no evalhook function */ setvalue(s_applyhook,NIL); /* no applyhook function */ setvalue(s_tracelist,NIL); /* no functions being traced */ setvalue(s_tracenable,NIL); /* traceback disabled */ setvalue(s_tlimit,NIL); /* trace limit infinite */ setvalue(s_breakenable,NIL); /* don't enter break loop on errors */ setvalue(s_loadingfiles,NIL); /* not loading any files initially */ setvalue(s_profile,NIL); /* don't do profiling */ setvalue(s_gcflag,NIL); /* don't show gc information */ setvalue(s_gchook,NIL); /* no gc hook active */ setvalue(s_ifmt,cvstring(IFMT)); /* integer print format */ setvalue(s_ffmt,cvstring("%g")); /* float print format */ setvalue(s_printcase,k_upcase); /* upper case output of symbols */ /* install the built-in functions and special forms */ for (i = 0, p = funtab; p->fd_subr != NULL; ++i, ++p) if (p->fd_name) xlsubr(p->fd_name,p->fd_type,p->fd_subr,i); /* add some synonyms */ setfunction(xlenter("NOT"),getfunction(xlenter("NULL"))); setfunction(xlenter("FIRST"),getfunction(xlenter("CAR"))); setfunction(xlenter("SECOND"),getfunction(xlenter("CADR"))); setfunction(xlenter("THIRD"),getfunction(xlenter("CADDR"))); setfunction(xlenter("FOURTH"),getfunction(xlenter("CADDDR"))); setfunction(xlenter("REST"),getfunction(xlenter("CDR"))); }
void register_Framebuffer(lua_State *l) { setfunction("framebuffer", FrameBuffer::_new); }
/** * create a new canvas * args: width, height, [title] */ int Canvas::_new(lua_State *l) { const char *title = "Aroma"; int width = luaL_checkint(l, 2); int height = luaL_checkint(l, 3); if (lua_gettop(l) > 3) { title = luaL_checkstring(l, 4); } GLContext* context = new GLFWContext(width, height, title); if (!context->make_current()) { return luaL_error(l, "fatal error: failed to open window"); } _canvas = new Canvas(context); Viewport &view = _canvas->view; lua_newtable(l); // functions setfunction("run", Canvas::_run); setfunction("rect", Canvas::_rect); setfunction("line", Canvas::_line); setfunction("viewport", Canvas::_viewport); setfunction("view3d", Canvas::_view3d); setfunction("look", Canvas::_look); setfunction("strip", Canvas::_strip); setfunction("rotate", Canvas::_rotate); setfunction("scale", Canvas::_scale); setfunction("translate", Canvas::_translate); setfunction("noise", Canvas::_noise); setfunction("save", Canvas::_save); setfunction("restore", Canvas::_restore); setfunction("getTime", Canvas::_getTime); setfunction("clear_color", Canvas::_clearColor); setfunction("clear", Canvas::_clear); setfunction("flush", Canvas::_flush); setfunction("set_mouse", Canvas::_setMouse); setfunction("hide_mouse", Canvas::_hideMouse); setfunction("show_mouse", Canvas::_showMouse); setfunction("key", Canvas::_key); setfunction("key_up", Canvas::_key_up); setfunction("key_down", Canvas::_key_down); // load libraries AromaRegister *lib = aroma_libs; while (*lib) { (*lib++)(l); } // properties setnumber("dt", 0); setnumber("time", glfwGetTime()); setnumber("width", view.getWidth()); setnumber("height", view.getHeight()); // mouse input lua_newtable(l); setint("x", 0); setint("y", 0); setbool("left", false); setbool("right", false); lua_setfield(l, -2, "mouse"); // create the input table lua_newtable(l); setnumber("xaxis", 0); setnumber("yaxis", 0); setbool("left", false); setbool("right", false); setbool("up", false); setbool("down", false); setbool("a", false); setbool("b", false); setbool("c", false); setbool("d", false); setbool("start", false); setbool("select", false); setbool("l", false); setbool("r", false); lua_setfield(l, -2, "input"); // the keys push_key_table(l); lua_setfield(l, -2, "keys"); // create meta table lua_newtable(l); setfunction("__call", _call); lua_setmetatable(l, -2); return 1; }