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