]> git.lizzy.rs Git - minetest.git/blob - src/guiEngine.cpp
Fix regression dirt texture not beeing default in non cloud menu
[minetest.git] / src / guiEngine.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 sapier
4
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.
9
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.
14
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.
18 */
19
20 #include "guiEngine.h"
21
22 #include "scripting_mainmenu.h"
23 #include "config.h"
24 #include "version.h"
25 #include "porting.h"
26 #include "filesys.h"
27 #include "main.h"
28 #include "settings.h"
29 #include "guiMainMenu.h"
30 #include "sound.h"
31 #include "sound_openal.h"
32 #include "clouds.h"
33 #include "httpfetch.h"
34 #include "util/numeric.h"
35
36 #include <IGUIStaticText.h>
37 #include <ICameraSceneNode.h>
38
39 #if USE_CURL
40 #include <curl/curl.h>
41 #endif
42
43 /******************************************************************************/
44 /** TextDestGuiEngine                                                         */
45 /******************************************************************************/
46 TextDestGuiEngine::TextDestGuiEngine(GUIEngine* engine)
47 {
48         m_engine = engine;
49 }
50
51 /******************************************************************************/
52 void TextDestGuiEngine::gotText(std::map<std::string, std::string> fields)
53 {
54         m_engine->getScriptIface()->handleMainMenuButtons(fields);
55 }
56
57 /******************************************************************************/
58 void TextDestGuiEngine::gotText(std::wstring text)
59 {
60         m_engine->getScriptIface()->handleMainMenuEvent(wide_to_narrow(text));
61 }
62
63 /******************************************************************************/
64 /** MenuTextureSource                                                         */
65 /******************************************************************************/
66 MenuTextureSource::MenuTextureSource(video::IVideoDriver *driver)
67 {
68         m_driver = driver;
69 }
70
71 /******************************************************************************/
72 MenuTextureSource::~MenuTextureSource()
73 {
74         for (std::set<std::string>::iterator it = m_to_delete.begin();
75                         it != m_to_delete.end(); ++it) {
76                 const char *tname = (*it).c_str();
77                 video::ITexture *texture = m_driver->getTexture(tname);
78                 m_driver->removeTexture(texture);
79         }
80 }
81
82 /******************************************************************************/
83 video::ITexture* MenuTextureSource::getTexture(const std::string &name, u32 *id)
84 {
85         if(id)
86                 *id = 0;
87         if(name.empty())
88                 return NULL;
89         m_to_delete.insert(name);
90         return m_driver->getTexture(name.c_str());
91 }
92
93 /******************************************************************************/
94 /** MenuMusicFetcher                                                          */
95 /******************************************************************************/
96 void MenuMusicFetcher::fetchSounds(const std::string &name,
97                         std::set<std::string> &dst_paths,
98                         std::set<std::string> &dst_datas)
99 {
100         if(m_fetched.count(name))
101                 return;
102         m_fetched.insert(name);
103         std::string base;
104         base = porting::path_share + DIR_DELIM + "sounds";
105         dst_paths.insert(base + DIR_DELIM + name + ".ogg");
106         int i;
107         for(i=0; i<10; i++)
108                 dst_paths.insert(base + DIR_DELIM + name + "."+itos(i)+".ogg");
109         base = porting::path_user + DIR_DELIM + "sounds";
110         dst_paths.insert(base + DIR_DELIM + name + ".ogg");
111         for(i=0; i<10; i++)
112                 dst_paths.insert(base + DIR_DELIM + name + "."+itos(i)+".ogg");
113 }
114
115 /******************************************************************************/
116 /** GUIEngine                                                                 */
117 /******************************************************************************/
118 GUIEngine::GUIEngine(   irr::IrrlichtDevice* dev,
119                                                 gui::IGUIElement* parent,
120                                                 IMenuManager *menumgr,
121                                                 scene::ISceneManager* smgr,
122                                                 MainMenuData* data,
123                                                 bool& kill) :
124         m_device(dev),
125         m_parent(parent),
126         m_menumanager(menumgr),
127         m_smgr(smgr),
128         m_data(data),
129         m_texture_source(NULL),
130         m_sound_manager(NULL),
131         m_formspecgui(0),
132         m_buttonhandler(0),
133         m_menu(0),
134         m_kill(kill),
135         m_startgame(false),
136         m_script(0),
137         m_scriptdir(""),
138         m_irr_toplefttext(0),
139         m_clouds_enabled(true),
140         m_cloud()
141 {
142         //initialize texture pointers
143         for (unsigned int i = 0; i < TEX_LAYER_MAX; i++) {
144                 m_textures[i].texture = NULL;
145         }
146         // is deleted by guiformspec!
147         m_buttonhandler = new TextDestGuiEngine(this);
148
149         //create texture source
150         m_texture_source = new MenuTextureSource(m_device->getVideoDriver());
151
152         //create soundmanager
153         MenuMusicFetcher soundfetcher;
154 #if USE_SOUND
155         m_sound_manager = createOpenALSoundManager(&soundfetcher);
156 #endif
157         if(!m_sound_manager)
158                 m_sound_manager = &dummySoundManager;
159
160         //create topleft header
161         core::rect<s32> rect(0, 0, 500, 20);
162         rect += v2s32(4, 0);
163         std::string t = std::string("Minetest ") + minetest_version_hash;
164
165         m_irr_toplefttext =
166                 m_device->getGUIEnvironment()->addStaticText(narrow_to_wide(t).c_str(),
167                 rect,false,true,0,-1);
168
169         //create formspecsource
170         m_formspecgui = new FormspecFormSource("");
171
172         /* Create menu */
173         m_menu = new GUIFormSpecMenu(m_device,
174                         m_parent,
175                         -1,
176                         m_menumanager,
177                         NULL /* &client */,
178                         NULL /* gamedef */,
179                         m_texture_source,
180                         m_formspecgui,
181                         m_buttonhandler,
182                         NULL);
183
184         m_menu->allowClose(false);
185         m_menu->lockSize(true,v2u32(800,600));
186
187         // Initialize scripting
188
189         infostream << "GUIEngine: Initializing Lua" << std::endl;
190
191         m_script = new MainMenuScripting(this);
192
193         try {
194                 if (m_data->errormessage != "") {
195                         m_script->setMainMenuErrorMessage(m_data->errormessage);
196                         m_data->errormessage = "";
197                 }
198
199                 if (!loadMainMenuScript()) {
200                         errorstream << "No future without mainmenu" << std::endl;
201                         abort();
202                 }
203
204                 run();
205         }
206         catch(LuaError &e) {
207                 errorstream << "MAINMENU ERROR: " << e.what() << std::endl;
208                 m_data->errormessage = e.what();
209         }
210
211         m_menu->quitMenu();
212         m_menu->drop();
213         m_menu = 0;
214 }
215
216 /******************************************************************************/
217 bool GUIEngine::loadMainMenuScript()
218 {
219         // Try custom menu script (main_menu_path)
220
221         m_scriptdir = g_settings->get("main_menu_path");
222         if (m_scriptdir.empty()) {
223                 m_scriptdir = porting::path_share + DIR_DELIM "builtin" + DIR_DELIM "mainmenu";
224         }
225
226         std::string script = porting::path_share + DIR_DELIM "builtin" + DIR_DELIM "init.lua";
227         if (m_script->loadScript(script)) {
228                 // Menu script loaded
229                 return true;
230         } else {
231                 infostream
232                         << "GUIEngine: execution of menu script in: \""
233                         << m_scriptdir << "\" failed!" << std::endl;
234         }
235
236         return false;
237 }
238
239 /******************************************************************************/
240 void GUIEngine::run()
241 {
242         // Always create clouds because they may or may not be
243         // needed based on the game selected
244         video::IVideoDriver* driver = m_device->getVideoDriver();
245
246         cloudInit();
247
248         while(m_device->run() && (!m_startgame) && (!m_kill)) {
249                 driver->beginScene(true, true, video::SColor(255,140,186,250));
250
251                 if (m_clouds_enabled)
252                 {
253                         cloudPreProcess();
254                         drawOverlay(driver);
255                 }
256                 else
257                         drawBackground(driver);
258
259                 drawHeader(driver);
260                 drawFooter(driver);
261
262                 m_device->getGUIEnvironment()->drawAll();
263
264                 driver->endScene();
265
266                 if (m_clouds_enabled)
267                         cloudPostProcess();
268                 else
269                         sleep_ms(25);
270
271                 m_script->step();
272         }
273 }
274
275 /******************************************************************************/
276 GUIEngine::~GUIEngine()
277 {
278         video::IVideoDriver* driver = m_device->getVideoDriver();
279         assert(driver != 0);
280
281         if(m_sound_manager != &dummySoundManager){
282                 delete m_sound_manager;
283                 m_sound_manager = NULL;
284         }
285
286         //TODO: clean up m_menu here
287
288         infostream<<"GUIEngine: Deinitializing scripting"<<std::endl;
289         delete m_script;
290
291         m_irr_toplefttext->setText(L"");
292
293         //clean up texture pointers
294         for (unsigned int i = 0; i < TEX_LAYER_MAX; i++) {
295                 if (m_textures[i].texture != NULL)
296                         driver->removeTexture(m_textures[i].texture);
297         }
298
299         delete m_texture_source;
300         
301         if (m_cloud.clouds)
302                 m_cloud.clouds->drop();
303 }
304
305 /******************************************************************************/
306 void GUIEngine::cloudInit()
307 {
308         m_cloud.clouds = new Clouds(m_smgr->getRootSceneNode(),
309                         m_smgr, -1, rand(), 100);
310         m_cloud.clouds->update(v2f(0, 0), video::SColor(255,200,200,255));
311
312         m_cloud.camera = m_smgr->addCameraSceneNode(0,
313                                 v3f(0,0,0), v3f(0, 60, 100));
314         m_cloud.camera->setFarValue(10000);
315
316         m_cloud.lasttime = m_device->getTimer()->getTime();
317 }
318
319 /******************************************************************************/
320 void GUIEngine::cloudPreProcess()
321 {
322         u32 time = m_device->getTimer()->getTime();
323
324         if(time > m_cloud.lasttime)
325                 m_cloud.dtime = (time - m_cloud.lasttime) / 1000.0;
326         else
327                 m_cloud.dtime = 0;
328
329         m_cloud.lasttime = time;
330
331         m_cloud.clouds->step(m_cloud.dtime*3);
332         m_cloud.clouds->render();
333         m_smgr->drawAll();
334 }
335
336 /******************************************************************************/
337 void GUIEngine::cloudPostProcess()
338 {
339         float fps_max = g_settings->getFloat("fps_max");
340         // Time of frame without fps limit
341         float busytime;
342         u32 busytime_u32;
343         // not using getRealTime is necessary for wine
344         u32 time = m_device->getTimer()->getTime();
345         if(time > m_cloud.lasttime)
346                 busytime_u32 = time - m_cloud.lasttime;
347         else
348                 busytime_u32 = 0;
349         busytime = busytime_u32 / 1000.0;
350
351         // FPS limiter
352         u32 frametime_min = 1000./fps_max;
353
354         if(busytime_u32 < frametime_min) {
355                 u32 sleeptime = frametime_min - busytime_u32;
356                 m_device->sleep(sleeptime);
357         }
358 }
359
360 /******************************************************************************/
361 void GUIEngine::drawBackground(video::IVideoDriver* driver)
362 {
363         v2u32 screensize = driver->getScreenSize();
364
365         video::ITexture* texture = m_textures[TEX_LAYER_BACKGROUND].texture;
366
367         /* If no texture, draw background of solid color */
368         if(!texture){
369                 video::SColor color(255,80,58,37);
370                 core::rect<s32> rect(0, 0, screensize.X, screensize.Y);
371                 driver->draw2DRectangle(color, rect, NULL);
372                 return;
373         }
374
375         v2u32 sourcesize = texture->getOriginalSize();
376
377         if (m_textures[TEX_LAYER_BACKGROUND].tile)
378         {
379                 v2u32 tilesize(
380                                 MYMAX(sourcesize.X,m_textures[TEX_LAYER_BACKGROUND].minsize),
381                                 MYMAX(sourcesize.Y,m_textures[TEX_LAYER_BACKGROUND].minsize));
382                 for (unsigned int x = 0; x < screensize.X; x += tilesize.X )
383                 {
384                         for (unsigned int y = 0; y < screensize.Y; y += tilesize.Y )
385                         {
386                                 driver->draw2DImage(texture,
387                                         core::rect<s32>(x, y, x+tilesize.X, y+tilesize.Y),
388                                         core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
389                                         NULL, NULL, true);
390                         }
391                 }
392                 return;
393         }
394
395         /* Draw background texture */
396         driver->draw2DImage(texture,
397                 core::rect<s32>(0, 0, screensize.X, screensize.Y),
398                 core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
399                 NULL, NULL, true);
400 }
401
402 /******************************************************************************/
403 void GUIEngine::drawOverlay(video::IVideoDriver* driver)
404 {
405         v2u32 screensize = driver->getScreenSize();
406
407         video::ITexture* texture = m_textures[TEX_LAYER_OVERLAY].texture;
408
409         /* If no texture, draw background of solid color */
410         if(!texture)
411                 return;
412
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),
418                 NULL, NULL, true);
419 }
420
421 /******************************************************************************/
422 void GUIEngine::drawHeader(video::IVideoDriver* driver)
423 {
424         core::dimension2d<u32> screensize = driver->getScreenSize();
425
426         video::ITexture* texture = m_textures[TEX_LAYER_HEADER].texture;
427
428         /* If no texture, draw nothing */
429         if(!texture)
430                 return;
431
432         f32 mult = (((f32)screensize.Width / 2.0)) /
433                         ((f32)texture->getOriginalSize().Width);
434
435         v2s32 splashsize(((f32)texture->getOriginalSize().Width) * mult,
436                         ((f32)texture->getOriginalSize().Height) * mult);
437
438         // Don't draw the header is there isn't enough room
439         s32 free_space = (((s32)screensize.Height)-320)/2;
440
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);
445
446         video::SColor bgcolor(255,50,50,50);
447
448         driver->draw2DImage(texture, splashrect,
449                 core::rect<s32>(core::position2d<s32>(0,0),
450                 core::dimension2di(texture->getOriginalSize())),
451                 NULL, NULL, true);
452         }
453 }
454
455 /******************************************************************************/
456 void GUIEngine::drawFooter(video::IVideoDriver* driver)
457 {
458         core::dimension2d<u32> screensize = driver->getScreenSize();
459
460         video::ITexture* texture = m_textures[TEX_LAYER_FOOTER].texture;
461
462         /* If no texture, draw nothing */
463         if(!texture)
464                 return;
465
466         f32 mult = (((f32)screensize.Width)) /
467                         ((f32)texture->getOriginalSize().Width);
468
469         v2s32 footersize(((f32)texture->getOriginalSize().Width) * mult,
470                         ((f32)texture->getOriginalSize().Height) * mult);
471
472         // Don't draw the footer if there isn't enough room
473         s32 free_space = (((s32)screensize.Height)-320)/2;
474
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);
479
480                 driver->draw2DImage(texture, rect,
481                         core::rect<s32>(core::position2d<s32>(0,0),
482                         core::dimension2di(texture->getOriginalSize())),
483                         NULL, NULL, true);
484         }
485 }
486
487 /******************************************************************************/
488 bool GUIEngine::setTexture(texture_layer layer, std::string texturepath,
489                 bool tile_image, unsigned int minsize)
490 {
491         video::IVideoDriver* driver = m_device->getVideoDriver();
492         assert(driver != 0);
493
494         if (m_textures[layer].texture != NULL)
495         {
496                 driver->removeTexture(m_textures[layer].texture);
497                 m_textures[layer].texture = NULL;
498         }
499
500         if ((texturepath == "") || !fs::PathExists(texturepath))
501         {
502                 return false;
503         }
504
505         m_textures[layer].texture = driver->getTexture(texturepath.c_str());
506         m_textures[layer].tile    = tile_image;
507         m_textures[layer].minsize = minsize;
508
509         if (m_textures[layer].texture == NULL)
510         {
511                 return false;
512         }
513
514         return true;
515 }
516
517 /******************************************************************************/
518 bool GUIEngine::downloadFile(std::string url,std::string target)
519 {
520 #if USE_CURL
521         std::ofstream targetfile(target.c_str(), std::ios::out | std::ios::binary);
522
523         if (!targetfile.good()) {
524                 return false;
525         }
526
527         HTTPFetchRequest fetchrequest;
528         HTTPFetchResult fetchresult;
529         fetchrequest.url = url;
530         fetchrequest.caller = HTTPFETCH_SYNC;
531         httpfetch_sync(fetchrequest, fetchresult);
532
533         if (fetchresult.succeeded) {
534                 targetfile << fetchresult.data;
535         } else {
536                 return false;
537         }
538
539         return true;
540 #else
541         return false;
542 #endif
543 }
544
545 /******************************************************************************/
546 void GUIEngine::setTopleftText(std::string append)
547 {
548         std::string toset = std::string("Minetest ") + minetest_version_hash;
549
550         if (append != "") {
551                 toset += " / ";
552                 toset += append;
553         }
554
555         m_irr_toplefttext->setText(narrow_to_wide(toset).c_str());
556 }
557
558 /******************************************************************************/
559 s32 GUIEngine::playSound(SimpleSoundSpec spec, bool looped)
560 {
561         s32 handle = m_sound_manager->playSound(spec, looped);
562         return handle;
563 }
564
565 /******************************************************************************/
566 void GUIEngine::stopSound(s32 handle)
567 {
568         m_sound_manager->stopSound(handle);
569 }
570
571 /******************************************************************************/
572 unsigned int GUIEngine::queueAsync(std::string serialized_func,
573                 std::string serialized_params)
574 {
575         return m_script->queueAsync(serialized_func, serialized_params);
576 }
577