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)
292 /******************************************************************************/
293 GUIEngine::~GUIEngine()
295 video::IVideoDriver* driver = m_device->getVideoDriver();
298 if(m_sound_manager != &dummySoundManager){
299 delete m_sound_manager;
300 m_sound_manager = NULL;
303 //TODO: clean up m_menu here
305 infostream<<"GUIEngine: Deinitializing scripting"<<std::endl;
308 m_irr_toplefttext->setText(L"");
310 //clean up texture pointers
311 for (unsigned int i = 0; i < TEX_LAYER_MAX; i++) {
312 if (m_textures[i] != 0)
313 driver->removeTexture(m_textures[i]);
316 delete m_texture_source;
319 m_cloud.clouds->drop();
322 /******************************************************************************/
323 void GUIEngine::cloudInit()
325 m_cloud.clouds = new Clouds(m_smgr->getRootSceneNode(),
326 m_smgr, -1, rand(), 100);
327 m_cloud.clouds->update(v2f(0, 0), video::SColor(255,200,200,255));
329 m_cloud.camera = m_smgr->addCameraSceneNode(0,
330 v3f(0,0,0), v3f(0, 60, 100));
331 m_cloud.camera->setFarValue(10000);
333 m_cloud.lasttime = m_device->getTimer()->getTime();
336 /******************************************************************************/
337 void GUIEngine::cloudPreProcess()
339 u32 time = m_device->getTimer()->getTime();
341 if(time > m_cloud.lasttime)
342 m_cloud.dtime = (time - m_cloud.lasttime) / 1000.0;
346 m_cloud.lasttime = time;
348 m_cloud.clouds->step(m_cloud.dtime*3);
349 m_cloud.clouds->render();
353 /******************************************************************************/
354 void GUIEngine::cloudPostProcess()
356 float fps_max = g_settings->getFloat("fps_max");
357 // Time of frame without fps limit
360 // not using getRealTime is necessary for wine
361 u32 time = m_device->getTimer()->getTime();
362 if(time > m_cloud.lasttime)
363 busytime_u32 = time - m_cloud.lasttime;
366 busytime = busytime_u32 / 1000.0;
369 u32 frametime_min = 1000./fps_max;
371 if(busytime_u32 < frametime_min) {
372 u32 sleeptime = frametime_min - busytime_u32;
373 m_device->sleep(sleeptime);
377 /******************************************************************************/
378 void GUIEngine::drawBackground(video::IVideoDriver* driver)
380 v2u32 screensize = driver->getScreenSize();
382 video::ITexture* texture = m_textures[TEX_LAYER_BACKGROUND];
384 /* If no texture, draw background of solid color */
386 video::SColor color(255,80,58,37);
387 core::rect<s32> rect(0, 0, screensize.X, screensize.Y);
388 driver->draw2DRectangle(color, rect, NULL);
392 /* Draw background texture */
393 v2u32 sourcesize = texture->getOriginalSize();
394 driver->draw2DImage(texture,
395 core::rect<s32>(0, 0, screensize.X, screensize.Y),
396 core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
400 /******************************************************************************/
401 void GUIEngine::drawOverlay(video::IVideoDriver* driver)
403 v2u32 screensize = driver->getScreenSize();
405 video::ITexture* texture = m_textures[TEX_LAYER_OVERLAY];
407 /* If no texture, draw background of solid color */
411 /* Draw background texture */
412 v2u32 sourcesize = texture->getOriginalSize();
413 driver->draw2DImage(texture,
414 core::rect<s32>(0, 0, screensize.X, screensize.Y),
415 core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
419 /******************************************************************************/
420 void GUIEngine::drawHeader(video::IVideoDriver* driver)
422 core::dimension2d<u32> screensize = driver->getScreenSize();
424 video::ITexture* texture = m_textures[TEX_LAYER_HEADER];
426 /* If no texture, draw nothing */
430 f32 mult = (((f32)screensize.Width / 2.0)) /
431 ((f32)texture->getOriginalSize().Width);
433 v2s32 splashsize(((f32)texture->getOriginalSize().Width) * mult,
434 ((f32)texture->getOriginalSize().Height) * mult);
436 // Don't draw the header is there isn't enough room
437 s32 free_space = (((s32)screensize.Height)-320)/2;
439 if (free_space > splashsize.Y) {
440 core::rect<s32> splashrect(0, 0, splashsize.X, splashsize.Y);
441 splashrect += v2s32((screensize.Width/2)-(splashsize.X/2),
442 ((free_space/2)-splashsize.Y/2)+10);
444 video::SColor bgcolor(255,50,50,50);
446 driver->draw2DImage(texture, splashrect,
447 core::rect<s32>(core::position2d<s32>(0,0),
448 core::dimension2di(texture->getOriginalSize())),
453 /******************************************************************************/
454 void GUIEngine::drawFooter(video::IVideoDriver* driver)
456 core::dimension2d<u32> screensize = driver->getScreenSize();
458 video::ITexture* texture = m_textures[TEX_LAYER_FOOTER];
460 /* If no texture, draw nothing */
464 f32 mult = (((f32)screensize.Width)) /
465 ((f32)texture->getOriginalSize().Width);
467 v2s32 footersize(((f32)texture->getOriginalSize().Width) * mult,
468 ((f32)texture->getOriginalSize().Height) * mult);
470 // Don't draw the footer if there isn't enough room
471 s32 free_space = (((s32)screensize.Height)-320)/2;
473 if (free_space > footersize.Y) {
474 core::rect<s32> rect(0,0,footersize.X,footersize.Y);
475 rect += v2s32(screensize.Width/2,screensize.Height-footersize.Y);
476 rect -= v2s32(footersize.X/2, 0);
478 driver->draw2DImage(texture, rect,
479 core::rect<s32>(core::position2d<s32>(0,0),
480 core::dimension2di(texture->getOriginalSize())),
485 /******************************************************************************/
486 bool GUIEngine::setTexture(texture_layer layer,std::string texturepath) {
488 video::IVideoDriver* driver = m_device->getVideoDriver();
491 if (m_textures[layer] != 0)
493 driver->removeTexture(m_textures[layer]);
494 m_textures[layer] = 0;
497 if ((texturepath == "") || !fs::PathExists(texturepath))
500 m_textures[layer] = driver->getTexture(texturepath.c_str());
502 if (m_textures[layer] == 0) return false;
507 /******************************************************************************/
509 static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
511 FILE* targetfile = (FILE*) userp;
512 fwrite(contents,size,nmemb,targetfile);
516 bool GUIEngine::downloadFile(std::string url,std::string target) {
518 //download file via curl
521 curl = curl_easy_init();
528 FILE* targetfile = fopen(target.c_str(),"wb");
531 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
532 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
533 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
534 curl_easy_setopt(curl, CURLOPT_WRITEDATA, targetfile);
536 res = curl_easy_perform(curl);
537 if (res != CURLE_OK) {
538 errorstream << "File at url \"" << url
539 <<"\" not found (" << curl_easy_strerror(res) << ")" <<std::endl;
548 curl_easy_cleanup(curl);
555 /******************************************************************************/
556 void GUIEngine::setTopleftText(std::string append) {
557 std::string toset = std::string("Minetest ") + minetest_version_hash;
564 m_irr_toplefttext->setText(narrow_to_wide(toset).c_str());
567 /******************************************************************************/
568 s32 GUIEngine::playSound(SimpleSoundSpec spec, bool looped)
570 s32 handle = m_sound_manager->playSound(spec, looped);
574 /******************************************************************************/
575 void GUIEngine::stopSound(s32 handle)
577 m_sound_manager->stopSound(handle);