]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/guiEngine.cpp
Fix player:set_animation() in third person view
[dragonfireclient.git] / src / guiEngine.cpp
index f00cd039c59c3634d0f1d2af235996b179bdf15a..e32d629d0da826d84b3e0d2d5964be23ea717240 100644 (file)
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #include "scripting_mainmenu.h"
 #include "config.h"
+#include "version.h"
 #include "porting.h"
 #include "filesys.h"
 #include "main.h"
@@ -28,6 +29,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "guiMainMenu.h"
 #include "sound.h"
 #include "sound_openal.h"
+#include "clouds.h"
+#include "httpfetch.h"
 
 #include <IGUIStaticText.h>
 #include <ICameraSceneNode.h>
@@ -36,6 +39,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <curl/curl.h>
 #endif
 
+/******************************************************************************/
+/** TextDestGuiEngine                                                         */
 /******************************************************************************/
 TextDestGuiEngine::TextDestGuiEngine(GUIEngine* engine)
 {
@@ -54,6 +59,38 @@ void TextDestGuiEngine::gotText(std::wstring 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,
@@ -74,6 +111,8 @@ void MenuMusicFetcher::fetchSounds(const std::string &name,
                dst_paths.insert(base + DIR_DELIM + name + "."+itos(i)+".ogg");
 }
 
+/******************************************************************************/
+/** GUIEngine                                                                 */
 /******************************************************************************/
 GUIEngine::GUIEngine(  irr::IrrlichtDevice* dev,
                                                gui::IGUIElement* parent,
@@ -86,6 +125,7 @@ GUIEngine::GUIEngine(        irr::IrrlichtDevice* dev,
        m_menumanager(menumgr),
        m_smgr(smgr),
        m_data(data),
+       m_texture_source(NULL),
        m_sound_manager(NULL),
        m_formspecgui(0),
        m_buttonhandler(0),
@@ -105,6 +145,9 @@ GUIEngine::GUIEngine(       irr::IrrlichtDevice* dev,
        // is deleted by guiformspec!
        m_buttonhandler = new TextDestGuiEngine(this);
 
+       //create texture source
+       m_texture_source = new MenuTextureSource(m_device->getVideoDriver());
+
        //create soundmanager
        MenuMusicFetcher soundfetcher;
 #if USE_SOUND
@@ -114,16 +157,16 @@ GUIEngine::GUIEngine(     irr::IrrlichtDevice* dev,
                m_sound_manager = &dummySoundManager;
 
        //create topleft header
-       core::rect<s32> rect(0, 0, 500, 40);
+       core::rect<s32> rect(0, 0, 500, 20);
        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(),
                rect,false,true,0,-1);
 
        //create formspecsource
-       m_formspecgui = new FormspecFormSource("",&m_formspecgui);
+       m_formspecgui = new FormspecFormSource("");
 
        /* Create menu */
        m_menu =
@@ -132,28 +175,31 @@ GUIEngine::GUIEngine(     irr::IrrlichtDevice* dev,
                                                                -1,
                                                                m_menumanager,
                                                                0 /* &client */,
-                                                               0 /* gamedef */);
+                                                               0 /* gamedef */,
+                                                               m_texture_source,
+                                                               m_formspecgui,
+                                                               m_buttonhandler,
+                                                               NULL);
 
        m_menu->allowClose(false);
        m_menu->lockSize(true,v2u32(800,600));
-       m_menu->setFormSource(m_formspecgui);
-       m_menu->setTextDest(m_buttonhandler);
 
        // Initialize scripting
 
-       infostream<<"GUIEngine: Initializing Lua"<<std::endl;
+       infostream << "GUIEngine: Initializing Lua" << std::endl;
 
        m_script = new MainMenuScripting(this);
 
        try {
-               if (m_data->errormessage != "")
-               {
+               if (m_data->errormessage != "") {
                        m_script->setMainMenuErrorMessage(m_data->errormessage);
                        m_data->errormessage = "";
                }
 
-               if (!loadMainMenuScript())
-                       assert("no future without mainmenu" == 0);
+               if (!loadMainMenuScript()) {
+                       errorstream << "No future without mainmenu" << std::endl;
+                       abort();
+               }
 
                run();
        }
@@ -182,7 +228,8 @@ bool GUIEngine::loadMainMenuScript()
                }
                else {
                        infostream
-                               << "GUIEngine: execution of custom menu failed!"
+                               << "GUIEngine: execution of custom menu: \""
+                               << menuscript << "\" failed!"
                                << std::endl
                                << "\tfalling back to builtin menu"
                                << std::endl;
@@ -243,6 +290,8 @@ void GUIEngine::run()
                        cloudPostProcess();
                else
                        sleep_ms(25);
+
+               m_script->step();
        }
 }
 
@@ -264,11 +313,13 @@ GUIEngine::~GUIEngine()
 
        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();
@@ -345,7 +396,7 @@ void GUIEngine::drawBackground(video::IVideoDriver* driver)
        }
 
        /* Draw background texture */
-       v2u32 sourcesize = texture->getSize();
+       v2u32 sourcesize = texture->getOriginalSize();
        driver->draw2DImage(texture,
                core::rect<s32>(0, 0, screensize.X, screensize.Y),
                core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
@@ -364,7 +415,7 @@ void GUIEngine::drawOverlay(video::IVideoDriver* driver)
                return;
 
        /* Draw background texture */
-       v2u32 sourcesize = texture->getSize();
+       v2u32 sourcesize = texture->getOriginalSize();
        driver->draw2DImage(texture,
                core::rect<s32>(0, 0, screensize.X, screensize.Y),
                core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
@@ -382,7 +433,7 @@ void GUIEngine::drawHeader(video::IVideoDriver* driver)
        if(!texture)
                return;
 
-       f32 mult = (((f32)screensize.Width / 2)) /
+       f32 mult = (((f32)screensize.Width / 2.0)) /
                        ((f32)texture->getOriginalSize().Width);
 
        v2s32 splashsize(((f32)texture->getOriginalSize().Width) * mult,
@@ -400,7 +451,7 @@ void GUIEngine::drawHeader(video::IVideoDriver* driver)
 
        driver->draw2DImage(texture, splashrect,
                core::rect<s32>(core::position2d<s32>(0,0),
-               core::dimension2di(texture->getSize())),
+               core::dimension2di(texture->getOriginalSize())),
                NULL, NULL, true);
        }
 }
@@ -432,7 +483,7 @@ void GUIEngine::drawFooter(video::IVideoDriver* driver)
 
                driver->draw2DImage(texture, rect,
                        core::rect<s32>(core::position2d<s32>(0,0),
-                       core::dimension2di(texture->getSize())),
+                       core::dimension2di(texture->getOriginalSize())),
                        NULL, NULL, true);
        }
 }
