Add SDL2 support in sys/glimp.cpp

This commit is contained in:
dhewg 2012-07-19 22:10:07 +02:00
parent 4f2f855105
commit 454bebf99f

View file

@ -38,6 +38,15 @@ idCVar in_nograb("in_nograb", "0", CVAR_SYSTEM | CVAR_NOCHEAT, "prevents input g
static bool grabbed = false;
#if SDL_VERSION_ATLEAST(2, 0, 0)
static SDL_Window *window = NULL;
static SDL_GLContext context = NULL;
#else
static SDL_Surface *window = NULL;
#define SDL_WINDOW_OPENGL SDL_OPENGL
#define SDL_WINDOW_FULLSCREEN SDL_FULLSCREEN
#endif
/*
===================
GLimp_Init
@ -48,12 +57,10 @@ bool GLimp_Init(glimpParms_t parms) {
assert(SDL_WasInit(SDL_INIT_VIDEO));
Uint32 flags = SDL_OPENGL;
Uint32 flags = SDL_WINDOW_OPENGL;
if (parms.fullScreen)
flags |= SDL_FULLSCREEN;
SDL_Surface *surf = NULL;
flags |= SDL_WINDOW_FULLSCREEN;
int colorbits = 24;
int depthbits = 24;
@ -116,8 +123,6 @@ bool GLimp_Init(glimpParms_t parms) {
if (tcolorbits == 24)
channelcolorbits = 8;
SDL_WM_SetCaption(GAME_NAME, GAME_NAME);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, channelcolorbits);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, channelcolorbits);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, channelcolorbits);
@ -131,16 +136,44 @@ bool GLimp_Init(glimpParms_t parms) {
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, parms.multiSamples ? 1 : 0);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, parms.multiSamples);
if (SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, r_swapInterval.GetInteger()) < 0)
common->Warning("SDL_GL_SWAP_CONTROL not supported");
#if SDL_VERSION_ATLEAST(2, 0, 0)
window = SDL_CreateWindow(GAME_NAME,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
parms.width, parms.height, flags);
context = SDL_GL_CreateContext(window);
surf = SDL_SetVideoMode(parms.width, parms.height, colorbits, flags);
if (!surf) {
if (!window) {
common->DPrintf("Couldn't set GL mode %d/%d/%d: %s",
channelcolorbits, tdepthbits, tstencilbits, SDL_GetError());
continue;
}
if (SDL_GL_SetSwapInterval(r_swapInterval.GetInteger()) < 0)
common->Warning("SDL_GL_SWAP_CONTROL not supported");
SDL_GetWindowSize(window, &glConfig.vidWidth, &glConfig.vidHeight);
glConfig.isFullscreen = (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) == SDL_WINDOW_FULLSCREEN;
#else
SDL_WM_SetCaption(GAME_NAME, GAME_NAME);
if (SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, r_swapInterval.GetInteger()) < 0)
common->Warning("SDL_GL_SWAP_CONTROL not supported");
window = SDL_SetVideoMode(parms.width, parms.height, colorbits, flags);
if (!window) {
common->DPrintf("Couldn't set GL mode %d/%d/%d: %s",
channelcolorbits, tdepthbits, tstencilbits, SDL_GetError());
continue;
}
glConfig.vidWidth = window->w;
glConfig.vidHeight = window->h;
glConfig.isFullscreen = (window->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN;
#endif
common->Printf("Using %d color bits, %d depth, %d stencil display\n",
channelcolorbits, tdepthbits, tstencilbits);
@ -148,16 +181,12 @@ bool GLimp_Init(glimpParms_t parms) {
glConfig.depthBits = tdepthbits;
glConfig.stencilBits = tstencilbits;
glConfig.vidWidth = surf->w;
glConfig.vidHeight = surf->h;
glConfig.isFullscreen = (surf->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN;
glConfig.displayFrequency = 0;
break;
}
if (!surf) {
if (!window) {
common->Warning("No usable GL mode found: %s", SDL_GetError());
return false;
}
@ -182,6 +211,18 @@ GLimp_Shutdown
*/
void GLimp_Shutdown() {
common->Printf("Shutting down OpenGL subsystem\n");
#if SDL_VERSION_ATLEAST(2, 0, 0)
if (context) {
SDL_GL_DeleteContext(context);
context = NULL;
}
if (window) {
SDL_DestroyWindow(window);
window = NULL;
}
#endif
}
/*
@ -190,7 +231,11 @@ GLimp_SwapBuffers
===================
*/
void GLimp_SwapBuffers() {
#if SDL_VERSION_ATLEAST(2, 0, 0)
SDL_GL_SwapWindow(window);
#else
SDL_GL_SwapBuffers();
#endif
}
/*
@ -199,7 +244,16 @@ GLimp_SetGamma
=================
*/
void GLimp_SetGamma(unsigned short red[256], unsigned short green[256], unsigned short blue[256]) {
if (!window) {
common->Warning("GLimp_SetGamma called without window");
return;
}
#if SDL_VERSION_ATLEAST(2, 0, 0)
if (SDL_SetWindowGammaRamp(window, red, green, blue))
#else
if (SDL_SetGammaRamp(red, green, blue))
#endif
common->Warning("Couldn't set gamma ramp: %s", SDL_GetError());
}
@ -244,17 +298,22 @@ void GLimp_GrabInput(int flags) {
if (flags & GRAB_SETSTATE)
grabbed = grab;
if (flags & GRAB_HIDECURSOR)
SDL_ShowCursor(SDL_DISABLE);
else
SDL_ShowCursor(SDL_ENABLE);
if (in_nograb.GetBool())
grab = false;
if (grab)
SDL_WM_GrabInput(SDL_GRAB_ON);
else
SDL_WM_GrabInput(SDL_GRAB_OFF);
if (!window) {
common->Warning("GLimp_GrabInput called without window");
return;
}
#if SDL_VERSION_ATLEAST(2, 0, 0)
SDL_ShowCursor(flags & GRAB_HIDECURSOR ? SDL_DISABLE : SDL_ENABLE);
if (SDL_SetRelativeMouseMode(flags & GRAB_HIDECURSOR ? SDL_TRUE : SDL_FALSE))
common->Warning("Failed to set relative mouse movement mode");
SDL_SetWindowGrab(window, grab ? SDL_TRUE : SDL_FALSE);
#else
SDL_ShowCursor(flags & GRAB_HIDECURSOR ? SDL_DISABLE : SDL_ENABLE);
SDL_WM_GrabInput(grab ? SDL_GRAB_ON : SDL_GRAB_OFF);
#endif
#endif
}