Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Added test program for SDL_CreateWindowFrom()
Browse files Browse the repository at this point in the history
Make sure OpenGL library is loaded before working with OpenGL windows,
even those created with SDL_CreateWindowFrom()
  • Loading branch information
slouken committed Feb 9, 2009
1 parent fdb12d7 commit 51b9053
Show file tree
Hide file tree
Showing 19 changed files with 513 additions and 287 deletions.
13 changes: 12 additions & 1 deletion include/SDL_video.h
Expand Up @@ -111,7 +111,8 @@ typedef enum
SDL_WINDOW_MAXIMIZED = 0x00000040, /**< maximized */
SDL_WINDOW_INPUT_GRABBED = 0x00000100, /**< window has grabbed input focus */
SDL_WINDOW_INPUT_FOCUS = 0x00000200, /**< window has input focus */
SDL_WINDOW_MOUSE_FOCUS = 0x00000400 /**< window has mouse focus */
SDL_WINDOW_MOUSE_FOCUS = 0x00000400, /**< window has mouse focus */
SDL_WINDOW_FOREIGN = 0x00000800 /**< window not created by SDL */
} SDL_WindowFlags;

/**
Expand Down Expand Up @@ -1363,6 +1364,7 @@ extern DECLSPEC void SDLCALL SDL_DisableScreenSaver(void);
* your program from the dynamic library using SDL_GL_GetProcAddress().
*
* \sa SDL_GL_GetProcAddress()
* \sa SDL_GL_UnloadLibrary()
*/
extern DECLSPEC int SDLCALL SDL_GL_LoadLibrary(const char *path);

Expand All @@ -1373,6 +1375,15 @@ extern DECLSPEC int SDLCALL SDL_GL_LoadLibrary(const char *path);
*/
extern DECLSPEC void *SDLCALL SDL_GL_GetProcAddress(const char *proc);

/**
* \fn void SDL_GL_UnloadLibrary(void)
*
* \brief Unload the OpenGL library previously loaded by SDL_GL_LoadLibrary()
*
* \sa SDL_GL_LoadLibrary()
*/
extern DECLSPEC void SDLCALL SDL_GL_UnloadLibrary(void);

/**
* \fn SDL_bool SDL_GL_ExtensionSupported(const char *extension)
*
Expand Down
1 change: 1 addition & 0 deletions src/video/SDL_sysvideo.h
Expand Up @@ -260,6 +260,7 @@ struct SDL_VideoDevice
*/
int (*GL_LoadLibrary) (_THIS, const char *path);
void *(*GL_GetProcAddress) (_THIS, const char *proc);
void (*GL_UnloadLibrary) (_THIS);
SDL_GLContext(*GL_CreateContext) (_THIS, SDL_Window * window);
int (*GL_MakeCurrent) (_THIS, SDL_Window * window, SDL_GLContext context);
int (*GL_SetSwapInterval) (_THIS, int interval);
Expand Down
101 changes: 87 additions & 14 deletions src/video/SDL_video.c
Expand Up @@ -762,9 +762,12 @@ SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
SDL_UninitializedVideo();
return 0;
}
if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
SDL_SetError("No OpenGL support in video driver");
return 0;
if (flags & SDL_WINDOW_OPENGL) {
if (!_this->GL_CreateContext) {
SDL_SetError("No OpenGL support in video driver");
return 0;
}
SDL_GL_LoadLibrary(NULL);
}
SDL_zero(window);
window.id = _this->next_object_id++;
Expand All @@ -776,6 +779,9 @@ SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
window.display = _this->current_display;

if (_this->CreateWindow && _this->CreateWindow(_this, &window) < 0) {
if (flags & SDL_WINDOW_OPENGL) {
SDL_GL_UnloadLibrary();
}
return 0;
}
display = &SDL_CurrentDisplay;
Expand All @@ -786,6 +792,9 @@ SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
if (_this->DestroyWindow) {
_this->DestroyWindow(_this, &window);
}
if (flags & SDL_WINDOW_OPENGL) {
SDL_GL_UnloadLibrary();
}
return 0;
}
windows[num_windows] = window;
Expand Down Expand Up @@ -824,6 +833,7 @@ SDL_CreateWindowFrom(const void *data)
SDL_zero(window);
window.id = _this->next_object_id++;
window.display = _this->current_display;
window.flags = SDL_WINDOW_FOREIGN;

