From d006ea7183b8555f4cc8241b391b7216bcdb2313 Mon Sep 17 00:00:00 2001 From: Taylor Richards Date: Tue, 24 Mar 2015 00:40:14 -0400 Subject: [PATCH] app window handling cleanup - global window ptr using os_get/set_window() - be sure GL window and context are closed out on cleanup --- include/osapi.h | 5 +++-- src/graphics/gropengl.cpp | 13 ++++++++++++- src/io/mouse.cpp | 8 +++----- src/osapi/osapi.cpp | 20 ++++++++++++-------- 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/include/osapi.h b/include/osapi.h index 7ce67f6..42bcca7 100644 --- a/include/osapi.h +++ b/include/osapi.h @@ -48,8 +48,9 @@ void os_toggle_fullscreen(); int os_foreground(); // Returns the handle to the main window -uint os_get_window(); - +SDL_Window *os_get_window(); +// Sets the handle to the main window +void os_set_window(SDL_Window *win); // process management -------------------------------------------------------------- diff --git a/src/graphics/gropengl.cpp b/src/graphics/gropengl.cpp index 2351e16..cc535f7 100644 --- a/src/graphics/gropengl.cpp +++ b/src/graphics/gropengl.cpp @@ -127,13 +127,22 @@ void gr_opengl_cleanup() opengl1_cleanup(); opengl_free_render_buffer(); + + os_set_window(NULL); + + SDL_GL_DeleteContext(GL_context); + GL_context = NULL; + + SDL_DestroyWindow(GL_window); + GL_window = NULL; + + OGL_inited = false; } void gr_opengl_init() { if ( OGL_inited ) { gr_opengl_cleanup(); - OGL_inited = false; } mprintf(( "Initializing OpenGL graphics device...\n" )); @@ -168,6 +177,8 @@ void gr_opengl_init() Error(LOCATION, "Couldn't create window: %s\n", SDL_GetError()); } + os_set_window(GL_window); + GL_context = SDL_GL_CreateContext(GL_window); const char *gl_version = (const char*)glGetString(GL_VERSION); diff --git a/src/io/mouse.cpp b/src/io/mouse.cpp index ba55916..63cf2f7 100644 --- a/src/io/mouse.cpp +++ b/src/io/mouse.cpp @@ -419,13 +419,11 @@ void mouse_get_delta(int *dx, int *dy, int *dz) *dz = Mouse_dz; } -extern SDL_Window *GL_window; - // Forces the actual windows cursor to be at (x,y). This may be independent of our tracked (x,y) mouse pos. void mouse_force_pos(int x, int y) { if (os_foreground()) { // only mess with windows's mouse if we are in control of it - SDL_WarpMouseInWindow(GL_window, x, y); + SDL_WarpMouseInWindow(os_get_window(), x, y); } } @@ -441,13 +439,13 @@ void mouse_eval_deltas() if (Keep_mouse_centered && Mouse_hidden) { if ( !Mouse_grabbed ) { SDL_SetRelativeMouseMode(SDL_TRUE); - SDL_SetWindowGrab(GL_window, SDL_TRUE); + SDL_SetWindowGrab(os_get_window(), SDL_TRUE); Mouse_grabbed = true; } } else { if (Mouse_grabbed) { SDL_SetRelativeMouseMode(SDL_FALSE); - SDL_SetWindowGrab(GL_window, SDL_FALSE); + SDL_SetWindowGrab(os_get_window(), SDL_FALSE); Mouse_grabbed = false; } } diff --git a/src/osapi/osapi.cpp b/src/osapi/osapi.cpp index 2fff75d..bf59ddc 100644 --- a/src/osapi/osapi.cpp +++ b/src/osapi/osapi.cpp @@ -165,6 +165,7 @@ static int Os_inited = 0; static char windowTitle[128]; static SDL_mutex *Os_lock; +static SDL_Window *Os_window = NULL; int Os_debugger_running = 0; @@ -214,17 +215,17 @@ void os_init(const char *wclass, const char *title, const char *app_name, const // set the main window title void os_set_title( const char *title ) { - extern SDL_Window *GL_window; - if ( !title ) { return; } - memset(windowTitle, 0, sizeof(windowTitle)); + SDL_strlcpy(windowTitle, title, SDL_arraysize(windowTitle)); - SDL_strlcpy(windowTitle, title, sizeof(windowTitle)); + if ( !Os_window ) { + return; + } - SDL_SetWindowTitle(GL_window, title); + SDL_SetWindowTitle(Os_window, title); } const char *os_get_title() @@ -250,12 +251,15 @@ int os_foreground() } // Returns the handle to the main window -uint os_get_window() +SDL_Window *os_get_window() { -// STUB_FUNCTION; // not used/needed with UNIX builds? - return 0; + return Os_window; } +void os_set_window(SDL_Window *win) +{ + Os_window = win; +} // process management ----------------------------------------------------------------- -- 2.39.2