3 Copyright (C) 2013 sapier
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "guiEngine.h"
22 #include "scripting_mainmenu.h"
28 #include "guiMainMenu.h"
30 #include "sound_openal.h"
33 #include <IGUIStaticText.h>
34 #include <ICameraSceneNode.h>
37 #include <curl/curl.h>
40 /******************************************************************************/
41 /** TextDestGuiEngine */
42 /******************************************************************************/
43 TextDestGuiEngine::TextDestGuiEngine(GUIEngine* engine)
48 /******************************************************************************/
49 void TextDestGuiEngine::gotText(std::map<std::string, std::string> fields)
51 m_engine->getScriptIface()->handleMainMenuButtons(fields);
54 /******************************************************************************/
55 void TextDestGuiEngine::gotText(std::wstring text)
57 m_engine->getScriptIface()->handleMainMenuEvent(wide_to_narrow(text));
60 /******************************************************************************/
61 /** MenuTextureSource */
62 /******************************************************************************/
63 MenuTextureSource::MenuTextureSource(video::IVideoDriver *driver)
68 /******************************************************************************/
69 MenuTextureSource::~MenuTextureSource()
71 for (std::set<std::string>::iterator it = m_to_delete.begin();
72 it != m_to_delete.end(); ++it) {
73 const char *tname = (*it).c_str();
74 video::ITexture *texture = m_driver->getTexture(tname);
75 m_driver->removeTexture(texture);
79 /******************************************************************************/
80 video::ITexture* MenuTextureSource::getTexture(const std::string &name, u32 *id)
86 m_to_delete.insert(name);
87 return m_driver->getTexture(name.c_str());
90 /******************************************************************************/
91 /** MenuMusicFetcher */
92 /******************************************************************************/
93 void MenuMusicFetcher::fetchSounds(const std::string &name,
94 std::set<std::string> &dst_paths,
95 std::set<std::string> &dst_datas)
97 if(m_fetched.count(name))
99 m_fetched.insert(name);
101 base = porting::path_share + DIR_DELIM + "sounds";
102 dst_paths.insert(base + DIR_DELIM + name + ".ogg");
105 dst_paths.insert(base + DIR_DELIM + name + "."+itos(i)+".ogg");
106 base = porting::path_user + DIR_DELIM + "sounds";
107 dst_paths.insert(base + DIR_DELIM + name + ".ogg");
109 dst_paths.insert(base + DIR_DELIM + name + "."+itos(i)+".ogg");
112 /******************************************************************************/
114 /******************************************************************************/
115 GUIEngine::GUIEngine( irr::IrrlichtDevice* dev,
116 gui::IGUIElement* parent,
117 IMenuManager *menumgr,
118 scene::ISceneManager* smgr,
123 m_menumanager(menumgr),
126 m_texture_source(NULL),
127 m_sound_manager(NULL),
135 m_irr_toplefttext(0),
136 m_clouds_enabled(true),
139 //initialize texture pointers
140 for (unsigned int i = 0; i < TEX_LAYER_MAX; i++) {
143 // is deleted by guiformspec!
144 m_buttonhandler = new TextDestGuiEngine(this);
146 //create texture source
147 m_texture_source = new MenuTextureSource(m_device->getVideoDriver());
149 //create soundmanager
150 MenuMusicFetcher soundfetcher;
152 m_sound_manager = createOpenALSoundManager(&soundfetcher);
155 m_sound_manager = &dummySoundManager;
157 //create topleft header
158 core::rect<s32> rect(0, 0, 500, 40);
160 std::string t = "Minetest " VERSION_STRING;
163 m_device->getGUIEnvironment()->addStaticText(narrow_to_wide(t).c_str(),
164 rect,false,true,0,-1);
166 //create formspecsource
167 m_formspecgui = new FormspecFormSource("",&m_formspecgui);
171 new GUIFormSpecMenu( m_device,
179 m_menu->allowClose(false);
180 m_menu->lockSize(true,v2u32(800,600));
181 m_menu->setFormSource(m_formspecgui);
182 m_menu->setTextDest(m_buttonhandler);
184 // Initialize scripting
186 infostream<<"GUIEngine: Initializing Lua"<<std::endl;
188 m_script = new MainMenuScripting(this);
191 if (m_data->errormessage != "")
193 m_script->setMainMenuErrorMessage(m_data->errormessage);
194 m_data->errormessage = "";
197 if (!loadMainMenuScript())
198 assert("no future without mainmenu" == 0);
203 errorstream << "MAINMENU ERROR: " << e.what() << std::endl;
204 m_data->errormessage = e.what();
212 /******************************************************************************/
213 bool GUIEngine::loadMainMenuScript()
215 // Try custom menu script (main_menu_script)
217 std::string menuscript = g_settings->get("main_menu_script");
218 if(menuscript != "") {
219 m_scriptdir = fs::RemoveLastPathComponent(menuscript);
221 if(m_script->loadMod(menuscript, "__custommenu")) {
222 // custom menu script loaded
227 << "GUIEngine: execution of custom menu failed!"
229 << "\tfalling back to builtin menu"
234 // Try builtin menu script (main_menu_script)
236 std::string builtin_menuscript =
237 porting::path_share + DIR_DELIM + "builtin"
238 + DIR_DELIM + "mainmenu.lua";
240 m_scriptdir = fs::RemoveRelativePathComponents(
241 fs::RemoveLastPathComponent(builtin_menuscript));
243 if(m_script->loadMod(builtin_menuscript, "__builtinmenu")) {
244 // builtin menu script loaded
249 << "GUIEngine: unable to load builtin menu"
256 /******************************************************************************/
257 void GUIEngine::run()
260 // Always create clouds because they may or may not be
261 // needed based on the game selected
262 video::IVideoDriver* driver = m_device->getVideoDriver();
266 while(m_device->run() && (!m_startgame) && (!m_kill)) {
267 driver->beginScene(true, true, video::SColor(255,140,186,250));
269 if (m_clouds_enabled)
275 drawBackground(driver);
280 m_device->getGUIEnvironment()->drawAll();
284 if (m_clouds_enabled)
291 /******************************************************************************/
292 GUIEngine::~GUIEngine()
294 video::IVideoDriver* driver = m_device->getVideoDriver();
297 if(m_sound_manager != &dummySoundManager){
298 delete m_sound_manager;
299 m_sound_manager = NULL;
302 //TODO: clean up m_menu here
304 infostream<<"GUIEngine: Deinitializing scripting"<<std::endl;
307 m_irr_toplefttext->setText(L"");
309 //clean up texture pointers
310 for (unsigned int i = 0; i < TEX_LAYER_MAX; i++) {
311 if (m_textures[i] != 0)
312 driver->removeTexture(m_textures[i]);
315 delete m_texture_source;
318 m_cloud.clouds->drop();
321 /******************************************************************************/
322 void GUIEngine::cloudInit()
324 m_cloud.clouds = new Clouds(m_smgr->getRootSceneNode(),
325 m_smgr, -1, rand(), 100);
326 m_cloud.clouds->update(v2f(0, 0), video::SColor(255,200,200,255));
328 m_cloud.camera = m_smgr->addCameraSceneNode(0,
329 v3f(0,0,0), v3f(0, 60, 100));
330 m_cloud.camera->setFarValue(10000);
332 m_cloud.lasttime = m_device->getTimer()->getTime();
335 /******************************************************************************/
336 void GUIEngine::cloudPreProcess()
338 u32 time = m_device->getTimer()->getTime();
340 if(time > m_cloud.lasttime)
341 m_cloud.dtime = (time - m_cloud.lasttime) / 1000.0;
345 m_cloud.lasttime = time;
347 m_cloud.clouds->step(m_cloud.dtime*3);
348 m_cloud.clouds->render();
352 /******************************************************************************/
353 void GUIEngine::cloudPostProcess()
355 float fps_max = g_settings->getFloat("fps_max");
356 // Time of frame without fps limit
359 // not using getRealTime is necessary for wine
360 u32 time = m_device->getTimer()->getTime();
361 if(time > m_cloud.lasttime)
362 busytime_u32 = time - m_cloud.lasttime;
365 busytime = busytime_u32 / 1000.0;
368 u32 frametime_min = 1000./fps_max;
370 if(busytime_u32 < frametime_min) {
371 u32 sleeptime = frametime_min - busytime_u32;
372 m_device->sleep(sleeptime);
376 /******************************************************************************/
377 void GUIEngine::drawBackground(video::IVideoDriver* driver)
379 v2u32 screensize = driver->getScreenSize();
381 video::ITexture* texture = m_textures[TEX_LAYER_BACKGROUND];
383 /* If no texture, draw background of solid color */
385 video::SColor color(255,80,58,37);
386 core::rect<s32> rect(0, 0, screensize.X, screensize.Y);
387 driver->draw2DRectangle(color, rect, NULL);
391 /* Draw background texture */
392 v2u32 sourcesize = texture->getSize();
393 driver->draw2DImage(texture,
394 core::rect<s32>(0, 0, screensize.X, screensize.Y),
395 core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
399 /******************************************************************************/
400 void GUIEngine::drawOverlay(video::IVideoDriver* driver)
402 v2u32 screensize = driver->getScreenSize();
404 video::ITexture* texture = m_textures[TEX_LAYER_OVERLAY];
406 /* If no texture, draw background of solid color */
410 /* Draw background texture */
411 v2u32 sourcesize = texture->getSize();
412 driver->draw2DImage(texture,
413 core::rect<s32>(0, 0, screensize.X, screensize.Y),
414 core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
418 /******************************************************************************/
419 void GUIEngine::drawHeader(video::IVideoDriver* driver)
421 core::dimension2d<u32> screensize = driver->getScreenSize();
423 video::ITexture* texture = m_textures[TEX_LAYER_HEADER];
425 /* If no texture, draw nothing */
429 f32 mult = (((f32)screensize.Width / 2)) /
430 ((f32)texture->getOriginalSize().Width);
432 v2s32 splashsize(((f32)texture->getOriginalSize().Width) * mult,
433 ((f32)texture->getOriginalSize().Height) * mult);
435 // Don't draw the header is there isn't enough room
436 s32 free_space = (((s32)screensize.Height)-320)/2;
438 if (free_space > splashsize.Y) {
439 core::rect<s32> splashrect(0, 0, splashsize.X, splashsize.Y);
440 splashrect += v2s32((screensize.Width/2)-(splashsize.X/2),
441 ((free_space/2)-splashsize.Y/2)+10);
443 video::SColor bgcolor(255,50,50,50);
445 driver->draw2DImage(texture, splashrect,
446 core::rect<s32>(core::position2d<s32>(0,0),
447 core::dimension2di(texture->getSize())),
452 /******************************************************************************/
453 void GUIEngine::drawFooter(video::IVideoDriver* driver)
455 core::dimension2d<u32> screensize = driver->getScreenSize();
457 video::ITexture* texture = m_textures[TEX_LAYER_FOOTER];
459 /* If no texture, draw nothing */
463 f32 mult = (((f32)screensize.Width)) /
464 ((f32)texture->getOriginalSize().Width);
466 v2s32 footersize(((f32)texture->getOriginalSize().Width) * mult,
467 ((f32)texture->getOriginalSize().Height) * mult);
469 // Don't draw the footer if there isn't enough room
470 s32 free_space = (((s32)screensize.Height)-320)/2;
472 if (free_space > footersize.Y) {
473 core::rect<s32> rect(0,0,footersize.X,footersize.Y);
474 rect += v2s32(screensize.Width/2,screensize.Height-footersize.Y);
475 rect -= v2s32(footersize.X/2, 0);
477 driver->draw2DImage(texture, rect,
478 core::rect<s32>(core::position2d<s32>(0,0),
479 core::dimension2di(texture->getSize())),
484 /******************************************************************************/
485 bool GUIEngine::setTexture(texture_layer layer,std::string texturepath) {
487 video::IVideoDriver* driver = m_device->getVideoDriver();
490 if (m_textures[layer] != 0)
492 driver->removeTexture(m_textures[layer]);
493 m_textures[layer] = 0;
496 if ((texturepath == "") || !fs::PathExists(texturepath))
499 m_textures[layer] = driver->getTexture(texturepath.c_str());
501 if (m_textures[layer] == 0) return false;
506 /******************************************************************************/
508 static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
510 FILE* targetfile = (FILE*) userp;
511 fwrite(contents,size,nmemb,targetfile);
515 bool GUIEngine::downloadFile(std::string url,std::string target) {
517 //download file via curl
520 curl = curl_easy_init();
527 FILE* targetfile = fopen(target.c_str(),"wb");
530 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
531 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
532 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
533 curl_easy_setopt(curl, CURLOPT_WRITEDATA, targetfile);
535 res = curl_easy_perform(curl);
536 if (res != CURLE_OK) {
537 errorstream << "File at url \"" << url
538 <<"\" not found (" << curl_easy_strerror(res) << ")" <<std::endl;
547 curl_easy_cleanup(curl);
554 /******************************************************************************/
555 void GUIEngine::setTopleftText(std::string append) {
556 std::string toset = "Minetest " VERSION_STRING;
563 m_irr_toplefttext->setText(narrow_to_wide(toset).c_str());
566 /******************************************************************************/
567 s32 GUIEngine::playSound(SimpleSoundSpec spec, bool looped)
569 s32 handle = m_sound_manager->playSound(spec, looped);
573 /******************************************************************************/
574 void GUIEngine::stopSound(s32 handle)
576 m_sound_manager->stopSound(handle);