51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-extern "C" {
-#include "lua.h"
-#include "lauxlib.h"
-#include "lualib.h"
-}
-
-#include "irrlicht.h"
+#include "guiEngine.h"
+#include "scripting_mainmenu.h"
+#include "config.h"
+#include "version.h"
#include "porting.h"
#include "filesys.h"
#include "main.h"
#include "settings.h"
#include "guiMainMenu.h"
+#include "sound.h"
+#include "sound_openal.h"
+#include "clouds.h"
-#include "guiEngine.h"
+#include <IGUIStaticText.h>
+#include <ICameraSceneNode.h>
#if USE_CURL
#include <curl/curl.h>
#endif
/******************************************************************************/
-int menuscript_ErrorHandler(lua_State *L) {
- lua_getfield(L, LUA_GLOBALSINDEX, "debug");
- if (!lua_istable(L, -1)) {
- lua_pop(L, 1);
- return 1;
- }
- lua_getfield(L, -1, "traceback");
- if (!lua_isfunction(L, -1)) {
- lua_pop(L, 2);
- return 1;
- }
- lua_pushvalue(L, 1);
- lua_pushinteger(L, 2);
- lua_call(L, 2, 1);
- return 1;
-}
-
+/** TextDestGuiEngine */
/******************************************************************************/
TextDestGuiEngine::TextDestGuiEngine(GUIEngine* engine)
{
/******************************************************************************/
void TextDestGuiEngine::gotText(std::map<std::string, std::string> fields)
{
- m_engine->handleButtons(fields);
+ m_engine->getScriptIface()->handleMainMenuButtons(fields);
}
/******************************************************************************/
void TextDestGuiEngine::gotText(std::wstring text)
{
- m_engine->handleEvent(wide_to_narrow(text));
+ m_engine->getScriptIface()->handleMainMenuEvent(wide_to_narrow(text));
}
+/******************************************************************************/
+/** MenuTextureSource */
+/******************************************************************************/
+MenuTextureSource::MenuTextureSource(video::IVideoDriver *driver)
+{
+ m_driver = driver;
+}
+
+/******************************************************************************/
+MenuTextureSource::~MenuTextureSource()
+{
+ for (std::set<std::string>::iterator it = m_to_delete.begin();
+ it != m_to_delete.end(); ++it) {
+ const char *tname = (*it).c_str();
+ video::ITexture *texture = m_driver->getTexture(tname);
+ m_driver->removeTexture(texture);
+ }
+}
+
+/******************************************************************************/
+video::ITexture* MenuTextureSource::getTexture(const std::string &name, u32 *id)
+{
+ if(id)
+ *id = 0;
+ if(name.empty())
+ return NULL;
+ m_to_delete.insert(name);
+ return m_driver->getTexture(name.c_str());
+}
+
+/******************************************************************************/
+/** MenuMusicFetcher */
+/******************************************************************************/
+void MenuMusicFetcher::fetchSounds(const std::string &name,
+ std::set<std::string> &dst_paths,
+ std::set<std::string> &dst_datas)
+{
+ if(m_fetched.count(name))
+ return;
+ m_fetched.insert(name);
+ std::string base;
+ base = porting::path_share + DIR_DELIM + "sounds";
+ dst_paths.insert(base + DIR_DELIM + name + ".ogg");
+ int i;
+ for(i=0; i<10; i++)
+ dst_paths.insert(base + DIR_DELIM + name + "."+itos(i)+".ogg");
+ base = porting::path_user + DIR_DELIM + "sounds";
+ dst_paths.insert(base + DIR_DELIM + name + ".ogg");
+ for(i=0; i<10; i++)
+ dst_paths.insert(base + DIR_DELIM + name + "."+itos(i)+".ogg");
+}
+
+/******************************************************************************/
+/** GUIEngine */
/******************************************************************************/
GUIEngine::GUIEngine( irr::IrrlichtDevice* dev,
gui::IGUIElement* parent,
IMenuManager *menumgr,
scene::ISceneManager* smgr,
- MainMenuData* data) :
+ MainMenuData* data,
+ bool& kill) :
m_device(dev),
m_parent(parent),
m_menumanager(menumgr),
m_smgr(smgr),
m_data(data),
+ m_texture_source(NULL),
+ m_sound_manager(NULL),
m_formspecgui(0),
m_buttonhandler(0),
m_menu(0),
+ m_kill(kill),
m_startgame(false),
- m_engineluastack(0),
- m_luaerrorhandler(-1),
+ m_script(0),
m_scriptdir(""),
m_irr_toplefttext(0),
m_clouds_enabled(true),
// is deleted by guiformspec!
m_buttonhandler = new TextDestGuiEngine(this);
- //create luastack
- m_engineluastack = luaL_newstate();
+ //create texture source
+ m_texture_source = new MenuTextureSource(m_device->getVideoDriver());
- //load basic lua modules
- luaL_openlibs(m_engineluastack);
-
- //init
- guiLuaApi::initialize(m_engineluastack,this);
-
- //push errorstring
- if (m_data->errormessage != "")
- {
- lua_getglobal(m_engineluastack, "gamedata");
- int gamedata_idx = lua_gettop(m_engineluastack);
- lua_pushstring(m_engineluastack, "errormessage");
- lua_pushstring(m_engineluastack,m_data->errormessage.c_str());
- lua_settable(m_engineluastack, gamedata_idx);
- m_data->errormessage = "";
- }
+ //create soundmanager
+ MenuMusicFetcher soundfetcher;
+#if USE_SOUND
+ m_sound_manager = createOpenALSoundManager(&soundfetcher);
+#endif
+ if(!m_sound_manager)
+ m_sound_manager = &dummySoundManager;
//create topleft header
core::rect<s32> rect(0, 0, 500, 40);
rect += v2s32(4, 0);
- std::string t = "Minetest " VERSION_STRING;
+ std::string t = std::string("Minetest ") + minetest_version_hash;
m_irr_toplefttext =
m_device->getGUIEnvironment()->addStaticText(narrow_to_wide(t).c_str(),
-1,
m_menumanager,
0 /* &client */,
- 0 /* gamedef */);
+ 0 /* gamedef */,
+ m_texture_source);
m_menu->allowClose(false);
m_menu->lockSize(true,v2u32(800,600));
m_menu->setFormSource(m_formspecgui);
m_menu->setTextDest(m_buttonhandler);
- m_menu->useGettext(true);
- std::string builtin_helpers
- = porting::path_share + DIR_DELIM + "builtin"
- + DIR_DELIM + "mainmenu_helper.lua";
+ // Initialize scripting
- if (!runScript(builtin_helpers)) {
- errorstream
- << "GUIEngine::GUIEngine unable to load builtin helper script"
- << std::endl;
- return;
- }
+ infostream<<"GUIEngine: Initializing Lua"<<std::endl;
- std::string menuscript = "";
- if (g_settings->exists("main_menu_script"))
- menuscript = g_settings->get("main_menu_script");
- std::string builtin_menuscript =
- porting::path_share + DIR_DELIM + "builtin"
- + DIR_DELIM + "mainmenu.lua";
+ m_script = new MainMenuScripting(this);
- lua_pushcfunction(m_engineluastack, menuscript_ErrorHandler);
- m_luaerrorhandler = lua_gettop(m_engineluastack);
+ try {
+ if (m_data->errormessage != "")
+ {
+ m_script->setMainMenuErrorMessage(m_data->errormessage);
+ m_data->errormessage = "";
+ }
- m_scriptdir = menuscript.substr(0,menuscript.find_last_of(DIR_DELIM)-1);
- if((menuscript == "") || (!runScript(menuscript))) {
- infostream
- << "GUIEngine::GUIEngine execution of custom menu failed!"
- << std::endl
- << "\tfalling back to builtin menu"
- << std::endl;
- m_scriptdir = fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "builtin"+ DIR_DELIM);
- if(!runScript(builtin_menuscript)) {
- errorstream
- << "GUIEngine::GUIEngine unable to load builtin menu"
- << std::endl;
+ if (!loadMainMenuScript())
assert("no future without mainmenu" == 0);
- }
- }
- run();
+ run();
+ }
+ catch(LuaError &e) {
+ errorstream << "MAINMENU ERROR: " << e.what() << std::endl;
+ m_data->errormessage = e.what();
+ }
- m_menumanager->deletingMenu(m_menu);
+ m_menu->quitMenu();
m_menu->drop();
m_menu = 0;
}
/******************************************************************************/
-bool GUIEngine::runScript(std::string script) {
-
- int ret = luaL_loadfile(m_engineluastack, script.c_str()) ||
- lua_pcall(m_engineluastack, 0, 0, m_luaerrorhandler);
- if(ret){
- errorstream<<"========== ERROR FROM LUA WHILE CREATING MAIN MENU ==========="<<std::endl;
- errorstream<<"Failed to load and run script from "<<std::endl;
- errorstream<<script<<":"<<std::endl;
- errorstream<<std::endl;
- errorstream<<lua_tostring(m_engineluastack, -1)<<std::endl;
- errorstream<<std::endl;
- errorstream<<"=================== END OF ERROR FROM LUA ===================="<<std::endl;
- lua_pop(m_engineluastack, 1); // Pop error message from stack
- lua_pop(m_engineluastack, 1); // Pop the error handler from stack
- return false;
+bool GUIEngine::loadMainMenuScript()
+{
+ // Try custom menu script (main_menu_script)
+
+ std::string menuscript = g_settings->get("main_menu_script");
+ if(menuscript != "") {
+ m_scriptdir = fs::RemoveLastPathComponent(menuscript);
+
+ if(m_script->loadMod(menuscript, "__custommenu")) {
+ // custom menu script loaded
+ return true;
+ }
+ else {
+ infostream
+ << "GUIEngine: execution of custom menu failed!"
+ << std::endl
+ << "\tfalling back to builtin menu"
+ << std::endl;
+ }
}
- return true;
+
+ // Try builtin menu script (main_menu_script)
+
+ std::string builtin_menuscript =
+ porting::path_share + DIR_DELIM + "builtin"
+ + DIR_DELIM + "mainmenu.lua";
+
+ m_scriptdir = fs::RemoveRelativePathComponents(
+ fs::RemoveLastPathComponent(builtin_menuscript));
+
+ if(m_script->loadMod(builtin_menuscript, "__builtinmenu")) {
+ // builtin menu script loaded
+ return true;
+ }
+ else {
+ errorstream
+ << "GUIEngine: unable to load builtin menu"
+ << std::endl;
+ }
+
+ return false;
}
/******************************************************************************/
cloudInit();
- while(m_device->run() && (!m_startgame)) {
+ while(m_device->run() && (!m_startgame) && (!m_kill)) {
driver->beginScene(true, true, video::SColor(255,140,186,250));
if (m_clouds_enabled)
else
sleep_ms(25);
}
-
- m_menu->quitMenu();
-}
-
-/******************************************************************************/
-void GUIEngine::handleEvent(std::string text)
-{
- lua_getglobal(m_engineluastack, "engine");
-
- lua_getfield(m_engineluastack, -1, "event_handler");
-
- if(lua_isnil(m_engineluastack, -1))
- return;
-
- luaL_checktype(m_engineluastack, -1, LUA_TFUNCTION);
-
- lua_pushstring(m_engineluastack, text.c_str());
-
- if(lua_pcall(m_engineluastack, 1, 0, m_luaerrorhandler))
- scriptError("error: %s", lua_tostring(m_engineluastack, -1));
-}
-
-/******************************************************************************/
-void GUIEngine::handleButtons(std::map<std::string, std::string> fields)
-{
- lua_getglobal(m_engineluastack, "engine");
-
- lua_getfield(m_engineluastack, -1, "button_handler");
-
- if(lua_isnil(m_engineluastack, -1))
- return;
-
- luaL_checktype(m_engineluastack, -1, LUA_TFUNCTION);
-
- lua_newtable(m_engineluastack);
- for(std::map<std::string, std::string>::const_iterator
- i = fields.begin(); i != fields.end(); i++){
- const std::string &name = i->first;
- const std::string &value = i->second;
- lua_pushstring(m_engineluastack, name.c_str());
- lua_pushlstring(m_engineluastack, value.c_str(), value.size());
- lua_settable(m_engineluastack, -3);
- }
-
- if(lua_pcall(m_engineluastack, 1, 0, m_luaerrorhandler))
- scriptError("error: %s", lua_tostring(m_engineluastack, -1));
}
/******************************************************************************/
{
video::IVideoDriver* driver = m_device->getVideoDriver();
assert(driver != 0);
-
+
+ if(m_sound_manager != &dummySoundManager){
+ delete m_sound_manager;
+ m_sound_manager = NULL;
+ }
+
//TODO: clean up m_menu here
- lua_close(m_engineluastack);
+ infostream<<"GUIEngine: Deinitializing scripting"<<std::endl;
+ delete m_script;
m_irr_toplefttext->setText(L"");
- //initialize texture pointers
+ //clean up texture pointers
for (unsigned int i = 0; i < TEX_LAYER_MAX; i++) {
if (m_textures[i] != 0)
driver->removeTexture(m_textures[i]);
}
+
+ delete m_texture_source;
if (m_cloud.clouds)
m_cloud.clouds->drop();
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
errorstream << "File at url \"" << url
- <<"\" not found (internet connection?)" <<std::endl;
+ <<"\" not found (" << curl_easy_strerror(res) << ")" <<std::endl;
retval = false;
}
fclose(targetfile);
return false;
}
-/******************************************************************************/
-void GUIEngine::scriptError(const char *fmt, ...)
-{
- va_list argp;
- va_start(argp, fmt);
- char buf[10000];
- vsnprintf(buf, 10000, fmt, argp);
- va_end(argp);
- errorstream<<"MAINMENU ERROR: "<<buf;
-}
-
/******************************************************************************/
void GUIEngine::setTopleftText(std::string append) {
- std::string toset = "Minetest " VERSION_STRING;
+ std::string toset = std::string("Minetest ") + minetest_version_hash;
if (append != "") {
toset += " / ";
m_irr_toplefttext->setText(narrow_to_wide(toset).c_str());
}
+
+/******************************************************************************/
+s32 GUIEngine::playSound(SimpleSoundSpec spec, bool looped)
+{
+ s32 handle = m_sound_manager->playSound(spec, looped);
+ return handle;
+}
+
+/******************************************************************************/
+void GUIEngine::stopSound(s32 handle)
+{
+ m_sound_manager->stopSound(handle);
+}