Added test program for SDL_CreateWindowFrom()
authorSam Lantinga <slouken@libsdl.org>
Mon, 09 Feb 2009 05:32:12 +0000
changeset 3057089a77aebb7d
parent 3056 a434fe6360df
child 3058 17c5930f498e
Added test program for SDL_CreateWindowFrom()
Make sure OpenGL library is loaded before working with OpenGL windows,
even those created with SDL_CreateWindowFrom()
include/SDL_video.h
src/video/SDL_sysvideo.h
src/video/SDL_video.c
src/video/cocoa/SDL_cocoaopengl.h
src/video/cocoa/SDL_cocoaopengl.m
src/video/cocoa/SDL_cocoavideo.m
src/video/cocoa/SDL_cocoawindow.m
src/video/win32/SDL_win32opengl.c
src/video/win32/SDL_win32opengl.h
src/video/win32/SDL_win32video.c
src/video/win32/SDL_win32window.c
src/video/x11/SDL_x11opengl.c
src/video/x11/SDL_x11opengl.h
src/video/x11/SDL_x11video.c
src/video/x11/SDL_x11window.c
test/testnative.c
test/testnativecocoa.m
test/testnativew32.c
test/testnativex11.c
     1.1 --- a/include/SDL_video.h	Sun Feb 08 15:35:06 2009 +0000
     1.2 +++ b/include/SDL_video.h	Mon Feb 09 05:32:12 2009 +0000
     1.3 @@ -111,7 +111,8 @@
     1.4      SDL_WINDOW_MAXIMIZED = 0x00000040,          /**< maximized */
     1.5      SDL_WINDOW_INPUT_GRABBED = 0x00000100,      /**< window has grabbed input focus */
     1.6      SDL_WINDOW_INPUT_FOCUS = 0x00000200,        /**< window has input focus */
     1.7 -    SDL_WINDOW_MOUSE_FOCUS = 0x00000400         /**< window has mouse focus */
     1.8 +    SDL_WINDOW_MOUSE_FOCUS = 0x00000400,        /**< window has mouse focus */
     1.9 +    SDL_WINDOW_FOREIGN = 0x00000800             /**< window not created by SDL */
    1.10  } SDL_WindowFlags;
    1.11  
    1.12  /**
    1.13 @@ -1363,6 +1364,7 @@
    1.14   *       your program from the dynamic library using SDL_GL_GetProcAddress().
    1.15   *
    1.16   * \sa SDL_GL_GetProcAddress()
    1.17 + * \sa SDL_GL_UnloadLibrary()
    1.18   */
    1.19  extern DECLSPEC int SDLCALL SDL_GL_LoadLibrary(const char *path);
    1.20  
    1.21 @@ -1374,6 +1376,15 @@
    1.22  extern DECLSPEC void *SDLCALL SDL_GL_GetProcAddress(const char *proc);
    1.23  
    1.24  /**
    1.25 + * \fn void SDL_GL_UnloadLibrary(void)
    1.26 + *
    1.27 + * \brief Unload the OpenGL library previously loaded by SDL_GL_LoadLibrary()
    1.28 + *
    1.29 + * \sa SDL_GL_LoadLibrary()
    1.30 + */
    1.31 +extern DECLSPEC void SDLCALL SDL_GL_UnloadLibrary(void);
    1.32 +
    1.33 +/**
    1.34   * \fn SDL_bool SDL_GL_ExtensionSupported(const char *extension)
    1.35   *
    1.36   * \brief Return true if an OpenGL extension is supported for the current context.
     2.1 --- a/src/video/SDL_sysvideo.h	Sun Feb 08 15:35:06 2009 +0000
     2.2 +++ b/src/video/SDL_sysvideo.h	Mon Feb 09 05:32:12 2009 +0000
     2.3 @@ -260,6 +260,7 @@
     2.4       */
     2.5      int (*GL_LoadLibrary) (_THIS, const char *path);
     2.6      void *(*GL_GetProcAddress) (_THIS, const char *proc);
     2.7 +    void (*GL_UnloadLibrary) (_THIS);
     2.8        SDL_GLContext(*GL_CreateContext) (_THIS, SDL_Window * window);
     2.9      int (*GL_MakeCurrent) (_THIS, SDL_Window * window, SDL_GLContext context);
    2.10      int (*GL_SetSwapInterval) (_THIS, int interval);
     3.1 --- a/src/video/SDL_video.c	Sun Feb 08 15:35:06 2009 +0000
     3.2 +++ b/src/video/SDL_video.c	Mon Feb 09 05:32:12 2009 +0000
     3.3 @@ -762,9 +762,12 @@
     3.4          SDL_UninitializedVideo();
     3.5          return 0;
     3.6      }
     3.7 -    if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
     3.8 -        SDL_SetError("No OpenGL support in video driver");
     3.9 -        return 0;
    3.10 +    if (flags & SDL_WINDOW_OPENGL) {
    3.11 +        if (!_this->GL_CreateContext) {
    3.12 +            SDL_SetError("No OpenGL support in video driver");
    3.13 +            return 0;
    3.14 +        }
    3.15 +        SDL_GL_LoadLibrary(NULL);
    3.16      }
    3.17      SDL_zero(window);
    3.18      window.id = _this->next_object_id++;
    3.19 @@ -776,6 +779,9 @@
    3.20      window.display = _this->current_display;
    3.21  
    3.22      if (_this->CreateWindow && _this->CreateWindow(_this, &window) < 0) {
    3.23 +        if (flags & SDL_WINDOW_OPENGL) {
    3.24 +            SDL_GL_UnloadLibrary();
    3.25 +        }
    3.26          return 0;
    3.27      }
    3.28      display = &SDL_CurrentDisplay;
    3.29 @@ -786,6 +792,9 @@
    3.30          if (_this->DestroyWindow) {
    3.31              _this->DestroyWindow(_this, &window);
    3.32          }
    3.33 +        if (flags & SDL_WINDOW_OPENGL) {
    3.34 +            SDL_GL_UnloadLibrary();
    3.35 +        }
    3.36          return 0;
    3.37      }
    3.38      windows[num_windows] = window;
    3.39 @@ -824,6 +833,7 @@
    3.40      SDL_zero(window);
    3.41      window.id = _this->next_object_id++;
    3.42      window.display = _this->current_display;
    3.43 +    window.flags = SDL_WINDOW_FOREIGN;
    3.44  
    3.45      if (!_this->CreateWindowFrom ||
    3.46          _this->CreateWindowFrom(_this, &window, data) < 0) {
    3.47 @@ -852,24 +862,48 @@
    3.48  int
    3.49  SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
    3.50  {
    3.51 +    const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
    3.52 +                                  SDL_WINDOW_OPENGL |
    3.53 +                                  SDL_WINDOW_BORDERLESS |
    3.54 +                                  SDL_WINDOW_RESIZABLE |
    3.55 +                                  SDL_WINDOW_INPUT_GRABBED);
    3.56      char *title = window->title;
    3.57  
    3.58      if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
    3.59          SDL_SetError("No OpenGL support in video driver");
    3.60          return -1;
    3.61      }
    3.62 -    if (_this->DestroyWindow) {
    3.63 +    if ((window->flags & SDL_WINDOW_OPENGL) != (flags & SDL_WINDOW_OPENGL)) {
    3.64 +        if (flags & SDL_WINDOW_OPENGL) {
    3.65 +            SDL_GL_LoadLibrary(NULL);
    3.66 +        } else {
    3.67 +            SDL_GL_UnloadLibrary();
    3.68 +        }
    3.69 +    }
    3.70 +
    3.71 +    if (window->flags & SDL_WINDOW_FOREIGN) {
    3.72 +        /* Can't destroy and re-create foreign windows, hrm */
    3.73 +        flags |= SDL_WINDOW_FOREIGN;
    3.74 +    } else {
    3.75 +        flags &= ~SDL_WINDOW_FOREIGN;
    3.76 +    }
    3.77 +
    3.78 +    if (_this->DestroyWindow && !(flags & SDL_WINDOW_FOREIGN)) {
    3.79          _this->DestroyWindow(_this, window);
    3.80      }
    3.81 +
    3.82      window->title = NULL;
    3.83 -    window->flags =
    3.84 -        (flags &
    3.85 -         ~(SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED | SDL_WINDOW_SHOWN |
    3.86 -           SDL_WINDOW_INPUT_GRABBED));
    3.87 +    window->flags = (flags & allowed_flags);
    3.88  
    3.89 -    if (_this->CreateWindow && _this->CreateWindow(_this, window) < 0) {
    3.90 -        return -1;
    3.91 +    if (_this->CreateWindow && !(flags & SDL_WINDOW_FOREIGN)) {
    3.92 +        if (_this->CreateWindow(_this, window) < 0) {
    3.93 +            if (flags & SDL_WINDOW_OPENGL) {
    3.94 +                SDL_GL_UnloadLibrary();
    3.95 +            }
    3.96 +            return -1;
    3.97 +        }
    3.98      }
    3.99 +
   3.100      if (title) {
   3.101          SDL_SetWindowTitle(window->id, title);
   3.102          SDL_free(title);
   3.103 @@ -1352,6 +1386,9 @@
   3.104              if (_this->DestroyWindow) {
   3.105                  _this->DestroyWindow(_this, window);
   3.106              }
   3.107 +            if (window->flags & SDL_WINDOW_OPENGL) {
   3.108 +                SDL_GL_UnloadLibrary();
   3.109 +            }
   3.110              if (j != display->num_windows - 1) {
   3.111                  SDL_memcpy(&display->windows[i],
   3.112                             &display->windows[i + 1],
   3.113 @@ -1543,6 +1580,7 @@
   3.114  SDL_CreateTextureFromSurface(Uint32 format, SDL_Surface * surface)
   3.115  {
   3.116      SDL_TextureID textureID;
   3.117 +    Uint32 requested_format = format;
   3.118      SDL_PixelFormat *fmt;
   3.119      int bpp;
   3.120      Uint32 Rmask, Gmask, Bmask, Amask;
   3.121 @@ -1586,6 +1624,14 @@
   3.122      textureID =
   3.123          SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
   3.124                            surface->h);
   3.125 +    if (!textureID && !requested_format) {
   3.126 +        SDL_DisplayMode desktop_mode;
   3.127 +        SDL_GetDesktopDisplayMode(&desktop_mode);
   3.128 +        format = desktop_mode.format;
   3.129 +        textureID =
   3.130 +            SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
   3.131 +                              surface->h);
   3.132 +    }
   3.133      if (!textureID) {
   3.134          return 0;
   3.135      }
   3.136 @@ -2460,11 +2506,21 @@
   3.137          SDL_UninitializedVideo();
   3.138          return -1;
   3.139      }
   3.140 -    if (_this->GL_LoadLibrary) {
   3.141 +    if (_this->gl_config.driver_loaded) {
   3.142 +        if (path && SDL_strcmp(path, _this->gl_config.driver_path) != 0) {
   3.143 +            SDL_SetError("OpenGL library already loaded");
   3.144 +            return -1;
   3.145 +        }
   3.146 +        retval = 0;
   3.147 +    } else {
   3.148 +        if (!_this->GL_LoadLibrary) {
   3.149 +            SDL_SetError("No dynamic GL support in video driver");
   3.150 +            return -1;
   3.151 +        }
   3.152          retval = _this->GL_LoadLibrary(_this, path);
   3.153 -    } else {
   3.154 -        SDL_SetError("No dynamic GL support in video driver");
   3.155 -        retval = -1;
   3.156 +    }
   3.157 +    if (retval == 0) {
   3.158 +        ++_this->gl_config.driver_loaded;
   3.159      }
   3.160      return (retval);
   3.161  }
   3.162 @@ -2491,6 +2547,23 @@
   3.163      return func;
   3.164  }
   3.165  
   3.166 +void
   3.167 +SDL_GL_UnloadLibrary(void)
   3.168 +{
   3.169 +    if (!_this) {
   3.170 +        SDL_UninitializedVideo();
   3.171 +        return;
   3.172 +    }
   3.173 +    if (_this->gl_config.driver_loaded > 0) {
   3.174 +        if (--_this->gl_config.driver_loaded > 0) {
   3.175 +            return;
   3.176 +        }
   3.177 +        if (_this->GL_UnloadLibrary) {
   3.178 +            _this->GL_UnloadLibrary(_this);
   3.179 +        }
   3.180 +    }
   3.181 +}
   3.182 +
   3.183  SDL_bool
   3.184  SDL_GL_ExtensionSupported(const char *extension)
   3.185  {
     4.1 --- a/src/video/cocoa/SDL_cocoaopengl.h	Sun Feb 08 15:35:06 2009 +0000
     4.2 +++ b/src/video/cocoa/SDL_cocoaopengl.h	Mon Feb 09 05:32:12 2009 +0000
     4.3 @@ -34,8 +34,7 @@
     4.4  /* OpenGL functions */
     4.5  extern int Cocoa_GL_LoadLibrary(_THIS, const char *path);
     4.6  extern void *Cocoa_GL_GetProcAddress(_THIS, const char *proc);
     4.7 -extern int Cocoa_GL_SetupWindow(_THIS, SDL_Window * window);
     4.8 -extern void Cocoa_GL_CleanupWindow(_THIS, SDL_Window * window);
     4.9 +extern void Cocoa_GL_UnloadLibrary(_THIS);
    4.10  extern SDL_GLContext Cocoa_GL_CreateContext(_THIS, SDL_Window * window);
    4.11  extern int Cocoa_GL_MakeCurrent(_THIS, SDL_Window * window,
    4.12                                  SDL_GLContext context);
     5.1 --- a/src/video/cocoa/SDL_cocoaopengl.m	Sun Feb 08 15:35:06 2009 +0000
     5.2 +++ b/src/video/cocoa/SDL_cocoaopengl.m	Mon Feb 09 05:32:12 2009 +0000
     5.3 @@ -48,15 +48,7 @@
     5.4  int
     5.5  Cocoa_GL_LoadLibrary(_THIS, const char *path)
     5.6  {
     5.7 -    if (_this->gl_config.driver_loaded) {
     5.8 -        if (path) {
     5.9 -            SDL_SetError("OpenGL library already loaded");
    5.10 -            return -1;
    5.11 -        } else {
    5.12 -            ++_this->gl_config.driver_loaded;
    5.13 -            return 0;
    5.14 -        }
    5.15 -    }
    5.16 +    /* Load the OpenGL library */
    5.17      if (path == NULL) {
    5.18          path = SDL_getenv("SDL_OPENGL_LIBRARY");
    5.19      }
    5.20 @@ -69,7 +61,6 @@
    5.21      }
    5.22      SDL_strlcpy(_this->gl_config.driver_path, path,
    5.23                  SDL_arraysize(_this->gl_config.driver_path));
    5.24 -    _this->gl_config.driver_loaded = 1;
    5.25      return 0;
    5.26  }
    5.27  
    5.28 @@ -79,68 +70,11 @@
    5.29      return SDL_LoadFunction(_this->gl_config.dll_handle, proc);
    5.30  }
    5.31  
    5.32 -static void
    5.33 +void
    5.34  Cocoa_GL_UnloadLibrary(_THIS)
    5.35  {
    5.36 -    if (_this->gl_config.driver_loaded > 0) {
    5.37 -        if (--_this->gl_config.driver_loaded > 0) {
    5.38 -            return;
    5.39 -        }
    5.40 -        SDL_UnloadObject(_this->gl_config.dll_handle);
    5.41 -        _this->gl_config.dll_handle = NULL;
    5.42 -    }
    5.43 -}
    5.44 -
    5.45 -static int
    5.46 -Cocoa_GL_Initialize(_THIS)
    5.47 -{
    5.48 -    if (_this->gl_data) {
    5.49 -        ++_this->gl_data->initialized;
    5.50 -        return 0;
    5.51 -    }
    5.52 -
    5.53 -    _this->gl_data =
    5.54 -        (struct SDL_GLDriverData *) SDL_calloc(1,
    5.55 -                                               sizeof(struct
    5.56 -                                                      SDL_GLDriverData));
    5.57 -    if (!_this->gl_data) {
    5.58 -        SDL_OutOfMemory();
    5.59 -        return -1;
    5.60 -    }
    5.61 -    _this->gl_data->initialized = 1;
    5.62 -
    5.63 -    if (Cocoa_GL_LoadLibrary(_this, NULL) < 0) {
    5.64 -        return -1;
    5.65 -    }
    5.66 -    return 0;
    5.67 -}
    5.68 -
    5.69 -static void
    5.70 -Cocoa_GL_Shutdown(_THIS)
    5.71 -{
    5.72 -    if (!_this->gl_data || (--_this->gl_data->initialized > 0)) {
    5.73 -        return;
    5.74 -    }
    5.75 -
    5.76 -    Cocoa_GL_UnloadLibrary(_this);
    5.77 -
    5.78 -    SDL_free(_this->gl_data);
    5.79 -    _this->gl_data = NULL;
    5.80 -}
    5.81 -
    5.82 -int
    5.83 -Cocoa_GL_SetupWindow(_THIS, SDL_Window * window)
    5.84 -{
    5.85 -    if (Cocoa_GL_Initialize(_this) < 0) {
    5.86 -        return -1;
    5.87 -    }
    5.88 -    return 0;
    5.89 -}
    5.90 -
    5.91 -void
    5.92 -Cocoa_GL_CleanupWindow(_THIS, SDL_Window * window)
    5.93 -{
    5.94 -    Cocoa_GL_Shutdown(_this);
    5.95 +    SDL_UnloadObject(_this->gl_config.dll_handle);
    5.96 +    _this->gl_config.dll_handle = NULL;
    5.97  }
    5.98  
    5.99  SDL_GLContext
     6.1 --- a/src/video/cocoa/SDL_cocoavideo.m	Sun Feb 08 15:35:06 2009 +0000
     6.2 +++ b/src/video/cocoa/SDL_cocoavideo.m	Mon Feb 09 05:32:12 2009 +0000
     6.3 @@ -93,6 +93,7 @@
     6.4  #ifdef SDL_VIDEO_OPENGL_CGL
     6.5      device->GL_LoadLibrary = Cocoa_GL_LoadLibrary;
     6.6      device->GL_GetProcAddress = Cocoa_GL_GetProcAddress;
     6.7 +    device->GL_UnloadLibrary = Cocoa_GL_UnloadLibrary;
     6.8      device->GL_CreateContext = Cocoa_GL_CreateContext;
     6.9      device->GL_MakeCurrent = Cocoa_GL_MakeCurrent;
    6.10      device->GL_SetSwapInterval = Cocoa_GL_SetSwapInterval;
     7.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Sun Feb 08 15:35:06 2009 +0000
     7.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Mon Feb 09 05:32:12 2009 +0000
     7.3 @@ -422,14 +422,6 @@
     7.4          [nswindow release];
     7.5          return -1;
     7.6      }
     7.7 -#ifdef SDL_VIDEO_OPENGL_CGL
     7.8 -    if (window->flags & SDL_WINDOW_OPENGL) {
     7.9 -        if (Cocoa_GL_SetupWindow(_this, window) < 0) {
    7.10 -            Cocoa_DestroyWindow(_this, window);
    7.11 -            return -1;
    7.12 -        }
    7.13 -    }
    7.14 -#endif
    7.15      return 0;
    7.16  }
    7.17  
    7.18 @@ -586,11 +578,6 @@
    7.19      SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
    7.20  
    7.21      if (data) {
    7.22 -#ifdef SDL_VIDEO_OPENGL_CGL
    7.23 -        if (window->flags & SDL_WINDOW_OPENGL) {
    7.24 -            Cocoa_GL_CleanupWindow(_this, window);
    7.25 -        }
    7.26 -#endif
    7.27          [data->listener close];
    7.28          [data->listener release];
    7.29          if (data->created) {
     8.1 --- a/src/video/win32/SDL_win32opengl.c	Sun Feb 08 15:35:06 2009 +0000
     8.2 +++ b/src/video/win32/SDL_win32opengl.c	Mon Feb 09 05:32:12 2009 +0000
     8.3 @@ -37,15 +37,6 @@
     8.4      LPTSTR wpath;
     8.5      HANDLE handle;
     8.6  
     8.7 -    if (_this->gl_config.driver_loaded) {
     8.8 -        if (path) {
     8.9 -            SDL_SetError("OpenGL library already loaded");
    8.10 -            return -1;
    8.11 -        } else {
    8.12 -            ++_this->gl_config.driver_loaded;
    8.13 -            return 0;
    8.14 -        }
    8.15 -    }
    8.16      if (path == NULL) {
    8.17          path = SDL_getenv("SDL_OPENGL_LIBRARY");
    8.18      }
    8.19 @@ -53,17 +44,30 @@
    8.20          path = DEFAULT_OPENGL;
    8.21      }
    8.22      wpath = WIN_UTF8ToString(path);
    8.23 -    handle = LoadLibrary(wpath);
    8.24 +    _this->gl_config.dll_handle = LoadLibrary(wpath);
    8.25      SDL_free(wpath);
    8.26 -    if (!handle) {
    8.27 +    if (!_this->gl_config.dll_handle) {
    8.28          char message[1024];
    8.29          SDL_snprintf(message, SDL_arraysize(message), "LoadLibrary(\"%s\")",
    8.30                       path);
    8.31          WIN_SetError(message);
    8.32          return -1;
    8.33      }
    8.34 +    SDL_strlcpy(_this->gl_config.driver_path, path,
    8.35 +                SDL_arraysize(_this->gl_config.driver_path));
    8.36 +
    8.37 +    /* Allocate OpenGL memory */
    8.38 +    _this->gl_data =
    8.39 +        (struct SDL_GLDriverData *) SDL_calloc(1,
    8.40 +                                               sizeof(struct
    8.41 +                                                      SDL_GLDriverData));
    8.42 +    if (!_this->gl_data) {
    8.43 +        SDL_OutOfMemory();
    8.44 +        return -1;
    8.45 +    }
    8.46  
    8.47      /* Load function pointers */
    8.48 +    handle = _this->gl_config.dll_handle;
    8.49      _this->gl_data->wglGetProcAddress = (void *(WINAPI *) (const char *))
    8.50          GetProcAddress(handle, "wglGetProcAddress");
    8.51      _this->gl_data->wglCreateContext = (HGLRC(WINAPI *) (HDC))
    8.52 @@ -86,10 +90,6 @@
    8.53          return -1;
    8.54      }
    8.55  
    8.56 -    _this->gl_config.dll_handle = handle;
    8.57 -    SDL_strlcpy(_this->gl_config.driver_path, path,
    8.58 -                SDL_arraysize(_this->gl_config.driver_path));
    8.59 -    _this->gl_config.driver_loaded = 1;
    8.60      return 0;
    8.61  }
    8.62  
    8.63 @@ -107,16 +107,15 @@
    8.64      return func;
    8.65  }
    8.66  
    8.67 -static void
    8.68 +void
    8.69  WIN_GL_UnloadLibrary(_THIS)
    8.70  {
    8.71 -    if (_this->gl_config.driver_loaded > 0) {
    8.72 -        if (--_this->gl_config.driver_loaded > 0) {
    8.73 -            return;
    8.74 -        }
    8.75 -        FreeLibrary((HMODULE) _this->gl_config.dll_handle);
    8.76 -        _this->gl_config.dll_handle = NULL;
    8.77 -    }
    8.78 +    FreeLibrary((HMODULE) _this->gl_config.dll_handle);
    8.79 +    _this->gl_config.dll_handle = NULL;
    8.80 +
    8.81 +    /* Free OpenGL memory */
    8.82 +    SDL_free(_this->gl_data);
    8.83 +    _this->gl_data = NULL;
    8.84  }
    8.85  
    8.86  static void
    8.87 @@ -378,44 +377,6 @@
    8.88      return pixel_format;
    8.89  }
    8.90  
    8.91 -static int
    8.92 -WIN_GL_Initialize(_THIS)
    8.93 -{
    8.94 -    if (_this->gl_data) {
    8.95 -        ++_this->gl_data->initialized;
    8.96 -        return 0;
    8.97 -    }
    8.98 -
    8.99 -    _this->gl_data =
   8.100 -        (struct SDL_GLDriverData *) SDL_calloc(1,
   8.101 -                                               sizeof(struct
   8.102 -                                                      SDL_GLDriverData));
   8.103 -    if (!_this->gl_data) {
   8.104 -        SDL_OutOfMemory();
   8.105 -        return -1;
   8.106 -    }
   8.107 -    _this->gl_data->initialized = 1;
   8.108 -
   8.109 -    if (WIN_GL_LoadLibrary(_this, NULL) < 0) {
   8.110 -        return -1;
   8.111 -    }
   8.112 -
   8.113 -    return 0;
   8.114 -}
   8.115 -
   8.116 -static void
   8.117 -WIN_GL_Shutdown(_THIS)
   8.118 -{
   8.119 -    if (!_this->gl_data || (--_this->gl_data->initialized > 0)) {
   8.120 -        return;
   8.121 -    }
   8.122 -
   8.123 -    WIN_GL_UnloadLibrary(_this);
   8.124 -
   8.125 -    SDL_free(_this->gl_data);
   8.126 -    _this->gl_data = NULL;
   8.127 -}
   8.128 -
   8.129  int
   8.130  WIN_GL_SetupWindow(_THIS, SDL_Window * window)
   8.131  {
   8.132 @@ -426,10 +387,6 @@
   8.133      int *iAttr;
   8.134      float fAttribs[1] = { 0 };
   8.135  
   8.136 -    if (WIN_GL_Initialize(_this) < 0) {
   8.137 -        return -1;
   8.138 -    }
   8.139 -
   8.140      WIN_GL_SetupPixelFormat(_this, &pfd);
   8.141  
   8.142      /* setup WGL_ARB_pixel_format attribs */
   8.143 @@ -522,12 +479,6 @@
   8.144      return 0;
   8.145  }
   8.146  
   8.147 -void
   8.148 -WIN_GL_CleanupWindow(_THIS, SDL_Window * window)
   8.149 -{
   8.150 -    WIN_GL_Shutdown(_this);
   8.151 -}
   8.152 -
   8.153  SDL_GLContext
   8.154  WIN_GL_CreateContext(_THIS, SDL_Window * window)
   8.155  {
     9.1 --- a/src/video/win32/SDL_win32opengl.h	Sun Feb 08 15:35:06 2009 +0000
     9.2 +++ b/src/video/win32/SDL_win32opengl.h	Mon Feb 09 05:32:12 2009 +0000
     9.3 @@ -28,7 +28,6 @@
     9.4  
     9.5  struct SDL_GLDriverData
     9.6  {
     9.7 -    int initialized;
     9.8      int WGL_ARB_pixel_format;
     9.9  
    9.10      void *(WINAPI * wglGetProcAddress) (const char *proc);
    9.11 @@ -53,8 +52,8 @@
    9.12  /* OpenGL functions */
    9.13  extern int WIN_GL_LoadLibrary(_THIS, const char *path);
    9.14  extern void *WIN_GL_GetProcAddress(_THIS, const char *proc);
    9.15 +extern void WIN_GL_UnloadLibrary(_THIS);
    9.16  extern int WIN_GL_SetupWindow(_THIS, SDL_Window * window);
    9.17 -extern void WIN_GL_CleanupWindow(_THIS, SDL_Window * window);
    9.18  extern SDL_GLContext WIN_GL_CreateContext(_THIS, SDL_Window * window);
    9.19  extern int WIN_GL_MakeCurrent(_THIS, SDL_Window * window,
    9.20                                SDL_GLContext context);
    10.1 --- a/src/video/win32/SDL_win32video.c	Sun Feb 08 15:35:06 2009 +0000
    10.2 +++ b/src/video/win32/SDL_win32video.c	Mon Feb 09 05:32:12 2009 +0000
    10.3 @@ -161,6 +161,7 @@
    10.4  #ifdef SDL_VIDEO_OPENGL_WGL
    10.5      device->GL_LoadLibrary = WIN_GL_LoadLibrary;
    10.6      device->GL_GetProcAddress = WIN_GL_GetProcAddress;
    10.7 +    device->GL_UnloadLibrary = WIN_GL_UnloadLibrary;
    10.8      device->GL_CreateContext = WIN_GL_CreateContext;
    10.9      device->GL_MakeCurrent = WIN_GL_MakeCurrent;
   10.10      device->GL_SetSwapInterval = WIN_GL_SetSwapInterval;
    11.1 --- a/src/video/win32/SDL_win32window.c	Sun Feb 08 15:35:06 2009 +0000
    11.2 +++ b/src/video/win32/SDL_win32window.c	Mon Feb 09 05:32:12 2009 +0000
    11.3 @@ -521,11 +521,6 @@
    11.4      SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
    11.5  
    11.6      if (data) {
    11.7 -#ifdef SDL_VIDEO_OPENGL_WGL
    11.8 -        if (window->flags & SDL_WINDOW_OPENGL) {
    11.9 -            WIN_GL_CleanupWindow(_this, window);
   11.10 -        }
   11.11 -#endif
   11.12          ReleaseDC(data->hwnd, data->hdc);
   11.13          if (data->created) {
   11.14              if (videodata->wintabDLL) {
    12.1 --- a/src/video/x11/SDL_x11opengl.c	Sun Feb 08 15:35:06 2009 +0000
    12.2 +++ b/src/video/x11/SDL_x11opengl.c	Mon Feb 09 05:32:12 2009 +0000
    12.3 @@ -65,37 +65,39 @@
    12.4  #define GL_UnloadObject	SDL_UnloadObject
    12.5  #endif
    12.6  
    12.7 -static int X11_GL_InitializeMemory(_THIS);
    12.8 +static void X11_GL_InitExtensions(_THIS);
    12.9  
   12.10  int
   12.11  X11_GL_LoadLibrary(_THIS, const char *path)
   12.12  {
   12.13      void *handle;
   12.14  
   12.15 -    if (_this->gl_config.driver_loaded) {
   12.16 -        /* do not return without reinitializing the function hooks */
   12.17 -        if (path) {
   12.18 -            SDL_SetError("OpenGL library already loaded");
   12.19 -        }
   12.20 -        handle = _this->gl_config.dll_handle;
   12.21 -    } else {
   12.22 -        if (path == NULL) {
   12.23 -            path = SDL_getenv("SDL_OPENGL_LIBRARY");
   12.24 -        }
   12.25 -        if (path == NULL) {
   12.26 -            path = DEFAULT_OPENGL;
   12.27 -        }
   12.28 -        handle = GL_LoadObject(path);
   12.29 -        if (!handle) {
   12.30 -            return -1;
   12.31 -        }
   12.32 -        _this->gl_config.dll_handle = handle;
   12.33 -        SDL_strlcpy(_this->gl_config.driver_path, path,
   12.34 -                    SDL_arraysize(_this->gl_config.driver_path));
   12.35 +    /* Load the OpenGL library */
   12.36 +    if (path == NULL) {
   12.37 +        path = SDL_getenv("SDL_OPENGL_LIBRARY");
   12.38      }
   12.39 -    X11_GL_InitializeMemory(_this);
   12.40 +    if (path == NULL) {
   12.41 +        path = DEFAULT_OPENGL;
   12.42 +    }
   12.43 +    _this->gl_config.dll_handle = SDL_LoadObject(path);
   12.44 +    if (!_this->gl_config.dll_handle) {
   12.45 +        return -1;
   12.46 +    }
   12.47 +    SDL_strlcpy(_this->gl_config.driver_path, path,
   12.48 +                SDL_arraysize(_this->gl_config.driver_path));
   12.49  
   12.50 -    /* Load new function pointers */
   12.51 +    /* Allocate OpenGL memory */
   12.52 +    _this->gl_data =
   12.53 +        (struct SDL_GLDriverData *) SDL_calloc(1,
   12.54 +                                               sizeof(struct
   12.55 +                                                      SDL_GLDriverData));
   12.56 +    if (!_this->gl_data) {
   12.57 +        SDL_OutOfMemory();
   12.58 +        return -1;
   12.59 +    }
   12.60 +
   12.61 +    /* Load function pointers */
   12.62 +    handle = _this->gl_config.dll_handle;
   12.63      _this->gl_data->glXGetProcAddress =
   12.64          (void *(*)(const GLubyte *)) GL_LoadFunction(handle,
   12.65                                                       "glXGetProcAddressARB");
   12.66 @@ -123,7 +125,9 @@
   12.67          return -1;
   12.68      }
   12.69  
   12.70 -    ++_this->gl_config.driver_loaded;
   12.71 +    /* Initialize extensions */
   12.72 +    X11_GL_InitExtensions(_this);
   12.73 +
   12.74      return 0;
   12.75  }
   12.76  
   12.77 @@ -139,16 +143,21 @@
   12.78      return GL_LoadFunction(handle, proc);
   12.79  }
   12.80  
   12.81 -static void
   12.82 +void
   12.83  X11_GL_UnloadLibrary(_THIS)
   12.84  {
   12.85 -    if (_this->gl_config.driver_loaded > 0) {
   12.86 -        if (--_this->gl_config.driver_loaded > 0) {
   12.87 -            return;
   12.88 -        }
   12.89 -        GL_UnloadObject(_this->gl_config.dll_handle);
   12.90 -        _this->gl_config.dll_handle = NULL;
   12.91 -    }
   12.92 +    /* Don't actually unload the library, since it may have registered
   12.93 +     * X11 shutdown hooks, per the notes at:
   12.94 +     * http://dri.sourceforge.net/doc/DRIuserguide.html
   12.95 +     */
   12.96 +#if 0
   12.97 +    GL_UnloadObject(_this->gl_config.dll_handle);
   12.98 +    _this->gl_config.dll_handle = NULL;
   12.99 +#endif
  12.100 +
  12.101 +    /* Free OpenGL memory */
  12.102 +    SDL_free(_this->gl_data);
  12.103 +    _this->gl_data = NULL;
  12.104  }
  12.105  
  12.106  static SDL_bool
  12.107 @@ -254,62 +263,6 @@
  12.108      X11_PumpEvents(_this);
  12.109  }
  12.110  
  12.111 -static int
  12.112 -X11_GL_InitializeMemory(_THIS)
  12.113 -{
  12.114 -    if (_this->gl_data) {
  12.115 -        return 0;
  12.116 -    }
  12.117 -
  12.118 -    _this->gl_data =
  12.119 -        (struct SDL_GLDriverData *) SDL_calloc(1,
  12.120 -                                               sizeof(struct
  12.121 -                                                      SDL_GLDriverData));
  12.122 -    if (!_this->gl_data) {
  12.123 -        SDL_OutOfMemory();
  12.124 -        return -1;
  12.125 -    }
  12.126 -    _this->gl_data->initialized = 0;
  12.127 -
  12.128 -    return 0;
  12.129 -}
  12.130 -
  12.131 -int
  12.132 -X11_GL_Initialize(_THIS)
  12.133 -{
  12.134 -
  12.135 -    if (X11_GL_InitializeMemory(_this) < 0) {
  12.136 -        return -1;
  12.137 -    }
  12.138 -    ++_this->gl_data->initialized;
  12.139 -
  12.140 -    if (X11_GL_LoadLibrary(_this, NULL) < 0) {
  12.141 -        return -1;
  12.142 -    }
  12.143 -
  12.144 -    /* Initialize extensions */
  12.145 -    X11_GL_InitExtensions(_this);
  12.146 -
  12.147 -    return 0;
  12.148 -}
  12.149 -
  12.150 -void
  12.151 -X11_GL_Shutdown(_THIS)
  12.152 -{
  12.153 -    if (!_this->gl_data || (--_this->gl_data->initialized > 0)) {
  12.154 -        return;
  12.155 -    }
  12.156 -
  12.157 -    /* Don't actually unload the library, since it may have registered
  12.158 -     * X11 shutdown hooks, per the notes at:
  12.159 -     * http://dri.sourceforge.net/doc/DRIuserguide.html
  12.160 -     * //X11_GL_UnloadLibrary(_this);
  12.161 -     */
  12.162 -
  12.163 -    SDL_free(_this->gl_data);
  12.164 -    _this->gl_data = NULL;
  12.165 -}
  12.166 -
  12.167  XVisualInfo *
  12.168  X11_GL_GetVisual(_THIS, Display * display, int screen)
  12.169  {
    13.1 --- a/src/video/x11/SDL_x11opengl.h	Sun Feb 08 15:35:06 2009 +0000
    13.2 +++ b/src/video/x11/SDL_x11opengl.h	Mon Feb 09 05:32:12 2009 +0000
    13.3 @@ -30,7 +30,6 @@
    13.4  
    13.5  struct SDL_GLDriverData
    13.6  {
    13.7 -    int initialized;
    13.8      SDL_bool HAS_GLX_EXT_visual_rating;
    13.9  
   13.10      void *(*glXGetProcAddress) (const GLubyte * procName);
   13.11 @@ -58,8 +57,7 @@
   13.12  /* OpenGL functions */
   13.13  extern int X11_GL_LoadLibrary(_THIS, const char *path);
   13.14  extern void *X11_GL_GetProcAddress(_THIS, const char *proc);
   13.15 -extern int X11_GL_Initialize(_THIS);
   13.16 -extern void X11_GL_Shutdown(_THIS);
   13.17 +extern void X11_GL_UnloadLibrary(_THIS);
   13.18  extern XVisualInfo *X11_GL_GetVisual(_THIS, Display * display, int screen);
   13.19  extern SDL_GLContext X11_GL_CreateContext(_THIS, SDL_Window * window);
   13.20  extern int X11_GL_MakeCurrent(_THIS, SDL_Window * window,
    14.1 --- a/src/video/x11/SDL_x11video.c	Sun Feb 08 15:35:06 2009 +0000
    14.2 +++ b/src/video/x11/SDL_x11video.c	Mon Feb 09 05:32:12 2009 +0000
    14.3 @@ -190,6 +190,7 @@
    14.4  #ifdef SDL_VIDEO_OPENGL_GLX
    14.5      device->GL_LoadLibrary = X11_GL_LoadLibrary;
    14.6      device->GL_GetProcAddress = X11_GL_GetProcAddress;
    14.7 +    device->GL_UnloadLibrary = X11_GL_UnloadLibrary;
    14.8      device->GL_CreateContext = X11_GL_CreateContext;
    14.9      device->GL_MakeCurrent = X11_GL_MakeCurrent;
   14.10      device->GL_SetSwapInterval = X11_GL_SetSwapInterval;
    15.1 --- a/src/video/x11/SDL_x11window.c	Sun Feb 08 15:35:06 2009 +0000
    15.2 +++ b/src/video/x11/SDL_x11window.c	Mon Feb 09 05:32:12 2009 +0000
    15.3 @@ -188,9 +188,6 @@
    15.4      if (window->flags & SDL_WINDOW_OPENGL) {
    15.5          XVisualInfo *vinfo;
    15.6  
    15.7 -        if (X11_GL_Initialize(_this) < 0) {
    15.8 -            return -1;
    15.9 -        }
   15.10          vinfo = X11_GL_GetVisual(_this, data->display, displaydata->screen);
   15.11          if (!vinfo) {
   15.12              return -1;
   15.13 @@ -461,11 +458,6 @@
   15.14                        (CWOverrideRedirect | CWBackPixel | CWBorderPixel |
   15.15                         CWColormap), &xattr);
   15.16      if (!w) {
   15.17 -#ifdef SDL_VIDEO_OPENGL_GLX
   15.18 -        if (window->flags & SDL_WINDOW_OPENGL) {
   15.19 -            X11_GL_Shutdown(_this);
   15.20 -        }
   15.21 -#endif
   15.22          SDL_SetError("Couldn't create window");
   15.23          return -1;
   15.24      }
   15.25 @@ -622,11 +614,6 @@
   15.26      XSetWMProtocols(data->display, w, &data->WM_DELETE_WINDOW, 1);
   15.27  
   15.28      if (SetupWindowData(_this, window, w, SDL_TRUE) < 0) {
   15.29 -#ifdef SDL_VIDEO_OPENGL_GLX
   15.30 -        if (window->flags & SDL_WINDOW_OPENGL) {
   15.31 -            X11_GL_Shutdown(_this);
   15.32 -        }
   15.33 -#endif
   15.34          XDestroyWindow(data->display, w);
   15.35          return -1;
   15.36      }
   15.37 @@ -942,11 +929,6 @@
   15.38                  }
   15.39              }
   15.40          }
   15.41 -#ifdef SDL_VIDEO_OPENGL_GLX
   15.42 -        if (window->flags & SDL_WINDOW_OPENGL) {
   15.43 -            X11_GL_Shutdown(_this);
   15.44 -        }
   15.45 -#endif
   15.46  #ifdef X_HAVE_UTF8_STRING
   15.47          if (data->ic) {
   15.48              XDestroyIC(data->ic);
    16.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.2 +++ b/test/testnative.c	Mon Feb 09 05:32:12 2009 +0000
    16.3 @@ -0,0 +1,219 @@
    16.4 +/* Simple program:  Create a native window and attach an SDL renderer */
    16.5 +
    16.6 +#include "testnative.h"
    16.7 +
    16.8 +#define WINDOW_W    640
    16.9 +#define WINDOW_H    480
   16.10 +#define NUM_SPRITES 100
   16.11 +#define MAX_SPEED 	1
   16.12 +
   16.13 +static NativeWindowFactory *factories[] = {
   16.14 +#ifdef TEST_NATIVE_WIN32
   16.15 +    &Win32WindowFactory,
   16.16 +#endif
   16.17 +#ifdef TEST_NATIVE_X11
   16.18 +    &X11WindowFactory,
   16.19 +#endif
   16.20 +#ifdef TEST_NATIVE_COCOA
   16.21 +    &CocoaWindowFactory,
   16.22 +#endif
   16.23 +    NULL
   16.24 +};
   16.25 +static NativeWindowFactory *factory = NULL;
   16.26 +static void *native_window;
   16.27 +static SDL_Rect *positions, *velocities;
   16.28 +
   16.29 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
   16.30 +static void
   16.31 +quit(int rc)
   16.32 +{
   16.33 +    SDL_VideoQuit();
   16.34 +    if (native_window) {
   16.35 +        factory->DestroyWindow(native_window);
   16.36 +    }
   16.37 +    exit(rc);
   16.38 +}
   16.39 +
   16.40 +SDL_TextureID
   16.41 +LoadSprite(SDL_WindowID window, char *file)
   16.42 +{
   16.43 +    SDL_Surface *temp;
   16.44 +    SDL_TextureID sprite;
   16.45 +
   16.46 +    /* Load the sprite image */
   16.47 +    temp = SDL_LoadBMP(file);
   16.48 +    if (temp == NULL) {
   16.49 +        fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
   16.50 +        return 0;
   16.51 +    }
   16.52 +
   16.53 +    /* Set transparent pixel as the pixel at (0,0) */
   16.54 +    if (temp->format->palette) {
   16.55 +        SDL_SetColorKey(temp, SDL_SRCCOLORKEY, *(Uint8 *) temp->pixels);
   16.56 +    }
   16.57 +
   16.58 +    /* Create textures from the image */
   16.59 +    SDL_SelectRenderer(window);
   16.60 +    sprite = SDL_CreateTextureFromSurface(0, temp);
   16.61 +    if (!sprite) {
   16.62 +        fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
   16.63 +        SDL_FreeSurface(temp);
   16.64 +        return 0;
   16.65 +    }
   16.66 +    SDL_FreeSurface(temp);
   16.67 +
   16.68 +    /* We're ready to roll. :) */
   16.69 +    return sprite;
   16.70 +}
   16.71 +
   16.72 +void
   16.73 +MoveSprites(SDL_WindowID window, SDL_TextureID sprite)
   16.74 +{
   16.75 +    int i, n;
   16.76 +    int window_w, window_h;
   16.77 +    int sprite_w, sprite_h;
   16.78 +    SDL_Rect *position, *velocity;
   16.79 +
   16.80 +    SDL_SelectRenderer(window);
   16.81 +
   16.82 +    /* Query the sizes */
   16.83 +    SDL_GetWindowSize(window, &window_w, &window_h);
   16.84 +    SDL_QueryTexture(sprite, NULL, NULL, &sprite_w, &sprite_h);
   16.85 +
   16.86 +    /* Move the sprite, bounce at the wall, and draw */
   16.87 +    n = 0;
   16.88 +    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   16.89 +    SDL_RenderFill(NULL);
   16.90 +    for (i = 0; i < NUM_SPRITES; ++i) {
   16.91 +        position = &positions[i];
   16.92 +        velocity = &velocities[i];
   16.93 +        position->x += velocity->x;
   16.94 +        if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
   16.95 +            velocity->x = -velocity->x;
   16.96 +            position->x += velocity->x;
   16.97 +        }
   16.98 +        position->y += velocity->y;
   16.99 +        if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
  16.100 +            velocity->y = -velocity->y;
  16.101 +            position->y += velocity->y;
  16.102 +        }
  16.103 +
  16.104 +        /* Blit the sprite onto the screen */
  16.105 +        SDL_RenderCopy(sprite, NULL, position);
  16.106 +    }
  16.107 +
  16.108 +    /* Update the screen! */
  16.109 +    SDL_RenderPresent();
  16.110 +}
  16.111 +
  16.112 +int
  16.113 +main(int argc, char *argv[])
  16.114 +{
  16.115 +    int i, done;
  16.116 +    const char *driver;
  16.117 +    SDL_WindowID window;
  16.118 +    SDL_TextureID sprite;
  16.119 +    int window_w, window_h;
  16.120 +    int sprite_w, sprite_h;
  16.121 +    SDL_Event event;
  16.122 +
  16.123 +    if (SDL_VideoInit(NULL, 0) < 0) {
  16.124 +        fprintf(stderr, "Couldn't initialize SDL video: %s\n",
  16.125 +                SDL_GetError());
  16.126 +        exit(1);
  16.127 +    }
  16.128 +    driver = SDL_GetCurrentVideoDriver();
  16.129 +
  16.130 +    /* Find a native window driver and create a native window */
  16.131 +    for (i = 0; factories[i]; ++i) {
  16.132 +        if (SDL_strcmp(driver, factories[i]->tag) == 0) {
  16.133 +            factory = factories[i];
  16.134 +            break;
  16.135 +        }
  16.136 +    }
  16.137 +    if (!factory) {
  16.138 +        fprintf(stderr, "Couldn't find native window code for %s driver\n",
  16.139 +                driver);
  16.140 +        quit(2);
  16.141 +    }
  16.142 +    printf("Creating native window for %s driver\n", driver);
  16.143 +    native_window = factory->CreateWindow(WINDOW_W, WINDOW_H);
  16.144 +    if (!native_window) {
  16.145 +        fprintf(stderr, "Couldn't create native window\n");
  16.146 +        quit(3);
  16.147 +    }
  16.148 +    window = SDL_CreateWindowFrom(native_window);
  16.149 +    if (!window) {
  16.150 +        fprintf(stderr, "Couldn't create SDL window: %s\n", SDL_GetError());
  16.151 +        quit(4);
  16.152 +    }
  16.153 +    SDL_SetWindowTitle(window, "SDL Native Window Test");
  16.154 +
  16.155 +    /* Create the renderer */
  16.156 +    if (SDL_CreateRenderer(window, -1, 0) < 0) {
  16.157 +        fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
  16.158 +        quit(5);
  16.159 +    }
  16.160 +
  16.161 +    /* Clear the window, load the sprite and go! */
  16.162 +    SDL_SelectRenderer(window);
  16.163 +    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
  16.164 +    SDL_RenderFill(NULL);
  16.165 +
  16.166 +    sprite = LoadSprite(window, "icon.bmp");
  16.167 +    if (!sprite) {
  16.168 +        quit(6);
  16.169 +    }
  16.170 +
  16.171 +    /* Allocate memory for the sprite info */
  16.172 +    SDL_GetWindowSize(window, &window_w, &window_h);
  16.173 +    SDL_QueryTexture(sprite, NULL, NULL, &sprite_w, &sprite_h);
  16.174 +    positions = (SDL_Rect *) SDL_malloc(NUM_SPRITES * sizeof(SDL_Rect));
  16.175 +    velocities = (SDL_Rect *) SDL_malloc(NUM_SPRITES * sizeof(SDL_Rect));
  16.176 +    if (!positions || !velocities) {
  16.177 +        fprintf(stderr, "Out of memory!\n");
  16.178 +        quit(2);
  16.179 +    }
  16.180 +    srand(time(NULL));
  16.181 +    for (i = 0; i < NUM_SPRITES; ++i) {
  16.182 +        positions[i].x = rand() % (window_w - sprite_w);
  16.183 +        positions[i].y = rand() % (window_h - sprite_h);
  16.184 +        positions[i].w = sprite_w;
  16.185 +        positions[i].h = sprite_h;
  16.186 +        velocities[i].x = 0;
  16.187 +        velocities[i].y = 0;
  16.188 +        while (!velocities[i].x && !velocities[i].y) {
  16.189 +            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
  16.190 +            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
  16.191 +        }
  16.192 +    }
  16.193 +
  16.194 +    /* Main render loop */
  16.195 +    done = 0;
  16.196 +    while (!done) {
  16.197 +        /* Check for events */
  16.198 +        while (SDL_PollEvent(&event)) {
  16.199 +            switch (event.type) {
  16.200 +            case SDL_WINDOWEVENT:
  16.201 +                switch (event.window.event) {
  16.202 +                case SDL_WINDOWEVENT_EXPOSED:
  16.203 +                    SDL_SelectRenderer(event.window.windowID);
  16.204 +                    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
  16.205 +                    SDL_RenderFill(NULL);
  16.206 +                    break;
  16.207 +                }
  16.208 +                break;
  16.209 +            case SDL_QUIT:
  16.210 +                done = 1;
  16.211 +                break;
  16.212 +            default:
  16.213 +                break;
  16.214 +            }
  16.215 +        }
  16.216 +        MoveSprites(window, sprite);
  16.217 +    }
  16.218 +
  16.219 +    quit(0);
  16.220 +}
  16.221 +
  16.222 +/* vi: set ts=4 sw=4 expandtab: */
    17.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.2 +++ b/test/testnativecocoa.m	Mon Feb 09 05:32:12 2009 +0000
    17.3 @@ -0,0 +1,51 @@
    17.4 +
    17.5 +#include "testnative.h"
    17.6 +
    17.7 +#ifdef TEST_NATIVE_COCOA
    17.8 +
    17.9 +#include <Cocoa/Cocoa.h>
   17.10 +
   17.11 +static void *CreateWindowCocoa(int w, int h);
   17.12 +static void DestroyWindowCocoa(void *window);
   17.13 +
   17.14 +NativeWindowFactory CocoaWindowFactory = {
   17.15 +    "cocoa",
   17.16 +    CreateWindowCocoa,
   17.17 +    DestroyWindowCocoa
   17.18 +};
   17.19 +
   17.20 +static void *CreateWindowCocoa(int w, int h)
   17.21 +{
   17.22 +    NSAutoreleasePool *pool;
   17.23 +    NSWindow *nswindow;
   17.24 +    NSRect rect;
   17.25 +    unsigned int style;
   17.26 +
   17.27 +    pool = [[NSAutoreleasePool alloc] init];
   17.28 +
   17.29 +    rect.origin.x = 0;
   17.30 +    rect.origin.y = 0;
   17.31 +    rect.size.width = w;
   17.32 +    rect.size.height = h;
   17.33 +    rect.origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - rect.origin.y - rect.size.height;
   17.34 +
   17.35 +    style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
   17.36 +
   17.37 +    nswindow = [[NSWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE];
   17.38 +    [nswindow makeKeyAndOrderFront:nil];
   17.39 +
   17.40 +    [pool release];
   17.41 +
   17.42 +    return nswindow;
   17.43 +}
   17.44 +
   17.45 +static void DestroyWindowCocoa(void *window)
   17.46 +{
   17.47 +    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   17.48 +    NSWindow *nswindow = (NSWindow *)window;
   17.49 +
   17.50 +    [nswindow close];
   17.51 +    [pool release];
   17.52 +}
   17.53 +
   17.54 +#endif
    18.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.2 +++ b/test/testnativew32.c	Mon Feb 09 05:32:12 2009 +0000
    18.3 @@ -0,0 +1,28 @@
    18.4 +
    18.5 +#include "testnative.h"
    18.6 +
    18.7 +#ifdef TEST_NATIVE_WIN32
    18.8 +
    18.9 +static void *CreateWindowWin32(int w, int h);
   18.10 +static void DestroyWindowWin32(void *window);
   18.11 +
   18.12 +NativeWindowFactory Win32WindowFactory = {
   18.13 +    "win32",
   18.14 +    CreateWindowWin32,
   18.15 +    DestroyWindowWin32
   18.16 +};
   18.17 +
   18.18 +static Display *dpy;
   18.19 +
   18.20 +static void *
   18.21 +CreateWindowWin32(int w, int h)
   18.22 +{
   18.23 +    return NULL;
   18.24 +}
   18.25 +
   18.26 +static void
   18.27 +DestroyWindowWin32(void *window)
   18.28 +{
   18.29 +}
   18.30 +
   18.31 +#endif
    19.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    19.2 +++ b/test/testnativex11.c	Mon Feb 09 05:32:12 2009 +0000
    19.3 @@ -0,0 +1,42 @@
    19.4 +
    19.5 +#include "testnative.h"
    19.6 +
    19.7 +#ifdef TEST_NATIVE_X11
    19.8 +
    19.9 +static void *CreateWindowX11(int w, int h);
   19.10 +static void DestroyWindowX11(void *window);
   19.11 +
   19.12 +NativeWindowFactory X11WindowFactory = {
   19.13 +    "x11",
   19.14 +    CreateWindowX11,
   19.15 +    DestroyWindowX11
   19.16 +};
   19.17 +
   19.18 +static Display *dpy;
   19.19 +
   19.20 +static void *
   19.21 +CreateWindowX11(int w, int h)
   19.22 +{
   19.23 +    Window window = 0;
   19.24 +
   19.25 +    dpy = XOpenDisplay(NULL);
   19.26 +    if (dpy) {
   19.27 +        window =
   19.28 +            XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, w, h, 0, 0,
   19.29 +                                0);
   19.30 +        XMapRaised(dpy, window);
   19.31 +        XSync(dpy, False);
   19.32 +    }
   19.33 +    return (void *) window;
   19.34 +}
   19.35 +
   19.36 +static void
   19.37 +DestroyWindowX11(void *window)
   19.38 +{
   19.39 +    if (dpy) {
   19.40 +        XDestroyWindow(dpy, (Window) window);
   19.41 +        XCloseDisplay(dpy);
   19.42 +    }
   19.43 +}
   19.44 +
   19.45 +#endif