/** * \brief Implementation of sol.surface.create(). * \param l the Lua context that is calling this function * \return number of values to return to Lua */ int LuaContext::surface_api_create(lua_State* l) { Surface* surface = NULL; if (lua_gettop(l) == 0) { // create an empty surface with the screen size surface = new Surface(VideoManager::get_instance()->get_quest_size()); } else if (lua_type(l, 1) == LUA_TNUMBER) { // create an empty surface with the specified size int width = luaL_checkint(l, 1); int height = luaL_checkint(l, 2); surface = new Surface(width, height); } else if (lua_type(l, 1) == LUA_TSTRING) { // load from a file const std::string& file_name = lua_tostring(l, 1); bool language_specific = lua_toboolean(l, 2); // default is false surface = Surface::create_from_file(file_name, language_specific ? Surface::DIR_LANGUAGE : Surface::DIR_SPRITES); } else { luaL_typerror(l, 1, "number, string or no value"); } if (surface == NULL) { // Image file not found or not valid. lua_pushnil(l); } else { get_lua_context(l).add_drawable(surface); push_surface(l, *surface); } return 1; }
/** * @brief Calls the on_draw() method of the object on top of the stack. * @param dst_surface The destination surface. */ void LuaContext::on_draw(Surface& dst_surface) { if (find_method("on_draw")) { push_surface(l, dst_surface); call_function(2, 0, "on_draw"); } }
/** * \brief Implementation of sol.surface.create(). * \param l The Lua context that is calling this function. * \return Number of values to return to Lua. */ int LuaContext::surface_api_create(lua_State* l) { return LuaTools::exception_boundary_handle(l, [&] { SurfacePtr surface; if (lua_gettop(l) == 0) { // create an empty surface with the screen size surface = Surface::create(Video::get_quest_size()); } else if (lua_type(l, 1) == LUA_TNUMBER) { // create an empty surface with the specified size int width = LuaTools::check_int(l, 1); int height = LuaTools::check_int(l, 2); surface = Surface::create(width, height); } else if (lua_type(l, 1) == LUA_TSTRING) { // load from a file const std::string& file_name = lua_tostring(l, 1); bool language_specific = LuaTools::opt_boolean(l, 2, false); surface = Surface::create(file_name, language_specific ? Surface::DIR_LANGUAGE : Surface::DIR_SPRITES); } else { LuaTools::type_error(l, 1, "number, string or no value"); } if (surface == nullptr) { // Image file not found or not valid. lua_pushnil(l); } else { get_lua_context(l).add_drawable(surface); push_surface(l, *surface); } return 1; }); }