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