3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
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.
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.
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.
21 #include <IrrlichtDevice.h>
23 #include "fontengine.h"
26 #include "util/numeric.h"
27 #include "guiscalingfilter.h"
28 #include "localplayer.h"
29 #include "client/hud.h"
32 #include "clientmap.h"
33 #include "renderingengine.h"
34 #include "render/core.h"
35 #include "render/factory.h"
36 #include "inputhandler.h"
39 #if !defined(_WIN32) && !defined(__APPLE__) && !defined(__ANDROID__) && \
40 !defined(SERVER) && !defined(__HAIKU__)
45 #include <X11/Xutil.h>
52 RenderingEngine *RenderingEngine::s_singleton = nullptr;
54 RenderingEngine::RenderingEngine(IEventReceiver *receiver)
56 sanity_check(!s_singleton);
58 // Resolution selection
59 bool fullscreen = g_settings->getBool("fullscreen");
60 u16 screen_w = g_settings->getU16("screen_w");
61 u16 screen_h = g_settings->getU16("screen_h");
64 bool vsync = g_settings->getBool("vsync");
65 u16 bits = g_settings->getU16("fullscreen_bpp");
66 u16 fsaa = g_settings->getU16("fsaa");
68 // stereo buffer required for pageflip stereo
69 bool stereo_buffer = g_settings->get("3d_mode") == "pageflip";
72 video::E_DRIVER_TYPE driverType = video::EDT_OPENGL;
73 const std::string &driverstring = g_settings->get("video_driver");
74 std::vector<video::E_DRIVER_TYPE> drivers =
75 RenderingEngine::getSupportedVideoDrivers();
77 for (i = 0; i != drivers.size(); i++) {
78 if (!strcasecmp(driverstring.c_str(),
79 RenderingEngine::getVideoDriverName(drivers[i]))) {
80 driverType = drivers[i];
84 if (i == drivers.size()) {
85 errorstream << "Invalid video_driver specified; "
86 "defaulting to opengl"
90 SIrrlichtCreationParameters params = SIrrlichtCreationParameters();
91 params.DriverType = driverType;
92 params.WindowSize = core::dimension2d<u32>(screen_w, screen_h);
94 params.AntiAlias = fsaa;
95 params.Fullscreen = fullscreen;
96 params.Stencilbuffer = false;
97 params.Stereobuffer = stereo_buffer;
99 params.EventReceiver = receiver;
100 params.HighPrecisionFPU = g_settings->getBool("high_precision_fpu");
101 params.ZBufferBits = 24;
104 params.PrivateData = porting::app_global;
105 params.OGLES2ShaderPath = std::string(porting::path_user + DIR_DELIM + "media" +
106 DIR_DELIM + "Shaders" + DIR_DELIM).c_str();
110 m_device = createDeviceEx(params);
111 driver = m_device->getVideoDriver();
116 RenderingEngine::~RenderingEngine()
120 s_singleton = nullptr;
123 v2u32 RenderingEngine::getWindowSize() const
126 return core->getVirtualSize();
127 return m_device->getVideoDriver()->getScreenSize();
130 void RenderingEngine::setResizable(bool resize)
132 m_device->setResizable(resize);
135 bool RenderingEngine::print_video_modes()
137 IrrlichtDevice *nulldevice;
139 bool vsync = g_settings->getBool("vsync");
140 u16 fsaa = g_settings->getU16("fsaa");
141 MyEventReceiver *receiver = new MyEventReceiver();
143 SIrrlichtCreationParameters params = SIrrlichtCreationParameters();
144 params.DriverType = video::EDT_NULL;
145 params.WindowSize = core::dimension2d<u32>(640, 480);
147 params.AntiAlias = fsaa;
148 params.Fullscreen = false;
149 params.Stencilbuffer = false;
150 params.Vsync = vsync;
151 params.EventReceiver = receiver;
152 params.HighPrecisionFPU = g_settings->getBool("high_precision_fpu");
154 nulldevice = createDeviceEx(params);
161 std::cout << _("Available video modes (WxHxD):") << std::endl;
163 video::IVideoModeList *videomode_list = nulldevice->getVideoModeList();
165 if (videomode_list != NULL) {
166 s32 videomode_count = videomode_list->getVideoModeCount();
167 core::dimension2d<u32> videomode_res;
169 for (s32 i = 0; i < videomode_count; ++i) {
170 videomode_res = videomode_list->getVideoModeResolution(i);
171 videomode_depth = videomode_list->getVideoModeDepth(i);
172 std::cout << videomode_res.Width << "x" << videomode_res.Height
173 << "x" << videomode_depth << std::endl;
176 std::cout << _("Active video mode (WxHxD):") << std::endl;
177 videomode_res = videomode_list->getDesktopResolution();
178 videomode_depth = videomode_list->getDesktopDepth();
179 std::cout << videomode_res.Width << "x" << videomode_res.Height << "x"
180 << videomode_depth << std::endl;
186 return videomode_list != NULL;
189 void RenderingEngine::setXorgClassHint(
190 const video::SExposedVideoData &video_data, const std::string &name)
193 if (video_data.OpenGLLinux.X11Display == NULL)
196 XClassHint *classhint = XAllocClassHint();
197 classhint->res_name = (char *)name.c_str();
198 classhint->res_class = (char *)name.c_str();
200 XSetClassHint((Display *)video_data.OpenGLLinux.X11Display,
201 video_data.OpenGLLinux.X11Window, classhint);
206 bool RenderingEngine::setWindowIcon()
208 #if defined(XORG_USED)
210 return setXorgWindowIconFromPath(
211 porting::path_share + "/misc/" PROJECT_NAME "-xorg-icon-128.png");
213 // We have semi-support for reading in-place data if we are
214 // compiled with RUN_IN_PLACE. Don't break with this and
215 // also try the path_share location.
216 return setXorgWindowIconFromPath(
217 ICON_DIR "/hicolor/128x128/apps/" PROJECT_NAME ".png") ||
218 setXorgWindowIconFromPath(porting::path_share + "/misc/" PROJECT_NAME
219 "-xorg-icon-128.png");
221 #elif defined(_WIN32)
222 const video::SExposedVideoData exposedData = driver->getExposedVideoData();
223 HWND hWnd; // Window handle
225 switch (driver->getDriverType()) {
226 case video::EDT_DIRECT3D8:
227 hWnd = reinterpret_cast<HWND>(exposedData.D3D8.HWnd);
229 case video::EDT_DIRECT3D9:
230 hWnd = reinterpret_cast<HWND>(exposedData.D3D9.HWnd);
232 case video::EDT_OPENGL:
233 hWnd = reinterpret_cast<HWND>(exposedData.OpenGLWin32.HWnd);
239 // Load the ICON from resource file
240 const HICON hicon = LoadIcon(GetModuleHandle(NULL),
241 MAKEINTRESOURCE(130) // The ID of the ICON defined in
246 SendMessage(hWnd, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(hicon));
247 SendMessage(hWnd, WM_SETICON, ICON_SMALL,
248 reinterpret_cast<LPARAM>(hicon));
257 bool RenderingEngine::setXorgWindowIconFromPath(const std::string &icon_file)
261 video::IImageLoader *image_loader = NULL;
262 u32 cnt = driver->getImageLoaderCount();
263 for (u32 i = 0; i < cnt; i++) {
264 if (driver->getImageLoader(i)->isALoadableFileExtension(
265 icon_file.c_str())) {
266 image_loader = driver->getImageLoader(i);
272 warningstream << "Could not find image loader for file '" << icon_file
277 io::IReadFile *icon_f =
278 m_device->getFileSystem()->createAndOpenFile(icon_file.c_str());
281 warningstream << "Could not load icon file '" << icon_file << "'"
286 video::IImage *img = image_loader->loadImage(icon_f);
289 warningstream << "Could not load icon file '" << icon_file << "'"
295 u32 height = img->getDimension().Height;
296 u32 width = img->getDimension().Width;
298 size_t icon_buffer_len = 2 + height * width;
299 long *icon_buffer = new long[icon_buffer_len];
301 icon_buffer[0] = width;
302 icon_buffer[1] = height;
304 for (u32 x = 0; x < width; x++) {
305 for (u32 y = 0; y < height; y++) {
306 video::SColor col = img->getPixel(x, y);
308 pixel_val |= (u8)col.getAlpha() << 24;
309 pixel_val |= (u8)col.getRed() << 16;
310 pixel_val |= (u8)col.getGreen() << 8;
311 pixel_val |= (u8)col.getBlue();
312 icon_buffer[2 + x + y * width] = pixel_val;
319 const video::SExposedVideoData &video_data = driver->getExposedVideoData();
321 Display *x11_dpl = (Display *)video_data.OpenGLLinux.X11Display;
323 if (x11_dpl == NULL) {
324 warningstream << "Could not find x11 display for setting its icon."
326 delete[] icon_buffer;
330 Window x11_win = (Window)video_data.OpenGLLinux.X11Window;
332 Atom net_wm_icon = XInternAtom(x11_dpl, "_NET_WM_ICON", False);
333 Atom cardinal = XInternAtom(x11_dpl, "CARDINAL", False);
334 XChangeProperty(x11_dpl, x11_win, net_wm_icon, cardinal, 32, PropModeReplace,
335 (const unsigned char *)icon_buffer, icon_buffer_len);
337 delete[] icon_buffer;
344 Draws a screen with a single text on it.
345 Text will be removed when the screen is drawn the next time.
346 Additionally, a progressbar can be drawn when percent is set between 0 and 100.
348 void RenderingEngine::_draw_load_screen(const std::wstring &text,
349 gui::IGUIEnvironment *guienv, ITextureSource *tsrc, float dtime,
350 int percent, bool clouds)
352 v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
354 v2s32 textsize(g_fontengine->getTextWidth(text), g_fontengine->getLineHeight());
355 v2s32 center(screensize.X / 2, screensize.Y / 2);
356 core::rect<s32> textrect(center - textsize / 2, center + textsize / 2);
358 gui::IGUIStaticText *guitext =
359 guienv->addStaticText(text.c_str(), textrect, false, false);
360 guitext->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
362 bool cloud_menu_background = clouds && g_settings->getBool("menu_clouds");
363 if (cloud_menu_background) {
364 g_menuclouds->step(dtime * 3);
365 g_menuclouds->render();
366 get_video_driver()->beginScene(
367 true, true, video::SColor(255, 140, 186, 250));
368 g_menucloudsmgr->drawAll();
370 get_video_driver()->beginScene(true, true, video::SColor(255, 0, 0, 0));
373 if ((percent >= 0) && (percent <= 100)) {
374 video::ITexture *progress_img = tsrc->getTexture("progress_bar.png");
375 video::ITexture *progress_img_bg =
376 tsrc->getTexture("progress_bar_bg.png");
378 if (progress_img && progress_img_bg) {
380 const core::dimension2d<u32> &img_size =
381 progress_img_bg->getSize();
382 u32 imgW = rangelim(img_size.Width, 200, 600);
383 u32 imgH = rangelim(img_size.Height, 24, 72);
385 const core::dimension2d<u32> img_size(256, 48);
386 float imgRatio = (float)img_size.Height / img_size.Width;
387 u32 imgW = screensize.X / 2.2f;
388 u32 imgH = floor(imgW * imgRatio);
390 v2s32 img_pos((screensize.X - imgW) / 2,
391 (screensize.Y - imgH) / 2);
393 draw2DImageFilterScaled(get_video_driver(), progress_img_bg,
394 core::rect<s32>(img_pos.X, img_pos.Y,
397 core::rect<s32>(0, 0, img_size.Width,
401 draw2DImageFilterScaled(get_video_driver(), progress_img,
402 core::rect<s32>(img_pos.X, img_pos.Y,
403 img_pos.X + (percent * imgW) / 100,
405 core::rect<s32>(0, 0,
406 (percent * img_size.Width) / 100,
413 get_video_driver()->endScene();
418 Draws the menu scene including (optional) cloud background.
420 void RenderingEngine::_draw_menu_scene(gui::IGUIEnvironment *guienv,
421 float dtime, bool clouds)
423 bool cloud_menu_background = clouds && g_settings->getBool("menu_clouds");
424 if (cloud_menu_background) {
425 g_menuclouds->step(dtime * 3);
426 g_menuclouds->render();
427 get_video_driver()->beginScene(
428 true, true, video::SColor(255, 140, 186, 250));
429 g_menucloudsmgr->drawAll();
431 get_video_driver()->beginScene(true, true, video::SColor(255, 0, 0, 0));
434 get_video_driver()->endScene();
437 std::vector<core::vector3d<u32>> RenderingEngine::getSupportedVideoModes()
439 IrrlichtDevice *nulldevice = createDevice(video::EDT_NULL);
440 sanity_check(nulldevice);
442 std::vector<core::vector3d<u32>> mlist;
443 video::IVideoModeList *modelist = nulldevice->getVideoModeList();
445 s32 num_modes = modelist->getVideoModeCount();
446 for (s32 i = 0; i != num_modes; i++) {
447 core::dimension2d<u32> mode_res = modelist->getVideoModeResolution(i);
448 u32 mode_depth = (u32)modelist->getVideoModeDepth(i);
449 mlist.emplace_back(mode_res.Width, mode_res.Height, mode_depth);
456 std::vector<irr::video::E_DRIVER_TYPE> RenderingEngine::getSupportedVideoDrivers()
458 std::vector<irr::video::E_DRIVER_TYPE> drivers;
460 for (int i = 0; i != irr::video::EDT_COUNT; i++) {
461 if (irr::IrrlichtDevice::isDriverSupported((irr::video::E_DRIVER_TYPE)i))
462 drivers.push_back((irr::video::E_DRIVER_TYPE)i);
468 void RenderingEngine::_initialize(Client *client, Hud *hud)
470 const std::string &draw_mode = g_settings->get("3d_mode");
471 core.reset(createRenderingCore(draw_mode, m_device, client, hud));
475 void RenderingEngine::_finalize()
480 void RenderingEngine::_draw_scene(video::SColor skycolor, bool show_hud,
481 bool show_minimap, bool draw_wield_tool, bool draw_crosshair)
483 core->draw(skycolor, show_hud, show_minimap, draw_wield_tool, draw_crosshair);
486 const char *RenderingEngine::getVideoDriverName(irr::video::E_DRIVER_TYPE type)
488 static const char *driver_ids[] = {
499 return driver_ids[type];
502 const char *RenderingEngine::getVideoDriverFriendlyName(irr::video::E_DRIVER_TYPE type)
504 static const char *driver_names[] = {
515 return driver_names[type];
521 static float calcDisplayDensity()
523 const char *current_display = getenv("DISPLAY");
525 if (current_display != NULL) {
526 Display *x11display = XOpenDisplay(current_display);
528 if (x11display != NULL) {
530 float dpi_height = floor(
531 DisplayHeight(x11display, 0) /
532 (DisplayHeightMM(x11display, 0) *
535 float dpi_width = floor(
536 DisplayWidth(x11display, 0) /
537 (DisplayWidthMM(x11display, 0) *
541 XCloseDisplay(x11display);
543 return std::max(dpi_height, dpi_width) / 96.0;
547 /* return manually specified dpi */
548 return g_settings->getFloat("screen_dpi") / 96.0;
551 float RenderingEngine::getDisplayDensity()
553 static float cached_display_density = calcDisplayDensity();
554 return cached_display_density;
558 float RenderingEngine::getDisplayDensity()
560 return g_settings->getFloat("screen_dpi") / 96.0;
564 v2u32 RenderingEngine::getDisplaySize()
566 IrrlichtDevice *nulldevice = createDevice(video::EDT_NULL);
568 core::dimension2d<u32> deskres =
569 nulldevice->getVideoModeList()->getDesktopResolution();
576 float RenderingEngine::getDisplayDensity()
578 return porting::getDisplayDensity();
581 v2u32 RenderingEngine::getDisplaySize()
583 return porting::getDisplaySize();
585 #endif // __ANDROID__