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