From 1e63c65c73a527c2ac31638142856d5c0315d6cf Mon Sep 17 00:00:00 2001 From: Inori Date: Thu, 17 Oct 2019 08:44:21 -0400 Subject: [PATCH] Setup GL context in main thread (macOS 10.15 requires it) --- src/eventthread.h | 8 +++- src/main.cpp | 92 ++++++++++++++++++++++----------------------- src/sharedstate.cpp | 3 +- 3 files changed, 54 insertions(+), 49 deletions(-) diff --git a/src/eventthread.h b/src/eventthread.h index 0d9db1a4..c1859674 100644 --- a/src/eventthread.h +++ b/src/eventthread.h @@ -260,6 +260,8 @@ struct RGSSThreadData SDL_Window *window; ALCdevice *alcDev; + + SDL_GLContext glContext; Vec2 sizeResoRatio; Vec2i screenOffset; @@ -274,14 +276,16 @@ struct RGSSThreadData SDL_Window *window, ALCdevice *alcDev, int refreshRate, - const Config& newconf) + const Config& newconf, + SDL_GLContext ctx) : ethread(ethread), argv0(argv0), window(window), alcDev(alcDev), sizeResoRatio(1, 1), refreshRate(refreshRate), - config(newconf) + config(newconf), + glContext(ctx) {} }; diff --git a/src/main.cpp b/src/main.cpp index 9050141a..5c15b4f5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -75,47 +75,6 @@ int rgssThreadFun(void *userdata) RGSSThreadData *threadData = static_cast(userdata); const Config &conf = threadData->config; SDL_Window *win = threadData->window; - SDL_GLContext glCtx; - - /* Setup GL context */ - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - - if (conf.debugMode) - SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); - - glCtx = SDL_GL_CreateContext(win); - - if (!glCtx) - { - rgssThreadError(threadData, std::string("Error creating context: ") + SDL_GetError()); - return 0; - } - - try - { - initGLFunctions(); - } - catch (const Exception &exc) - { - rgssThreadError(threadData, exc.msg); - SDL_GL_DeleteContext(glCtx); - - return 0; - } - - if (!conf.enableBlitting) - gl.BlitFramebuffer = 0; - - gl.ClearColor(0, 0, 0, 1); - gl.Clear(GL_COLOR_BUFFER_BIT); - SDL_GL_SwapWindow(win); - - printGLInfo(); - - bool vsync = conf.vsync || conf.syncToRefreshrate; - SDL_GL_SetSwapInterval(vsync ? 1 : 0); - - GLDebugLogger dLogger; /* Setup AL context */ ALCcontext *alcCtx = alcCreateContext(threadData->alcDev, 0); @@ -123,8 +82,6 @@ int rgssThreadFun(void *userdata) if (!alcCtx) { rgssThreadError(threadData, "Error creating OpenAL context"); - SDL_GL_DeleteContext(glCtx); - return 0; } @@ -138,7 +95,6 @@ int rgssThreadFun(void *userdata) { rgssThreadError(threadData, exc.msg); alcDestroyContext(alcCtx); - SDL_GL_DeleteContext(glCtx); return 0; } @@ -152,7 +108,6 @@ int rgssThreadFun(void *userdata) SharedState::finiInstance(); alcDestroyContext(alcCtx); - SDL_GL_DeleteContext(glCtx); return 0; } @@ -358,8 +313,52 @@ int main(int argc, char *argv[]) conf.syncToRefreshrate = false; EventThread eventThread; + + SDL_GLContext glCtx{}; + + /* Setup GL context. Must be done in main thread since macOS 10.15 */ + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + if (conf.debugMode) + SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); + + glCtx = SDL_GL_CreateContext(win); + + if (!glCtx) + { + showInitError(std::string("Error creating context: ") + SDL_GetError()); + return 0; + } + + try + { + initGLFunctions(); + } + catch (const Exception &exc) + { + showInitError(exc.msg); + SDL_GL_DeleteContext(glCtx); + + return 0; + } + + if (!conf.enableBlitting) + gl.BlitFramebuffer = 0; + + gl.ClearColor(0, 0, 0, 1); + gl.Clear(GL_COLOR_BUFFER_BIT); + SDL_GL_SwapWindow(win); + + printGLInfo(); + + bool vsync = conf.vsync || conf.syncToRefreshrate; + SDL_GL_SetSwapInterval(vsync ? 1 : 0); + + GLDebugLogger dLogger; + + RGSSThreadData rtData(&eventThread, argv[0], win, - alcDev, mode.refresh_rate, conf); + alcDev, mode.refresh_rate, conf, glCtx); int winW, winH; SDL_GetWindowSize(win, &winW, &winH); @@ -412,6 +411,7 @@ int main(int argc, char *argv[]) Debug() << "Shutting down."; + SDL_GL_DeleteContext(glCtx); alcCloseDevice(alcDev); SDL_DestroyWindow(win); diff --git a/src/sharedstate.cpp b/src/sharedstate.cpp index b2d62729..e528fb8d 100644 --- a/src/sharedstate.cpp +++ b/src/sharedstate.cpp @@ -184,7 +184,8 @@ void SharedState::initInstance(RGSSThreadData *threadData) * Font depends on SharedState existing */ rgssVersion = threadData->config.rgssVersion; - + SDL_GL_MakeCurrent(threadData->window, threadData->glContext); + _globalIBO = new GlobalIBO(); _globalIBO->ensureSize(1);