From 012eb10e1901fe240d0b1f0ea607a5f712c2f8e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kapu=C5=9Bci=C5=84ski?= Date: Mon, 15 Jun 2015 20:20:03 +0200 Subject: [PATCH] Added automatic graphics detection (-graphics auto) --- src/app/app.cpp | 15 ++++--------- src/graphics/core/device.h | 22 +++++++++++++++++++ src/graphics/opengl/glutil.cpp | 40 +++++++++++++++++++++++++++------- src/graphics/opengl/glutil.h | 21 ++++++------------ 4 files changed, 65 insertions(+), 33 deletions(-) diff --git a/src/app/app.cpp b/src/app/app.cpp index 3a1d2180..b9961b3e 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -35,9 +35,7 @@ #include "graphics/engine/modelmanager.h" #include "graphics/core/nulldevice.h" -#include "graphics/opengl/gldevice.h" -#include "graphics/opengl/gl21device.h" -#include "graphics/opengl/gl33device.h" +#include "graphics/opengl/glutil.h" #include "object/robotmain.h" #include "object/objman.h" @@ -563,14 +561,9 @@ bool CApplication::Create() if(!m_headless) { - // The video is ready, we can create and initalize the graphics device - if (m_graphics == "opengl") - m_device = new Gfx::CGLDevice(m_deviceConfig); - else if (m_graphics == "gl21") - m_device = new Gfx::CGL21Device(m_deviceConfig); - else if (m_graphics == "gl33") - m_device = new Gfx::CGL33Device(m_deviceConfig); - else + m_device = Gfx::CreateDevice(m_deviceConfig, m_graphics.c_str()); + + if (m_device == nullptr) { m_device = new Gfx::CNullDevice(); GetLogger()->Error("Unknown graphics device: %s\n", m_graphics.c_str()); diff --git a/src/graphics/core/device.h b/src/graphics/core/device.h index a7d10b5f..74600c81 100644 --- a/src/graphics/core/device.h +++ b/src/graphics/core/device.h @@ -65,6 +65,20 @@ struct DeviceConfig //! No window frame (also set with full screen) bool noFrame; + //! Size of red channel in bits + int redSize; + //! Size of green channel in bits + int greenSize; + //! Size of blue channel in bits + int blueSize; + //! Size of alpha channel in bits + int alphaSize; + //! Color depth in bits + int depthSize; + + //! Force hardware acceleration (video mode set will fail on lack of hw accel) + bool hardwareAccel; + //! Constructor calls LoadDefault() DeviceConfig() { LoadDefault(); } @@ -77,6 +91,14 @@ struct DeviceConfig resizeable = true; doubleBuf = true; noFrame = false; + + hardwareAccel = true; + + redSize = 8; + blueSize = 8; + greenSize = 8; + alphaSize = 8; + depthSize = 24; } }; diff --git a/src/graphics/opengl/glutil.cpp b/src/graphics/opengl/glutil.cpp index 5dbb97e5..290ba6b7 100644 --- a/src/graphics/opengl/glutil.cpp +++ b/src/graphics/opengl/glutil.cpp @@ -18,6 +18,9 @@ */ #include "graphics/opengl/glutil.h" +#include "graphics/opengl/gldevice.h" +#include "graphics/opengl/gl21device.h" +#include "graphics/opengl/gl33device.h" #include "common/logger.h" #include @@ -36,17 +39,38 @@ void GLDeviceConfig::LoadDefault() { DeviceConfig::LoadDefault(); - hardwareAccel = true; - - redSize = 8; - blueSize = 8; - greenSize = 8; - alphaSize = 8; - depthSize = 24; - vboMode = VBO_MODE_AUTO; } +CDevice* CreateDevice(const GLDeviceConfig &config, const char *name) +{ + if (name == nullptr) return nullptr; + else if (strcmp(name, "default") == 0) return new CGLDevice(config); + else if (strcmp(name, "opengl") == 0) return new CGLDevice(config); + else if (strcmp(name, "gl14") == 0) return new CGLDevice(config); + else if (strcmp(name, "gl21") == 0) return new CGL21Device(config); + else if (strcmp(name, "gl33") == 0) return new CGL33Device(config); + else if (strcmp(name, "auto") == 0) + { + int version = GetOpenGLVersion(); + + if (version >= 33) return new CGL33Device(config); + else if (version >= 21) return new CGL21Device(config); + else return new CGLDevice(config); + } + else return nullptr; +} + +int GetOpenGLVersion() +{ + const char *version = reinterpret_cast(glGetString(GL_VERSION)); + int major = 0, minor = 0; + + sscanf(version, "%d.%d", &major, &minor); + + return 10 * major + minor; +} + GLenum TranslateGfxPrimitive(PrimitiveType type) { GLenum flag = 0; diff --git a/src/graphics/opengl/glutil.h b/src/graphics/opengl/glutil.h index 54086ec2..fccc7e21 100644 --- a/src/graphics/opengl/glutil.h +++ b/src/graphics/opengl/glutil.h @@ -47,20 +47,6 @@ enum VBOMode \brief Additional config with OpenGL-specific settings */ struct GLDeviceConfig : public DeviceConfig { - //! Size of red channel in bits - int redSize; - //! Size of green channel in bits - int greenSize; - //! Size of blue channel in bits - int blueSize; - //! Size of alpha channel in bits - int alphaSize; - //! Color depth in bits - int depthSize; - - //! Force hardware acceleration (video mode set will fail on lack of hw accel) - bool hardwareAccel; - //! VBO override/autodetect VBOMode vboMode; @@ -71,6 +57,13 @@ struct GLDeviceConfig : public DeviceConfig void LoadDefault(); }; +//! Creates OpenGL device +CDevice* CreateDevice(const GLDeviceConfig &config, const char *name); + +//! Returns OpenGL version as one number. +// First digit is major part, second digit is minor part. +int GetOpenGLVersion(); + //! Translate Gfx primitive type to OpenGL primitive type GLenum TranslateGfxPrimitive(PrimitiveType type);