Skip to content

Commit

Permalink
Restore the OpenGL context whenever we use a SDL2 renderer in legacy …
Browse files Browse the repository at this point in the history
…display mode.
  • Loading branch information
ronsaldo committed Mar 28, 2017
1 parent 4828318 commit 008052c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
61 changes: 60 additions & 1 deletion platforms/minheadless/sdl2-window/sqWindow-SDL2.c
Expand Up @@ -74,6 +74,34 @@ static int mousePositionY = 0;
static sqInt sdl2InputEventSemaIndex = 0;
static char droppedFileName[FILENAME_MAX];

static SDL_GLContext currentOpenGLContext = 0;
static SDL_Window *currentOpenGLWindow = 0;
static int openglStoreCount = 0;

static void storeOpenGLState(void)
{
if(openglStoreCount == 0)
{
currentOpenGLContext = SDL_GL_GetCurrentContext();
currentOpenGLWindow = SDL_GL_GetCurrentWindow();
}
++openglStoreCount;
}

static void restoreOpenGLState(void)
{
--openglStoreCount;
if(openglStoreCount == 0)
{
SDL_GL_MakeCurrent(currentOpenGLWindow, currentOpenGLContext);
currentOpenGLContext = 0;
currentOpenGLWindow = 0;
}

if(openglStoreCount < 0)
abort();
}

static sqInt setSDL2InputSemaphoreIndex(sqInt semaIndex)
{
if (semaIndex == 0)
Expand All @@ -91,14 +119,24 @@ static void sdl2SignalInputEvent(void)

static int convertButton(int button)
{
#ifdef __APPLE__
// On OS X, swap the middle and right buttons.
switch(button)
{
case SDL_BUTTON_LEFT: return RedButtonBit;
case SDL_BUTTON_RIGHT: return BlueButtonBit;
case SDL_BUTTON_MIDDLE: return YellowButtonBit;
default: return 0;
}
#else
switch(button)
{
case SDL_BUTTON_LEFT: return RedButtonBit;
case SDL_BUTTON_MIDDLE: return BlueButtonBit;
case SDL_BUTTON_RIGHT: return YellowButtonBit;
default: return 0;
}

#endif
return 0;
}

Expand Down Expand Up @@ -169,14 +207,18 @@ static void createWindow(sqInt width, sqInt height, sqInt fullscreenFlag)
if(window)
return;

storeOpenGLState();
flags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
if(fullscreenFlag)
flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;

modifiersState = convertModifiers(SDL_GetModState());
window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags);
if(!window)
{
restoreOpenGLState();
return;
}

if(!fullscreenFlag)
{
Expand All @@ -193,19 +235,22 @@ static void createWindow(sqInt width, sqInt height, sqInt fullscreenFlag)

windowID = SDL_GetWindowID(window);
windowRenderer = SDL_CreateRenderer(window, 0, 0);
restoreOpenGLState();
}

static int ensureTextureOfSize(sqInt width, sqInt height)
{
if(windowTexture && windowTextureWidth == width && windowTextureHeight == height)
return 0;

storeOpenGLState();
if(windowTexture)
SDL_DestroyTexture(windowTexture);

windowTexture = SDL_CreateTexture(windowRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, width, height);
windowTextureWidth = width;
windowTextureHeight = height;
restoreOpenGLState();
return 1;
}

Expand All @@ -214,13 +259,15 @@ static void presentWindow()
if(!window || !windowRenderer)
return;

storeOpenGLState();
SDL_SetRenderDrawColor(windowRenderer, 0, 0, 0, 0);
SDL_RenderClear(windowRenderer);

if(windowTexture)
SDL_RenderCopy(windowRenderer, windowTexture, NULL, NULL);

SDL_RenderPresent(windowRenderer);
restoreOpenGLState();
}

