src/SDL_compat.c
branchSDL-1.3
changeset 1678 90bf530ced8e
parent 1675 d33dcfc3fde7
child 1680 9488fca10677
     1.1 --- a/src/SDL_compat.c	Tue Jun 13 04:37:45 2006 +0000
     1.2 +++ b/src/SDL_compat.c	Wed Jun 14 06:14:26 2006 +0000
     1.3 @@ -25,10 +25,14 @@
     1.4  
     1.5  #include "SDL.h"
     1.6  
     1.7 -#include "video/SDL_sysvideo.h"
     1.8 +#include "video/SDL_pixels_c.h"
     1.9  
    1.10  
    1.11 -static SDL_WindowID window;
    1.12 +static SDL_WindowID SDL_VideoWindow;
    1.13 +static SDL_TextureID SDL_VideoTexture;
    1.14 +static SDL_Surface *SDL_VideoSurface;
    1.15 +static SDL_Surface *SDL_ShadowSurface;
    1.16 +static SDL_Surface *SDL_PublicSurface;
    1.17  static char *wm_title;
    1.18  
    1.19  char *
    1.20 @@ -213,6 +217,7 @@
    1.21      Uint32 window_flags;
    1.22      Uint32 desktop_format;
    1.23      Uint32 desired_format;
    1.24 +    Uint32 texture_format;
    1.25  
    1.26      if (!SDL_GetVideoDevice()) {
    1.27          if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
    1.28 @@ -221,7 +226,16 @@
    1.29      }
    1.30  
    1.31      /* Destroy existing window */
    1.32 -    SDL_DestroyWindow(window);
    1.33 +    SDL_PublicSurface = NULL;
    1.34 +    if (SDL_ShadowSurface) {
    1.35 +        SDL_FreeSurface(SDL_ShadowSurface);
    1.36 +        SDL_ShadowSurface = NULL;
    1.37 +    }
    1.38 +    if (SDL_VideoSurface) {
    1.39 +        SDL_FreeSurface(SDL_ShadowSurface);
    1.40 +        SDL_VideoSurface = NULL;
    1.41 +    }
    1.42 +    SDL_DestroyWindow(SDL_VideoWindow);
    1.43  
    1.44      /* Set up the event filter */
    1.45      filter = SDL_GetEventFilter();
    1.46 @@ -244,8 +258,9 @@
    1.47      if (flags & SDL_NOFRAME) {
    1.48          window_flags |= SDL_WINDOW_BORDERLESS;
    1.49      }
    1.50 -    window = SDL_CreateWindow(wm_title, 0, 0, width, height, window_flags);
    1.51 -    if (!window) {
    1.52 +    SDL_VideoWindow =
    1.53 +        SDL_CreateWindow(wm_title, 0, 0, width, height, window_flags);
    1.54 +    if (!SDL_VideoWindow) {
    1.55          return NULL;
    1.56      }
    1.57  
    1.58 @@ -308,101 +323,82 @@
    1.59          return NULL;
    1.60      }
    1.61  
    1.62 -    /* Create the display surface */
    1.63 -    return SDL_CreateWindowSurface(window, desired_format, flags);
    1.64 -}
    1.65 -
    1.66 -SDL_Surface *
    1.67 -SDL_CreateWindowSurface(SDL_WindowID windowID, Uint32 format, Uint32 flags)
    1.68 -{
    1.69 -    SDL_Window *window = SDL_GetWindowFromID(windowID);
    1.70 -    Uint32 black;
    1.71 -    SDL_Surface *surface;
    1.72 -
    1.73 -    if (!window) {
    1.74 +    /* Create a renderer for the window */
    1.75 +    if (SDL_CreateRenderer(SDL_VideoWindow, -1, 0) < 0) {
    1.76          return NULL;
    1.77      }
    1.78  
    1.79 -    if (!_this->CreateWindowSurface) {
    1.80 +    /* Create a texture for the screen surface */
    1.81 +    SDL_VideoTexture =
    1.82 +        SDL_CreateTexture(desired_format, SDL_TextureAccess_Local, width,
    1.83 +                          height);
    1.84 +    if (!SDL_VideoTexture) {
    1.85 +        SDL_VideoTexture =
    1.86 +            SDL_CreateTexture(0, SDL_TextureAccess_Local, width, height);
    1.87 +    }
    1.88 +    if (!SDL_VideoTexture) {
    1.89          return NULL;
    1.90      }
    1.91  
    1.92 -    if (!window->surface) {
    1.93 -        _this->CreateWindowSurface(_this, window, flags);
    1.94 -        if (!window->surface) {
    1.95 -            return NULL;
    1.96 -        }
    1.97 -        window->surface->flags |= SDL_SCREEN_SURFACE;
    1.98 +    /* Create the screen surface */
    1.99 +    SDL_VideoSurface = SDL_CreateRGBSurfaceFromTexture(SDL_VideoTexture);
   1.100 +    if (!SDL_VideoSurface) {
   1.101 +        return NULL;
   1.102 +    }
   1.103  
   1.104 -        /* If we have a palettized surface, create a default palette */
   1.105 -        if (window->surface->format->palette) {
   1.106 -            SDL_Color colors[256];
   1.107 -            SDL_PixelFormat *vf = window->surface->format;
   1.108 -            SDL_DitherColors(colors, vf->BitsPerPixel);
   1.109 -            SDL_SetColors(window->surface, colors, 0, vf->palette->ncolors);
   1.110 -        }
   1.111 -    }
   1.112 -    surface = window->surface;
   1.113 -
   1.114 -    if (window->shadow) {
   1.115 -        SDL_FreeSurface(window->shadow);
   1.116 -        window->shadow = NULL;
   1.117 +    /* Set a default screen palette */
   1.118 +    if (SDL_VideoSurface->format->palette) {
   1.119 +        SDL_VideoSurface->flags |= SDL_HWPALETTE;
   1.120 +        SDL_DitherColors(SDL_VideoSurface->format->palette->colors,
   1.121 +                         SDL_VideoSurface->format->BitsPerPixel);
   1.122 +        SDL_SetTexturePalette(SDL_VideoTexture,
   1.123 +                              SDL_VideoSurface->format->palette->colors, 0,
   1.124 +                              SDL_VideoSurface->format->palette->ncolors);
   1.125      }
   1.126  
   1.127      /* Create a shadow surface if necessary */
   1.128 -    if ((!(flags & SDL_ANYFORMAT)
   1.129 -         && (format != SDL_GetCurrentDisplayMode()->format))
   1.130 -        || ((flags & SDL_HWPALETTE)
   1.131 -            && !(window->surface->flags & SDL_HWPALETTE))) {
   1.132 -        int bpp;
   1.133 -        Uint32 Rmask, Gmask, Bmask, Amask;
   1.134 -
   1.135 -        SDL_PixelFormatEnumToMasks(format, &bpp, &Amask, &Gmask, &Bmask,
   1.136 -                                   &Amask);
   1.137 -        window->shadow =
   1.138 -            SDL_CreateRGBSurface(SDL_SWSURFACE, surface->w, surface->h, bpp,
   1.139 -                                 Rmask, Gmask, Bmask, Amask);
   1.140 -        if (window->shadow == NULL) {
   1.141 +    if (((bpp != SDL_VideoSurface->format->BitsPerPixel)
   1.142 +         && !(flags & SDL_ANYFORMAT))
   1.143 +        || ((SDL_VideoSurface->flags & SDL_HWSURFACE)
   1.144 +            && !(flags & SDL_HWSURFACE))) {
   1.145 +        if ((bpp == SDL_VideoSurface->format->BitsPerPixel)
   1.146 +            || (flags & SDL_ANYFORMAT)) {
   1.147 +            SDL_ShadowSurface =
   1.148 +                SDL_CreateRGBSurface(0, width, height,
   1.149 +                                     SDL_VideoSurface->format->BitsPerPixel,
   1.150 +                                     SDL_VideoSurface->format->Rmask,
   1.151 +                                     SDL_VideoSurface->format->Gmask,
   1.152 +                                     SDL_VideoSurface->format->Bmask,
   1.153 +                                     SDL_VideoSurface->format->Amask);
   1.154 +        } else {
   1.155 +            SDL_ShadowSurface =
   1.156 +                SDL_CreateRGBSurface(0, width, height, bpp, 0, 0, 0, 0);
   1.157 +        }
   1.158 +        if (!SDL_ShadowSurface) {
   1.159              return NULL;
   1.160          }
   1.161 -        window->shadow->flags |= SDL_SHADOW_SURFACE;
   1.162  
   1.163 -        surface = window->shadow;
   1.164 -
   1.165 -        /* 8-bit shadow surfaces report that they have exclusive palette */
   1.166 -        if (surface->format->palette) {
   1.167 -            surface->flags |= SDL_HWPALETTE;
   1.168 -            if (format == SDL_GetCurrentDisplayMode()->format) {
   1.169 -                SDL_memcpy(surface->format->palette->colors,
   1.170 -                           window->surface->format->palette->colors,
   1.171 -                           window->surface->format->palette->ncolors *
   1.172 -                           sizeof(SDL_Color));
   1.173 -            } else {
   1.174 -                SDL_DitherColors(surface->format->palette->colors, bpp);
   1.175 -            }
   1.176 +        /* 8-bit SDL_ShadowSurface surfaces report that they have exclusive palette */
   1.177 +        if (SDL_ShadowSurface->format->palette) {
   1.178 +            SDL_ShadowSurface->flags |= SDL_HWPALETTE;
   1.179 +            SDL_DitherColors(SDL_ShadowSurface->format->palette->colors,
   1.180 +                             SDL_ShadowSurface->format->BitsPerPixel);
   1.181          }
   1.182      }
   1.183 +    SDL_PublicSurface =
   1.184 +        (SDL_ShadowSurface ? SDL_ShadowSurface : SDL_VideoSurface);
   1.185  
   1.186      /* Clear the surface for display */
   1.187 -    {
   1.188 -        Uint32 black = SDL_MapRGB(surface->format, 0, 0, 0);
   1.189 -        SDL_FillRect(surface, NULL, black);
   1.190 -        if (surface->flags & SDL_DOUBLEBUF) {
   1.191 -            SDL_Flip(surface);
   1.192 -            SDL_FillRect(surface, NULL, black);
   1.193 -        }
   1.194 -        SDL_Flip(surface);
   1.195 -    }
   1.196 +    SDL_FillRect(SDL_PublicSurface, NULL, 0);
   1.197  
   1.198 -    return surface;
   1.199 +    /* We're finally done! */
   1.200 +    return SDL_PublicSurface;
   1.201  }
   1.202  
   1.203  SDL_Surface *
   1.204  SDL_GetVideoSurface(void)
   1.205  {
   1.206 -    SDL_VideoDevice *_this = SDL_GetVideoDevice();
   1.207 -
   1.208 -    return SDL_VideoSurface;
   1.209 +    return SDL_PublicSurface;
   1.210  }
   1.211  
   1.212  SDL_Surface *
   1.213 @@ -412,14 +408,11 @@
   1.214  
   1.215      if (!SDL_PublicSurface) {
   1.216          SDL_SetError("No video mode has been set");
   1.217 -        return (NULL);
   1.218 +        return NULL;
   1.219      }
   1.220 +
   1.221      /* Set the flags appropriate for copying to display surface */
   1.222 -    if (((SDL_PublicSurface->flags & SDL_HWSURFACE) == SDL_HWSURFACE)
   1.223 -        && _this->info.blit_hw)
   1.224 -        flags = SDL_HWSURFACE;
   1.225 -    else
   1.226 -        flags = SDL_SWSURFACE;
   1.227 +    flags = SDL_SWSURFACE;
   1.228  #ifdef AUTORLE_DISPLAYFORMAT
   1.229      flags |= (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA));
   1.230      flags |= SDL_RLEACCELOK;
   1.231 @@ -427,7 +420,7 @@
   1.232      flags |=
   1.233          surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA | SDL_RLEACCELOK);
   1.234  #endif
   1.235 -    return (SDL_ConvertSurface(surface, SDL_PublicSurface->format, flags));
   1.236 +    return SDL_ConvertSurface(surface, SDL_PublicSurface->format, flags);
   1.237  }
   1.238  
   1.239  SDL_Surface *
   1.240 @@ -445,7 +438,7 @@
   1.241  
   1.242      if (!SDL_PublicSurface) {
   1.243          SDL_SetError("No video mode has been set");
   1.244 -        return (NULL);
   1.245 +        return NULL;
   1.246      }
   1.247      vf = SDL_PublicSurface->format;
   1.248  
   1.249 @@ -481,7 +474,14 @@
   1.250      flags |= surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
   1.251      converted = SDL_ConvertSurface(surface, format, flags);
   1.252      SDL_FreeFormat(format);
   1.253 -    return (converted);
   1.254 +    return converted;
   1.255 +}
   1.256 +
   1.257 +int
   1.258 +SDL_Flip(SDL_Surface * screen)
   1.259 +{
   1.260 +    SDL_UpdateRect(screen, 0, 0, 0, 0);
   1.261 +    return 0;
   1.262  }
   1.263  
   1.264  void
   1.265 @@ -512,94 +512,26 @@
   1.266  SDL_UpdateRects(SDL_Surface * screen, int numrects, SDL_Rect * rects)
   1.267  {
   1.268      int i;
   1.269 -    SDL_Window *window;
   1.270  
   1.271 -    /* Find the window corresponding to this surface */
   1.272 -    window = SDL_GetWindowFromSurface(screen);
   1.273 -    if (!window) {
   1.274 -        SDL_SetError("Couldn't find window associated with surface");
   1.275 -        return;
   1.276 -    }
   1.277 -
   1.278 -    if (screen->flags & SDL_SHADOW_SURFACE) {
   1.279 -        if (SHOULD_DRAWCURSOR(SDL_cursorstate)) {
   1.280 -            SDL_LockCursor();
   1.281 -            SDL_DrawCursor(screen);
   1.282 -            for (i = 0; i < numrects; ++i) {
   1.283 -                SDL_LowerBlit(screen, &rects[i], window->surface, &rects[i]);
   1.284 -            }
   1.285 -            SDL_EraseCursor(screen);
   1.286 -            SDL_UnlockCursor();
   1.287 -        } else {
   1.288 -            for (i = 0; i < numrects; ++i) {
   1.289 -                SDL_LowerBlit(screen, &rects[i], window->surface, &rects[i]);
   1.290 -            }
   1.291 +    if (screen == SDL_ShadowSurface) {
   1.292 +        for (i = 0; i < numrects; ++i) {
   1.293 +            SDL_LowerBlit(SDL_ShadowSurface, &rects[i], SDL_VideoSurface,
   1.294 +                          &rects[i]);
   1.295          }
   1.296  
   1.297          /* Fall through to video surface update */
   1.298 -        screen = window->surface;
   1.299 +        screen = SDL_VideoSurface;
   1.300      }
   1.301 -    if ((screen->flags & SDL_SCREEN_SURFACE) && _this->UpdateWindowSurface) {
   1.302 -        /* Update the video surface */
   1.303 -        if (screen->offset) {
   1.304 -            int offset_y = screen->offset / screen->pitch;
   1.305 -            int offset_x = screen->offset % screen->pitch;
   1.306 -            for (i = 0; i < numrects; ++i) {
   1.307 -                rects[i].x += offset_x;
   1.308 -                rects[i].y += offset_y;
   1.309 -            }
   1.310 -            _this->UpdateWindowSurface(_this, window, numrects, rects);
   1.311 -            for (i = 0; i < numrects; ++i) {
   1.312 -                rects[i].x -= offset_x;
   1.313 -                rects[i].y -= offset_y;
   1.314 -            }
   1.315 -        } else {
   1.316 -            _this->UpdateWindowSurface(_this, window, numrects, rects);
   1.317 +    if (screen == SDL_VideoSurface) {
   1.318 +        for (i = 0; i < numrects; ++i) {
   1.319 +            SDL_RenderCopy(SDL_VideoTexture, &rects[i], &rects[i],
   1.320 +                           SDL_TextureBlendMode_None,
   1.321 +                           SDL_TextureScaleMode_None);
   1.322          }
   1.323 +        SDL_RenderPresent();
   1.324      }
   1.325  }
   1.326  
   1.327 -int
   1.328 -SDL_Flip(SDL_Surface * screen)
   1.329 -{
   1.330 -    SDL_Window *window;
   1.331 -
   1.332 -    /* Find the window corresponding to this surface */
   1.333 -    window = SDL_GetWindowFromSurface(screen);
   1.334 -    if (!window) {
   1.335 -        SDL_SetError("Couldn't find window associated with surface");
   1.336 -        return;
   1.337 -    }
   1.338 -
   1.339 -    /* Copy the shadow surface to the video surface */
   1.340 -    if (screen->flags & SDL_SHADOW_SURFACE) {
   1.341 -        SDL_Rect rect;
   1.342 -
   1.343 -        rect.x = 0;
   1.344 -        rect.y = 0;
   1.345 -        rect.w = screen->w;
   1.346 -        rect.h = screen->h;
   1.347 -        if (SHOULD_DRAWCURSOR(SDL_cursorstate)) {
   1.348 -            SDL_LockCursor();
   1.349 -            SDL_DrawCursor(screen);
   1.350 -            SDL_LowerBlit(screen, &rect, window->surface, &rect);
   1.351 -            SDL_EraseCursor(screen);
   1.352 -            SDL_UnlockCursor();
   1.353 -        } else {
   1.354 -            SDL_LowerBlit(screen, &rect, window->surface, &rect);
   1.355 -        }
   1.356 -
   1.357 -        /* Fall through to video surface update */
   1.358 -        screen = window->surface;
   1.359 -    }
   1.360 -    if (screen->flags & SDL_DOUBLEBUF) {
   1.361 -        _this->FlipWindowSurface(_this, window);
   1.362 -    } else {
   1.363 -        SDL_UpdateRect(screen, 0, 0, 0, 0);
   1.364 -    }
   1.365 -    return (0);
   1.366 -}
   1.367 -
   1.368  void
   1.369  SDL_WM_SetCaption(const char *title, const char *icon)
   1.370  {
   1.371 @@ -608,7 +540,7 @@
   1.372      } else {
   1.373          wm_title = SDL_strdup(title);
   1.374      }
   1.375 -    SDL_SetWindowTitle(window, wm_title);
   1.376 +    SDL_SetWindowTitle(SDL_VideoWindow, wm_title);
   1.377  }
   1.378  
   1.379  void
   1.380 @@ -631,7 +563,7 @@
   1.381  int
   1.382  SDL_WM_IconifyWindow(void)
   1.383  {
   1.384 -    SDL_MinimizeWindow(window);
   1.385 +    SDL_MinimizeWindow(SDL_VideoWindow);
   1.386  }
   1.387  
   1.388  int
   1.389 @@ -644,9 +576,9 @@
   1.390  SDL_WM_GrabInput(SDL_GrabMode mode)
   1.391  {
   1.392      if (mode != SDL_GRAB_QUERY) {
   1.393 -        SDL_SetWindowGrab(window, mode);
   1.394 +        SDL_SetWindowGrab(SDL_VideoWindow, mode);
   1.395      }
   1.396 -    return (SDL_GrabMode) SDL_GetWindowGrab(window);
   1.397 +    return (SDL_GrabMode) SDL_GetWindowGrab(SDL_VideoWindow);
   1.398  }
   1.399  
   1.400  Uint8
   1.401 @@ -655,7 +587,7 @@
   1.402      Uint8 state = 0;
   1.403      Uint32 flags = 0;
   1.404  
   1.405 -    flags = SDL_GetWindowFlags(window);
   1.406 +    flags = SDL_GetWindowFlags(SDL_VideoWindow);
   1.407      if ((flags & SDL_WINDOW_SHOWN) && !(flags & SDL_WINDOW_MINIMIZED)) {
   1.408          state |= SDL_APPACTIVE;
   1.409      }
   1.410 @@ -687,7 +619,6 @@
   1.411  SDL_SetScreenColors(SDL_Surface * screen, SDL_Color * colors, int firstcolor,
   1.412                      int ncolors)
   1.413  {
   1.414 -    SDL_Window *window = NULL;
   1.415      SDL_Palette *pal;
   1.416      int gotall;
   1.417      int palsize;
   1.418 @@ -704,17 +635,10 @@
   1.419          gotall = 0;
   1.420      }
   1.421  
   1.422 -    if (screen->flags & (SDL_SHADOW_SURFACE | SDL_SCREEN_SURFACE)) {
   1.423 -        window = SDL_GetWindowFromSurface(screen);
   1.424 -        if (!window) {
   1.425 -            return 0;
   1.426 -        }
   1.427 -    }
   1.428 -
   1.429 -    if (screen->flags & SDL_SHADOW_SURFACE) {
   1.430 +    if (screen == SDL_ShadowSurface) {
   1.431          SDL_Palette *vidpal;
   1.432  
   1.433 -        vidpal = window->surface->format->palette;
   1.434 +        vidpal = SDL_VideoSurface->format->palette;
   1.435          if (vidpal && vidpal->ncolors == pal->ncolors) {
   1.436              /* This is a shadow surface, and the physical
   1.437               * framebuffer is also indexed. Propagate the
   1.438 @@ -724,28 +648,18 @@
   1.439              SDL_memcpy(vidpal->colors + firstcolor, colors,
   1.440                         ncolors * sizeof(*colors));
   1.441          }
   1.442 -        if (window->surface->flags & SDL_HWPALETTE) {
   1.443 +        if (SDL_VideoSurface->flags & SDL_HWPALETTE) {
   1.444              /* Set the physical palette */
   1.445 -            screen = window->surface;
   1.446 +            screen = SDL_VideoSurface;
   1.447          } else {
   1.448              SDL_UpdateRect(screen, 0, 0, 0, 0);
   1.449          }
   1.450      }
   1.451  
   1.452 -    if (screen->flags & SDL_SCREEN_SURFACE) {
   1.453 -        if (_this->SetWindowColors) {
   1.454 -            gotall =
   1.455 -                _this->SetWindowColors(_this, window, firstcolor, ncolors,
   1.456 -                                       colors);
   1.457 -            if (!gotall) {
   1.458 -                /* The video flags shouldn't have SDL_HWPALETTE, and
   1.459 -                   the video driver is responsible for copying back the
   1.460 -                   correct colors into the video surface palette.
   1.461 -                 */
   1.462 -                ;
   1.463 -            }
   1.464 -        }
   1.465 -        SDL_CursorPaletteChanged();
   1.466 +    if (screen == SDL_VideoSurface) {
   1.467 +        SDL_SetTexturePalette(SDL_VideoTexture,
   1.468 +                              SDL_VideoSurface->format->palette->colors, 0,
   1.469 +                              SDL_VideoSurface->format->palette->ncolors);
   1.470      }
   1.471  
   1.472      return gotall;
   1.473 @@ -754,9 +668,10 @@
   1.474  int
   1.475  SDL_GetWMInfo(SDL_SysWMinfo * info)
   1.476  {
   1.477 -    return SDL_GetWindowWMInfo(window, info);
   1.478 +    return SDL_GetWindowWMInfo(SDL_VideoWindow, info);
   1.479  }
   1.480  
   1.481 +#if 0
   1.482  void
   1.483  SDL_MoveCursor(int x, int y)
   1.484  {
   1.485 @@ -1349,5 +1264,6 @@
   1.486          SDL_free(overlay);
   1.487      }
   1.488  }
   1.489 +#endif
   1.490  
   1.491  /* vi: set ts=4 sw=4 expandtab: */