@@ -460,56 +511,35 @@ bool GUIEngine::setTexture(texture_layer layer,std::string texturepath) {
 }
 
 /******************************************************************************/
-#if USE_CURL
-static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
-{
-       FILE* targetfile = (FILE*) userp;
-       fwrite(contents,size,nmemb,targetfile);
-       return size * nmemb;
-}
-#endif
 bool GUIEngine::downloadFile(std::string url,std::string target) {
 #if USE_CURL
-       //download file via curl
-       CURL *curl;
+       std::ofstream targetfile(target.c_str(), std::ios::out | std::ios::binary);
 
-       curl = curl_easy_init();
+       if (!targetfile.good()) {
+               return false;
+       }
 
-       if (curl)
-       {
-               CURLcode res;
-               bool retval = true;
-
-               FILE* targetfile = fopen(target.c_str(),"wb");
-
-               if (targetfile) {
-                       curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
-                       curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
-                       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
-                       curl_easy_setopt(curl, CURLOPT_WRITEDATA, targetfile);
-
-                       res = curl_easy_perform(curl);
-                       if (res != CURLE_OK) {
-                               errorstream << "File at url \"" << url
-                                       <<"\" not found (" << curl_easy_strerror(res) << ")" <<std::endl;
-                               retval = false;
-                       }
-                       fclose(targetfile);
-               }
-               else {
-                       retval = false;
-               }
+       HTTPFetchRequest fetchrequest;
+       HTTPFetchResult fetchresult;
+       fetchrequest.url = url;
+       fetchrequest.caller = HTTPFETCH_SYNC;
+       httpfetch_sync(fetchrequest, fetchresult);
 
-               curl_easy_cleanup(curl);
-               return retval;
+       if (fetchresult.succeeded) {
+               targetfile << fetchresult.data;
+       } else {
+               return false;
        }
-#endif
+
+       return true;
+#else
        return false;
+#endif
 }
 
 /******************************************************************************/
 void GUIEngine::setTopleftText(std::string append) {
-       std::string toset = "Minetest " VERSION_STRING;
+       std::string toset = std::string("Minetest ") + minetest_version_hash;
 
        if (append != "") {
                toset += " / ";
@@ -531,3 +561,10 @@ void GUIEngine::stopSound(s32 handle)
 {
        m_sound_manager->stopSound(handle);
 }
+
+/******************************************************************************/
+unsigned int GUIEngine::queueAsync(std::string serialized_func,
+               std::string serialized_params) {
+       return m_script->queueAsync(serialized_func, serialized_params);
+}
+