ddnet/src/engine/client/backend_sdl.cpp

1277 lines
33 KiB
C++
Raw Normal View History

#include <base/detect.h>
#if defined(CONF_FAMILY_WINDOWS)
// For FlashWindowEx, FLASHWINFO, FLASHW_TRAY
#define _WIN32_WINNT 0x0501
#define WINVER 0x0501
#endif
2021-04-30 22:42:37 +00:00
#ifndef CONF_BACKEND_OPENGL_ES
#include <GL/glew.h>
2021-04-30 22:42:37 +00:00
#endif
#include <engine/storage.h>
2017-10-20 07:08:49 +00:00
#include "SDL.h"
2021-04-30 22:42:37 +00:00
#include "SDL_syswm.h"
2015-08-24 20:46:28 +00:00
#include <base/detect.h>
#include <base/math.h>
2018-03-13 20:44:58 +00:00
#include <cmath>
#include <stdlib.h>
#include "SDL_hints.h"
2021-08-21 19:41:51 +00:00
#include "SDL_pixels.h"
#include "SDL_video.h"
#include <engine/shared/config.h>
2012-02-05 12:22:39 +00:00
#include <base/tl/threading.h>
2016-08-27 19:10:27 +00:00
#if defined(CONF_VIDEORECORDER)
#include "video.h"
2016-08-27 19:10:27 +00:00
#endif
#include "backend_sdl.h"
#if !defined(CONF_BACKEND_OPENGL_ES)
#include "backend/opengl/backend_opengl3.h"
#endif
#if defined(CONF_BACKEND_OPENGL_ES3) || defined(CONF_BACKEND_OPENGL_ES)
#include "backend/opengles/backend_opengles3.h"
#endif
#include "graphics_threaded.h"
#include <engine/shared/image_manipulation.h>
2021-08-21 19:41:51 +00:00
#include <engine/graphics.h>
2017-10-17 14:38:40 +00:00
#ifdef __MINGW32__
extern "C" {
int putenv(const char *);
2017-10-17 14:38:40 +00:00
}
#endif
// ------------ CGraphicsBackend_Threaded
void CGraphicsBackend_Threaded::ThreadFunc(void *pUser)
{
auto *pSelf = (CGraphicsBackend_Threaded *)pUser;
std::unique_lock<std::mutex> Lock(pSelf->m_BufferSwapMutex);
2021-10-27 22:52:06 +00:00
// notify, that the thread started
pSelf->m_BufferDoneCond.notify_all();
while(!pSelf->m_Shutdown)
{
pSelf->m_BufferSwapCond.wait(Lock);
if(pSelf->m_pBuffer)
{
#ifdef CONF_PLATFORM_MACOS
CAutoreleasePool AutoreleasePool;
#endif
pSelf->m_pProcessor->RunBuffer(pSelf->m_pBuffer);
pSelf->m_pBuffer = nullptr;
pSelf->m_BufferInProcess.store(false, std::memory_order_relaxed);
pSelf->m_BufferDoneCond.notify_all();
}
#if defined(CONF_VIDEORECORDER)
if(IVideo::Current())
IVideo::Current()->NextVideoFrameThread();
#endif
}
}
CGraphicsBackend_Threaded::CGraphicsBackend_Threaded()
{
2021-10-27 22:52:06 +00:00
m_pBuffer = nullptr;
m_pProcessor = nullptr;
m_BufferInProcess.store(false, std::memory_order_relaxed);
}
void CGraphicsBackend_Threaded::StartProcessor(ICommandProcessor *pProcessor)
{
m_Shutdown = false;
m_pProcessor = pProcessor;
2021-10-27 22:52:06 +00:00
std::unique_lock<std::mutex> Lock(m_BufferSwapMutex);
m_Thread = thread_init(ThreadFunc, this, "Graphics thread");
2021-10-27 22:52:06 +00:00
// wait for the thread to start
m_BufferDoneCond.wait(Lock);
}
void CGraphicsBackend_Threaded::StopProcessor()
{
m_Shutdown = true;
2021-10-27 22:52:06 +00:00
{
std::unique_lock<std::mutex> Lock(m_BufferSwapMutex);
m_BufferSwapCond.notify_all();
}
thread_wait(m_Thread);
}
void CGraphicsBackend_Threaded::RunBuffer(CCommandBuffer *pBuffer)
{
WaitForIdle();
2021-10-27 22:52:06 +00:00
std::unique_lock<std::mutex> Lock(m_BufferSwapMutex);
2021-10-28 11:46:02 +00:00
m_pBuffer = pBuffer;
2021-10-27 22:52:06 +00:00
m_BufferInProcess.store(true, std::memory_order_relaxed);
m_BufferSwapCond.notify_all();
}
bool CGraphicsBackend_Threaded::IsIdle() const
{
2021-10-27 22:52:06 +00:00
return !m_BufferInProcess.load(std::memory_order_relaxed);
}
void CGraphicsBackend_Threaded::WaitForIdle()
{
2021-10-27 22:52:06 +00:00
std::unique_lock<std::mutex> Lock(m_BufferSwapMutex);
if(m_pBuffer != nullptr)
m_BufferDoneCond.wait(Lock);
}
// ------------ CCommandProcessorFragment_General
void CCommandProcessorFragment_General::Cmd_Signal(const CCommandBuffer::SCommand_Signal *pCommand)
{
pCommand->m_pSemaphore->Signal();
}
bool CCommandProcessorFragment_General::RunCommand(const CCommandBuffer::SCommand *pBaseCommand)
{
switch(pBaseCommand->m_Cmd)
{
case CCommandBuffer::CMD_NOP: break;
case CCommandBuffer::CMD_SIGNAL: Cmd_Signal(static_cast<const CCommandBuffer::SCommand_Signal *>(pBaseCommand)); break;
default: return false;
}
return true;
2021-04-30 22:42:37 +00:00
}
2021-04-30 22:42:37 +00:00
// ------------ CCommandProcessorFragment_SDL
void CCommandProcessorFragment_SDL::Cmd_Init(const SCommand_Init *pCommand)
{
m_GLContext = pCommand->m_GLContext;
m_pWindow = pCommand->m_pWindow;
SDL_GL_MakeCurrent(m_pWindow, m_GLContext);
}
void CCommandProcessorFragment_SDL::Cmd_Shutdown(const SCommand_Shutdown *pCommand)
{
2015-08-24 20:46:28 +00:00
SDL_GL_MakeCurrent(NULL, NULL);
}
void CCommandProcessorFragment_SDL::Cmd_Swap(const CCommandBuffer::SCommand_Swap *pCommand)
{
2015-08-24 20:46:28 +00:00
SDL_GL_SwapWindow(m_pWindow);
}
void CCommandProcessorFragment_SDL::Cmd_VSync(const CCommandBuffer::SCommand_VSync *pCommand)
{
*pCommand->m_pRetOk = SDL_GL_SetSwapInterval(pCommand->m_VSync) == 0;
}
2021-08-24 10:18:20 +00:00
void CCommandProcessorFragment_SDL::Cmd_WindowCreateNtf(const CCommandBuffer::SCommand_WindowCreateNtf *pCommand)
{
m_pWindow = SDL_GetWindowFromID(pCommand->m_WindowID);
// Android destroys windows when they are not visible, so we get the new one and work with that
// The graphic context does not need to be recreated, just unbound see @see SCommand_WindowDestroyNtf
#ifdef CONF_PLATFORM_ANDROID
SDL_GL_MakeCurrent(m_pWindow, m_GLContext);
dbg_msg("gfx", "render surface created.");
#endif
}
void CCommandProcessorFragment_SDL::Cmd_WindowDestroyNtf(const CCommandBuffer::SCommand_WindowDestroyNtf *pCommand)
{
// Unbind the graphic context from the window, so it does not get destroyed
#ifdef CONF_PLATFORM_ANDROID
dbg_msg("gfx", "render surface destroyed.");
SDL_GL_MakeCurrent(NULL, NULL);
#endif
}
CCommandProcessorFragment_SDL::CCommandProcessorFragment_SDL()
2022-02-14 23:17:57 +00:00
= default;
bool CCommandProcessorFragment_SDL::RunCommand(const CCommandBuffer::SCommand *pBaseCommand)
{
switch(pBaseCommand->m_Cmd)
{
2021-08-24 10:18:20 +00:00
case CCommandBuffer::CMD_WINDOW_CREATE_NTF: Cmd_WindowCreateNtf(static_cast<const CCommandBuffer::SCommand_WindowCreateNtf *>(pBaseCommand)); break;
case CCommandBuffer::CMD_WINDOW_DESTROY_NTF: Cmd_WindowDestroyNtf(static_cast<const CCommandBuffer::SCommand_WindowDestroyNtf *>(pBaseCommand)); break;
case CCommandBuffer::CMD_SWAP: Cmd_Swap(static_cast<const CCommandBuffer::SCommand_Swap *>(pBaseCommand)); break;
case CCommandBuffer::CMD_VSYNC: Cmd_VSync(static_cast<const CCommandBuffer::SCommand_VSync *>(pBaseCommand)); break;
case CMD_INIT: Cmd_Init(static_cast<const SCommand_Init *>(pBaseCommand)); break;
case CMD_SHUTDOWN: Cmd_Shutdown(static_cast<const SCommand_Shutdown *>(pBaseCommand)); break;
default: return false;
}
return true;
}
// ------------ CCommandProcessor_SDL_OpenGL
void CCommandProcessor_SDL_OpenGL::RunBuffer(CCommandBuffer *pBuffer)
{
2020-10-11 15:08:04 +00:00
for(CCommandBuffer::SCommand *pCommand = pBuffer->Head(); pCommand; pCommand = pCommand->m_pNext)
{
2020-10-11 15:08:04 +00:00
if(m_pOpenGL->RunCommand(pCommand))
continue;
2020-10-11 15:08:04 +00:00
if(m_SDL.RunCommand(pCommand))
continue;
2020-10-11 15:08:04 +00:00
if(m_General.RunCommand(pCommand))
continue;
2015-07-09 00:08:14 +00:00
dbg_msg("gfx", "unknown command %d", pCommand->m_Cmd);
}
}
2021-04-30 22:42:37 +00:00
CCommandProcessor_SDL_OpenGL::CCommandProcessor_SDL_OpenGL(EBackendType BackendType, int OpenGLMajor, int OpenGLMinor, int OpenGLPatch)
{
2021-04-30 22:42:37 +00:00
m_BackendType = BackendType;
if(BackendType == BACKEND_TYPE_OPENGL_ES)
{
#if defined(CONF_BACKEND_OPENGL_ES) || defined(CONF_BACKEND_OPENGL_ES3)
2021-04-30 22:42:37 +00:00
if(OpenGLMajor < 3)
{
m_pOpenGL = new CCommandProcessorFragment_OpenGLES();
2021-04-30 22:42:37 +00:00
}
else
{
m_pOpenGL = new CCommandProcessorFragment_OpenGLES3();
2021-04-30 22:42:37 +00:00
}
#endif
}
2021-04-30 22:42:37 +00:00
else if(BackendType == BACKEND_TYPE_OPENGL)
{
#if !defined(CONF_BACKEND_OPENGL_ES)
2021-04-30 22:42:37 +00:00
if(OpenGLMajor < 2)
{
m_pOpenGL = new CCommandProcessorFragment_OpenGL();
}
if(OpenGLMajor == 2)
{
m_pOpenGL = new CCommandProcessorFragment_OpenGL2();
}
if(OpenGLMajor == 3 && OpenGLMinor == 0)
{
m_pOpenGL = new CCommandProcessorFragment_OpenGL3();
}
else if((OpenGLMajor == 3 && OpenGLMinor == 3) || OpenGLMajor >= 4)
{
m_pOpenGL = new CCommandProcessorFragment_OpenGL3_3();
}
#endif
}
}
2020-09-30 21:51:33 +00:00
CCommandProcessor_SDL_OpenGL::~CCommandProcessor_SDL_OpenGL()
{
delete m_pOpenGL;
}
// ------------ CGraphicsBackend_SDL_OpenGL
2021-04-30 22:42:37 +00:00
static bool BackendInitGlew(EBackendType BackendType, int &GlewMajor, int &GlewMinor, int &GlewPatch)
{
2021-04-30 22:42:37 +00:00
if(BackendType == BACKEND_TYPE_OPENGL)
{
2021-04-30 22:42:37 +00:00
#ifndef CONF_BACKEND_OPENGL_ES
//support graphic cards that are pretty old(and linux)
glewExperimental = GL_TRUE;
2021-08-23 10:31:41 +00:00
#ifdef CONF_GLEW_HAS_CONTEXT_INIT
if(GLEW_OK != glewContextInit())
#else
2021-04-30 22:42:37 +00:00
if(GLEW_OK != glewInit())
2021-08-23 10:31:41 +00:00
#endif
2021-04-30 22:42:37 +00:00
return false;
2021-04-30 22:42:37 +00:00
#ifdef GLEW_VERSION_4_6
if(GLEW_VERSION_4_6)
{
2021-04-30 22:42:37 +00:00
GlewMajor = 4;
GlewMinor = 6;
GlewPatch = 0;
return true;
}
2021-04-30 22:42:37 +00:00
#endif
#ifdef GLEW_VERSION_4_5
2021-04-30 22:42:37 +00:00
if(GLEW_VERSION_4_5)
{
2021-04-30 22:42:37 +00:00
GlewMajor = 4;
GlewMinor = 5;
GlewPatch = 0;
return true;
}
#endif
2021-08-18 10:03:42 +00:00
// Don't allow GL 3.3, if the driver doesn't support atleast OpenGL 4.5
#ifndef CONF_FAMILY_WINDOWS
2021-04-30 22:42:37 +00:00
if(GLEW_VERSION_4_4)
{
2021-04-30 22:42:37 +00:00
GlewMajor = 4;
GlewMinor = 4;
GlewPatch = 0;
return true;
}
2021-04-30 22:42:37 +00:00
if(GLEW_VERSION_4_3)
{
2021-04-30 22:42:37 +00:00
GlewMajor = 4;
GlewMinor = 3;
GlewPatch = 0;
return true;
}
2021-04-30 22:42:37 +00:00
if(GLEW_VERSION_4_2)
{
2021-04-30 22:42:37 +00:00
GlewMajor = 4;
GlewMinor = 2;
GlewPatch = 0;
return true;
}
2021-04-30 22:42:37 +00:00
if(GLEW_VERSION_4_1)
{
2021-04-30 22:42:37 +00:00
GlewMajor = 4;
GlewMinor = 1;
GlewPatch = 0;
return true;
}
if(GLEW_VERSION_4_0)
{
GlewMajor = 4;
GlewMinor = 0;
GlewPatch = 0;
return true;
}
if(GLEW_VERSION_3_3)
{
GlewMajor = 3;
GlewMinor = 3;
GlewPatch = 0;
return true;
}
2021-08-18 10:03:42 +00:00
#endif
2021-04-30 22:42:37 +00:00
if(GLEW_VERSION_3_0)
{
GlewMajor = 3;
GlewMinor = 0;
GlewPatch = 0;
return true;
}
if(GLEW_VERSION_2_1)
{
GlewMajor = 2;
GlewMinor = 1;
GlewPatch = 0;
return true;
}
if(GLEW_VERSION_2_0)
{
GlewMajor = 2;
GlewMinor = 0;
GlewPatch = 0;
return true;
}
if(GLEW_VERSION_1_5)
{
GlewMajor = 1;
GlewMinor = 5;
GlewPatch = 0;
return true;
}
if(GLEW_VERSION_1_4)
{
GlewMajor = 1;
GlewMinor = 4;
GlewPatch = 0;
return true;
}
if(GLEW_VERSION_1_3)
{
GlewMajor = 1;
GlewMinor = 3;
GlewPatch = 0;
return true;
}
if(GLEW_VERSION_1_2_1)
{
GlewMajor = 1;
GlewMinor = 2;
GlewPatch = 1;
return true;
}
if(GLEW_VERSION_1_2)
{
GlewMajor = 1;
GlewMinor = 2;
GlewPatch = 0;
return true;
}
if(GLEW_VERSION_1_1)
{
GlewMajor = 1;
GlewMinor = 1;
GlewPatch = 0;
return true;
}
2021-04-30 22:42:37 +00:00
#endif
}
2021-04-30 22:42:37 +00:00
else if(BackendType == BACKEND_TYPE_OPENGL_ES)
{
2021-04-30 22:42:37 +00:00
// just assume the version we need
GlewMajor = 3;
GlewMinor = 0;
GlewPatch = 0;
return true;
}
2021-04-30 22:42:37 +00:00
return true;
}
static int IsVersionSupportedGlew(EBackendType BackendType, int VersionMajor, int VersionMinor, int VersionPatch, int GlewMajor, int GlewMinor, int GlewPatch)
{
int InitError = 0;
if(BackendType == BACKEND_TYPE_OPENGL)
{
2021-04-30 22:42:37 +00:00
if(VersionMajor >= 4 && GlewMajor < 4)
{
InitError = -1;
}
2021-04-30 22:42:37 +00:00
else if(VersionMajor >= 3 && GlewMajor < 3)
{
InitError = -1;
}
2021-04-30 22:42:37 +00:00
else if(VersionMajor == 3 && GlewMajor == 3)
{
2021-04-30 22:42:37 +00:00
if(VersionMinor >= 3 && GlewMinor < 3)
{
InitError = -1;
}
if(VersionMinor >= 2 && GlewMinor < 2)
{
InitError = -1;
}
if(VersionMinor >= 1 && GlewMinor < 1)
{
InitError = -1;
}
if(VersionMinor >= 0 && GlewMinor < 0)
{
InitError = -1;
}
}
2021-04-30 22:42:37 +00:00
else if(VersionMajor >= 2 && GlewMajor < 2)
{
InitError = -1;
}
2021-04-30 22:42:37 +00:00
else if(VersionMajor == 2 && GlewMajor == 2)
{
2021-04-30 22:42:37 +00:00
if(VersionMinor >= 1 && GlewMinor < 1)
{
InitError = -1;
}
2021-04-30 22:42:37 +00:00
if(VersionMinor >= 0 && GlewMinor < 0)
{
InitError = -1;
}
}
2021-04-30 22:42:37 +00:00
else if(VersionMajor >= 1 && GlewMajor < 1)
{
InitError = -1;
}
2021-04-30 22:42:37 +00:00
else if(VersionMajor == 1 && GlewMajor == 1)
{
2021-04-30 22:42:37 +00:00
if(VersionMinor >= 5 && GlewMinor < 5)
{
InitError = -1;
}
if(VersionMinor >= 4 && GlewMinor < 4)
{
InitError = -1;
}
if(VersionMinor >= 3 && GlewMinor < 3)
{
InitError = -1;
}
if(VersionMinor >= 2 && GlewMinor < 2)
{
InitError = -1;
}
else if(VersionMinor == 2 && GlewMinor == 2)
{
if(VersionPatch >= 1 && GlewPatch < 1)
{
InitError = -1;
}
if(VersionPatch >= 0 && GlewPatch < 0)
{
InitError = -1;
}
}
if(VersionMinor >= 1 && GlewMinor < 1)
{
InitError = -1;
}
if(VersionMinor >= 0 && GlewMinor < 0)
{
InitError = -1;
}
}
}
return InitError;
}
2021-04-30 22:42:37 +00:00
EBackendType CGraphicsBackend_SDL_OpenGL::DetectBackend()
{
#ifndef CONF_BACKEND_OPENGL_ES
#ifdef CONF_BACKEND_OPENGL_ES3
const char *pEnvDriver = getenv("DDNET_DRIVER");
if(pEnvDriver && str_comp(pEnvDriver, "GLES") == 0)
return BACKEND_TYPE_OPENGL_ES;
else
return BACKEND_TYPE_OPENGL;
#else
2021-04-30 22:42:37 +00:00
return BACKEND_TYPE_OPENGL;
#endif
2021-04-30 22:42:37 +00:00
#else
return BACKEND_TYPE_OPENGL_ES;
#endif
}
void CGraphicsBackend_SDL_OpenGL::ClampDriverVersion(EBackendType BackendType)
{
if(BackendType == BACKEND_TYPE_OPENGL)
{
//clamp the versions to existing versions(only for OpenGL major <= 3)
if(g_Config.m_GfxOpenGLMajor == 1)
{
g_Config.m_GfxOpenGLMinor = clamp(g_Config.m_GfxOpenGLMinor, 1, 5);
if(g_Config.m_GfxOpenGLMinor == 2)
g_Config.m_GfxOpenGLPatch = clamp(g_Config.m_GfxOpenGLPatch, 0, 1);
else
g_Config.m_GfxOpenGLPatch = 0;
}
else if(g_Config.m_GfxOpenGLMajor == 2)
{
g_Config.m_GfxOpenGLMinor = clamp(g_Config.m_GfxOpenGLMinor, 0, 1);
g_Config.m_GfxOpenGLPatch = 0;
}
else if(g_Config.m_GfxOpenGLMajor == 3)
{
g_Config.m_GfxOpenGLMinor = clamp(g_Config.m_GfxOpenGLMinor, 0, 3);
if(g_Config.m_GfxOpenGLMinor < 3)
g_Config.m_GfxOpenGLMinor = 0;
g_Config.m_GfxOpenGLPatch = 0;
}
}
else if(BackendType == BACKEND_TYPE_OPENGL_ES)
{
#if !defined(CONF_BACKEND_OPENGL_ES3)
2021-04-30 22:42:37 +00:00
// Make sure GLES is set to 1.0 (which is equivalent to OpenGL 1.3), if its not set to >= 3.0(which is equivalent to OpenGL 3.3)
if(g_Config.m_GfxOpenGLMajor < 3)
{
g_Config.m_GfxOpenGLMajor = 1;
g_Config.m_GfxOpenGLMinor = 0;
g_Config.m_GfxOpenGLPatch = 0;
// GLES also doesnt know GL_QUAD
g_Config.m_GfxQuadAsTriangle = 1;
}
2021-05-01 00:39:06 +00:00
#else
g_Config.m_GfxOpenGLMajor = 3;
g_Config.m_GfxOpenGLMinor = 0;
g_Config.m_GfxOpenGLPatch = 0;
#endif
2021-04-30 22:42:37 +00:00
}
}
bool CGraphicsBackend_SDL_OpenGL::IsModernAPI(EBackendType BackendType)
{
if(BackendType == BACKEND_TYPE_OPENGL)
return (g_Config.m_GfxOpenGLMajor == 3 && g_Config.m_GfxOpenGLMinor == 3) || g_Config.m_GfxOpenGLMajor >= 4;
else if(BackendType == BACKEND_TYPE_OPENGL_ES)
return g_Config.m_GfxOpenGLMajor >= 3;
return false;
}
void CGraphicsBackend_SDL_OpenGL::GetDriverVersion(EGraphicsDriverAgeType DriverAgeType, int &Major, int &Minor, int &Patch)
{
if(m_BackendType == BACKEND_TYPE_OPENGL)
{
if(DriverAgeType == GRAPHICS_DRIVER_AGE_TYPE_LEGACY)
{
Major = 1;
Minor = 4;
Patch = 0;
}
else if(DriverAgeType == GRAPHICS_DRIVER_AGE_TYPE_DEFAULT)
{
Major = 3;
Minor = 0;
Patch = 0;
}
else if(DriverAgeType == GRAPHICS_DRIVER_AGE_TYPE_MODERN)
{
Major = 3;
Minor = 3;
Patch = 0;
}
}
else if(m_BackendType == BACKEND_TYPE_OPENGL_ES)
{
if(DriverAgeType == GRAPHICS_DRIVER_AGE_TYPE_LEGACY)
{
Major = 1;
Minor = 0;
Patch = 0;
}
else if(DriverAgeType == GRAPHICS_DRIVER_AGE_TYPE_DEFAULT)
{
Major = 3;
Minor = 0;
Patch = 0;
}
else if(DriverAgeType == GRAPHICS_DRIVER_AGE_TYPE_MODERN)
{
Major = 3;
Minor = 0;
Patch = 0;
}
}
}
2021-08-21 19:41:51 +00:00
static void DisplayToVideoMode(CVideoMode *pVMode, SDL_DisplayMode *pMode, int HiDPIScale, int RefreshRate)
{
pVMode->m_CanvasWidth = pMode->w * HiDPIScale;
pVMode->m_CanvasHeight = pMode->h * HiDPIScale;
pVMode->m_WindowWidth = pMode->w;
pVMode->m_WindowHeight = pMode->h;
pVMode->m_RefreshRate = RefreshRate;
pVMode->m_Red = SDL_BITSPERPIXEL(pMode->format);
pVMode->m_Green = SDL_BITSPERPIXEL(pMode->format);
pVMode->m_Blue = SDL_BITSPERPIXEL(pMode->format);
pVMode->m_Format = pMode->format;
}
void CGraphicsBackend_SDL_OpenGL::GetVideoModes(CVideoMode *pModes, int MaxModes, int *pNumModes, int HiDPIScale, int MaxWindowWidth, int MaxWindowHeight, int Screen)
{
SDL_DisplayMode DesktopMode;
int maxModes = SDL_GetNumDisplayModes(Screen);
int numModes = 0;
// Only collect fullscreen modes when requested, that makes sure in windowed mode no refresh rates are shown that aren't supported without
// fullscreen anyway(except fullscreen desktop)
bool IsFullscreenDestkop = m_pWindow != NULL && (((SDL_GetWindowFlags(m_pWindow) & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) || g_Config.m_GfxFullscreen == 3);
2021-08-21 19:41:51 +00:00
bool CollectFullscreenModes = m_pWindow == NULL || ((SDL_GetWindowFlags(m_pWindow) & SDL_WINDOW_FULLSCREEN) != 0 && !IsFullscreenDestkop);
if(SDL_GetDesktopDisplayMode(Screen, &DesktopMode) < 0)
{
dbg_msg("gfx", "unable to get display mode: %s", SDL_GetError());
}
static constexpr int ModeCount = 256;
SDL_DisplayMode aModes[ModeCount];
int NumModes = 0;
for(int i = 0; i < maxModes && NumModes < ModeCount; i++)
{
SDL_DisplayMode mode;
if(SDL_GetDisplayMode(Screen, i, &mode) < 0)
{
dbg_msg("gfx", "unable to get display mode: %s", SDL_GetError());
continue;
}
aModes[NumModes] = mode;
++NumModes;
}
auto &&ModeInsert = [&](SDL_DisplayMode &mode) {
if(numModes < MaxModes)
{
// if last mode was equal, ignore this one --- in fullscreen this can really only happen if the screen
// supports different color modes
// in non fullscren these are the modes that show different refresh rate, but are basically the same
if(numModes > 0 && pModes[numModes - 1].m_WindowWidth == mode.w && pModes[numModes - 1].m_WindowHeight == mode.h && (pModes[numModes - 1].m_RefreshRate == mode.refresh_rate || (mode.refresh_rate != DesktopMode.refresh_rate && !CollectFullscreenModes)))
return;
DisplayToVideoMode(&pModes[numModes], &mode, HiDPIScale, !CollectFullscreenModes ? DesktopMode.refresh_rate : mode.refresh_rate);
numModes++;
}
};
for(int i = 0; i < NumModes; i++)
{
SDL_DisplayMode &mode = aModes[i];
if(mode.w > MaxWindowWidth || mode.h > MaxWindowHeight)
continue;
ModeInsert(mode);
if(IsFullscreenDestkop)
break;
if(numModes >= MaxModes)
break;
}
*pNumModes = numModes;
}
void CGraphicsBackend_SDL_OpenGL::GetCurrentVideoMode(CVideoMode &CurMode, int HiDPIScale, int MaxWindowWidth, int MaxWindowHeight, int Screen)
{
SDL_DisplayMode DPMode;
// if "real" fullscreen, obtain the video mode for that
if((SDL_GetWindowFlags(m_pWindow) & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN)
2021-08-21 19:41:51 +00:00
{
if(SDL_GetCurrentDisplayMode(Screen, &DPMode))
{
dbg_msg("gfx", "unable to get display mode: %s", SDL_GetError());
}
2021-08-21 19:41:51 +00:00
}
else
{
if(SDL_GetDesktopDisplayMode(Screen, &DPMode) < 0)
{
dbg_msg("gfx", "unable to get display mode: %s", SDL_GetError());
}
else
{
int Width = 0;
int Height = 0;
SDL_GL_GetDrawableSize(m_pWindow, &Width, &Height);
DPMode.w = Width;
DPMode.h = Height;
}
2021-08-21 19:41:51 +00:00
}
DisplayToVideoMode(&CurMode, &DPMode, HiDPIScale, DPMode.refresh_rate);
}
int CGraphicsBackend_SDL_OpenGL::Init(const char *pName, int *pScreen, int *pWidth, int *pHeight, int *pRefreshRate, int FsaaSamples, int Flags, int *pDesktopWidth, int *pDesktopHeight, int *pCurrentWidth, int *pCurrentHeight, IStorage *pStorage)
2018-04-13 19:34:12 +00:00
{
2020-03-20 12:48:45 +00:00
// print sdl version
{
SDL_version Compiled;
SDL_version Linked;
SDL_VERSION(&Compiled);
SDL_GetVersion(&Linked);
dbg_msg("sdl", "SDL version %d.%d.%d (compiled = %d.%d.%d)", Linked.major, Linked.minor, Linked.patch,
Compiled.major, Compiled.minor, Compiled.patch);
2020-03-20 12:48:45 +00:00
}
if(!SDL_WasInit(SDL_INIT_VIDEO))
{
if(SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
{
dbg_msg("gfx", "unable to init SDL video: %s", SDL_GetError());
return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_SDL_INIT_FAILED;
}
}
m_BackendType = DetectBackend();
ClampDriverVersion(m_BackendType);
m_UseNewOpenGL = IsModernAPI(m_BackendType);
2021-04-30 22:42:37 +00:00
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, g_Config.m_GfxOpenGLMajor);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, g_Config.m_GfxOpenGLMinor);
dbg_msg("gfx", "Created OpenGL %zu.%zu context.", (size_t)g_Config.m_GfxOpenGLMajor, (size_t)g_Config.m_GfxOpenGLMinor);
if(m_BackendType == BACKEND_TYPE_OPENGL)
2017-09-13 18:33:58 +00:00
{
2021-04-30 22:42:37 +00:00
if(g_Config.m_GfxOpenGLMajor == 3 && g_Config.m_GfxOpenGLMinor == 0)
2018-04-13 19:34:12 +00:00
{
2021-04-30 22:42:37 +00:00
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
}
2021-04-30 22:42:37 +00:00
else if(m_UseNewOpenGL)
2018-04-13 19:34:12 +00:00
{
2021-04-30 22:42:37 +00:00
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
2018-04-13 19:34:12 +00:00
}
}
else if(m_BackendType == BACKEND_TYPE_OPENGL_ES)
{
2021-04-30 22:42:37 +00:00
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
}
// set screen
SDL_Rect ScreenPos;
m_NumScreens = SDL_GetNumVideoDisplays();
if(m_NumScreens > 0)
{
2021-08-21 19:41:51 +00:00
*pScreen = clamp(*pScreen, 0, m_NumScreens - 1);
if(SDL_GetDisplayBounds(*pScreen, &ScreenPos) != 0)
{
dbg_msg("gfx", "unable to retrieve screen information: %s", SDL_GetError());
return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_SDL_SCREEN_INFO_REQUEST_FAILED;
}
}
else
2015-08-24 20:46:28 +00:00
{
dbg_msg("gfx", "unable to retrieve number of screens: %s", SDL_GetError());
return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_SDL_SCREEN_REQUEST_FAILED;
2015-08-24 20:46:28 +00:00
}
// store desktop resolution for settings reset button
SDL_DisplayMode DisplayMode;
2021-08-21 19:41:51 +00:00
if(SDL_GetDesktopDisplayMode(*pScreen, &DisplayMode))
{
dbg_msg("gfx", "unable to get desktop resolution: %s", SDL_GetError());
return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_SDL_SCREEN_RESOLUTION_REQUEST_FAILED;
}
bool IsDesktopChanged = *pDesktopWidth == 0 || *pDesktopHeight == 0 || *pDesktopWidth != DisplayMode.w || *pDesktopHeight != DisplayMode.h;
*pDesktopWidth = DisplayMode.w;
*pDesktopHeight = DisplayMode.h;
2021-08-21 19:41:51 +00:00
// fetch supported video modes
bool SupportedResolution = false;
CVideoMode aModes[256];
int ModesCount = 0;
int IndexOfResolution = -1;
GetVideoModes(aModes, sizeof(aModes) / sizeof(aModes[0]), &ModesCount, 1, *pDesktopWidth, *pDesktopHeight, *pScreen);
for(int i = 0; i < ModesCount; i++)
{
2021-08-21 19:41:51 +00:00
if(*pWidth == aModes[i].m_WindowWidth && *pHeight == aModes[i].m_WindowHeight && (*pRefreshRate == aModes[i].m_RefreshRate || *pRefreshRate == 0))
{
SupportedResolution = true;
IndexOfResolution = i;
break;
}
}
2015-08-24 20:46:28 +00:00
// set flags
int SdlFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS;
if(Flags & IGraphicsBackend::INITFLAG_HIGHDPI)
2020-04-07 20:37:46 +00:00
SdlFlags |= SDL_WINDOW_ALLOW_HIGHDPI;
if(Flags & IGraphicsBackend::INITFLAG_RESIZABLE)
2015-08-24 20:46:28 +00:00
SdlFlags |= SDL_WINDOW_RESIZABLE;
if(Flags & IGraphicsBackend::INITFLAG_BORDERLESS)
2015-08-24 20:46:28 +00:00
SdlFlags |= SDL_WINDOW_BORDERLESS;
if(Flags & IGraphicsBackend::INITFLAG_FULLSCREEN)
2021-08-21 19:41:51 +00:00
SdlFlags |= SDL_WINDOW_FULLSCREEN;
else if(Flags & (IGraphicsBackend::INITFLAG_DESKTOP_FULLSCREEN))
SdlFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
bool IsFullscreen = (SdlFlags & SDL_WINDOW_FULLSCREEN) != 0 || g_Config.m_GfxFullscreen == 3;
2021-08-21 19:41:51 +00:00
// use desktop resolution as default resolution, clamp resolution if users's display is smaller than we remembered
// if the user starts in fullscreen, and the resolution was not found use the desktop one
if((IsFullscreen && !SupportedResolution) || *pWidth == 0 || *pHeight == 0 || (IsDesktopChanged && (*pWidth > *pDesktopWidth || *pHeight > *pDesktopHeight)))
{
*pWidth = *pDesktopWidth;
*pHeight = *pDesktopHeight;
*pRefreshRate = DisplayMode.refresh_rate;
}
2021-08-21 19:41:51 +00:00
// if in fullscreen and refresh rate wasn't set yet, just use the one from the found list
if(*pRefreshRate == 0 && SupportedResolution)
{
*pRefreshRate = aModes[IndexOfResolution].m_RefreshRate;
2016-04-30 21:10:09 +00:00
}
2021-08-21 19:41:51 +00:00
else if(*pRefreshRate == 0)
{
2021-08-21 19:41:51 +00:00
*pRefreshRate = DisplayMode.refresh_rate;
}
// set gl attributes
2015-08-24 20:46:28 +00:00
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
if(FsaaSamples)
{
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, FsaaSamples);
}
else
{
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
}
if(g_Config.m_InpMouseOld)
SDL_SetHint(SDL_HINT_MOUSE_RELATIVE_MODE_WARP, "1");
2015-08-24 20:46:28 +00:00
m_pWindow = SDL_CreateWindow(
pName,
2021-08-21 19:41:51 +00:00
SDL_WINDOWPOS_CENTERED_DISPLAY(*pScreen),
SDL_WINDOWPOS_CENTERED_DISPLAY(*pScreen),
2015-08-24 20:46:28 +00:00
*pWidth,
*pHeight,
SdlFlags);
// set caption
2015-08-24 20:46:28 +00:00
if(m_pWindow == NULL)
{
dbg_msg("gfx", "unable to create window: %s", SDL_GetError());
return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_SDL_WINDOW_CREATE_FAILED;
2015-08-24 20:46:28 +00:00
}
m_GLContext = SDL_GL_CreateContext(m_pWindow);
2015-08-24 20:46:28 +00:00
if(m_GLContext == NULL)
{
2021-04-30 22:42:37 +00:00
SDL_DestroyWindow(m_pWindow);
2015-08-24 20:46:28 +00:00
dbg_msg("gfx", "unable to create OpenGL context: %s", SDL_GetError());
return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_OPENGL_CONTEXT_FAILED;
2015-07-09 00:08:14 +00:00
}
2017-10-20 07:08:49 +00:00
int GlewMajor = 0;
int GlewMinor = 0;
int GlewPatch = 0;
if(!BackendInitGlew(m_BackendType, GlewMajor, GlewMinor, GlewPatch))
2021-04-30 22:42:37 +00:00
{
SDL_GL_DeleteContext(m_GLContext);
SDL_DestroyWindow(m_pWindow);
return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_UNKNOWN;
}
int InitError = 0;
2020-11-05 19:38:37 +00:00
const char *pErrorStr = NULL;
InitError = IsVersionSupportedGlew(m_BackendType, g_Config.m_GfxOpenGLMajor, g_Config.m_GfxOpenGLMinor, g_Config.m_GfxOpenGLPatch, GlewMajor, GlewMinor, GlewPatch);
2021-05-06 10:59:30 +00:00
// SDL_GL_GetDrawableSize reports HiDPI resolution even with SDL_WINDOW_ALLOW_HIGHDPI not set, which is wrong
if(SdlFlags & SDL_WINDOW_ALLOW_HIGHDPI)
SDL_GL_GetDrawableSize(m_pWindow, pCurrentWidth, pCurrentHeight);
else
SDL_GetWindowSize(m_pWindow, pCurrentWidth, pCurrentHeight);
SDL_GL_SetSwapInterval(Flags & IGraphicsBackend::INITFLAG_VSYNC ? 1 : 0);
2015-08-24 20:46:28 +00:00
SDL_GL_MakeCurrent(NULL, NULL);
if(InitError != 0)
{
SDL_GL_DeleteContext(m_GLContext);
SDL_DestroyWindow(m_pWindow);
// try setting to glew supported version
g_Config.m_GfxOpenGLMajor = GlewMajor;
g_Config.m_GfxOpenGLMinor = GlewMinor;
g_Config.m_GfxOpenGLPatch = GlewPatch;
return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_OPENGL_VERSION_FAILED;
}
// start the command processor
m_pProcessor = new CCommandProcessor_SDL_OpenGL(m_BackendType, g_Config.m_GfxOpenGLMajor, g_Config.m_GfxOpenGLMinor, g_Config.m_GfxOpenGLPatch);
StartProcessor(m_pProcessor);
2020-11-05 19:38:37 +00:00
mem_zero(m_aErrorString, sizeof(m_aErrorString) / sizeof(m_aErrorString[0]));
// issue init commands for OpenGL and SDL
CCommandBuffer CmdBuffer(1024, 512);
//run sdl first to have the context in the thread
CCommandProcessorFragment_SDL::SCommand_Init CmdSDL;
CmdSDL.m_pWindow = m_pWindow;
CmdSDL.m_GLContext = m_GLContext;
CmdBuffer.AddCommandUnsafe(CmdSDL);
RunBuffer(&CmdBuffer);
WaitForIdle();
CmdBuffer.Reset();
if(InitError == 0)
{
CCommandProcessorFragment_OpenGLBase::SCommand_Init CmdOpenGL;
CmdOpenGL.m_pTextureMemoryUsage = &m_TextureMemoryUsage;
CmdOpenGL.m_pStorage = pStorage;
CmdOpenGL.m_pCapabilities = &m_Capabilites;
CmdOpenGL.m_pInitError = &InitError;
2021-04-30 22:42:37 +00:00
CmdOpenGL.m_RequestedMajor = g_Config.m_GfxOpenGLMajor;
CmdOpenGL.m_RequestedMinor = g_Config.m_GfxOpenGLMinor;
CmdOpenGL.m_RequestedPatch = g_Config.m_GfxOpenGLPatch;
CmdOpenGL.m_GlewMajor = GlewMajor;
CmdOpenGL.m_GlewMinor = GlewMinor;
CmdOpenGL.m_GlewPatch = GlewPatch;
CmdOpenGL.m_pErrStringPtr = &pErrorStr;
CmdOpenGL.m_pVendorString = m_aVendorString;
CmdOpenGL.m_pVersionString = m_aVersionString;
CmdOpenGL.m_pRendererString = m_aRendererString;
CmdOpenGL.m_RequestedBackend = m_BackendType;
CmdBuffer.AddCommandUnsafe(CmdOpenGL);
2021-04-30 22:42:37 +00:00
RunBuffer(&CmdBuffer);
WaitForIdle();
CmdBuffer.Reset();
}
if(InitError != 0)
{
if(InitError != -2)
{
// shutdown the context, as it might have been initialized
CCommandProcessorFragment_OpenGLBase::SCommand_Shutdown CmdGL;
CmdBuffer.AddCommandUnsafe(CmdGL);
2020-09-03 09:43:32 +00:00
RunBuffer(&CmdBuffer);
WaitForIdle();
CmdBuffer.Reset();
}
CCommandProcessorFragment_SDL::SCommand_Shutdown Cmd;
CmdBuffer.AddCommandUnsafe(Cmd);
RunBuffer(&CmdBuffer);
WaitForIdle();
CmdBuffer.Reset();
// stop and delete the processor
StopProcessor();
delete m_pProcessor;
m_pProcessor = 0;
SDL_GL_DeleteContext(m_GLContext);
SDL_DestroyWindow(m_pWindow);
// try setting to version string's supported version
if(InitError == -2)
{
g_Config.m_GfxOpenGLMajor = m_Capabilites.m_ContextMajor;
g_Config.m_GfxOpenGLMinor = m_Capabilites.m_ContextMinor;
g_Config.m_GfxOpenGLPatch = m_Capabilites.m_ContextPatch;
}
2020-11-05 19:38:37 +00:00
if(pErrorStr != NULL)
{
str_copy(m_aErrorString, pErrorStr, sizeof(m_aErrorString) / sizeof(m_aErrorString[0]));
}
return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_OPENGL_VERSION_FAILED;
}
{
2021-08-21 19:41:51 +00:00
CCommandBuffer::SCommand_Update_Viewport CmdSDL;
CmdSDL.m_X = 0;
CmdSDL.m_Y = 0;
2021-08-21 19:41:51 +00:00
CmdSDL.m_Width = *pCurrentWidth;
CmdSDL.m_Height = *pCurrentHeight;
CmdBuffer.AddCommandUnsafe(CmdSDL);
RunBuffer(&CmdBuffer);
WaitForIdle();
CmdBuffer.Reset();
}
// return
return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_NONE;
}
int CGraphicsBackend_SDL_OpenGL::Shutdown()
{
// issue a shutdown command
CCommandBuffer CmdBuffer(1024, 512);
CCommandProcessorFragment_OpenGLBase::SCommand_Shutdown CmdGL;
CmdBuffer.AddCommandUnsafe(CmdGL);
2020-09-03 09:43:32 +00:00
RunBuffer(&CmdBuffer);
WaitForIdle();
CmdBuffer.Reset();
CCommandProcessorFragment_SDL::SCommand_Shutdown Cmd;
CmdBuffer.AddCommandUnsafe(Cmd);
RunBuffer(&CmdBuffer);
WaitForIdle();
CmdBuffer.Reset();
2015-07-09 00:08:14 +00:00
// stop and delete the processor
StopProcessor();
delete m_pProcessor;
m_pProcessor = 0;
2015-08-24 20:46:28 +00:00
SDL_GL_DeleteContext(m_GLContext);
SDL_DestroyWindow(m_pWindow);
SDL_QuitSubSystem(SDL_INIT_VIDEO);
return 0;
}
int CGraphicsBackend_SDL_OpenGL::MemoryUsage() const
{
return m_TextureMemoryUsage;
}
void CGraphicsBackend_SDL_OpenGL::Minimize()
{
2015-08-24 20:46:28 +00:00
SDL_MinimizeWindow(m_pWindow);
}
void CGraphicsBackend_SDL_OpenGL::Maximize()
{
// TODO: SDL
}
void CGraphicsBackend_SDL_OpenGL::SetWindowParams(int FullscreenMode, bool IsBorderless, bool AllowResizing)
{
if(FullscreenMode > 0)
{
bool IsDesktopFullscreen = FullscreenMode == 2;
#ifndef CONF_FAMILY_WINDOWS
// special mode for windows only
IsDesktopFullscreen |= FullscreenMode == 3;
#endif
if(FullscreenMode == 1)
{
#if defined(CONF_PLATFORM_MACOS) || defined(CONF_PLATFORM_HAIKU)
2021-09-13 19:13:39 +00:00
// Todo SDL: remove this when fixed (game freezes when losing focus in fullscreen)
SDL_SetWindowFullscreen(m_pWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
#else
SDL_SetWindowFullscreen(m_pWindow, SDL_WINDOW_FULLSCREEN);
#endif
SDL_SetWindowResizable(m_pWindow, SDL_TRUE);
}
else if(IsDesktopFullscreen)
{
SDL_SetWindowFullscreen(m_pWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
SDL_SetWindowResizable(m_pWindow, SDL_TRUE);
}
else
{
SDL_SetWindowFullscreen(m_pWindow, 0);
SDL_SetWindowBordered(m_pWindow, SDL_TRUE);
SDL_SetWindowResizable(m_pWindow, SDL_FALSE);
SDL_DisplayMode DPMode;
if(SDL_GetDesktopDisplayMode(g_Config.m_GfxScreen, &DPMode) < 0)
{
dbg_msg("gfx", "unable to get display mode: %s", SDL_GetError());
}
else
{
ResizeWindow(DPMode.w, DPMode.h, DPMode.refresh_rate);
SDL_SetWindowPosition(m_pWindow, SDL_WINDOWPOS_CENTERED_DISPLAY(g_Config.m_GfxScreen), SDL_WINDOWPOS_CENTERED_DISPLAY(g_Config.m_GfxScreen));
}
}
}
else
{
SDL_SetWindowFullscreen(m_pWindow, 0);
SDL_SetWindowBordered(m_pWindow, SDL_bool(!IsBorderless));
SDL_SetWindowResizable(m_pWindow, SDL_TRUE);
}
}
bool CGraphicsBackend_SDL_OpenGL::SetWindowScreen(int Index)
{
if(Index < 0 || Index >= m_NumScreens)
{
return false;
}
SDL_Rect ScreenPos;
if(SDL_GetDisplayBounds(Index, &ScreenPos) != 0)
{
return false;
}
SDL_SetWindowPosition(m_pWindow,
SDL_WINDOWPOS_CENTERED_DISPLAY(Index),
SDL_WINDOWPOS_CENTERED_DISPLAY(Index));
return UpdateDisplayMode(Index);
}
bool CGraphicsBackend_SDL_OpenGL::UpdateDisplayMode(int Index)
{
SDL_DisplayMode DisplayMode;
if(SDL_GetDesktopDisplayMode(Index, &DisplayMode) < 0)
{
dbg_msg("gfx", "unable to get display mode: %s", SDL_GetError());
return false;
}
g_Config.m_GfxDesktopWidth = DisplayMode.w;
g_Config.m_GfxDesktopHeight = DisplayMode.h;
return true;
}
int CGraphicsBackend_SDL_OpenGL::GetWindowScreen()
{
return SDL_GetWindowDisplayIndex(m_pWindow);
}
int CGraphicsBackend_SDL_OpenGL::WindowActive()
{
return m_pWindow && SDL_GetWindowFlags(m_pWindow) & SDL_WINDOW_INPUT_FOCUS;
}
int CGraphicsBackend_SDL_OpenGL::WindowOpen()
{
return m_pWindow && SDL_GetWindowFlags(m_pWindow) & SDL_WINDOW_SHOWN;
}
void CGraphicsBackend_SDL_OpenGL::SetWindowGrab(bool Grab)
{
SDL_SetWindowGrab(m_pWindow, Grab ? SDL_TRUE : SDL_FALSE);
}
bool CGraphicsBackend_SDL_OpenGL::ResizeWindow(int w, int h, int RefreshRate)
2020-12-12 11:58:59 +00:00
{
// don't call resize events when the window is at fullscreen desktop
if(!m_pWindow || (SDL_GetWindowFlags(m_pWindow) & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP)
return false;
// if the window is at fullscreen use SDL_SetWindowDisplayMode instead, suggested by SDL
if(SDL_GetWindowFlags(m_pWindow) & SDL_WINDOW_FULLSCREEN)
{
#ifdef CONF_FAMILY_WINDOWS
// in windows make the window windowed mode first, this prevents strange window glitches (other games probably do something similar)
SetWindowParams(0, 1, true);
#endif
SDL_DisplayMode SetMode = {};
SDL_DisplayMode ClosestMode = {};
SetMode.format = 0;
SetMode.w = w;
SetMode.h = h;
SetMode.refresh_rate = RefreshRate;
SDL_SetWindowDisplayMode(m_pWindow, SDL_GetClosestDisplayMode(g_Config.m_GfxScreen, &SetMode, &ClosestMode));
#ifdef CONF_FAMILY_WINDOWS
// now change it back to fullscreen, this will restore the above set state, bcs SDL saves fullscreen modes appart from other video modes (as of SDL 2.0.16)
// see implementation of SDL_SetWindowDisplayMode
SetWindowParams(1, 0, true);
#endif
return true;
}
else
{
SDL_SetWindowSize(m_pWindow, w, h);
if(SDL_GetWindowFlags(m_pWindow) & SDL_WINDOW_MAXIMIZED)
// remove maximize flag
SDL_RestoreWindow(m_pWindow);
}
return false;
2020-12-12 11:58:59 +00:00
}
void CGraphicsBackend_SDL_OpenGL::GetViewportSize(int &w, int &h)
{
SDL_GL_GetDrawableSize(m_pWindow, &w, &h);
}
void CGraphicsBackend_SDL_OpenGL::NotifyWindow()
{
#if SDL_MAJOR_VERSION > 2 || (SDL_MAJOR_VERSION == 2 && SDL_PATCHLEVEL >= 16)
if(SDL_FlashWindow(m_pWindow, SDL_FlashOperation::SDL_FLASH_UNTIL_FOCUSED) != 0)
2015-08-25 00:39:48 +00:00
{
// fails if SDL hasn't implemented it
2015-08-25 00:39:48 +00:00
return;
}
#endif
}
2022-01-20 09:49:31 +00:00
void CGraphicsBackend_SDL_OpenGL::WindowDestroyNtf(uint32_t WindowID)
{
}
void CGraphicsBackend_SDL_OpenGL::WindowCreateNtf(uint32_t WindowID)
{
m_pWindow = SDL_GetWindowFromID(WindowID);
}
IGraphicsBackend *CreateGraphicsBackend() { return new CGraphicsBackend_SDL_OpenGL; }