if (!_this->CreateWindowFrom ||
_this->CreateWindowFrom(_this, &window, data) < 0) {
Expand Down Expand Up @@ -852,24 +862,48 @@ SDL_CreateWindowFrom(const void *data)
int
SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
{
const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
SDL_WINDOW_OPENGL |
SDL_WINDOW_BORDERLESS |
SDL_WINDOW_RESIZABLE |
SDL_WINDOW_INPUT_GRABBED);
char *title = window->title;

if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
SDL_SetError("No OpenGL support in video driver");
return -1;
}
if (_this->DestroyWindow) {
if ((window->flags & SDL_WINDOW_OPENGL) != (flags & SDL_WINDOW_OPENGL)) {
if (flags & SDL_WINDOW_OPENGL) {
SDL_GL_LoadLibrary(NULL);
} else {
SDL_GL_UnloadLibrary();
}
}

if (window->flags & SDL_WINDOW_FOREIGN) {
/* Can't destroy and re-create foreign windows, hrm */
flags |= SDL_WINDOW_FOREIGN;
} else {
flags &= ~SDL_WINDOW_FOREIGN;
}

if (_this->DestroyWindow && !(flags & SDL_WINDOW_FOREIGN)) {
_this->DestroyWindow(_this, window);
}

window->title = NULL;
window->flags =
(flags &
~(SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED | SDL_WINDOW_SHOWN |
SDL_WINDOW_INPUT_GRABBED));
window->flags = (flags & allowed_flags);

if (_this->CreateWindow && _this->CreateWindow(_this, window) < 0) {
return -1;
if (_this->CreateWindow && !(flags & SDL_WINDOW_FOREIGN)) {
if (_this->CreateWindow(_this, window) < 0) {
if (flags & SDL_WINDOW_OPENGL) {
SDL_GL_UnloadLibrary();
}
return -1;
}
}

if (title) {
SDL_SetWindowTitle(window->id, title);
SDL_free(title);
Expand Down Expand Up @@ -1352,6 +1386,9 @@ SDL_DestroyWindow(SDL_WindowID windowID)
if (_this->DestroyWindow) {
_this->DestroyWindow(_this, window);
}
if (window->flags & SDL_WINDOW_OPENGL) {
SDL_GL_UnloadLibrary();
}
if (j != display->num_windows - 1) {
SDL_memcpy(&display->windows[i],
&display->windows[i + 1],
Expand Down Expand Up @@ -1543,6 +1580,7 @@ SDL_TextureID
SDL_CreateTextureFromSurface(Uint32 format, SDL_Surface * surface)
{
SDL_TextureID textureID;
Uint32 requested_format = format;
SDL_PixelFormat *fmt;
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
Expand Down Expand Up @@ -1586,6 +1624,14 @@ SDL_CreateTextureFromSurface(Uint32 format, SDL_Surface * surface)
textureID =
SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
surface->h);
if (!textureID && !requested_format) {
SDL_DisplayMode desktop_mode;
SDL_GetDesktopDisplayMode(&desktop_mode);
format = desktop_mode.format;
textureID =
SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
surface->h);
}
if (!textureID) {
return 0;
}
Expand Down Expand Up @@ -2460,11 +2506,21 @@ SDL_GL_LoadLibrary(const char *path)
SDL_UninitializedVideo();
return -1;
}
if (_this->GL_LoadLibrary) {
retval = _this->GL_LoadLibrary(_this, path);
if (_this->gl_config.driver_loaded) {
if (path && SDL_strcmp(path, _this->gl_config.driver_path) != 0) {
SDL_SetError("OpenGL library already loaded");
return -1;
}
retval = 0;
} else {
SDL_SetError("No dynamic GL support in video driver");
retval = -1;
if (!_this->GL_LoadLibrary) {
SDL_SetError("No dynamic GL support in video driver");
return -1;
}
retval = _this->GL_LoadLibrary(_this, path);
}
if (retval == 0) {
++_this->gl_config.driver_loaded;
}
return (retval);
}
Expand All @@ -2491,6 +2547,23 @@ SDL_GL_GetProcAddress(const char *proc)
return func;
}

void
SDL_GL_UnloadLibrary(void)
{
if (!_this) {
SDL_UninitializedVideo();
return;
}
if (_this->gl_config.driver_loaded > 0) {
if (--_this->gl_config.driver_loaded > 0) {
return;
}
if (_this->GL_UnloadLibrary) {
_this->GL_UnloadLibrary(_this);
}
}
}

SDL_bool
SDL_GL_ExtensionSupported(const char *extension)
{
Expand Down
3 changes: 1 addition & 2 deletions src/video/cocoa/SDL_cocoaopengl.h
Expand Up @@ -34,8 +34,7 @@ struct SDL_GLDriverData
/* OpenGL functions */
extern int Cocoa_GL_LoadLibrary(_THIS, const char *path);
extern void *Cocoa_GL_GetProcAddress(_THIS, const char *proc);
extern int Cocoa_GL_SetupWindow(_THIS, SDL_Window * window);
extern void Cocoa_GL_CleanupWindow(_THIS, SDL_Window * window);
extern void Cocoa_GL_UnloadLibrary(_THIS);
extern SDL_GLContext Cocoa_GL_CreateContext(_THIS, SDL_Window * window);
extern int Cocoa_GL_MakeCurrent(_THIS, SDL_Window * window,
SDL_GLContext context);
Expand Down
74 changes: 4 additions & 70 deletions src/video/cocoa/SDL_cocoaopengl.m
Expand Up @@ -48,15 +48,7 @@ - (CGLContextObj)CGLContextObj;
int
Cocoa_GL_LoadLibrary(_THIS, const char *path)
{
if (_this->gl_config.driver_loaded) {
if (path) {
SDL_SetError("OpenGL library already loaded");
return -1;
} else {
++_this->gl_config.driver_loaded;
return 0;
}
}
/* Load the OpenGL library */
if (path == NULL) {
path = SDL_getenv("SDL_OPENGL_LIBRARY");
}
Expand All @@ -69,7 +61,6 @@ - (CGLContextObj)CGLContextObj;
}
SDL_strlcpy(_this->gl_config.driver_path, path,
SDL_arraysize(_this->gl_config.driver_path));
_this->gl_config.driver_loaded = 1;
return 0;
}

Expand All @@ -79,68 +70,11 @@ - (CGLContextObj)CGLContextObj;
return SDL_LoadFunction(_this->gl_config.dll_handle, proc);
}

static void
Cocoa_GL_UnloadLibrary(_THIS)
{
if (_this->gl_config.driver_loaded > 0) {
if (--_this->gl_config.driver_loaded > 0) {
return;
}
SDL_UnloadObject(_this->gl_config.dll_handle);
_this->gl_config.dll_handle = NULL;
}
}

static int
Cocoa_GL_Initialize(_THIS)
{
if (_this->gl_data) {
++_this->gl_data->initialized;
return 0;
}

_this->gl_data =
(struct SDL_GLDriverData *) SDL_calloc(1,
sizeof(struct
SDL_GLDriverData));
if (!_this->gl_data) {
SDL_OutOfMemory();
return -1;
}
_this->gl_data->initialized = 1;

if (Cocoa_GL_LoadLibrary(_this, NULL) < 0) {
return -1;
}
return 0;
}

static void
Cocoa_GL_Shutdown(_THIS)
{
if (!_this->gl_data || (--_this->gl_data->initialized > 0)) {
return;
}

Cocoa_GL_UnloadLibrary(_this);

SDL_free(_this->gl_data);
_this->gl_data = NULL;
}

int
Cocoa_GL_SetupWindow(_THIS, SDL_Window * window)
{
if (Cocoa_GL_Initialize(_this) < 0) {
return -1;
}
return 0;
}

void
Cocoa_GL_CleanupWindow(_THIS, SDL_Window * window)
Cocoa_GL_UnloadLibrary(_THIS)
{
Cocoa_GL_Shutdown(_this);
SDL_UnloadObject(_this->gl_config.dll_handle);
_this->gl_config.dll_handle = NULL;
}

SDL_GLContext
Expand Down
1 change: 1 addition & 0 deletions src/video/cocoa/SDL_cocoavideo.m
Expand Up @@ -93,6 +93,7 @@
#ifdef SDL_VIDEO_OPENGL_CGL
device->GL_LoadLibrary = Cocoa_GL_LoadLibrary;
device->GL_GetProcAddress = Cocoa_GL_GetProcAddress;
device->GL_UnloadLibrary = Cocoa_GL_UnloadLibrary;
device->GL_CreateContext = Cocoa_GL_CreateContext;
device->GL_MakeCurrent = Cocoa_GL_MakeCurrent;
device->GL_SetSwapInterval = Cocoa_GL_SetSwapInterval;
Expand Down
13 changes: 0 additions & 13 deletions src/video/cocoa/SDL_cocoawindow.m
Expand Up @@ -422,14 +422,6 @@ - (BOOL)canBecomeMainWindow
[nswindow release];
return -1;
}
#ifdef SDL_VIDEO_OPENGL_CGL
if (window->flags & SDL_WINDOW_OPENGL) {
if (Cocoa_GL_SetupWindow(_this, window) < 0) {
Cocoa_DestroyWindow(_this, window);
return -1;
}
}
#endif
return 0;
}

Expand Down Expand Up @@ -586,11 +578,6 @@ - (BOOL)canBecomeMainWindow
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;

if (data) {
#ifdef SDL_VIDEO_OPENGL_CGL
if (window->flags & SDL_WINDOW_OPENGL) {
Cocoa_GL_CleanupWindow(_this, window);
}
#endif
[data->listener close];
[data->listener release];
if (data->created) {
Expand Down

0 comments on commit 51b9053

Please sign in to comment.