static void recordSDLEvent(const SDL_Event *rawEvent)
Expand Down Expand Up @@ -686,6 +733,7 @@ static void blitRect32(
static sqInt sqSDL2_showDisplay(sqInt dispBitsIndex, sqInt width, sqInt height, sqInt depth,
sqInt affectedL, sqInt affectedR, sqInt affectedT, sqInt affectedB)
{
storeOpenGLState();
if(!window)
createWindow(width, height, 0);

Expand All @@ -706,12 +754,18 @@ static sqInt sqSDL2_showDisplay(sqInt dispBitsIndex, sqInt width, sqInt height,
}

if(!windowTexture)
{
restoreOpenGLState();
return 0;
}

uint8_t *pixels;
int pitch;
if(SDL_LockTexture(windowTexture, NULL, (void**)&pixels, &pitch))
{
restoreOpenGLState();
return 0;
}

int sourcePitch = windowTextureWidth*4;
blitRect32(windowTextureWidth, windowTextureHeight,
Expand All @@ -722,6 +776,7 @@ static sqInt sqSDL2_showDisplay(sqInt dispBitsIndex, sqInt width, sqInt height,

SDL_UnlockTexture(windowTexture);
presentWindow();
restoreOpenGLState();
return 0;
}

Expand All @@ -734,12 +789,16 @@ static sqInt sqSDL2_setDisplayMode(sqInt width, sqInt height, sqInt depth, sqInt
{
if(window)
{
storeOpenGLState();
ioSetWindowWidthHeight(width, height);
ioSetFullScreen(fullscreenFlag);
restoreOpenGLState();
return 0;
}

storeOpenGLState();
createWindow(width, height, fullscreenFlag);
restoreOpenGLState();
return 0;
}

Expand Down
13 changes: 8 additions & 5 deletions platforms/minheadless/unix/sqPlatformSpecific-Unix.c
Expand Up @@ -432,6 +432,9 @@ reportStackState(const char *msg, char *date, int printAll, ucontext_t *uap)
void *fp = (void *)(uap ? uap->uc_mcontext->ss.ebp: 0);
void *sp = (void *)(uap ? uap->uc_mcontext->ss.esp: 0);
# endif
# elif __APPLE__ && __MACH__ && __x86_64__
void *fp = (void *)(uap ? uap->uc_mcontext->__ss.__rbp: 0);
void *sp = (void *)(uap ? uap->uc_mcontext->__ss.__rsp: 0);
# elif __linux__ && __i386__
void *fp = (void *)(uap ? uap->uc_mcontext.gregs[REG_EBP]: 0);
void *sp = (void *)(uap ? uap->uc_mcontext.gregs[REG_ESP]: 0);
Expand Down Expand Up @@ -521,11 +524,11 @@ printRegisterState(ucontext_t *uap)
return (void *)(regs->eip);
#elif __APPLE__ && __x86_64__
_STRUCT_X86_THREAD_STATE64 *regs = &uap->uc_mcontext->__ss;
printf( "\trax 0x%016lx rbx 0x%016lx rcx 0x%016lx rdx 0x%016lx\n"
"\trdi 0x%016lx rsi 0x%016lx rbp 0x%016lx rsp 0x%016lx\n"
"\tr8 0x%016lx r9 0x%016lx r10 0x%016lx r11 0x%016lx\n"
"\tr12 0x%016lx r13 0x%016lx r14 0x%016lx r15 0x%016lx\n"
"\trip 0x%016lx\n",
printf( "\trax 0x%016llx rbx 0x%016llx rcx 0x%016llx rdx 0x%016llx\n"
"\trdi 0x%016llx rsi 0x%016llx rbp 0x%016llx rsp 0x%016llx\n"
"\tr8 0x%016llx r9 0x%016llx r10 0x%016llx r11 0x%016llx\n"
"\tr12 0x%016llx r13 0x%016llx r14 0x%016llx r15 0x%016llx\n"
"\trip 0x%016llx\n",
regs->__rax, regs->__rbx, regs->__rcx, regs->__rdx,
regs->__rdi, regs->__rdi, regs->__rbp, regs->__rsp,
regs->__r8 , regs->__r9 , regs->__r10, regs->__r11,
Expand Down

0 comments on commit 008052c

Please sign in to comment.