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,
86 m_menumanager(menumgr),
89 m_sound_manager(NULL),
98 m_clouds_enabled(true),
101 //initialize texture pointers
102 for (unsigned int i = 0; i < TEX_LAYER_MAX; i++) {
105 // is deleted by guiformspec!
106 m_buttonhandler = new TextDestGuiEngine(this);
108 //create soundmanager
109 MenuMusicFetcher soundfetcher;
111 m_sound_manager = createOpenALSoundManager(&soundfetcher);
114 m_sound_manager = &dummySoundManager;
116 //create topleft header
117 core::rect<s32> rect(0, 0, 500, 40);
119 std::string t = "Minetest " VERSION_STRING;
122 m_device->getGUIEnvironment()->addStaticText(narrow_to_wide(t).c_str(),
123 rect,false,true,0,-1);
125 //create formspecsource
126 m_formspecgui = new FormspecFormSource("",&m_formspecgui);
130 new GUIFormSpecMenu( m_device,
137 m_menu->allowClose(false);
138 m_menu->lockSize(true,v2u32(800,600));
139 m_menu->setFormSource(m_formspecgui);
140 m_menu->setTextDest(m_buttonhandler);
142 // Initialize scripting
144 infostream<<"GUIEngine: Initializing Lua"<<std::endl;
146 m_script = new MainMenuScripting(this);
149 if (m_data->errormessage != "")
151 m_script->setMainMenuErrorMessage(m_data->errormessage);
152 m_data->errormessage = "";
155 if (!loadMainMenuScript())
156 assert("no future without mainmenu" == 0);
161 errorstream << "MAINMENU ERROR: " << e.what() << std::endl;
162 m_data->errormessage = e.what();
170 /******************************************************************************/
171 bool GUIEngine::loadMainMenuScript()
173 // Try custom menu script (main_menu_script)
175 std::string menuscript = g_settings->get("main_menu_script");
176 if(menuscript != "") {
177 m_scriptdir = fs::RemoveLastPathComponent(menuscript);
179 if(m_script->loadMod(menuscript, "__custommenu")) {
180 // custom menu script loaded
185 << "GUIEngine: execution of custom menu failed!"
187 << "\tfalling back to builtin menu"
192 // Try builtin menu script (main_menu_script)
194 std::string builtin_menuscript =
195 porting::path_share + DIR_DELIM + "builtin"
196 + DIR_DELIM + "mainmenu.lua";
198 m_scriptdir = fs::RemoveRelativePathComponents(
199 fs::RemoveLastPathComponent(builtin_menuscript));
201 if(m_script->loadMod(builtin_menuscript, "__builtinmenu")) {
202 // builtin menu script loaded
207 << "GUIEngine: unable to load builtin menu"
214 /******************************************************************************/
215 void GUIEngine::run()
218 // Always create clouds because they may or may not be
219 // needed based on the game selected
220 video::IVideoDriver* driver = m_device->getVideoDriver();
224 while(m_device->run() && (!m_startgame) && (!m_kill)) {
225 driver->beginScene(true, true, video::SColor(255,140,186,250));
227 if (m_clouds_enabled)
233 drawBackground(driver);
238 m_device->getGUIEnvironment()->drawAll();
242 if (m_clouds_enabled)
249 /******************************************************************************/
250 GUIEngine::~GUIEngine()
252 video::IVideoDriver* driver = m_device->getVideoDriver();
255 if(m_sound_manager != &dummySoundManager){
256 delete m_sound_manager;
257 m_sound_manager = NULL;
260 //TODO: clean up m_menu here
262 infostream<<"GUIEngine: Deinitializing scripting"<<std::endl;
265 m_irr_toplefttext->setText(L"");
267 //initialize texture pointers
268 for (unsigned int i = 0; i < TEX_LAYER_MAX; i++) {
269 if (m_textures[i] != 0)
270 driver->removeTexture(m_textures[i]);
274 m_cloud.clouds->drop();
277 /******************************************************************************/
278 void GUIEngine::cloudInit()
280 m_cloud.clouds = new Clouds(m_smgr->getRootSceneNode(),
281 m_smgr, -1, rand(), 100);
282 m_cloud.clouds->update(v2f(0, 0), video::SColor(255,200,200,255));
284 m_cloud.camera = m_smgr->addCameraSceneNode(0,
285 v3f(0,0,0), v3f(0, 60, 100));
286 m_cloud.camera->setFarValue(10000);
288 m_cloud.lasttime = m_device->getTimer()->getTime();
291 /******************************************************************************/
292 void GUIEngine::cloudPreProcess()
294 u32 time = m_device->getTimer()->getTime();
296 if(time > m_cloud.lasttime)
297 m_cloud.dtime = (time - m_cloud.lasttime) / 1000.0;
301 m_cloud.lasttime = time;
303 m_cloud.clouds->step(m_cloud.dtime*3);
304 m_cloud.clouds->render();
308 /******************************************************************************/
309 void GUIEngine::cloudPostProcess()
311 float fps_max = g_settings->getFloat("fps_max");
312 // Time of frame without fps limit
315 // not using getRealTime is necessary for wine
316 u32 time = m_device->getTimer()->getTime();
317 if(time > m_cloud.lasttime)
318 busytime_u32 = time - m_cloud.lasttime;
321 busytime = busytime_u32 / 1000.0;
324 u32 frametime_min = 1000./fps_max;
326 if(busytime_u32 < frametime_min) {
327 u32 sleeptime = frametime_min - busytime_u32;
328 m_device->sleep(sleeptime);
332 /******************************************************************************/
333 void GUIEngine::drawBackground(video::IVideoDriver* driver)
335 v2u32 screensize = driver->getScreenSize();
337 video::ITexture* texture = m_textures[TEX_LAYER_BACKGROUND];
339 /* If no texture, draw background of solid color */
341 video::SColor color(255,80,58,37);
342 core::rect<s32> rect(0, 0, screensize.X, screensize.Y);
343 driver->draw2DRectangle(color, rect, NULL);
347 /* Draw background texture */
348 v2u32 sourcesize = texture->getSize();
349 driver->draw2DImage(texture,
350 core::rect<s32>(0, 0, screensize.X, screensize.Y),
351 core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
355 /******************************************************************************/
356 void GUIEngine::drawOverlay(video::IVideoDriver* driver)
358 v2u32 screensize = driver->getScreenSize();
360 video::ITexture* texture = m_textures[TEX_LAYER_OVERLAY];
362 /* If no texture, draw background of solid color */
366 /* Draw background texture */
367 v2u32 sourcesize = texture->getSize();
368 driver->draw2DImage(texture,
369 core::rect<s32>(0, 0, screensize.X, screensize.Y),
370 core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
374 /******************************************************************************/
375 void GUIEngine::drawHeader(video::IVideoDriver* driver)
377 core::dimension2d<u32> screensize = driver->getScreenSize();
379 video::ITexture* texture = m_textures[TEX_LAYER_HEADER];
381 /* If no texture, draw nothing */
385 f32 mult = (((f32)screensize.Width / 2)) /
386 ((f32)texture->getOriginalSize().Width);
388 v2s32 splashsize(((f32)texture->getOriginalSize().Width) * mult,
389 ((f32)texture->getOriginalSize().Height) * mult);
391 // Don't draw the header is there isn't enough room
392 s32 free_space = (((s32)screensize.Height)-320)/2;
394 if (free_space > splashsize.Y) {
395 core::rect<s32> splashrect(0, 0, splashsize.X, splashsize.Y);
396 splashrect += v2s32((screensize.Width/2)-(splashsize.X/2),
397 ((free_space/2)-splashsize.Y/2)+10);
399 video::SColor bgcolor(255,50,50,50);
401 driver->draw2DImage(texture, splashrect,
402 core::rect<s32>(core::position2d<s32>(0,0),
403 core::dimension2di(texture->getSize())),
408 /******************************************************************************/
409 void GUIEngine::drawFooter(video::IVideoDriver* driver)
411 core::dimension2d<u32> screensize = driver->getScreenSize();
413 video::ITexture* texture = m_textures[TEX_LAYER_FOOTER];
415 /* If no texture, draw nothing */
419 f32 mult = (((f32)screensize.Width)) /
420 ((f32)texture->getOriginalSize().Width);
422 v2s32 footersize(((f32)texture->getOriginalSize().Width) * mult,
423 ((f32)texture->getOriginalSize().Height) * mult);
425 // Don't draw the footer if there isn't enough room
426 s32 free_space = (((s32)screensize.Height)-320)/2;
428 if (free_space > footersize.Y) {
429 core::rect<s32> rect(0,0,footersize.X,footersize.Y);
430 rect += v2s32(screensize.Width/2,screensize.Height-footersize.Y);
431 rect -= v2s32(footersize.X/2, 0);
433 driver->draw2DImage(texture, rect,
434 core::rect<s32>(core::position2d<s32>(0,0),
435 core::dimension2di(texture->getSize())),
440 /******************************************************************************/
441 bool GUIEngine::setTexture(texture_layer layer,std::string texturepath) {
443 video::IVideoDriver* driver = m_device->getVideoDriver();
446 if (m_textures[layer] != 0)
448 driver->removeTexture(m_textures[layer]);
449 m_textures[layer] = 0;
452 if ((texturepath == "") || !fs::PathExists(texturepath))
455 m_textures[layer] = driver->getTexture(texturepath.c_str());
457 if (m_textures[layer] == 0) return false;
462 /******************************************************************************/
464 static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
466 FILE* targetfile = (FILE*) userp;
467 fwrite(contents,size,nmemb,targetfile);
471 bool GUIEngine::downloadFile(std::string url,std::string target) {
473 //download file via curl
476 curl = curl_easy_init();
483 FILE* targetfile = fopen(target.c_str(),"wb");
486 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
487 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
488 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
489 curl_easy_setopt(curl, CURLOPT_WRITEDATA, targetfile);
491 res = curl_easy_perform(curl);
492 if (res != CURLE_OK) {
493 errorstream << "File at url \"" << url
494 <<"\" not found (" << curl_easy_strerror(res) << ")" <<std::endl;
503 curl_easy_cleanup(curl);
510 /******************************************************************************/
511 void GUIEngine::setTopleftText(std::string append) {
512 std::string toset = "Minetest " VERSION_STRING;
519 m_irr_toplefttext->setText(narrow_to_wide(toset).c_str());
522 /******************************************************************************/
523 s32 GUIEngine::playSound(SimpleSoundSpec spec, bool looped)
525 s32 handle = m_sound_manager->playSound(spec, looped);
529 /******************************************************************************/
530 void GUIEngine::stopSound(s32 handle)
532 m_sound_manager->stopSound(handle);