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>
48 RenderingEngine *RenderingEngine::s_singleton = nullptr;
50 RenderingEngine::RenderingEngine(IEventReceiver *receiver)
52 sanity_check(!s_singleton);
54 // Resolution selection
55 bool fullscreen = g_settings->getBool("fullscreen");
56 u16 screen_w = g_settings->getU16("screen_w");
57 u16 screen_h = g_settings->getU16("screen_h");
60 bool vsync = g_settings->getBool("vsync");
61 u16 bits = g_settings->getU16("fullscreen_bpp");
62 u16 fsaa = g_settings->getU16("fsaa");
64 // stereo buffer required for pageflip stereo
65 bool stereo_buffer = g_settings->get("3d_mode") == "pageflip";
68 video::E_DRIVER_TYPE driverType = video::EDT_OPENGL;
69 const std::string &driverstring = g_settings->get("video_driver");
70 std::vector<video::E_DRIVER_TYPE> drivers =
71 RenderingEngine::getSupportedVideoDrivers();
73 for (i = 0; i != drivers.size(); i++) {
74 if (!strcasecmp(driverstring.c_str(),
75 RenderingEngine::getVideoDriverName(drivers[i]))) {
76 driverType = drivers[i];
80 if (i == drivers.size()) {
81 errorstream << "Invalid video_driver specified; "
82 "defaulting to opengl"
86 SIrrlichtCreationParameters params = SIrrlichtCreationParameters();
87 params.DriverType = driverType;
88 params.WindowSize = core::dimension2d<u32>(screen_w, screen_h);
90 params.AntiAlias = fsaa;
91 params.Fullscreen = fullscreen;
92 params.Stencilbuffer = false;
93 params.Stereobuffer = stereo_buffer;
95 params.EventReceiver = receiver;
96 params.HighPrecisionFPU = g_settings->getBool("high_precision_fpu");
97 params.ZBufferBits = 24;
100 params.PrivateData = porting::app_global;
101 params.OGLES2ShaderPath = std::string(porting::path_user + DIR_DELIM + "media" +
102 DIR_DELIM + "Shaders" + DIR_DELIM).c_str();
106 m_device = createDeviceEx(params);
107 driver = m_device->getVideoDriver();
112 RenderingEngine::~RenderingEngine()
116 s_singleton = nullptr;
119 v2u32 RenderingEngine::getWindowSize() const
122 return core->getVirtualSize();
123 return m_device->getVideoDriver()->getScreenSize();
126 void RenderingEngine::setResizable(bool resize)
128 m_device->setResizable(resize);
131 bool RenderingEngine::print_video_modes()
133 IrrlichtDevice *nulldevice;
135 bool vsync = g_settings->getBool("vsync");
136 u16 fsaa = g_settings->getU16("fsaa");
137 MyEventReceiver *receiver = new MyEventReceiver();
139 SIrrlichtCreationParameters params = SIrrlichtCreationParameters();
140 params.DriverType = video::EDT_NULL;
141 params.WindowSize = core::dimension2d<u32>(640, 480);
143 params.AntiAlias = fsaa;
144 params.Fullscreen = false;
145 params.Stencilbuffer = false;
146 params.Vsync = vsync;
147 params.EventReceiver = receiver;
148 params.HighPrecisionFPU = g_settings->getBool("high_precision_fpu");
150 nulldevice = createDeviceEx(params);
157 std::cout << _("Available video modes (WxHxD):") << std::endl;
159 video::IVideoModeList *videomode_list = nulldevice->getVideoModeList();
161 if (videomode_list != NULL) {
162 s32 videomode_count = videomode_list->getVideoModeCount();
163 core::dimension2d<u32> videomode_res;
165 for (s32 i = 0; i < videomode_count; ++i) {
166 videomode_res = videomode_list->getVideoModeResolution(i);
167 videomode_depth = videomode_list->getVideoModeDepth(i);
168 std::cout << videomode_res.Width << "x" << videomode_res.Height
169 << "x" << videomode_depth << std::endl;
172 std::cout << _("Active video mode (WxHxD):") << std::endl;
173 videomode_res = videomode_list->getDesktopResolution();
174 videomode_depth = videomode_list->getDesktopDepth();
175 std::cout << videomode_res.Width << "x" << videomode_res.Height << "x"
176 << videomode_depth << std::endl;
182 return videomode_list != NULL;
185 void RenderingEngine::setXorgClassHint(
186 const video::SExposedVideoData &video_data, const std::string &name)
189 if (video_data.OpenGLLinux.X11Display == NULL)
192 XClassHint *classhint = XAllocClassHint();
193 classhint->res_name = (char *)name.c_str();
194 classhint->res_class = (char *)name.c_str();
196 XSetClassHint((Display *)video_data.OpenGLLinux.X11Display,
197 video_data.OpenGLLinux.X11Window, classhint);
202 bool RenderingEngine::setWindowIcon()
204 #if defined(XORG_USED)
206 return setXorgWindowIconFromPath(
207 porting::path_share + "/misc/" PROJECT_NAME "-xorg-icon-128.png");
209 // We have semi-support for reading in-place data if we are
210 // compiled with RUN_IN_PLACE. Don't break with this and
211 // also try the path_share location.
212 return setXorgWindowIconFromPath(
213 ICON_DIR "/hicolor/128x128/apps/" PROJECT_NAME ".png") ||
214 setXorgWindowIconFromPath(porting::path_share + "/misc/" PROJECT_NAME
215 "-xorg-icon-128.png");
217 #elif defined(_WIN32)
218 const video::SExposedVideoData exposedData = driver->getExposedVideoData();
219 HWND hWnd; // Window handle
221 switch (driver->getDriverType()) {
222 case video::EDT_DIRECT3D8:
223 hWnd = reinterpret_cast<HWND>(exposedData.D3D8.HWnd);
225 case video::EDT_DIRECT3D9:
226 hWnd = reinterpret_cast<HWND>(exposedData.D3D9.HWnd);
228 case video::EDT_OPENGL:
229 hWnd = reinterpret_cast<HWND>(exposedData.OpenGLWin32.HWnd);
235 // Load the ICON from resource file
236 const HICON hicon = LoadIcon(GetModuleHandle(NULL),
237 MAKEINTRESOURCE(130) // The ID of the ICON defined in
242 SendMessage(hWnd, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(hicon));
243 SendMessage(hWnd, WM_SETICON, ICON_SMALL,
244 reinterpret_cast<LPARAM>(hicon));
253 bool RenderingEngine::setXorgWindowIconFromPath(const std::string &icon_file)
257 video::IImageLoader *image_loader = NULL;
258 u32 cnt = driver->getImageLoaderCount();
259 for (u32 i = 0; i < cnt; i++) {
260 if (driver->getImageLoader(i)->isALoadableFileExtension(
261 icon_file.c_str())) {
262 image_loader = driver->getImageLoader(i);
268 warningstream << "Could not find image loader for file '" << icon_file
273 io::IReadFile *icon_f =
274 m_device->getFileSystem()->createAndOpenFile(icon_file.c_str());
277 warningstream << "Could not load icon file '" << icon_file << "'"
282 video::IImage *img = image_loader->loadImage(icon_f);
285 warningstream << "Could not load icon file '" << icon_file << "'"
291 u32 height = img->getDimension().Height;
292 u32 width = img->getDimension().Width;
294 size_t icon_buffer_len = 2 + height * width;
295 long *icon_buffer = new long[icon_buffer_len];
297 icon_buffer[0] = width;
298 icon_buffer[1] = height;
300 for (u32 x = 0; x < width; x++) {
301 for (u32 y = 0; y < height; y++) {
302 video::SColor col = img->getPixel(x, y);
304 pixel_val |= (u8)col.getAlpha() << 24;
305 pixel_val |= (u8)col.getRed() << 16;
306 pixel_val |= (u8)col.getGreen() << 8;
307 pixel_val |= (u8)col.getBlue();
308 icon_buffer[2 + x + y * width] = pixel_val;
315 const video::SExposedVideoData &video_data = driver->getExposedVideoData();
317 Display *x11_dpl = (Display *)video_data.OpenGLLinux.X11Display;
319 if (x11_dpl == NULL) {
320 warningstream << "Could not find x11 display for setting its icon."
322 delete[] icon_buffer;
326 Window x11_win = (Window)video_data.OpenGLLinux.X11Window;
328 Atom net_wm_icon = XInternAtom(x11_dpl, "_NET_WM_ICON", False);
329 Atom cardinal = XInternAtom(x11_dpl, "CARDINAL", False);
330 XChangeProperty(x11_dpl, x11_win, net_wm_icon, cardinal, 32, PropModeReplace,
331 (const unsigned char *)icon_buffer, icon_buffer_len);
333 delete[] icon_buffer;
340 Draws a screen with a single text on it.
341 Text will be removed when the screen is drawn the next time.
342 Additionally, a progressbar can be drawn when percent is set between 0 and 100.
344 void RenderingEngine::_draw_load_screen(const std::wstring &text,
345 gui::IGUIEnvironment *guienv, ITextureSource *tsrc, float dtime,
346 int percent, bool clouds)
348 v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
350 v2s32 textsize(g_fontengine->getTextWidth(text), g_fontengine->getLineHeight());
351 v2s32 center(screensize.X / 2, screensize.Y / 2);
352 core::rect<s32> textrect(center - textsize / 2, center + textsize / 2);
354 gui::IGUIStaticText *guitext =
355 guienv->addStaticText(text.c_str(), textrect, false, false);
356 guitext->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
358 bool cloud_menu_background = clouds && g_settings->getBool("menu_clouds");
359 if (cloud_menu_background) {
360 g_menuclouds->step(dtime * 3);
361 g_menuclouds->render();
362 get_video_driver()->beginScene(
363 true, true, video::SColor(255, 140, 186, 250));
364 g_menucloudsmgr->drawAll();
366 get_video_driver()->beginScene(true, true, video::SColor(255, 0, 0, 0));
369 if ((percent >= 0) && (percent <= 100)) {
370 video::ITexture *progress_img = tsrc->getTexture("progress_bar.png");
371 video::ITexture *progress_img_bg =
372 tsrc->getTexture("progress_bar_bg.png");
374 if (progress_img && progress_img_bg) {
376 const core::dimension2d<u32> &img_size =
377 progress_img_bg->getSize();
378 u32 imgW = rangelim(img_size.Width, 200, 600);
379 u32 imgH = rangelim(img_size.Height, 24, 72);
381 const core::dimension2d<u32> img_size(256, 48);
382 float imgRatio = (float)img_size.Height / img_size.Width;
383 u32 imgW = screensize.X / 2.2f;
384 u32 imgH = floor(imgW * imgRatio);
386 v2s32 img_pos((screensize.X - imgW) / 2,
387 (screensize.Y - imgH) / 2);
389 draw2DImageFilterScaled(get_video_driver(), progress_img_bg,
390 core::rect<s32>(img_pos.X, img_pos.Y,
393 core::rect<s32>(0, 0, img_size.Width,
397 draw2DImageFilterScaled(get_video_driver(), progress_img,
398 core::rect<s32>(img_pos.X, img_pos.Y,
399 img_pos.X + (percent * imgW) / 100,
401 core::rect<s32>(0, 0,
402 (percent * img_size.Width) / 100,
409 get_video_driver()->endScene();
414 Draws the menu scene including (optional) cloud background.
416 void RenderingEngine::_draw_menu_scene(gui::IGUIEnvironment *guienv,
417 float dtime, bool clouds)
419 bool cloud_menu_background = clouds && g_settings->getBool("menu_clouds");
420 if (cloud_menu_background) {
421 g_menuclouds->step(dtime * 3);
422 g_menuclouds->render();
423 get_video_driver()->beginScene(
424 true, true, video::SColor(255, 140, 186, 250));
425 g_menucloudsmgr->drawAll();
427 get_video_driver()->beginScene(true, true, video::SColor(255, 0, 0, 0));
430 get_video_driver()->endScene();
433 std::vector<core::vector3d<u32>> RenderingEngine::getSupportedVideoModes()
435 IrrlichtDevice *nulldevice = createDevice(video::EDT_NULL);
436 sanity_check(nulldevice);
438 std::vector<core::vector3d<u32>> mlist;
439 video::IVideoModeList *modelist = nulldevice->getVideoModeList();
441 s32 num_modes = modelist->getVideoModeCount();
442 for (s32 i = 0; i != num_modes; i++) {
443 core::dimension2d<u32> mode_res = modelist->getVideoModeResolution(i);
444 u32 mode_depth = (u32)modelist->getVideoModeDepth(i);
445 mlist.emplace_back(mode_res.Width, mode_res.Height, mode_depth);
452 std::vector<irr::video::E_DRIVER_TYPE> RenderingEngine::getSupportedVideoDrivers()
454 std::vector<irr::video::E_DRIVER_TYPE> drivers;
456 for (int i = 0; i != irr::video::EDT_COUNT; i++) {
457 if (irr::IrrlichtDevice::isDriverSupported((irr::video::E_DRIVER_TYPE)i))
458 drivers.push_back((irr::video::E_DRIVER_TYPE)i);
464 void RenderingEngine::_initialize(Client *client, Hud *hud)
466 const std::string &draw_mode = g_settings->get("3d_mode");
467 core.reset(createRenderingCore(draw_mode, m_device, client, hud));
471 void RenderingEngine::_finalize()
476 void RenderingEngine::_draw_scene(video::SColor skycolor, bool show_hud,
477 bool show_minimap, bool draw_wield_tool, bool draw_crosshair)
479 core->draw(skycolor, show_hud, show_minimap, draw_wield_tool, draw_crosshair);
482 const char *RenderingEngine::getVideoDriverName(irr::video::E_DRIVER_TYPE type)
484 static const char *driver_ids[] = {
495 return driver_ids[type];
498 const char *RenderingEngine::getVideoDriverFriendlyName(irr::video::E_DRIVER_TYPE type)
500 static const char *driver_names[] = {
511 return driver_names[type];
517 static float calcDisplayDensity()
519 const char *current_display = getenv("DISPLAY");
521 if (current_display != NULL) {
522 Display *x11display = XOpenDisplay(current_display);
524 if (x11display != NULL) {
526 float dpi_height = floor(
527 DisplayHeight(x11display, 0) /
528 (DisplayHeightMM(x11display, 0) *
531 float dpi_width = floor(
532 DisplayWidth(x11display, 0) /
533 (DisplayWidthMM(x11display, 0) *
537 XCloseDisplay(x11display);
539 return std::max(dpi_height, dpi_width) / 96.0;
543 /* return manually specified dpi */
544 return g_settings->getFloat("screen_dpi") / 96.0;
547 float RenderingEngine::getDisplayDensity()
549 static float cached_display_density = calcDisplayDensity();
550 return cached_display_density;
554 float RenderingEngine::getDisplayDensity()
556 return g_settings->getFloat("screen_dpi") / 96.0;
560 v2u32 RenderingEngine::getDisplaySize()
562 IrrlichtDevice *nulldevice = createDevice(video::EDT_NULL);
564 core::dimension2d<u32> deskres =
565 nulldevice->getVideoModeList()->getDesktopResolution();
570 #endif // __ANDROID__