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