static int Lua_FS_dofile(lua_State *L) { const char *filename = luaL_optstring(L, 1, NULL); int n = lua_gettop(L); if (PHYSFS_exists(filename)) { if (FS_loadFile(L, filename) == FILEIO_ERROR) { return lua_error(L); } } else if (luaL_loadfile(L, filename) != 0) { return lua_error(L); } lua_call(L, 0, LUA_MULTRET); return lua_gettop(L) - n; }
/* based on code from lua.c of the Lua distribution */ static int loader_PhysFS(lua_State *L) { const char *filename; const char *name = luaL_checkstring(L, 1); int result; filename = findfile(L, name); if (filename == NULL) { return 1; /* library not found in this path */ } result = FS_loadFile(L, filename); if (result == FILEIO_ERROR) { return luaL_error(L, "error loading module " LUA_QS " from file " LUA_QL("searchpath://%s") ":\n\t%s", lua_tostring(L, 1), filename, lua_tostring(L, -1)); } return 1; /* library loaded successfully */ }
/* main - function - entry point */ int main(int argc, char *argv[]) { lua_State *L; SDL_Event event; Uint32 lastTick; /* Last iteration's tick value */ Uint32 delta = 0; int i, n, narg; char *filename = NULL; fprintf(stdout, "%s v%s\n", PROG_NAME, VERSION); if (argc > 1) { n = 1; if (strcmp(argv[1], "--") != 0) filename = argv[1]; } else { n = argc - 1; } L = luaL_newstate(); /* initialize Lua */ luaL_openlibs(L); /* load Lua base libraries */ FS_Init(L, argv, &filename); /* initialize virtual filesystem */ /* register Lua functions */ lua_newtable(L); luaopen_main(L, NULL); luaopen_fileio(L, NULL); luaopen_font(L, NULL); luaopen_graphics(L, NULL); luaopen_sound(L, NULL); luaopen_mouse(L, NULL); luaopen_keyboard(L, NULL); luaopen_movie(L, NULL); lua_setglobal(L, NAMESPACE); /* put luaopen_oocairo in package.preload["oocairo"] */ lua_getglobal(L, "package"); lua_getfield(L, -1, "preload"); lua_pushcfunction(L, luaopen_oocairo); lua_setfield(L, -2, "oocairo"); /* put luaopen_socket_core in package.preload["socket.core"] */ lua_pushcfunction(L, luaopen_socket_core); lua_setfield(L, -2, "socket.core"); /* put luaopen_mime_core in package.preload["mime.core"] */ lua_pushcfunction(L, luaopen_mime_core); lua_setfield(L, -2, "mime.core"); lua_pop(L, 2); /* push the error function for the protected calls later on */ lua_pushcfunction(L, error_function); /* load and compile main lua file */ if (FS_loadFile(L, filename) == FILEIO_ERROR) { error(L, "Error loading '%s':\n\t%s", filename, lua_tostring(L, -1)); } /* push all script arguments */ /* create arg-table for the command-line-arguments */ /* copied from lua.c of the Lua distribution */ narg = argc - (n+1); /* number of arguments to the script */ luaL_checkstack(L, narg+2, "too many arguments to script"); for (i=n+1; i<argc; i++) { lua_pushstring(L, argv[i]); } lua_createtable(L, narg, n+1); for (i=0; i<argc; i++) { lua_pushstring(L, argv[i]); lua_rawseti(L, -2, i - n); } lua_setglobal(L, "arg"); /* run the compiled chunk */ if ((lua_pcall(L, narg, 0, -(narg+2)) != 0) && !check_for_exit(L)) { error(L, "Error running '%s':\n\t%s", filename, lua_tostring(L, -1)); } /* the error function stays on the stack */ if (SDL_GetVideoSurface() == NULL) error(L, "Error: " PROG_NAME " was not initialized by " NAMESPACE ".init()!\n"); /* main loop */ /* Wait until SDL_QUIT event type (window closed) or a call to scrupp.exit() occurs */ while ( !done ) { lastTick = SDL_GetTicks(); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); /* maybe the 'main' table has changed so get a new reference in every cycle */ lua_getglobal(L, "main"); if (lua_isnil(L, -1)) { error(L, "Error: Table 'main' not found!\n"); } /* at this point, the stack always contains the error function and the 'main' table */ /* main.render(delta) */ lua_getfield(L, -1, "render"); lua_pushinteger(L, delta); if ((lua_pcall(L, 1, 0, -4) != 0) && !check_for_exit(L)) { error(L, "Error running main.render:\n\t%s\n", lua_tostring(L, -1)); } SDL_GL_SwapBuffers(); while ( SDL_PollEvent( &event ) ) { switch ( event.type ) { case SDL_QUIT: done = 1; break; case SDL_KEYDOWN: lua_getfield(L, -1, "keypressed"); if (lua_isnil(L, -1)) { lua_pop(L, 1); /* pop if it's nil */ break; } lua_pushliteral(L, "Scrupp:key_table"); lua_rawget(L, LUA_REGISTRYINDEX); lua_rawgeti(L, -1, event.key.keysym.sym); lua_remove(L, -2); /* remove the key_table */ if ((lua_pcall(L, 1, 0, -4) != 0) && !check_for_exit(L)) { error(L, "Error running main.keypressed:\n\t%s\n", lua_tostring(L, -1)); } break; case SDL_KEYUP: lua_getfield(L, -1, "keyreleased"); if (lua_isnil(L, -1)) { lua_pop(L, 1); /* pop if it's nil */ break; } lua_pushliteral(L, "Scrupp:key_table"); lua_rawget(L, LUA_REGISTRYINDEX); lua_rawgeti(L, -1, event.key.keysym.sym); lua_remove(L, -2); /* remove the key_table */ if ((lua_pcall(L, 1, 0, -4) != 0) && !check_for_exit(L)) { error(L, "Error running main.keyreleased:\n\t%s\n", lua_tostring(L, -1)); } break; case SDL_MOUSEBUTTONDOWN: lua_getfield(L, -1, "mousepressed"); if (lua_isnil(L, -1)) { lua_pop(L, 1); /* pop if it's nil */ break; } lua_pushinteger(L, event.button.x); lua_pushinteger(L, event.button.y); lua_pushstring(L, buttonNames[event.button.button-1]); if ((lua_pcall(L, 3, 0, -6) != 0) && !check_for_exit(L)) { error(L, "Error running main.mousepressed:\n\t%s\n", lua_tostring(L, -1)); } break; case SDL_MOUSEBUTTONUP: lua_getfield(L, -1, "mousereleased"); if (lua_isnil(L, -1)) { lua_pop(L, 1); /* pop if it's nil */ break; } lua_pushinteger(L, event.button.x); lua_pushinteger(L, event.button.y); lua_pushstring(L, buttonNames[event.button.button-1]); if ((lua_pcall(L, 3, 0, -6) != 0) && !check_for_exit(L)) { error(L, "Error running main.mousereleased:\n\t%s\n", lua_tostring(L, -1)); } break; case SDL_VIDEORESIZE: lua_getfield(L, -1, "resized"); if (lua_isnil(L, -1)) { lua_pop(L, 1); /* pop if it's nil */ break; } lua_pushinteger(L, event.resize.w); lua_pushinteger(L, event.resize.h); if ((lua_pcall(L, 2, 0, -5) != 0) && !check_for_exit(L)) { error(L, "Error running main.resized:\n\t%s\n", lua_tostring(L, -1)); } break; } } lua_pop(L, 1); /* pop 'main' table */ delta = SDL_GetTicks() - lastTick; /* if (delta>1) { fprintf(stdout, "delta: %d\n", delta); } */ while (delta < minimumDelta) { SDL_Delay(1); delta = SDL_GetTicks() - lastTick; } } lua_close(L); return 0; }