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"
29 #include "guiMainMenu.h"
31 #include "sound_openal.h"
34 #include <IGUIStaticText.h>
35 #include <ICameraSceneNode.h>
38 #include <curl/curl.h>
41 /******************************************************************************/
42 /** TextDestGuiEngine */
43 /******************************************************************************/
44 TextDestGuiEngine::TextDestGuiEngine(GUIEngine* engine)
49 /******************************************************************************/
50 void TextDestGuiEngine::gotText(std::map<std::string, std::string> fields)
52 m_engine->getScriptIface()->handleMainMenuButtons(fields);
55 /******************************************************************************/
56 void TextDestGuiEngine::gotText(std::wstring text)
58 m_engine->getScriptIface()->handleMainMenuEvent(wide_to_narrow(text));
61 /******************************************************************************/
62 /** MenuTextureSource */
63 /******************************************************************************/
64 MenuTextureSource::MenuTextureSource(video::IVideoDriver *driver)
69 /******************************************************************************/
70 MenuTextureSource::~MenuTextureSource()
72 for (std::set<std::string>::iterator it = m_to_delete.begin();
73 it != m_to_delete.end(); ++it) {
74 const char *tname = (*it).c_str();
75 video::ITexture *texture = m_driver->getTexture(tname);
76 m_driver->removeTexture(texture);
80 /******************************************************************************/
81 video::ITexture* MenuTextureSource::getTexture(const std::string &name, u32 *id)
87 m_to_delete.insert(name);
88 return m_driver->getTexture(name.c_str());
91 /******************************************************************************/
92 /** MenuMusicFetcher */
93 /******************************************************************************/
94 void MenuMusicFetcher::fetchSounds(const std::string &name,
95 std::set<std::string> &dst_paths,
96 std::set<std::string> &dst_datas)
98 if(m_fetched.count(name))
100 m_fetched.insert(name);
102 base = porting::path_share + DIR_DELIM + "sounds";
103 dst_paths.insert(base + DIR_DELIM + name + ".ogg");
106 dst_paths.insert(base + DIR_DELIM + name + "."+itos(i)+".ogg");
107 base = porting::path_user + DIR_DELIM + "sounds";
108 dst_paths.insert(base + DIR_DELIM + name + ".ogg");
110 dst_paths.insert(base + DIR_DELIM + name + "."+itos(i)+".ogg");
113 /******************************************************************************/
115 /******************************************************************************/
116 GUIEngine::GUIEngine( irr::IrrlichtDevice* dev,
117 gui::IGUIElement* parent,
118 IMenuManager *menumgr,
119 scene::ISceneManager* smgr,
124 m_menumanager(menumgr),
127 m_texture_source(NULL),
128 m_sound_manager(NULL),
136 m_irr_toplefttext(0),
137 m_clouds_enabled(true),
140 //initialize texture pointers
141 for (unsigned int i = 0; i < TEX_LAYER_MAX; i++) {
144 // is deleted by guiformspec!
145 m_buttonhandler = new TextDestGuiEngine(this);
147 //create texture source
148 m_texture_source = new MenuTextureSource(m_device->getVideoDriver());
150 //create soundmanager
151 MenuMusicFetcher soundfetcher;
153 m_sound_manager = createOpenALSoundManager(&soundfetcher);
156 m_sound_manager = &dummySoundManager;
158 //create topleft header
159 core::rect<s32> rect(0, 0, 500, 40);
161 std::string t = std::string("Minetest ") + minetest_version_hash;
164 m_device->getGUIEnvironment()->addStaticText(narrow_to_wide(t).c_str(),
165 rect,false,true,0,-1);
167 //create formspecsource
168 m_formspecgui = new FormspecFormSource("",&m_formspecgui);
172 new GUIFormSpecMenu( m_device,
180 m_menu->allowClose(false);
181 m_menu->lockSize(true,v2u32(800,600));
182 m_menu->setFormSource(m_formspecgui);
183 m_menu->setTextDest(m_buttonhandler);
185 // Initialize scripting
187 infostream<<"GUIEngine: Initializing Lua"<<std::endl;
189 m_script = new MainMenuScripting(this);
192 if (m_data->errormessage != "")
194 m_script->setMainMenuErrorMessage(m_data->errormessage);
195 m_data->errormessage = "";
198 if (!loadMainMenuScript())
199 assert("no future without mainmenu" == 0);
204 errorstream << "MAINMENU ERROR: " << e.what() << std::endl;
205 m_data->errormessage = e.what();
213 /******************************************************************************/
214 bool GUIEngine::loadMainMenuScript()
216 // Try custom menu script (main_menu_script)
218 std::string menuscript = g_settings->get("main_menu_script");
219 if(menuscript != "") {
220 m_scriptdir = fs::RemoveLastPathComponent(menuscript);
222 if(m_script->loadMod(menuscript, "__custommenu")) {
223 // custom menu script loaded
228 << "GUIEngine: execution of custom menu failed!"
230 << "\tfalling back to builtin menu"
235 // Try builtin menu script (main_menu_script)
237 std::string builtin_menuscript =
238 porting::path_share + DIR_DELIM + "builtin"
239 + DIR_DELIM + "mainmenu.lua";
241 m_scriptdir = fs::RemoveRelativePathComponents(
242 fs::RemoveLastPathComponent(builtin_menuscript));
244 if(m_script->loadMod(builtin_menuscript, "__builtinmenu")) {
245 // builtin menu script loaded
250 << "GUIEngine: unable to load builtin menu"
257 /******************************************************************************/
258 void GUIEngine::run()
261 // Always create clouds because they may or may not be
262 // needed based on the game selected
263 video::IVideoDriver* driver = m_device->getVideoDriver();
267 while(m_device->run() && (!m_startgame) && (!m_kill)) {
268 driver->beginScene(true, true, video::SColor(255,140,186,250));
270 if (m_clouds_enabled)
276 drawBackground(driver);
281 m_device->getGUIEnvironment()->drawAll();
285 if (m_clouds_enabled)
294 /******************************************************************************/
295 GUIEngine::~GUIEngine()
297 video::IVideoDriver* driver = m_device->getVideoDriver();
300 if(m_sound_manager != &dummySoundManager){
301 delete m_sound_manager;
302 m_sound_manager = NULL;
305 //TODO: clean up m_menu here
307 infostream<<"GUIEngine: Deinitializing scripting"<<std::endl;
310 m_irr_toplefttext->setText(L"");
312 //clean up texture pointers
313 for (unsigned int i = 0; i < TEX_LAYER_MAX; i++) {
314 if (m_textures[i] != 0)
315 driver->removeTexture(m_textures[i]);
318 delete m_texture_source;
321 m_cloud.clouds->drop();
324 /******************************************************************************/
325 void GUIEngine::cloudInit()
327 m_cloud.clouds = new Clouds(m_smgr->getRootSceneNode(),
328 m_smgr, -1, rand(), 100);
329 m_cloud.clouds->update(v2f(0, 0), video::SColor(255,200,200,255));
331 m_cloud.camera = m_smgr->addCameraSceneNode(0,
332 v3f(0,0,0), v3f(0, 60, 100));
333 m_cloud.camera->setFarValue(10000);
335 m_cloud.lasttime = m_device->getTimer()->getTime();
338 /******************************************************************************/
339 void GUIEngine::cloudPreProcess()
341 u32 time = m_device->getTimer()->getTime();
343 if(time > m_cloud.lasttime)
344 m_cloud.dtime = (time - m_cloud.lasttime) / 1000.0;
348 m_cloud.lasttime = time;
350 m_cloud.clouds->step(m_cloud.dtime*3);
351 m_cloud.clouds->render();
355 /******************************************************************************/
356 void GUIEngine::cloudPostProcess()
358 float fps_max = g_settings->getFloat("fps_max");
359 // Time of frame without fps limit
362 // not using getRealTime is necessary for wine
363 u32 time = m_device->getTimer()->getTime();
364 if(time > m_cloud.lasttime)
365 busytime_u32 = time - m_cloud.lasttime;
368 busytime = busytime_u32 / 1000.0;
371 u32 frametime_min = 1000./fps_max;
373 if(busytime_u32 < frametime_min) {
374 u32 sleeptime = frametime_min - busytime_u32;
375 m_device->sleep(sleeptime);
379 /******************************************************************************/
380 void GUIEngine::drawBackground(video::IVideoDriver* driver)
382 v2u32 screensize = driver->getScreenSize();
384 video::ITexture* texture = m_textures[TEX_LAYER_BACKGROUND];
386 /* If no texture, draw background of solid color */
388 video::SColor color(255,80,58,37);
389 core::rect<s32> rect(0, 0, screensize.X, screensize.Y);
390 driver->draw2DRectangle(color, rect, NULL);
394 /* Draw background texture */
395 v2u32 sourcesize = texture->getOriginalSize();
396 driver->draw2DImage(texture,
397 core::rect<s32>(0, 0, screensize.X, screensize.Y),
398 core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
402 /******************************************************************************/
403 void GUIEngine::drawOverlay(video::IVideoDriver* driver)
405 v2u32 screensize = driver->getScreenSize();
407 video::ITexture* texture = m_textures[TEX_LAYER_OVERLAY];
409 /* If no texture, draw background of solid color */
413 /* Draw background texture */
414 v2u32 sourcesize = texture->getOriginalSize();
415 driver->draw2DImage(texture,
416 core::rect<s32>(0, 0, screensize.X, screensize.Y),
417 core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
421 /******************************************************************************/
422 void GUIEngine::drawHeader(video::IVideoDriver* driver)
424 core::dimension2d<u32> screensize = driver->getScreenSize();
426 video::ITexture* texture = m_textures[TEX_LAYER_HEADER];
428 /* If no texture, draw nothing */
432 f32 mult = (((f32)screensize.Width / 2.0)) /
433 ((f32)texture->getOriginalSize().Width);
435 v2s32 splashsize(((f32)texture->getOriginalSize().Width) * mult,
436 ((f32)texture->getOriginalSize().Height) * mult);
438 // Don't draw the header is there isn't enough room
439 s32 free_space = (((s32)screensize.Height)-320)/2;
441 if (free_space > splashsize.Y) {
442 core::rect<s32> splashrect(0, 0, splashsize.X, splashsize.Y);
443 splashrect += v2s32((screensize.Width/2)-(splashsize.X/2),
444 ((free_space/2)-splashsize.Y/2)+10);
446 video::SColor bgcolor(255,50,50,50);
448 driver->draw2DImage(texture, splashrect,
449 core::rect<s32>(core::position2d<s32>(0,0),
450 core::dimension2di(texture->getOriginalSize())),
455 /******************************************************************************/
456 void GUIEngine::drawFooter(video::IVideoDriver* driver)
458 core::dimension2d<u32> screensize = driver->getScreenSize();
460 video::ITexture* texture = m_textures[TEX_LAYER_FOOTER];
462 /* If no texture, draw nothing */
466 f32 mult = (((f32)screensize.Width)) /
467 ((f32)texture->getOriginalSize().Width);
469 v2s32 footersize(((f32)texture->getOriginalSize().Width) * mult,
470 ((f32)texture->getOriginalSize().Height) * mult);
472 // Don't draw the footer if there isn't enough room
473 s32 free_space = (((s32)screensize.Height)-320)/2;
475 if (free_space > footersize.Y) {
476 core::rect<s32> rect(0,0,footersize.X,footersize.Y);
477 rect += v2s32(screensize.Width/2,screensize.Height-footersize.Y);
478 rect -= v2s32(footersize.X/2, 0);
480 driver->draw2DImage(texture, rect,
481 core::rect<s32>(core::position2d<s32>(0,0),
482 core::dimension2di(texture->getOriginalSize())),
487 /******************************************************************************/
488 bool GUIEngine::setTexture(texture_layer layer,std::string texturepath) {
490 video::IVideoDriver* driver = m_device->getVideoDriver();
493 if (m_textures[layer] != 0)
495 driver->removeTexture(m_textures[layer]);
496 m_textures[layer] = 0;
499 if ((texturepath == "") || !fs::PathExists(texturepath))
502 m_textures[layer] = driver->getTexture(texturepath.c_str());
504 if (m_textures[layer] == 0) return false;
509 /******************************************************************************/
511 static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
513 FILE* targetfile = (FILE*) userp;
514 fwrite(contents,size,nmemb,targetfile);
518 bool GUIEngine::downloadFile(std::string url,std::string target) {
520 //download file via curl
523 curl = curl_easy_init();
530 FILE* targetfile = fopen(target.c_str(),"wb");
533 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
534 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
535 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
536 curl_easy_setopt(curl, CURLOPT_WRITEDATA, targetfile);
537 curl_easy_setopt(curl, CURLOPT_USERAGENT, (std::string("Minetest ")+minetest_version_hash).c_str());
538 res = curl_easy_perform(curl);
539 if (res != CURLE_OK) {
540 errorstream << "File at url \"" << url
541 <<"\" not found (" << curl_easy_strerror(res) << ")" <<std::endl;
550 curl_easy_cleanup(curl);
557 /******************************************************************************/
558 void GUIEngine::setTopleftText(std::string append) {
559 std::string toset = std::string("Minetest ") + minetest_version_hash;
566 m_irr_toplefttext->setText(narrow_to_wide(toset).c_str());
569 /******************************************************************************/
570 s32 GUIEngine::playSound(SimpleSoundSpec spec, bool looped)
572 s32 handle = m_sound_manager->playSound(spec, looped);
576 /******************************************************************************/
577 void GUIEngine::stopSound(s32 handle)
579 m_sound_manager->stopSound(handle);
582 /******************************************************************************/
583 unsigned int GUIEngine::DoAsync(std::string serialized_fct,
584 std::string serialized_params) {
585 return m_script->DoAsync(serialized_fct,serialized_params);