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