src/video/SDL_video.c
changeset 3057 089a77aebb7d
parent 3053 aa34d1180d30
child 3058 17c5930f498e
     1.1 --- a/src/video/SDL_video.c	Sun Feb 08 15:35:06 2009 +0000
     1.2 +++ b/src/video/SDL_video.c	Mon Feb 09 05:32:12 2009 +0000
     1.3 @@ -762,9 +762,12 @@
     1.4          SDL_UninitializedVideo();
     1.5          return 0;
     1.6      }
     1.7 -    if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
     1.8 -        SDL_SetError("No OpenGL support in video driver");
     1.9 -        return 0;
    1.10 +    if (flags & SDL_WINDOW_OPENGL) {
    1.11 +        if (!_this->GL_CreateContext) {
    1.12 +            SDL_SetError("No OpenGL support in video driver");
    1.13 +            return 0;
    1.14 +        }
    1.15 +        SDL_GL_LoadLibrary(NULL);
    1.16      }
    1.17      SDL_zero(window);
    1.18      window.id = _this->next_object_id++;
    1.19 @@ -776,6 +779,9 @@
    1.20      window.display = _this->current_display;
    1.21  
    1.22      if (_this->CreateWindow && _this->CreateWindow(_this, &window) < 0) {
    1.23 +        if (flags & SDL_WINDOW_OPENGL) {
    1.24 +            SDL_GL_UnloadLibrary();
    1.25 +        }
    1.26          return 0;
    1.27      }
    1.28      display = &SDL_CurrentDisplay;
    1.29 @@ -786,6 +792,9 @@
    1.30          if (_this->DestroyWindow) {
    1.31              _this->DestroyWindow(_this, &window);
    1.32          }
    1.33 +        if (flags & SDL_WINDOW_OPENGL) {
    1.34 +            SDL_GL_UnloadLibrary();
    1.35 +        }
    1.36          return 0;
    1.37      }
    1.38      windows[num_windows] = window;
    1.39 @@ -824,6 +833,7 @@
    1.40      SDL_zero(window);
    1.41      window.id = _this->next_object_id++;
    1.42      window.display = _this->current_display;
    1.43 +    window.flags = SDL_WINDOW_FOREIGN;
    1.44  
    1.45      if (!_this->CreateWindowFrom ||
    1.46          _this->CreateWindowFrom(_this, &window, data) < 0) {
    1.47 @@ -852,24 +862,48 @@
    1.48  int
    1.49  SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
    1.50  {
    1.51 +    const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
    1.52 +                                  SDL_WINDOW_OPENGL |
    1.53 +                                  SDL_WINDOW_BORDERLESS |
    1.54 +                                  SDL_WINDOW_RESIZABLE |
    1.55 +                                  SDL_WINDOW_INPUT_GRABBED);
    1.56      char *title = window->title;
    1.57  
    1.58      if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
    1.59          SDL_SetError("No OpenGL support in video driver");
    1.60          return -1;
    1.61      }
    1.62 -    if (_this->DestroyWindow) {
    1.63 +    if ((window->flags & SDL_WINDOW_OPENGL) != (flags & SDL_WINDOW_OPENGL)) {
    1.64 +        if (flags & SDL_WINDOW_OPENGL) {
    1.65 +            SDL_GL_LoadLibrary(NULL);
    1.66 +        } else {
    1.67 +            SDL_GL_UnloadLibrary();
    1.68 +        }
    1.69 +    }
    1.70 +
    1.71 +    if (window->flags & SDL_WINDOW_FOREIGN) {
    1.72 +        /* Can't destroy and re-create foreign windows, hrm */
    1.73 +        flags |= SDL_WINDOW_FOREIGN;
    1.74 +    } else {
    1.75 +        flags &= ~SDL_WINDOW_FOREIGN;
    1.76 +    }
    1.77 +
    1.78 +    if (_this->DestroyWindow && !(flags & SDL_WINDOW_FOREIGN)) {
    1.79          _this->DestroyWindow(_this, window);
    1.80      }
    1.81 +
    1.82      window->title = NULL;
    1.83 -    window->flags =
    1.84 -        (flags &
    1.85 -         ~(SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED | SDL_WINDOW_SHOWN |
    1.86 -           SDL_WINDOW_INPUT_GRABBED));
    1.87 +    window->flags = (flags & allowed_flags);
    1.88  
    1.89 -    if (_this->CreateWindow && _this->CreateWindow(_this, window) < 0) {
    1.90 -        return -1;
    1.91 +    if (_this->CreateWindow && !(flags & SDL_WINDOW_FOREIGN)) {
    1.92 +        if (_this->CreateWindow(_this, window) < 0) {
    1.93 +            if (flags & SDL_WINDOW_OPENGL) {
    1.94 +                SDL_GL_UnloadLibrary();
    1.95 +            }
    1.96 +            return -1;
    1.97 +        }
    1.98      }
    1.99 +
   1.100      if (title) {
   1.101          SDL_SetWindowTitle(window->id, title);
   1.102          SDL_free(title);
   1.103 @@ -1352,6 +1386,9 @@
   1.104              if (_this->DestroyWindow) {
   1.105                  _this->DestroyWindow(_this, window);
   1.106              }
   1.107 +            if (window->flags & SDL_WINDOW_OPENGL) {
   1.108 +                SDL_GL_UnloadLibrary();
   1.109 +            }
   1.110              if (j != display->num_windows - 1) {
   1.111                  SDL_memcpy(&display->windows[i],
   1.112                             &display->windows[i + 1],
   1.113 @@ -1543,6 +1580,7 @@
   1.114  SDL_CreateTextureFromSurface(Uint32 format, SDL_Surface * surface)
   1.115  {
   1.116      SDL_TextureID textureID;
   1.117 +    Uint32 requested_format = format;
   1.118      SDL_PixelFormat *fmt;
   1.119      int bpp;
   1.120      Uint32 Rmask, Gmask, Bmask, Amask;
   1.121 @@ -1586,6 +1624,14 @@
   1.122      textureID =
   1.123          SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
   1.124                            surface->h);
   1.125 +    if (!textureID && !requested_format) {
   1.126 +        SDL_DisplayMode desktop_mode;
   1.127 +        SDL_GetDesktopDisplayMode(&desktop_mode);
   1.128 +        format = desktop_mode.format;
   1.129 +        textureID =
   1.130 +            SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
   1.131 +                              surface->h);
   1.132 +    }
   1.133      if (!textureID) {
   1.134          return 0;
   1.135      }
   1.136 @@ -2460,11 +2506,21 @@
   1.137          SDL_UninitializedVideo();
   1.138          return -1;
   1.139      }
   1.140 -    if (_this->GL_LoadLibrary) {
   1.141 +    if (_this->gl_config.driver_loaded) {
   1.142 +        if (path && SDL_strcmp(path, _this->gl_config.driver_path) != 0) {
   1.143 +            SDL_SetError("OpenGL library already loaded");
   1.144 +            return -1;
   1.145 +        }
   1.146 +        retval = 0;
   1.147 +    } else {
   1.148 +        if (!_this->GL_LoadLibrary) {
   1.149 +            SDL_SetError("No dynamic GL support in video driver");
   1.150 +            return -1;
   1.151 +        }
   1.152          retval = _this->GL_LoadLibrary(_this, path);
   1.153 -    } else {
   1.154 -        SDL_SetError("No dynamic GL support in video driver");
   1.155 -        retval = -1;
   1.156 +    }
   1.157 +    if (retval == 0) {
   1.158 +        ++_this->gl_config.driver_loaded;
   1.159      }
   1.160      return (retval);
   1.161  }
   1.162 @@ -2491,6 +2547,23 @@
   1.163      return func;
   1.164  }
   1.165  
   1.166 +void
   1.167 +SDL_GL_UnloadLibrary(void)
   1.168 +{
   1.169 +    if (!_this) {
   1.170 +        SDL_UninitializedVideo();
   1.171 +        return;
   1.172 +    }
   1.173 +    if (_this->gl_config.driver_loaded > 0) {
   1.174 +        if (--_this->gl_config.driver_loaded > 0) {
   1.175 +            return;
   1.176 +        }
   1.177 +        if (_this->GL_UnloadLibrary) {
   1.178 +            _this->GL_UnloadLibrary(_this);
   1.179 +        }
   1.180 +    }
   1.181 +}
   1.182 +
   1.183  SDL_bool
   1.184  SDL_GL_ExtensionSupported(const char *extension)
   1.185  {