// Expects errorhandler at index 1 void pcall(lua_State *state, int nargs) { if(lua_pcall(state, nargs, 0, 1)) { char const *msg = lua_tostring(state, -1); printf("Lua error: %s\n", msg); graphics_Font font; graphics_setBackgroundColor(0.64f, 0.27f, 0.26f, 1.0f); graphics_clear(); graphics_setColor(1.0f, 1.0f, 1.0f, 0.8f); graphics_Font_new(&font, 0, 12); graphics_Font_render(&font, msg, 10, 40, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f); graphics_swap(); #ifdef EMSCRIPTEN emscripten_force_exit(1); #else exit(1); #endif } }
static void l_graphics_loadDefaultFont() { graphics_Font_new(&moduleData.defaultFont, NULL, 12); moduleData.currentFont = &moduleData.defaultFont; }
int l_graphics_newFont(lua_State* state) { // TODO: alternative signatures for newFont char const * filename = NULL; int ptsize; const int type = lua_type(state, 1); if(type == LUA_TSTRING) { printf("a\n"); filename = lua_tostring(state, 1); if(lua_isnumber(state, 2)) { printf("b\n"); ptsize = lua_tonumber(state, 2); } else { printf("c\n"); ptsize = 12; lua_settop(state, 1); lua_pushnumber(state, ptsize); } lua_settop(state, 2); } else if(type == LUA_TNUMBER) { printf("d\n"); ptsize = l_tools_toNumberOrError(state, 1); lua_settop(state, 1); lua_pushstring(state, "(default)"); lua_insert(state, 1); } // Create string font:size // Stack: ... fontname lua_pushstring(state, ":"); lua_insert(state, -2); lua_concat(state, 3); printf("font: %s\n", lua_tostring(state, -1)); // Load font table to -2 // Stack: ... fonts fontname lua_rawgeti(state, LUA_REGISTRYINDEX, moduleData.loadedFontsRef); lua_insert(state, -2); // Save fontname for later // Stack: ... fonts fontname fontname lua_pushvalue(state, -1); // Load font // Stack: ... fonts fontname maybefont lua_gettable(state, -3); if(lua_isnoneornil(state, -1)) { // Stack: ... fonts fontname lua_pop(state, 1); // Stack: ... fonts fontname raw-font graphics_Font* font = lua_newuserdata(state, sizeof(graphics_Font)); if(graphics_Font_new(font, filename, ptsize)) { lua_pushstring(state, "Could not open font"); lua_error(state); } // Stack: ... fonts fontname raw-font metatable lua_rawgeti(state, LUA_REGISTRYINDEX, moduleData.fontMT); // Stack: ... fonts fontname constructed-font lua_setmetatable(state, -2); // Stack: ... fonts fontname constructed-font constructed-font lua_pushvalue(state, -1); // Stack: ... fonts constructed-font fontname constructed-font lua_insert(state, -3); // Stack: ... fonts constructed-font lua_settable(state, -4); } return 1; }