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"
32 #include <IGUIStaticText.h>
33 #include <ICameraSceneNode.h>
36 #include <curl/curl.h>
39 /******************************************************************************/
40 TextDestGuiEngine::TextDestGuiEngine(GUIEngine* engine)
45 /******************************************************************************/
46 void TextDestGuiEngine::gotText(std::map<std::string, std::string> fields)
48 m_engine->getScriptIface()->handleMainMenuButtons(fields);
51 /******************************************************************************/
52 void TextDestGuiEngine::gotText(std::wstring text)
54 m_engine->getScriptIface()->handleMainMenuEvent(wide_to_narrow(text));
57 /******************************************************************************/
58 void MenuMusicFetcher::fetchSounds(const std::string &name,
59 std::set<std::string> &dst_paths,
60 std::set<std::string> &dst_datas)
62 if(m_fetched.count(name))
64 m_fetched.insert(name);
66 base = porting::path_share + DIR_DELIM + "sounds";
67 dst_paths.insert(base + DIR_DELIM + name + ".ogg");
70 dst_paths.insert(base + DIR_DELIM + name + "."+itos(i)+".ogg");
71 base = porting::path_user + DIR_DELIM + "sounds";
72 dst_paths.insert(base + DIR_DELIM + name + ".ogg");
74 dst_paths.insert(base + DIR_DELIM + name + "."+itos(i)+".ogg");
77 /******************************************************************************/
78 GUIEngine::GUIEngine( irr::IrrlichtDevice* dev,
79 gui::IGUIElement* parent,
80 IMenuManager *menumgr,
81 scene::ISceneManager* smgr,
85 m_menumanager(menumgr),
88 m_sound_manager(NULL),
96 m_clouds_enabled(true),
99 //initialize texture pointers
100 for (unsigned int i = 0; i < TEX_LAYER_MAX; i++) {
103 // is deleted by guiformspec!
104 m_buttonhandler = new TextDestGuiEngine(this);
106 //create soundmanager
107 MenuMusicFetcher soundfetcher;
109 m_sound_manager = createOpenALSoundManager(&soundfetcher);
112 m_sound_manager = &dummySoundManager;
114 //create topleft header
115 core::rect<s32> rect(0, 0, 500, 40);
117 std::string t = "Minetest " VERSION_STRING;
120 m_device->getGUIEnvironment()->addStaticText(narrow_to_wide(t).c_str(),
121 rect,false,true,0,-1);
123 //create formspecsource
124 m_formspecgui = new FormspecFormSource("",&m_formspecgui);
128 new GUIFormSpecMenu( m_device,
135 m_menu->allowClose(false);
136 m_menu->lockSize(true,v2u32(800,600));
137 m_menu->setFormSource(m_formspecgui);
138 m_menu->setTextDest(m_buttonhandler);
139 m_menu->useGettext(true);
141 // Initialize scripting
143 infostream<<"GUIEngine: Initializing Lua"<<std::endl;
145 m_script = new MainMenuScripting(this);
148 if (m_data->errormessage != "")
150 m_script->setMainMenuErrorMessage(m_data->errormessage);
151 m_data->errormessage = "";
154 if (!loadMainMenuScript())
155 assert("no future without mainmenu" == 0);
160 errorstream << "MAINMENU ERROR: " << e.what() << std::endl;
161 m_data->errormessage = e.what();
169 /******************************************************************************/
170 bool GUIEngine::loadMainMenuScript()
172 // Try custom menu script (main_menu_script)
174 std::string menuscript = g_settings->get("main_menu_script");
175 if(menuscript != "") {
176 m_scriptdir = fs::RemoveLastPathComponent(menuscript);
178 if(m_script->loadMod(menuscript, "__custommenu")) {
179 // custom menu script loaded
184 << "GUIEngine: execution of custom menu failed!"
186 << "\tfalling back to builtin menu"
191 // Try builtin menu script (main_menu_script)
193 std::string builtin_menuscript =
194 porting::path_share + DIR_DELIM + "builtin"
195 + DIR_DELIM + "mainmenu.lua";
197 m_scriptdir = fs::RemoveRelativePathComponents(
198 fs::RemoveLastPathComponent(builtin_menuscript));
200 if(m_script->loadMod(builtin_menuscript, "__builtinmenu")) {
201 // builtin menu script loaded
206 << "GUIEngine: unable to load builtin menu"
213 /******************************************************************************/
214 void GUIEngine::run()
217 // Always create clouds because they may or may not be
218 // needed based on the game selected
219 video::IVideoDriver* driver = m_device->getVideoDriver();
223 while(m_device->run() && (!m_startgame)) {
224 driver->beginScene(true, true, video::SColor(255,140,186,250));
226 if (m_clouds_enabled)
232 drawBackground(driver);
237 m_device->getGUIEnvironment()->drawAll();
241 if (m_clouds_enabled)
248 /******************************************************************************/
249 GUIEngine::~GUIEngine()
251 video::IVideoDriver* driver = m_device->getVideoDriver();
254 if(m_sound_manager != &dummySoundManager){
255 delete m_sound_manager;
256 m_sound_manager = NULL;
259 //TODO: clean up m_menu here
261 infostream<<"GUIEngine: Deinitializing scripting"<<std::endl;
264 m_irr_toplefttext->setText(L"");
266 //initialize texture pointers
267 for (unsigned int i = 0; i < TEX_LAYER_MAX; i++) {
268 if (m_textures[i] != 0)
269 driver->removeTexture(m_textures[i]);
273 m_cloud.clouds->drop();
276 /******************************************************************************/
277 void GUIEngine::cloudInit()
279 m_cloud.clouds = new Clouds(m_smgr->getRootSceneNode(),
280 m_smgr, -1, rand(), 100);
281 m_cloud.clouds->update(v2f(0, 0), video::SColor(255,200,200,255));
283 m_cloud.camera = m_smgr->addCameraSceneNode(0,
284 v3f(0,0,0), v3f(0, 60, 100));
285 m_cloud.camera->setFarValue(10000);
287 m_cloud.lasttime = m_device->getTimer()->getTime();
290 /******************************************************************************/
291 void GUIEngine::cloudPreProcess()
293 u32 time = m_device->getTimer()->getTime();
295 if(time > m_cloud.lasttime)
296 m_cloud.dtime = (time - m_cloud.lasttime) / 1000.0;
300 m_cloud.lasttime = time;
302 m_cloud.clouds->step(m_cloud.dtime*3);
303 m_cloud.clouds->render();
307 /******************************************************************************/
308 void GUIEngine::cloudPostProcess()
310 float fps_max = g_settings->getFloat("fps_max");
311 // Time of frame without fps limit
314 // not using getRealTime is necessary for wine
315 u32 time = m_device->getTimer()->getTime();
316 if(time > m_cloud.lasttime)
317 busytime_u32 = time - m_cloud.lasttime;
320 busytime = busytime_u32 / 1000.0;
323 u32 frametime_min = 1000./fps_max;
325 if(busytime_u32 < frametime_min) {
326 u32 sleeptime = frametime_min - busytime_u32;
327 m_device->sleep(sleeptime);
331 /******************************************************************************/
332 void GUIEngine::drawBackground(video::IVideoDriver* driver)
334 v2u32 screensize = driver->getScreenSize();
336 video::ITexture* texture = m_textures[TEX_LAYER_BACKGROUND];
338 /* If no texture, draw background of solid color */
340 video::SColor color(255,80,58,37);
341 core::rect<s32> rect(0, 0, screensize.X, screensize.Y);
342 driver->draw2DRectangle(color, rect, NULL);
346 /* Draw background texture */
347 v2u32 sourcesize = texture->getSize();
348 driver->draw2DImage(texture,
349 core::rect<s32>(0, 0, screensize.X, screensize.Y),
350 core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
354 /******************************************************************************/
355 void GUIEngine::drawOverlay(video::IVideoDriver* driver)
357 v2u32 screensize = driver->getScreenSize();
359 video::ITexture* texture = m_textures[TEX_LAYER_OVERLAY];
361 /* If no texture, draw background of solid color */
365 /* Draw background texture */
366 v2u32 sourcesize = texture->getSize();
367 driver->draw2DImage(texture,
368 core::rect<s32>(0, 0, screensize.X, screensize.Y),
369 core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
373 /******************************************************************************/
374 void GUIEngine::drawHeader(video::IVideoDriver* driver)
376 core::dimension2d<u32> screensize = driver->getScreenSize();
378 video::ITexture* texture = m_textures[TEX_LAYER_HEADER];
380 /* If no texture, draw nothing */
384 f32 mult = (((f32)screensize.Width / 2)) /
385 ((f32)texture->getOriginalSize().Width);
387 v2s32 splashsize(((f32)texture->getOriginalSize().Width) * mult,
388 ((f32)texture->getOriginalSize().Height) * mult);
390 // Don't draw the header is there isn't enough room
391 s32 free_space = (((s32)screensize.Height)-320)/2;
393 if (free_space > splashsize.Y) {
394 core::rect<s32> splashrect(0, 0, splashsize.X, splashsize.Y);
395 splashrect += v2s32((screensize.Width/2)-(splashsize.X/2),
396 ((free_space/2)-splashsize.Y/2)+10);
398 video::SColor bgcolor(255,50,50,50);
400 driver->draw2DImage(texture, splashrect,
401 core::rect<s32>(core::position2d<s32>(0,0),
402 core::dimension2di(texture->getSize())),
407 /******************************************************************************/
408 void GUIEngine::drawFooter(video::IVideoDriver* driver)
410 core::dimension2d<u32> screensize = driver->getScreenSize();
412 video::ITexture* texture = m_textures[TEX_LAYER_FOOTER];
414 /* If no texture, draw nothing */
418 f32 mult = (((f32)screensize.Width)) /
419 ((f32)texture->getOriginalSize().Width);
421 v2s32 footersize(((f32)texture->getOriginalSize().Width) * mult,
422 ((f32)texture->getOriginalSize().Height) * mult);
424 // Don't draw the footer if there isn't enough room
425 s32 free_space = (((s32)screensize.Height)-320)/2;
427 if (free_space > footersize.Y) {
428 core::rect<s32> rect(0,0,footersize.X,footersize.Y);
429 rect += v2s32(screensize.Width/2,screensize.Height-footersize.Y);
430 rect -= v2s32(footersize.X/2, 0);
432 driver->draw2DImage(texture, rect,
433 core::rect<s32>(core::position2d<s32>(0,0),
434 core::dimension2di(texture->getSize())),
439 /******************************************************************************/
440 bool GUIEngine::setTexture(texture_layer layer,std::string texturepath) {
442 video::IVideoDriver* driver = m_device->getVideoDriver();
445 if (m_textures[layer] != 0)
447 driver->removeTexture(m_textures[layer]);
448 m_textures[layer] = 0;
451 if ((texturepath == "") || !fs::PathExists(texturepath))
454 m_textures[layer] = driver->getTexture(texturepath.c_str());
456 if (m_textures[layer] == 0) return false;
461 /******************************************************************************/
463 static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
465 FILE* targetfile = (FILE*) userp;
466 fwrite(contents,size,nmemb,targetfile);
470 bool GUIEngine::downloadFile(std::string url,std::string target) {
472 //download file via curl
475 curl = curl_easy_init();
482 FILE* targetfile = fopen(target.c_str(),"wb");
485 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
486 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
487 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
488 curl_easy_setopt(curl, CURLOPT_WRITEDATA, targetfile);
490 res = curl_easy_perform(curl);
491 if (res != CURLE_OK) {
492 errorstream << "File at url \"" << url
493 <<"\" not found (" << curl_easy_strerror(res) << ")" <<std::endl;
502 curl_easy_cleanup(curl);
509 /******************************************************************************/
510 void GUIEngine::setTopleftText(std::string append) {
511 std::string toset = "Minetest " VERSION_STRING;
518 m_irr_toplefttext->setText(narrow_to_wide(toset).c_str());
521 /******************************************************************************/
522 s32 GUIEngine::playSound(SimpleSoundSpec spec, bool looped)
524 s32 handle = m_sound_manager->playSound(spec, looped);
528 /******************************************************************************/
529 void GUIEngine::stopSound(s32 handle)
531 m_sound_manager->stopSound(handle);