ddnet/src/engine/client/backend_sdl.cpp

1091 lines
26 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
#else
#define GL_GLEXT_PROTOTYPES 1
#include "SDL_opengles2.h"
#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"
#if defined(SDL_VIDEO_DRIVER_X11)
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#endif
#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"
#include "graphics_threaded.h"
#include "opengl_sl.h"
#include "opengl_sl_program.h"
#include <engine/shared/image_manipulation.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
/*
sync_barrier - creates a full hardware fence
*/
#if defined(__GNUC__)
inline void sync_barrier()
{
__sync_synchronize();
}
#elif defined(_MSC_VER)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
inline void sync_barrier()
{
MemoryBarrier();
}
#else
#error missing atomic implementation for this compiler
#endif
// ------------ CGraphicsBackend_Threaded
void CGraphicsBackend_Threaded::ThreadFunc(void *pUser)
{
CGraphicsBackend_Threaded *pThis = (CGraphicsBackend_Threaded *)pUser;
while(!pThis->m_Shutdown)
{
pThis->m_Activity.Wait();
if(pThis->m_pBuffer)
{
#ifdef CONF_PLATFORM_MACOS
CAutoreleasePool AutoreleasePool;
#endif
pThis->m_pProcessor->RunBuffer(pThis->m_pBuffer);
sync_barrier();
pThis->m_pBuffer = 0x0;
pThis->m_BufferDone.Signal();
}
#if defined(CONF_VIDEORECORDER)
if(IVideo::Current())
IVideo::Current()->NextVideoFrameThread();
#endif
}
}
CGraphicsBackend_Threaded::CGraphicsBackend_Threaded()
{
m_pBuffer = 0x0;
m_pProcessor = 0x0;
m_pThread = 0x0;
}
void CGraphicsBackend_Threaded::StartProcessor(ICommandProcessor *pProcessor)
{
m_Shutdown = false;
m_pProcessor = pProcessor;
m_pThread = thread_init(ThreadFunc, this, "CGraphicsBackend_Threaded");
m_BufferDone.Signal();
}
void CGraphicsBackend_Threaded::StopProcessor()
{
m_Shutdown = true;
m_Activity.Signal();
if(m_pThread)
thread_wait(m_pThread);
}
void CGraphicsBackend_Threaded::RunBuffer(CCommandBuffer *pBuffer)
{
WaitForIdle();
m_pBuffer = pBuffer;
m_Activity.Signal();
}
bool CGraphicsBackend_Threaded::IsIdle() const
{
return m_pBuffer == 0x0;
}
void CGraphicsBackend_Threaded::WaitForIdle()
{
while(m_pBuffer != 0x0)
m_BufferDone.Wait();
}
// ------------ 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_Update_Viewport(const SCommand_Update_Viewport *pCommand)
{
glViewport(pCommand->m_X, pCommand->m_Y, pCommand->m_Width, pCommand->m_Height);
}
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);
2012-01-06 13:12:49 +00:00
if(pCommand->m_Finish)
glFinish();
}
void CCommandProcessorFragment_SDL::Cmd_VSync(const CCommandBuffer::SCommand_VSync *pCommand)
{
*pCommand->m_pRetOk = SDL_GL_SetSwapInterval(pCommand->m_VSync) == 0;
}
void CCommandProcessorFragment_SDL::Cmd_Resize(const CCommandBuffer::SCommand_Resize *pCommand)
{
glViewport(0, 0, pCommand->m_Width, pCommand->m_Height);
}
void CCommandProcessorFragment_SDL::Cmd_VideoModes(const CCommandBuffer::SCommand_VideoModes *pCommand)
{
2015-08-24 20:46:28 +00:00
SDL_DisplayMode mode;
int maxModes = SDL_GetNumDisplayModes(pCommand->m_Screen),
numModes = 0;
2015-08-24 20:46:28 +00:00
for(int i = 0; i < maxModes; i++)
{
2015-08-24 20:46:28 +00:00
if(SDL_GetDisplayMode(pCommand->m_Screen, i, &mode) < 0)
{
dbg_msg("gfx", "unable to get display mode: %s", SDL_GetError());
continue;
}
2017-03-21 10:24:44 +00:00
bool AlreadyFound = false;
2015-08-24 20:46:28 +00:00
for(int j = 0; j < numModes; j++)
{
2015-08-24 20:46:28 +00:00
if(pCommand->m_pModes[j].m_Width == mode.w && pCommand->m_pModes[j].m_Height == mode.h)
{
2017-03-21 10:24:44 +00:00
AlreadyFound = true;
break;
2015-08-24 20:46:28 +00:00
}
}
2017-03-21 10:24:44 +00:00
if(AlreadyFound)
2015-08-24 20:46:28 +00:00
continue;
2015-08-24 20:46:28 +00:00
pCommand->m_pModes[numModes].m_Width = mode.w;
pCommand->m_pModes[numModes].m_Height = mode.h;
pCommand->m_pModes[numModes].m_Red = 8;
pCommand->m_pModes[numModes].m_Green = 8;
pCommand->m_pModes[numModes].m_Blue = 8;
numModes++;
}
2015-08-24 20:46:28 +00:00
*pCommand->m_pNumModes = numModes;
}
CCommandProcessorFragment_SDL::CCommandProcessorFragment_SDL()
{
}
bool CCommandProcessorFragment_SDL::RunCommand(const CCommandBuffer::SCommand *pBaseCommand)
{
switch(pBaseCommand->m_Cmd)
{
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 CCommandBuffer::CMD_RESIZE: Cmd_Resize(static_cast<const CCommandBuffer::SCommand_Resize *>(pBaseCommand)); break;
case CCommandBuffer::CMD_VIDEOMODES: Cmd_VideoModes(static_cast<const CCommandBuffer::SCommand_VideoModes *>(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;
case CMD_UPDATE_VIEWPORT: Cmd_Update_Viewport(static_cast<const SCommand_Update_Viewport *>(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)
{
2021-04-30 22:42:37 +00:00
if(OpenGLMajor < 3)
{
m_pOpenGL = new CCommandProcessorFragment_OpenGL();
}
else
{
m_pOpenGL = new CCommandProcessorFragment_OpenGL3_3();
}
}
2021-04-30 22:42:37 +00:00
else if(BackendType == BACKEND_TYPE_OPENGL)
{
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();
}
}
}
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;
if(GLEW_OK != glewInit())
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
if(GLEW_VERSION_4_5)
{
2021-04-30 22:42:37 +00:00
GlewMajor = 4;
GlewMinor = 5;
GlewPatch = 0;
return true;
}
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;
}
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
return BACKEND_TYPE_OPENGL;
#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)
{
// 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;
}
}
}
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;
}
int CGraphicsBackend_SDL_OpenGL::Init(const char *pName, int *Screen, int *pWidth, int *pHeight, 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;
}
}
2021-04-30 22:42:37 +00:00
EBackendType BackendType = DetectBackend();
2021-04-30 22:42:37 +00:00
ClampDriverVersion(BackendType);
2021-04-30 22:42:37 +00:00
m_UseNewOpenGL = IsModernAPI(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);
2021-04-30 22:42:37 +00:00
if(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
}
}
2021-04-30 22:42:37 +00:00
else if(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)
{
if(*Screen < 0 || *Screen >= m_NumScreens)
*Screen = 0;
if(SDL_GetDisplayBounds(*Screen, &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;
if(SDL_GetDesktopDisplayMode(*Screen, &DisplayMode))
{
dbg_msg("gfx", "unable to get desktop resolution: %s", SDL_GetError());
return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_SDL_SCREEN_RESOLUTION_REQUEST_FAILED;
}
*pDesktopWidth = DisplayMode.w;
*pDesktopHeight = DisplayMode.h;
// use desktop resolution as default resolution
if(*pWidth == 0 || *pHeight == 0)
{
*pWidth = *pDesktopWidth;
*pHeight = *pDesktopHeight;
}
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)
2016-04-30 21:10:09 +00:00
{
// when we are at fullscreen, we really shouldn't allow window sizes, that aren't supported by the driver
bool SupportedResolution = false;
SDL_DisplayMode mode;
2017-10-23 16:31:44 +00:00
int maxModes = SDL_GetNumDisplayModes(g_Config.m_GfxScreen);
2018-03-13 20:44:58 +00:00
for(int i = 0; i < maxModes; i++)
{
2018-03-13 20:44:58 +00:00
if(SDL_GetDisplayMode(g_Config.m_GfxScreen, i, &mode) < 0)
{
dbg_msg("gfx", "unable to get display mode: %s", SDL_GetError());
continue;
}
2018-03-13 20:44:58 +00:00
if(*pWidth == mode.w && *pHeight == mode.h)
{
SupportedResolution = true;
break;
}
}
if(SupportedResolution)
SdlFlags |= SDL_WINDOW_FULLSCREEN;
else
SdlFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
2016-04-30 21:10:09 +00:00
}
else if(Flags & (IGraphicsBackend::INITFLAG_DESKTOP_FULLSCREEN))
{
SdlFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
}
// 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,
2020-12-12 11:58:59 +00:00
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
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;
2021-04-30 22:42:37 +00:00
if(!BackendInitGlew(BackendType, GlewMajor, GlewMinor, GlewPatch))
{
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;
2021-04-30 22:42:37 +00:00
InitError = IsVersionSupportedGlew(BackendType, g_Config.m_GfxOpenGLMajor, g_Config.m_GfxOpenGLMinor, g_Config.m_GfxOpenGLPatch, GlewMajor, GlewMinor, GlewPatch);
2018-07-17 07:46:17 +00:00
SDL_GL_GetDrawableSize(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
2021-04-30 22:42:37 +00:00
m_pProcessor = new CCommandProcessor_SDL_OpenGL(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_OpenGL::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_pCapabilities = &m_Capabilites;
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_pInitError = &InitError;
CmdOpenGL.m_pErrStringPtr = &pErrorStr;
CmdOpenGL.m_pVendorString = m_aVendorString;
CmdOpenGL.m_pVersionString = m_aVersionString;
CmdOpenGL.m_pRendererString = m_aRendererString;
CmdOpenGL.m_RequestedBackend = BackendType;
CmdBuffer.AddCommandUnsafe(CmdOpenGL);
2021-04-30 22:42:37 +00:00
RunBuffer(&CmdBuffer);
WaitForIdle();
CmdBuffer.Reset();
if(InitError == -2)
{
2020-09-03 09:43:32 +00:00
CCommandProcessorFragment_OpenGL::SCommand_Shutdown CmdGL;
CmdBuffer.AddCommandUnsafe(CmdGL);
2020-09-03 09:43:32 +00:00
RunBuffer(&CmdBuffer);
WaitForIdle();
CmdBuffer.Reset();
2021-04-30 22:42:37 +00:00
g_Config.m_GfxOpenGLMajor = m_Capabilites.m_ContextMajor;
g_Config.m_GfxOpenGLMinor = m_Capabilites.m_ContextMinor;
g_Config.m_GfxOpenGLPatch = m_Capabilites.m_ContextPatch;
}
}
if(InitError != 0)
{
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;
}
2018-03-13 20:44:58 +00:00
if(SetWindowScreen(g_Config.m_GfxScreen))
{
// query the current displaymode, when running in fullscreen
// this is required if DPI scaling is active
if(SdlFlags & SDL_WINDOW_FULLSCREEN)
{
SDL_DisplayMode CurrentDisplayMode;
SDL_GetCurrentDisplayMode(g_Config.m_GfxScreen, &CurrentDisplayMode);
*pCurrentWidth = CurrentDisplayMode.w;
*pCurrentHeight = CurrentDisplayMode.h;
// since the window is centered, calculate how much the viewport has to be fixed
//int XOverflow = (*pWidth > *pCurrentWidth ? (*pWidth - *pCurrentWidth) : 0);
//int YOverflow = (*pHeight > *pCurrentHeight ? (*pHeight - *pCurrentHeight) : 0);
//TODO: current problem is, that the opengl driver knows about the scaled display,
//so the viewport cannot be adjusted for resolutions, that are higher than allowed by the display driver
2018-04-13 19:34:12 +00:00
CCommandProcessorFragment_SDL::SCommand_Update_Viewport CmdSDL;
CmdSDL.m_X = 0;
CmdSDL.m_Y = 0;
CmdSDL.m_Width = CurrentDisplayMode.w;
CmdSDL.m_Height = CurrentDisplayMode.h;
CmdBuffer.AddCommandUnsafe(CmdSDL);
RunBuffer(&CmdBuffer);
2018-04-13 19:34:12 +00:00
WaitForIdle();
CmdBuffer.Reset();
}
}
// return
return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_NONE;
}
int CGraphicsBackend_SDL_OpenGL::Shutdown()
{
// issue a shutdown command
CCommandBuffer CmdBuffer(1024, 512);
2020-09-03 09:43:32 +00:00
CCommandProcessorFragment_OpenGL::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)
{
if(FullscreenMode > 0)
{
if(FullscreenMode == 1)
{
#if defined(CONF_PLATFORM_MACOS) || defined(CONF_PLATFORM_HAIKU) // 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
}
else if(FullscreenMode == 2)
{
SDL_SetWindowFullscreen(m_pWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
}
}
else
{
SDL_SetWindowFullscreen(m_pWindow, 0);
SDL_SetWindowBordered(m_pWindow, SDL_bool(!IsBorderless));
}
}
bool CGraphicsBackend_SDL_OpenGL::SetWindowScreen(int Index)
{
if(Index >= 0 && Index < m_NumScreens)
{
SDL_Rect ScreenPos;
if(SDL_GetDisplayBounds(Index, &ScreenPos) == 0)
{
SDL_SetWindowPosition(m_pWindow,
2016-05-07 14:35:31 +00:00
SDL_WINDOWPOS_CENTERED_DISPLAY(Index),
SDL_WINDOWPOS_CENTERED_DISPLAY(Index));
return true;
}
}
return false;
}
int CGraphicsBackend_SDL_OpenGL::GetWindowScreen()
{
return SDL_GetWindowDisplayIndex(m_pWindow);
}
int CGraphicsBackend_SDL_OpenGL::WindowActive()
{
return SDL_GetWindowFlags(m_pWindow) & SDL_WINDOW_INPUT_FOCUS;
}
int CGraphicsBackend_SDL_OpenGL::WindowOpen()
{
return SDL_GetWindowFlags(m_pWindow) & SDL_WINDOW_SHOWN;
}
void CGraphicsBackend_SDL_OpenGL::SetWindowGrab(bool Grab)
{
SDL_SetWindowGrab(m_pWindow, Grab ? SDL_TRUE : SDL_FALSE);
}
2020-12-12 11:58:59 +00:00
void CGraphicsBackend_SDL_OpenGL::ResizeWindow(int w, int h)
{
SDL_SetWindowSize(m_pWindow, w, h);
}
void CGraphicsBackend_SDL_OpenGL::GetViewportSize(int &w, int &h)
{
SDL_GL_GetDrawableSize(m_pWindow, &w, &h);
}
void CGraphicsBackend_SDL_OpenGL::NotifyWindow()
{
2015-08-25 00:39:48 +00:00
// get window handle
SDL_SysWMinfo info;
SDL_VERSION(&info.version);
if(!SDL_GetWindowWMInfo(m_pWindow, &info))
{
dbg_msg("gfx", "unable to obtain window handle");
return;
}
#if defined(CONF_FAMILY_WINDOWS)
FLASHWINFO desc;
desc.cbSize = sizeof(desc);
desc.hwnd = info.info.win.window;
desc.dwFlags = FLASHW_TRAY;
desc.uCount = 3; // flash 3 times
desc.dwTimeout = 0;
FlashWindowEx(&desc);
#elif defined(SDL_VIDEO_DRIVER_X11) && !defined(CONF_PLATFORM_MACOS)
Display *dpy = info.info.x11.display;
Window win = info.info.x11.window;
// Old hints
XWMHints *wmhints;
wmhints = XAllocWMHints();
wmhints->flags = XUrgencyHint;
XSetWMHints(dpy, win, wmhints);
XFree(wmhints);
// More modern way of notifying
static Atom demandsAttention = XInternAtom(dpy, "_NET_WM_STATE_DEMANDS_ATTENTION", true);
static Atom wmState = XInternAtom(dpy, "_NET_WM_STATE", true);
XChangeProperty(dpy, win, wmState, XA_ATOM, 32, PropModeReplace,
(unsigned char *)&demandsAttention, 1);
#endif
}
IGraphicsBackend *CreateGraphicsBackend() { return new CGraphicsBackend_SDL_OpenGL; }