]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/renderingengine.cpp
Make Lint Happy
[dragonfireclient.git] / src / client / renderingengine.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include <IrrlichtDevice.h>
22 #include <irrlicht.h>
23 #include "fontengine.h"
24 #include "client.h"
25 #include "clouds.h"
26 #include "util/numeric.h"
27 #include "guiscalingfilter.h"
28 #include "localplayer.h"
29 #include "client/hud.h"
30 #include "camera.h"
31 #include "minimap.h"
32 #include "clientmap.h"
33 #include "renderingengine.h"
34 #include "render/core.h"
35 #include "render/factory.h"
36 #include "inputhandler.h"
37 #include "gettext.h"
38 #include "../gui/guiSkin.h"
39
40 #if !defined(_WIN32) && !defined(__APPLE__) && !defined(__ANDROID__) &&                  \
41                 !defined(SERVER) && !defined(__HAIKU__)
42 #define XORG_USED
43 #endif
44 #ifdef XORG_USED
45 #include <X11/Xlib.h>
46 #include <X11/Xutil.h>
47 #include <X11/Xatom.h>
48 #endif
49
50 #ifdef _WIN32
51 #include <windows.h>
52 #include <winuser.h>
53 #endif
54
55 #if ENABLE_GLES
56 #include "filesys.h"
57 #endif
58
59 RenderingEngine *RenderingEngine::s_singleton = nullptr;
60
61 static gui::GUISkin *createSkin(gui::IGUIEnvironment *environment,
62                 gui::EGUI_SKIN_TYPE type, video::IVideoDriver *driver)
63 {
64         gui::GUISkin *skin = new gui::GUISkin(type, driver);
65
66         gui::IGUIFont *builtinfont = environment->getBuiltInFont();
67         gui::IGUIFontBitmap *bitfont = nullptr;
68         if (builtinfont && builtinfont->getType() == gui::EGFT_BITMAP)
69                 bitfont = (gui::IGUIFontBitmap *)builtinfont;
70
71         gui::IGUISpriteBank *bank = 0;
72         skin->setFont(builtinfont);
73
74         if (bitfont)
75                 bank = bitfont->getSpriteBank();
76
77         skin->setSpriteBank(bank);
78
79         return skin;
80 }
81
82 RenderingEngine::RenderingEngine(IEventReceiver *receiver)
83 {
84         sanity_check(!s_singleton);
85
86         // Resolution selection
87         bool fullscreen = g_settings->getBool("fullscreen");
88         u16 screen_w = g_settings->getU16("screen_w");
89         u16 screen_h = g_settings->getU16("screen_h");
90
91         // bpp, fsaa, vsync
92         bool vsync = g_settings->getBool("vsync");
93         u16 bits = g_settings->getU16("fullscreen_bpp");
94         u16 fsaa = g_settings->getU16("fsaa");
95
96         // stereo buffer required for pageflip stereo
97         bool stereo_buffer = g_settings->get("3d_mode") == "pageflip";
98
99         // Determine driver
100         video::E_DRIVER_TYPE driverType = video::EDT_OPENGL;
101         const std::string &driverstring = g_settings->get("video_driver");
102         std::vector<video::E_DRIVER_TYPE> drivers =
103                         RenderingEngine::getSupportedVideoDrivers();
104         u32 i;
105         for (i = 0; i != drivers.size(); i++) {
106                 if (!strcasecmp(driverstring.c_str(),
107                                     RenderingEngine::getVideoDriverName(drivers[i]))) {
108                         driverType = drivers[i];
109                         break;
110                 }
111         }
112         if (i == drivers.size()) {
113                 errorstream << "Invalid video_driver specified; "
114                                "defaulting to opengl"
115                             << std::endl;
116         }
117
118         SIrrlichtCreationParameters params = SIrrlichtCreationParameters();
119         params.DriverType = driverType;
120         params.WindowSize = core::dimension2d<u32>(screen_w, screen_h);
121         params.Bits = bits;
122         params.AntiAlias = fsaa;
123         params.Fullscreen = fullscreen;
124         params.Stencilbuffer = false;
125         params.Stereobuffer = stereo_buffer;
126         params.Vsync = vsync;
127         params.EventReceiver = receiver;
128         params.HighPrecisionFPU = g_settings->getBool("high_precision_fpu");
129         params.ZBufferBits = 24;
130 #ifdef __ANDROID__
131         params.PrivateData = porting::app_global;
132 #endif
133 #if ENABLE_GLES
134         // there is no standardized path for these on desktop
135         std::string rel_path = std::string("client") + DIR_DELIM + "shaders" + DIR_DELIM +
136                                "Irrlicht";
137         params.OGLES2ShaderPath =
138                         (porting::path_share + DIR_DELIM + rel_path + DIR_DELIM).c_str();
139 #endif
140
141         m_device = createDeviceEx(params);
142         driver = m_device->getVideoDriver();
143
144         s_singleton = this;
145
146         auto skin = createSkin(m_device->getGUIEnvironment(), gui::EGST_WINDOWS_METALLIC,
147                         driver);
148         m_device->getGUIEnvironment()->setSkin(skin);
149         skin->drop();
150 }
151
152 RenderingEngine::~RenderingEngine()
153 {
154         core.reset();
155         m_device->drop();
156         s_singleton = nullptr;
157 }
158
159 v2u32 RenderingEngine::getWindowSize() const
160 {
161         if (core)
162                 return core->getVirtualSize();
163         return m_device->getVideoDriver()->getScreenSize();
164 }
165
166 void RenderingEngine::setResizable(bool resize)
167 {
168         m_device->setResizable(resize);
169 }
170
171 bool RenderingEngine::print_video_modes()
172 {
173         IrrlichtDevice *nulldevice;
174
175         bool vsync = g_settings->getBool("vsync");
176         u16 fsaa = g_settings->getU16("fsaa");
177         MyEventReceiver *receiver = new MyEventReceiver();
178
179         SIrrlichtCreationParameters params = SIrrlichtCreationParameters();
180         params.DriverType = video::EDT_NULL;
181         params.WindowSize = core::dimension2d<u32>(640, 480);
182         params.Bits = 24;
183         params.AntiAlias = fsaa;
184         params.Fullscreen = false;
185         params.Stencilbuffer = false;
186         params.Vsync = vsync;
187         params.EventReceiver = receiver;
188         params.HighPrecisionFPU = g_settings->getBool("high_precision_fpu");
189
190         nulldevice = createDeviceEx(params);
191
192         if (!nulldevice) {
193                 delete receiver;
194                 return false;
195         }
196
197         std::cout << _("Available video modes (WxHxD):") << std::endl;
198
199         video::IVideoModeList *videomode_list = nulldevice->getVideoModeList();
200
201         if (videomode_list != NULL) {
202                 s32 videomode_count = videomode_list->getVideoModeCount();
203                 core::dimension2d<u32> videomode_res;
204                 s32 videomode_depth;
205                 for (s32 i = 0; i < videomode_count; ++i) {
206                         videomode_res = videomode_list->getVideoModeResolution(i);
207                         videomode_depth = videomode_list->getVideoModeDepth(i);
208                         std::cout << videomode_res.Width << "x" << videomode_res.Height
209                                   << "x" << videomode_depth << std::endl;
210                 }
211
212                 std::cout << _("Active video mode (WxHxD):") << std::endl;
213                 videomode_res = videomode_list->getDesktopResolution();
214                 videomode_depth = videomode_list->getDesktopDepth();
215                 std::cout << videomode_res.Width << "x" << videomode_res.Height << "x"
216                           << videomode_depth << std::endl;
217         }
218
219         nulldevice->drop();
220         delete receiver;
221
222         return videomode_list != NULL;
223 }
224
225 bool RenderingEngine::setupTopLevelWindow(const std::string &name)
226 {
227         // FIXME: It would make more sense for there to be a switch of some
228         // sort here that would call the correct toplevel setup methods for
229         // the environment Minetest is running in.
230
231         /* Setting Xorg properties for the top level window */
232         setupTopLevelXorgWindow(name);
233
234         /* Setting general properties for the top level window */
235         verbosestream << "Client: Configuring general top level"
236                       << " window properties" << std::endl;
237         bool result = setWindowIcon();
238
239         return result;
240 }
241
242 void RenderingEngine::setupTopLevelXorgWindow(const std::string &name)
243 {
244 #ifdef XORG_USED
245         const video::SExposedVideoData exposedData = driver->getExposedVideoData();
246
247         Display *x11_dpl =
248                         reinterpret_cast<Display *>(exposedData.OpenGLLinux.X11Display);
249         if (x11_dpl == NULL) {
250                 warningstream << "Client: Could not find X11 Display in ExposedVideoData"
251                               << std::endl;
252                 return;
253         }
254
255         verbosestream << "Client: Configuring X11-specific top level"
256                       << " window properties" << std::endl;
257
258         Window x11_win = reinterpret_cast<Window>(exposedData.OpenGLLinux.X11Window);
259
260         // Set application name and class hints. For now name and class are the same.
261         XClassHint *classhint = XAllocClassHint();
262         classhint->res_name = const_cast<char *>(name.c_str());
263         classhint->res_class = const_cast<char *>(name.c_str());
264
265         XSetClassHint(x11_dpl, x11_win, classhint);
266         XFree(classhint);
267
268         // FIXME: In the future WMNormalHints should be set ... e.g see the
269         // gtk/gdk code (gdk/x11/gdksurface-x11.c) for the setup_top_level
270         // method. But for now (as it would require some significant changes)
271         // leave the code as is.
272
273         // The following is borrowed from the above gdk source for setting top
274         // level windows. The source indicates and the Xlib docs suggest that
275         // this will set the WM_CLIENT_MACHINE and WM_LOCAL_NAME. This will not
276         // set the WM_CLIENT_MACHINE to a Fully Qualified Domain Name (FQDN) which is
277         // required by the Extended Window Manager Hints (EWMH) spec when setting
278         // the _NET_WM_PID (see further down) but running Minetest in an env
279         // where the window manager is on another machine from Minetest (therefore
280         // making the PID useless) is not expected to be a problem. Further
281         // more, using gtk/gdk as the model it would seem that not using a FQDN is
282         // not an issue for modern Xorg window managers.
283
284         verbosestream << "Client: Setting Xorg window manager Properties" << std::endl;
285
286         XSetWMProperties(x11_dpl, x11_win, NULL, NULL, NULL, 0, NULL, NULL, NULL);
287
288         // Set the _NET_WM_PID window property according to the EWMH spec. _NET_WM_PID
289         // (in conjunction with WM_CLIENT_MACHINE) can be used by window managers to
290         // force a shutdown of an application if it doesn't respond to the destroy
291         // window message.
292
293         verbosestream << "Client: Setting Xorg _NET_WM_PID extened window manager "
294                          "property"
295                       << std::endl;
296
297         Atom NET_WM_PID = XInternAtom(x11_dpl, "_NET_WM_PID", false);
298
299         pid_t pid = getpid();
300
301         XChangeProperty(x11_dpl, x11_win, NET_WM_PID, XA_CARDINAL, 32, PropModeReplace,
302                         reinterpret_cast<unsigned char *>(&pid), 1);
303
304         // Set the WM_CLIENT_LEADER window property here. Minetest has only one
305         // window and that window will always be the leader.
306
307         verbosestream << "Client: Setting Xorg WM_CLIENT_LEADER property" << std::endl;
308
309         Atom WM_CLIENT_LEADER = XInternAtom(x11_dpl, "WM_CLIENT_LEADER", false);
310
311         XChangeProperty(x11_dpl, x11_win, WM_CLIENT_LEADER, XA_WINDOW, 32,
312                         PropModeReplace, reinterpret_cast<unsigned char *>(&x11_win), 1);
313 #endif
314 }
315
316 #ifdef _WIN32
317 static bool getWindowHandle(irr::video::IVideoDriver *driver, HWND &hWnd)
318 {
319         const video::SExposedVideoData exposedData = driver->getExposedVideoData();
320
321         switch (driver->getDriverType()) {
322         case video::EDT_DIRECT3D8:
323                 hWnd = reinterpret_cast<HWND>(exposedData.D3D8.HWnd);
324                 break;
325         case video::EDT_DIRECT3D9:
326                 hWnd = reinterpret_cast<HWND>(exposedData.D3D9.HWnd);
327                 break;
328         case video::EDT_OPENGL:
329                 hWnd = reinterpret_cast<HWND>(exposedData.OpenGLWin32.HWnd);
330                 break;
331         default:
332                 return false;
333         }
334
335         return true;
336 }
337 #endif
338
339 bool RenderingEngine::setWindowIcon()
340 {
341 #if defined(XORG_USED)
342 #if RUN_IN_PLACE
343         return setXorgWindowIconFromPath(
344                         porting::path_share + "/misc/" PROJECT_NAME "-xorg-icon-128.png");
345 #else
346         // We have semi-support for reading in-place data if we are
347         // compiled with RUN_IN_PLACE. Don't break with this and
348         // also try the path_share location.
349         return setXorgWindowIconFromPath(
350                                ICON_DIR "/hicolor/128x128/apps/" PROJECT_NAME ".png") ||
351                setXorgWindowIconFromPath(porting::path_share + "/misc/" PROJECT_NAME
352                                                                "-xorg-icon-128.png");
353 #endif
354 #elif defined(_WIN32)
355         HWND hWnd; // Window handle
356         if (!getWindowHandle(driver, hWnd))
357                 return false;
358
359         // Load the ICON from resource file
360         const HICON hicon = LoadIcon(GetModuleHandle(NULL),
361                         MAKEINTRESOURCE(130) // The ID of the ICON defined in
362                                              // winresource.rc
363         );
364
365         if (hicon) {
366                 SendMessage(hWnd, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(hicon));
367                 SendMessage(hWnd, WM_SETICON, ICON_SMALL,
368                                 reinterpret_cast<LPARAM>(hicon));
369                 return true;
370         }
371         return false;
372 #else
373         return false;
374 #endif
375 }
376
377 bool RenderingEngine::setXorgWindowIconFromPath(const std::string &icon_file)
378 {
379 #ifdef XORG_USED
380
381         video::IImageLoader *image_loader = NULL;
382         u32 cnt = driver->getImageLoaderCount();
383         for (u32 i = 0; i < cnt; i++) {
384                 if (driver->getImageLoader(i)->isALoadableFileExtension(
385                                     icon_file.c_str())) {
386                         image_loader = driver->getImageLoader(i);
387                         break;
388                 }
389         }
390
391         if (!image_loader) {
392                 warningstream << "Could not find image loader for file '" << icon_file
393                               << "'" << std::endl;
394                 return false;
395         }
396
397         io::IReadFile *icon_f =
398                         m_device->getFileSystem()->createAndOpenFile(icon_file.c_str());
399
400         if (!icon_f) {
401                 warningstream << "Could not load icon file '" << icon_file << "'"
402                               << std::endl;
403                 return false;
404         }
405
406         video::IImage *img = image_loader->loadImage(icon_f);
407
408         if (!img) {
409                 warningstream << "Could not load icon file '" << icon_file << "'"
410                               << std::endl;
411                 icon_f->drop();
412                 return false;
413         }
414
415         u32 height = img->getDimension().Height;
416         u32 width = img->getDimension().Width;
417
418         size_t icon_buffer_len = 2 + height * width;
419         long *icon_buffer = new long[icon_buffer_len];
420
421         icon_buffer[0] = width;
422         icon_buffer[1] = height;
423
424         for (u32 x = 0; x < width; x++) {
425                 for (u32 y = 0; y < height; y++) {
426                         video::SColor col = img->getPixel(x, y);
427                         long pixel_val = 0;
428                         pixel_val |= (u8)col.getAlpha() << 24;
429                         pixel_val |= (u8)col.getRed() << 16;
430                         pixel_val |= (u8)col.getGreen() << 8;
431                         pixel_val |= (u8)col.getBlue();
432                         icon_buffer[2 + x + y * width] = pixel_val;
433                 }
434         }
435
436         img->drop();
437         icon_f->drop();
438
439         const video::SExposedVideoData &video_data = driver->getExposedVideoData();
440
441         Display *x11_dpl = (Display *)video_data.OpenGLLinux.X11Display;
442
443         if (x11_dpl == NULL) {
444                 warningstream << "Could not find x11 display for setting its icon."
445                               << std::endl;
446                 delete[] icon_buffer;
447                 return false;
448         }
449
450         Window x11_win = (Window)video_data.OpenGLLinux.X11Window;
451
452         Atom net_wm_icon = XInternAtom(x11_dpl, "_NET_WM_ICON", False);
453         Atom cardinal = XInternAtom(x11_dpl, "CARDINAL", False);
454         XChangeProperty(x11_dpl, x11_win, net_wm_icon, cardinal, 32, PropModeReplace,
455                         (const unsigned char *)icon_buffer, icon_buffer_len);
456
457         delete[] icon_buffer;
458
459 #endif
460         return true;
461 }
462
463 /*
464         Draws a screen with a single text on it.
465         Text will be removed when the screen is drawn the next time.
466         Additionally, a progressbar can be drawn when percent is set between 0 and 100.
467 */
468 void RenderingEngine::_draw_load_screen(const std::wstring &text,
469                 gui::IGUIEnvironment *guienv, ITextureSource *tsrc, float dtime,
470                 int percent, bool clouds)
471 {
472         v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
473
474         v2s32 textsize(g_fontengine->getTextWidth(text), g_fontengine->getLineHeight());
475         v2s32 center(screensize.X / 2, screensize.Y / 2);
476         core::rect<s32> textrect(center - textsize / 2, center + textsize / 2);
477
478         gui::IGUIStaticText *guitext =
479                         guienv->addStaticText(text.c_str(), textrect, false, false);
480         guitext->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
481
482         bool cloud_menu_background = clouds && g_settings->getBool("menu_clouds");
483         if (cloud_menu_background) {
484                 g_menuclouds->step(dtime * 3);
485                 g_menuclouds->render();
486                 get_video_driver()->beginScene(
487                                 true, true, video::SColor(255, 140, 186, 250));
488                 g_menucloudsmgr->drawAll();
489         } else
490                 get_video_driver()->beginScene(true, true, video::SColor(255, 0, 0, 0));
491
492         // draw progress bar
493         if ((percent >= 0) && (percent <= 100)) {
494                 video::ITexture *progress_img = tsrc->getTexture("progress_bar.png");
495                 video::ITexture *progress_img_bg =
496                                 tsrc->getTexture("progress_bar_bg.png");
497
498                 if (progress_img && progress_img_bg) {
499 #ifndef __ANDROID__
500                         const core::dimension2d<u32> &img_size =
501                                         progress_img_bg->getSize();
502                         u32 imgW = rangelim(img_size.Width, 200, 600);
503                         u32 imgH = rangelim(img_size.Height, 24, 72);
504 #else
505                         const core::dimension2d<u32> img_size(256, 48);
506                         float imgRatio = (float)img_size.Height / img_size.Width;
507                         u32 imgW = screensize.X / 2.2f;
508                         u32 imgH = floor(imgW * imgRatio);
509 #endif
510                         v2s32 img_pos((screensize.X - imgW) / 2,
511                                         (screensize.Y - imgH) / 2);
512
513                         draw2DImageFilterScaled(get_video_driver(), progress_img_bg,
514                                         core::rect<s32>(img_pos.X, img_pos.Y,
515                                                         img_pos.X + imgW,
516                                                         img_pos.Y + imgH),
517                                         core::rect<s32>(0, 0, img_size.Width,
518                                                         img_size.Height),
519                                         0, 0, true);
520
521                         draw2DImageFilterScaled(get_video_driver(), progress_img,
522                                         core::rect<s32>(img_pos.X, img_pos.Y,
523                                                         img_pos.X + (percent * imgW) / 100,
524                                                         img_pos.Y + imgH),
525                                         core::rect<s32>(0, 0,
526                                                         (percent * img_size.Width) / 100,
527                                                         img_size.Height),
528                                         0, 0, true);
529                 }
530         }
531
532         guienv->drawAll();
533         get_video_driver()->endScene();
534         guitext->remove();
535 }
536
537 /*
538         Draws the menu scene including (optional) cloud background.
539 */
540 void RenderingEngine::_draw_menu_scene(
541                 gui::IGUIEnvironment *guienv, float dtime, bool clouds)
542 {
543         bool cloud_menu_background = clouds && g_settings->getBool("menu_clouds");
544         if (cloud_menu_background) {
545                 g_menuclouds->step(dtime * 3);
546                 g_menuclouds->render();
547                 get_video_driver()->beginScene(
548                                 true, true, video::SColor(255, 140, 186, 250));
549                 g_menucloudsmgr->drawAll();
550         } else
551                 get_video_driver()->beginScene(true, true, video::SColor(255, 0, 0, 0));
552
553         guienv->drawAll();
554         get_video_driver()->endScene();
555 }
556
557 std::vector<core::vector3d<u32>> RenderingEngine::getSupportedVideoModes()
558 {
559         IrrlichtDevice *nulldevice = createDevice(video::EDT_NULL);
560         sanity_check(nulldevice);
561
562         std::vector<core::vector3d<u32>> mlist;
563         video::IVideoModeList *modelist = nulldevice->getVideoModeList();
564
565         s32 num_modes = modelist->getVideoModeCount();
566         for (s32 i = 0; i != num_modes; i++) {
567                 core::dimension2d<u32> mode_res = modelist->getVideoModeResolution(i);
568                 u32 mode_depth = (u32)modelist->getVideoModeDepth(i);
569                 mlist.emplace_back(mode_res.Width, mode_res.Height, mode_depth);
570         }
571
572         nulldevice->drop();
573         return mlist;
574 }
575
576 std::vector<irr::video::E_DRIVER_TYPE> RenderingEngine::getSupportedVideoDrivers()
577 {
578         std::vector<irr::video::E_DRIVER_TYPE> drivers;
579
580         for (int i = 0; i != irr::video::EDT_COUNT; i++) {
581                 if (irr::IrrlichtDevice::isDriverSupported((irr::video::E_DRIVER_TYPE)i))
582                         drivers.push_back((irr::video::E_DRIVER_TYPE)i);
583         }
584
585         return drivers;
586 }
587
588 void RenderingEngine::_initialize(Client *client, Hud *hud)
589 {
590         const std::string &draw_mode = g_settings->get("3d_mode");
591         core.reset(createRenderingCore(draw_mode, m_device, client, hud));
592         core->initialize();
593 }
594
595 void RenderingEngine::_finalize()
596 {
597         core.reset();
598 }
599
600 void RenderingEngine::_draw_scene(video::SColor skycolor, bool show_hud,
601                 bool show_minimap, bool draw_wield_tool, bool draw_crosshair,
602                 bool draw_tracers, bool draw_esp)
603 {
604         core->draw(skycolor, show_hud, show_minimap, draw_wield_tool, draw_crosshair,
605                         draw_tracers, draw_esp);
606 }
607
608 const char *RenderingEngine::getVideoDriverName(irr::video::E_DRIVER_TYPE type)
609 {
610         static const char *driver_ids[] = {
611                         "null",
612                         "software",
613                         "burningsvideo",
614                         "direct3d8",
615                         "direct3d9",
616                         "opengl",
617                         "ogles1",
618                         "ogles2",
619         };
620
621         return driver_ids[type];
622 }
623
624 const char *RenderingEngine::getVideoDriverFriendlyName(irr::video::E_DRIVER_TYPE type)
625 {
626         static const char *driver_names[] = {
627                         "NULL Driver",
628                         "Software Renderer",
629                         "Burning's Video",
630                         "Direct3D 8",
631                         "Direct3D 9",
632                         "OpenGL",
633                         "OpenGL ES1",
634                         "OpenGL ES2",
635         };
636
637         return driver_names[type];
638 }
639
640 #ifndef __ANDROID__
641 #if defined(XORG_USED)
642
643 static float calcDisplayDensity()
644 {
645         const char *current_display = getenv("DISPLAY");
646
647         if (current_display != NULL) {
648                 Display *x11display = XOpenDisplay(current_display);
649
650                 if (x11display != NULL) {
651                         /* try x direct */
652                         int dh = DisplayHeight(x11display, 0);
653                         int dw = DisplayWidth(x11display, 0);
654                         int dh_mm = DisplayHeightMM(x11display, 0);
655                         int dw_mm = DisplayWidthMM(x11display, 0);
656                         XCloseDisplay(x11display);
657
658                         if (dh_mm != 0 && dw_mm != 0) {
659                                 float dpi_height = floor(dh / (dh_mm * 0.039370) + 0.5);
660                                 float dpi_width = floor(dw / (dw_mm * 0.039370) + 0.5);
661                                 return std::max(dpi_height, dpi_width) / 96.0;
662                         }
663                 }
664         }
665
666         /* return manually specified dpi */
667         return g_settings->getFloat("screen_dpi") / 96.0;
668 }
669
670 float RenderingEngine::getDisplayDensity()
671 {
672         static float cached_display_density = calcDisplayDensity();
673         return cached_display_density;
674 }
675
676 #elif defined(_WIN32)
677
678 static float calcDisplayDensity(irr::video::IVideoDriver *driver)
679 {
680         HWND hWnd;
681         if (getWindowHandle(driver, hWnd)) {
682                 HDC hdc = GetDC(hWnd);
683                 float dpi = GetDeviceCaps(hdc, LOGPIXELSX);
684                 ReleaseDC(hWnd, hdc);
685                 return dpi / 96.0f;
686         }
687
688         /* return manually specified dpi */
689         return g_settings->getFloat("screen_dpi") / 96.0f;
690 }
691
692 float RenderingEngine::getDisplayDensity()
693 {
694         static bool cached = false;
695         static float display_density;
696         if (!cached) {
697                 display_density = calcDisplayDensity(get_video_driver());
698                 cached = true;
699         }
700         return display_density;
701 }
702
703 #else
704
705 float RenderingEngine::getDisplayDensity()
706 {
707         return g_settings->getFloat("screen_dpi") / 96.0;
708 }
709
710 #endif
711
712 v2u32 RenderingEngine::getDisplaySize()
713 {
714         IrrlichtDevice *nulldevice = createDevice(video::EDT_NULL);
715
716         core::dimension2d<u32> deskres =
717                         nulldevice->getVideoModeList()->getDesktopResolution();
718         nulldevice->drop();
719
720         return deskres;
721 }
722
723 #else  // __ANDROID__
724 float RenderingEngine::getDisplayDensity()
725 {
726         return porting::getDisplayDensity();
727 }
728
729 v2u32 RenderingEngine::getDisplaySize()
730 {
731         return porting::getDisplaySize();
732 }
733 #endif // __ANDROID__