src/SDL_compat.c
branchSDL-1.3
changeset 1670 eef792d31de8
parent 1669 9857d21967bb
child 1675 d33dcfc3fde7
     1.1 --- a/src/SDL_compat.c	Mon May 29 05:08:33 2006 +0000
     1.2 +++ b/src/SDL_compat.c	Wed Jun 07 16:10:28 2006 +0000
     1.3 @@ -53,6 +53,23 @@
     1.4      return NULL;
     1.5  }
     1.6  
     1.7 +const SDL_VideoInfo *
     1.8 +SDL_GetVideoInfo(void)
     1.9 +{
    1.10 +    static SDL_VideoInfo info;
    1.11 +
    1.12 +    /* Memory leak, compatibility code, who cares? */
    1.13 +    if (!info.vfmt && SDL_GetDesktopDisplayMode()) {
    1.14 +        int bpp;
    1.15 +        Uint32 Rmask, Gmask, Bmask, Amask;
    1.16 +
    1.17 +        SDL_PixelFormatEnumToMasks(SDL_GetDesktopDisplayMode()->format, &bpp,
    1.18 +                                   &Rmask, &Gmask, &Bmask, &Amask);
    1.19 +        info.vfmt = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
    1.20 +    }
    1.21 +    return &info;
    1.22 +}
    1.23 +
    1.24  int
    1.25  SDL_VideoModeOK(int width, int height, int bpp, Uint32 flags)
    1.26  {
    1.27 @@ -296,6 +313,91 @@
    1.28  }
    1.29  
    1.30  SDL_Surface *
    1.31 +SDL_CreateWindowSurface(SDL_WindowID windowID, Uint32 format, Uint32 flags)
    1.32 +{
    1.33 +    SDL_Window *window = SDL_GetWindowFromID(windowID);
    1.34 +    Uint32 black;
    1.35 +    SDL_Surface *surface;
    1.36 +
    1.37 +    if (!window) {
    1.38 +        return NULL;
    1.39 +    }
    1.40 +
    1.41 +    if (!_this->CreateWindowSurface) {
    1.42 +        return NULL;
    1.43 +    }
    1.44 +
    1.45 +    if (!window->surface) {
    1.46 +        _this->CreateWindowSurface(_this, window, flags);
    1.47 +        if (!window->surface) {
    1.48 +            return NULL;
    1.49 +        }
    1.50 +        window->surface->flags |= SDL_SCREEN_SURFACE;
    1.51 +
    1.52 +        /* If we have a palettized surface, create a default palette */
    1.53 +        if (window->surface->format->palette) {
    1.54 +            SDL_Color colors[256];
    1.55 +            SDL_PixelFormat *vf = window->surface->format;
    1.56 +            SDL_DitherColors(colors, vf->BitsPerPixel);
    1.57 +            SDL_SetColors(window->surface, colors, 0, vf->palette->ncolors);
    1.58 +        }
    1.59 +    }
    1.60 +    surface = window->surface;
    1.61 +
    1.62 +    if (window->shadow) {
    1.63 +        SDL_FreeSurface(window->shadow);
    1.64 +        window->shadow = NULL;
    1.65 +    }
    1.66 +
    1.67 +    /* Create a shadow surface if necessary */
    1.68 +    if ((!(flags & SDL_ANYFORMAT)
    1.69 +         && (format != SDL_GetCurrentDisplayMode()->format))
    1.70 +        || ((flags & SDL_HWPALETTE)
    1.71 +            && !(window->surface->flags & SDL_HWPALETTE))) {
    1.72 +        int bpp;
    1.73 +        Uint32 Rmask, Gmask, Bmask, Amask;
    1.74 +
    1.75 +        SDL_PixelFormatEnumToMasks(format, &bpp, &Amask, &Gmask, &Bmask,
    1.76 +                                   &Amask);
    1.77 +        window->shadow =
    1.78 +            SDL_CreateRGBSurface(SDL_SWSURFACE, surface->w, surface->h, bpp,
    1.79 +                                 Rmask, Gmask, Bmask, Amask);
    1.80 +        if (window->shadow == NULL) {
    1.81 +            return NULL;
    1.82 +        }
    1.83 +        window->shadow->flags |= SDL_SHADOW_SURFACE;
    1.84 +
    1.85 +        surface = window->shadow;
    1.86 +
    1.87 +        /* 8-bit shadow surfaces report that they have exclusive palette */
    1.88 +        if (surface->format->palette) {
    1.89 +            surface->flags |= SDL_HWPALETTE;
    1.90 +            if (format == SDL_GetCurrentDisplayMode()->format) {
    1.91 +                SDL_memcpy(surface->format->palette->colors,
    1.92 +                           window->surface->format->palette->colors,
    1.93 +                           window->surface->format->palette->ncolors *
    1.94 +                           sizeof(SDL_Color));
    1.95 +            } else {
    1.96 +                SDL_DitherColors(surface->format->palette->colors, bpp);
    1.97 +            }
    1.98 +        }
    1.99 +    }
   1.100 +
   1.101 +    /* Clear the surface for display */
   1.102 +    {
   1.103 +        Uint32 black = SDL_MapRGB(surface->format, 0, 0, 0);
   1.104 +        SDL_FillRect(surface, NULL, black);
   1.105 +        if (surface->flags & SDL_DOUBLEBUF) {
   1.106 +            SDL_Flip(surface);
   1.107 +            SDL_FillRect(surface, NULL, black);
   1.108 +        }
   1.109 +        SDL_Flip(surface);
   1.110 +    }
   1.111 +
   1.112 +    return surface;
   1.113 +}
   1.114 +
   1.115 +SDL_Surface *
   1.116  SDL_GetVideoSurface(void)
   1.117  {
   1.118      SDL_VideoDevice *_this = SDL_GetVideoDevice();
   1.119 @@ -303,6 +405,201 @@
   1.120      return SDL_VideoSurface;
   1.121  }
   1.122  
   1.123 +SDL_Surface *
   1.124 +SDL_DisplayFormat(SDL_Surface * surface)
   1.125 +{
   1.126 +    Uint32 flags;
   1.127 +
   1.128 +    if (!SDL_PublicSurface) {
   1.129 +        SDL_SetError("No video mode has been set");
   1.130 +        return (NULL);
   1.131 +    }
   1.132 +    /* Set the flags appropriate for copying to display surface */
   1.133 +    if (((SDL_PublicSurface->flags & SDL_HWSURFACE) == SDL_HWSURFACE)
   1.134 +        && _this->info.blit_hw)
   1.135 +        flags = SDL_HWSURFACE;
   1.136 +    else
   1.137 +        flags = SDL_SWSURFACE;
   1.138 +#ifdef AUTORLE_DISPLAYFORMAT
   1.139 +    flags |= (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA));
   1.140 +    flags |= SDL_RLEACCELOK;
   1.141 +#else
   1.142 +    flags |=
   1.143 +        surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA | SDL_RLEACCELOK);
   1.144 +#endif
   1.145 +    return (SDL_ConvertSurface(surface, SDL_PublicSurface->format, flags));
   1.146 +}
   1.147 +
   1.148 +SDL_Surface *
   1.149 +SDL_DisplayFormatAlpha(SDL_Surface * surface)
   1.150 +{
   1.151 +    SDL_PixelFormat *vf;
   1.152 +    SDL_PixelFormat *format;
   1.153 +    SDL_Surface *converted;
   1.154 +    Uint32 flags;
   1.155 +    /* default to ARGB8888 */
   1.156 +    Uint32 amask = 0xff000000;
   1.157 +    Uint32 rmask = 0x00ff0000;
   1.158 +    Uint32 gmask = 0x0000ff00;
   1.159 +    Uint32 bmask = 0x000000ff;
   1.160 +
   1.161 +    if (!SDL_PublicSurface) {
   1.162 +        SDL_SetError("No video mode has been set");
   1.163 +        return (NULL);
   1.164 +    }
   1.165 +    vf = SDL_PublicSurface->format;
   1.166 +
   1.167 +    switch (vf->BytesPerPixel) {
   1.168 +    case 2:
   1.169 +        /* For XGY5[56]5, use, AXGY8888, where {X, Y} = {R, B}.
   1.170 +           For anything else (like ARGB4444) it doesn't matter
   1.171 +           since we have no special code for it anyway */
   1.172 +        if ((vf->Rmask == 0x1f) &&
   1.173 +            (vf->Bmask == 0xf800 || vf->Bmask == 0x7c00)) {
   1.174 +            rmask = 0xff;
   1.175 +            bmask = 0xff0000;
   1.176 +        }
   1.177 +        break;
   1.178 +
   1.179 +    case 3:
   1.180 +    case 4:
   1.181 +        /* Keep the video format, as long as the high 8 bits are
   1.182 +           unused or alpha */
   1.183 +        if ((vf->Rmask == 0xff) && (vf->Bmask == 0xff0000)) {
   1.184 +            rmask = 0xff;
   1.185 +            bmask = 0xff0000;
   1.186 +        }
   1.187 +        break;
   1.188 +
   1.189 +    default:
   1.190 +        /* We have no other optimised formats right now. When/if a new
   1.191 +           optimised alpha format is written, add the converter here */
   1.192 +        break;
   1.193 +    }
   1.194 +    format = SDL_AllocFormat(32, rmask, gmask, bmask, amask);
   1.195 +    flags = SDL_PublicSurface->flags & SDL_HWSURFACE;
   1.196 +    flags |= surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
   1.197 +    converted = SDL_ConvertSurface(surface, format, flags);
   1.198 +    SDL_FreeFormat(format);
   1.199 +    return (converted);
   1.200 +}
   1.201 +
   1.202 +void
   1.203 +SDL_UpdateRect(SDL_Surface * screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h)
   1.204 +{
   1.205 +    if (screen) {
   1.206 +        SDL_Rect rect;
   1.207 +
   1.208 +        /* Perform some checking */
   1.209 +        if (w == 0)
   1.210 +            w = screen->w;
   1.211 +        if (h == 0)
   1.212 +            h = screen->h;
   1.213 +        if ((int) (x + w) > screen->w)
   1.214 +            return;
   1.215 +        if ((int) (y + h) > screen->h)
   1.216 +            return;
   1.217 +
   1.218 +        /* Fill the rectangle */
   1.219 +        rect.x = (Sint16) x;
   1.220 +        rect.y = (Sint16) y;
   1.221 +        rect.w = (Uint16) w;
   1.222 +        rect.h = (Uint16) h;
   1.223 +        SDL_UpdateRects(screen, 1, &rect);
   1.224 +    }
   1.225 +}
   1.226 +void
   1.227 +SDL_UpdateRects(SDL_Surface * screen, int numrects, SDL_Rect * rects)
   1.228 +{
   1.229 +    int i;
   1.230 +    SDL_Window *window;
   1.231 +
   1.232 +    /* Find the window corresponding to this surface */
   1.233 +    window = SDL_GetWindowFromSurface(screen);
   1.234 +    if (!window) {
   1.235 +        SDL_SetError("Couldn't find window associated with surface");
   1.236 +        return;
   1.237 +    }
   1.238 +
   1.239 +    if (screen->flags & SDL_SHADOW_SURFACE) {
   1.240 +        if (SHOULD_DRAWCURSOR(SDL_cursorstate)) {
   1.241 +            SDL_LockCursor();
   1.242 +            SDL_DrawCursor(screen);
   1.243 +            for (i = 0; i < numrects; ++i) {
   1.244 +                SDL_LowerBlit(screen, &rects[i], window->surface, &rects[i]);
   1.245 +            }
   1.246 +            SDL_EraseCursor(screen);
   1.247 +            SDL_UnlockCursor();
   1.248 +        } else {
   1.249 +            for (i = 0; i < numrects; ++i) {
   1.250 +                SDL_LowerBlit(screen, &rects[i], window->surface, &rects[i]);
   1.251 +            }
   1.252 +        }
   1.253 +
   1.254 +        /* Fall through to video surface update */
   1.255 +        screen = window->surface;
   1.256 +    }
   1.257 +    if ((screen->flags & SDL_SCREEN_SURFACE) && _this->UpdateWindowSurface) {
   1.258 +        /* Update the video surface */
   1.259 +        if (screen->offset) {
   1.260 +            int offset_y = screen->offset / screen->pitch;
   1.261 +            int offset_x = screen->offset % screen->pitch;
   1.262 +            for (i = 0; i < numrects; ++i) {
   1.263 +                rects[i].x += offset_x;
   1.264 +                rects[i].y += offset_y;
   1.265 +            }
   1.266 +            _this->UpdateWindowSurface(_this, window, numrects, rects);
   1.267 +            for (i = 0; i < numrects; ++i) {
   1.268 +                rects[i].x -= offset_x;
   1.269 +                rects[i].y -= offset_y;
   1.270 +            }
   1.271 +        } else {
   1.272 +            _this->UpdateWindowSurface(_this, window, numrects, rects);
   1.273 +        }
   1.274 +    }
   1.275 +}
   1.276 +
   1.277 +int
   1.278 +SDL_Flip(SDL_Surface * screen)
   1.279 +{
   1.280 +    SDL_Window *window;
   1.281 +
   1.282 +    /* Find the window corresponding to this surface */
   1.283 +    window = SDL_GetWindowFromSurface(screen);
   1.284 +    if (!window) {
   1.285 +        SDL_SetError("Couldn't find window associated with surface");
   1.286 +        return;
   1.287 +    }
   1.288 +
   1.289 +    /* Copy the shadow surface to the video surface */
   1.290 +    if (screen->flags & SDL_SHADOW_SURFACE) {
   1.291 +        SDL_Rect rect;
   1.292 +
   1.293 +        rect.x = 0;
   1.294 +        rect.y = 0;
   1.295 +        rect.w = screen->w;
   1.296 +        rect.h = screen->h;
   1.297 +        if (SHOULD_DRAWCURSOR(SDL_cursorstate)) {
   1.298 +            SDL_LockCursor();
   1.299 +            SDL_DrawCursor(screen);
   1.300 +            SDL_LowerBlit(screen, &rect, window->surface, &rect);
   1.301 +            SDL_EraseCursor(screen);
   1.302 +            SDL_UnlockCursor();
   1.303 +        } else {
   1.304 +            SDL_LowerBlit(screen, &rect, window->surface, &rect);
   1.305 +        }
   1.306 +
   1.307 +        /* Fall through to video surface update */
   1.308 +        screen = window->surface;
   1.309 +    }
   1.310 +    if (screen->flags & SDL_DOUBLEBUF) {
   1.311 +        _this->FlipWindowSurface(_this, window);
   1.312 +    } else {
   1.313 +        SDL_UpdateRect(screen, 0, 0, 0, 0);
   1.314 +    }
   1.315 +    return (0);
   1.316 +}
   1.317 +
   1.318  void
   1.319  SDL_WM_SetCaption(const char *title, const char *icon)
   1.320  {
   1.321 @@ -387,9 +684,555 @@
   1.322  }
   1.323  
   1.324  int
   1.325 +SDL_SetScreenColors(SDL_Surface * screen, SDL_Color * colors, int firstcolor,
   1.326 +                    int ncolors)
   1.327 +{
   1.328 +    SDL_Window *window = NULL;
   1.329 +    SDL_Palette *pal;
   1.330 +    int gotall;
   1.331 +    int palsize;
   1.332 +
   1.333 +    /* Verify the parameters */
   1.334 +    pal = screen->format->palette;
   1.335 +    if (!pal) {
   1.336 +        return 0;               /* not a palettized surface */
   1.337 +    }
   1.338 +    gotall = 1;
   1.339 +    palsize = 1 << screen->format->BitsPerPixel;
   1.340 +    if (ncolors > (palsize - firstcolor)) {
   1.341 +        ncolors = (palsize - firstcolor);
   1.342 +        gotall = 0;
   1.343 +    }
   1.344 +
   1.345 +    if (screen->flags & (SDL_SHADOW_SURFACE | SDL_SCREEN_SURFACE)) {
   1.346 +        window = SDL_GetWindowFromSurface(screen);
   1.347 +        if (!window) {
   1.348 +            return 0;
   1.349 +        }
   1.350 +    }
   1.351 +
   1.352 +    if (screen->flags & SDL_SHADOW_SURFACE) {
   1.353 +        SDL_Palette *vidpal;
   1.354 +
   1.355 +        vidpal = window->surface->format->palette;
   1.356 +        if (vidpal && vidpal->ncolors == pal->ncolors) {
   1.357 +            /* This is a shadow surface, and the physical
   1.358 +             * framebuffer is also indexed. Propagate the
   1.359 +             * changes to its logical palette so that
   1.360 +             * updates are always identity blits
   1.361 +             */
   1.362 +            SDL_memcpy(vidpal->colors + firstcolor, colors,
   1.363 +                       ncolors * sizeof(*colors));
   1.364 +        }
   1.365 +        if (window->surface->flags & SDL_HWPALETTE) {
   1.366 +            /* Set the physical palette */
   1.367 +            screen = window->surface;
   1.368 +        } else {
   1.369 +            SDL_UpdateRect(screen, 0, 0, 0, 0);
   1.370 +        }
   1.371 +    }
   1.372 +
   1.373 +    if (screen->flags & SDL_SCREEN_SURFACE) {
   1.374 +        if (_this->SetWindowColors) {
   1.375 +            gotall =
   1.376 +                _this->SetWindowColors(_this, window, firstcolor, ncolors,
   1.377 +                                       colors);
   1.378 +            if (!gotall) {
   1.379 +                /* The video flags shouldn't have SDL_HWPALETTE, and
   1.380 +                   the video driver is responsible for copying back the
   1.381 +                   correct colors into the video surface palette.
   1.382 +                 */
   1.383 +                ;
   1.384 +            }
   1.385 +        }
   1.386 +        SDL_CursorPaletteChanged();
   1.387 +    }
   1.388 +
   1.389 +    return gotall;
   1.390 +}
   1.391 +
   1.392 +int
   1.393  SDL_GetWMInfo(SDL_SysWMinfo * info)
   1.394  {
   1.395      return SDL_GetWindowWMInfo(window, info);
   1.396  }
   1.397  
   1.398 +void
   1.399 +SDL_MoveCursor(int x, int y)
   1.400 +{
   1.401 +    SDL_VideoDevice *_this = SDL_GetVideoDevice();
   1.402 +
   1.403 +    /* Erase and update the current mouse position */
   1.404 +    if (SHOULD_DRAWCURSOR(SDL_cursorstate)) {
   1.405 +        /* Erase and redraw mouse cursor in new position */
   1.406 +        SDL_LockCursor();
   1.407 +        SDL_EraseCursor(SDL_VideoSurface);
   1.408 +        SDL_cursor->area.x = (x - SDL_cursor->hot_x);
   1.409 +        SDL_cursor->area.y = (y - SDL_cursor->hot_y);
   1.410 +        SDL_DrawCursor(SDL_VideoSurface);
   1.411 +        SDL_UnlockCursor();
   1.412 +    } else if (_this->MoveWMCursor) {
   1.413 +        _this->MoveWMCursor(_this, x, y);
   1.414 +    }
   1.415 +}
   1.416 +
   1.417 +/* Keep track of the current cursor colors */
   1.418 +static int palette_changed = 1;
   1.419 +static Uint8 pixels8[2];
   1.420 +
   1.421 +void
   1.422 +SDL_CursorPaletteChanged(void)
   1.423 +{
   1.424 +    palette_changed = 1;
   1.425 +}
   1.426 +
   1.427 +void
   1.428 +SDL_MouseRect(SDL_Rect * area)
   1.429 +{
   1.430 +    SDL_VideoDevice *_this = SDL_GetVideoDevice();
   1.431 +    int clip_diff;
   1.432 +
   1.433 +    *area = SDL_cursor->area;
   1.434 +    if (area->x < 0) {
   1.435 +        area->w += area->x;
   1.436 +        area->x = 0;
   1.437 +    }
   1.438 +    if (area->y < 0) {
   1.439 +        area->h += area->y;
   1.440 +        area->y = 0;
   1.441 +    }
   1.442 +    clip_diff = (area->x + area->w) - SDL_VideoSurface->w;
   1.443 +    if (clip_diff > 0) {
   1.444 +        area->w = area->w < clip_diff ? 0 : area->w - clip_diff;
   1.445 +    }
   1.446 +    clip_diff = (area->y + area->h) - SDL_VideoSurface->h;
   1.447 +    if (clip_diff > 0) {
   1.448 +        area->h = area->h < clip_diff ? 0 : area->h - clip_diff;
   1.449 +    }
   1.450 +}
   1.451 +
   1.452 +static void
   1.453 +SDL_DrawCursorFast(SDL_Surface * screen, SDL_Rect * area)
   1.454 +{
   1.455 +    const Uint32 pixels[2] = { 0xFFFFFFFF, 0x00000000 };
   1.456 +    int i, w, h;
   1.457 +    Uint8 *data, datab;
   1.458 +    Uint8 *mask, maskb;
   1.459 +
   1.460 +    data = SDL_cursor->data + area->y * SDL_cursor->area.w / 8;
   1.461 +    mask = SDL_cursor->mask + area->y * SDL_cursor->area.w / 8;
   1.462 +    switch (screen->format->BytesPerPixel) {
   1.463 +
   1.464 +    case 1:
   1.465 +        {
   1.466 +            Uint8 *dst;
   1.467 +            int dstskip;
   1.468 +
   1.469 +            if (palette_changed) {
   1.470 +                pixels8[0] =
   1.471 +                    (Uint8) SDL_MapRGB(screen->format, 255, 255, 255);
   1.472 +                pixels8[1] = (Uint8) SDL_MapRGB(screen->format, 0, 0, 0);
   1.473 +                palette_changed = 0;
   1.474 +            }
   1.475 +            dst = (Uint8 *) screen->pixels +
   1.476 +                (SDL_cursor->area.y + area->y) * screen->pitch +
   1.477 +                SDL_cursor->area.x;
   1.478 +            dstskip = screen->pitch - area->w;
   1.479 +
   1.480 +            for (h = area->h; h; h--) {
   1.481 +                for (w = area->w / 8; w; w--) {
   1.482 +                    maskb = *mask++;
   1.483 +                    datab = *data++;
   1.484 +                    for (i = 0; i < 8; ++i) {
   1.485 +                        if (maskb & 0x80) {
   1.486 +                            *dst = pixels8[datab >> 7];
   1.487 +                        }
   1.488 +                        maskb <<= 1;
   1.489 +                        datab <<= 1;
   1.490 +                        dst++;
   1.491 +                    }
   1.492 +                }
   1.493 +                dst += dstskip;
   1.494 +            }
   1.495 +        }
   1.496 +        break;
   1.497 +
   1.498 +    case 2:
   1.499 +        {
   1.500 +            Uint16 *dst;
   1.501 +            int dstskip;
   1.502 +
   1.503 +            dst = (Uint16 *) screen->pixels +
   1.504 +                (SDL_cursor->area.y + area->y) * screen->pitch / 2 +
   1.505 +                SDL_cursor->area.x;
   1.506 +            dstskip = (screen->pitch / 2) - area->w;
   1.507 +
   1.508 +            for (h = area->h; h; h--) {
   1.509 +                for (w = area->w / 8; w; w--) {
   1.510 +                    maskb = *mask++;
   1.511 +                    datab = *data++;
   1.512 +                    for (i = 0; i < 8; ++i) {
   1.513 +                        if (maskb & 0x80) {
   1.514 +                            *dst = (Uint16) pixels[datab >> 7];
   1.515 +                        }
   1.516 +                        maskb <<= 1;
   1.517 +                        datab <<= 1;
   1.518 +                        dst++;
   1.519 +                    }
   1.520 +                }
   1.521 +                dst += dstskip;
   1.522 +            }
   1.523 +        }
   1.524 +        break;
   1.525 +
   1.526 +    case 3:
   1.527 +        {
   1.528 +            Uint8 *dst;
   1.529 +            int dstskip;
   1.530 +
   1.531 +            dst = (Uint8 *) screen->pixels +
   1.532 +                (SDL_cursor->area.y + area->y) * screen->pitch +
   1.533 +                SDL_cursor->area.x * 3;
   1.534 +            dstskip = screen->pitch - area->w * 3;
   1.535 +
   1.536 +            for (h = area->h; h; h--) {
   1.537 +                for (w = area->w / 8; w; w--) {
   1.538 +                    maskb = *mask++;
   1.539 +                    datab = *data++;
   1.540 +                    for (i = 0; i < 8; ++i) {
   1.541 +                        if (maskb & 0x80) {
   1.542 +                            SDL_memset(dst, pixels[datab >> 7], 3);
   1.543 +                        }
   1.544 +                        maskb <<= 1;
   1.545 +                        datab <<= 1;
   1.546 +                        dst += 3;
   1.547 +                    }
   1.548 +                }
   1.549 +                dst += dstskip;
   1.550 +            }
   1.551 +        }
   1.552 +        break;
   1.553 +
   1.554 +    case 4:
   1.555 +        {
   1.556 +            Uint32 *dst;
   1.557 +            int dstskip;
   1.558 +
   1.559 +            dst = (Uint32 *) screen->pixels +
   1.560 +                (SDL_cursor->area.y + area->y) * screen->pitch / 4 +
   1.561 +                SDL_cursor->area.x;
   1.562 +            dstskip = (screen->pitch / 4) - area->w;
   1.563 +
   1.564 +            for (h = area->h; h; h--) {
   1.565 +                for (w = area->w / 8; w; w--) {
   1.566 +                    maskb = *mask++;
   1.567 +                    datab = *data++;
   1.568 +                    for (i = 0; i < 8; ++i) {
   1.569 +                        if (maskb & 0x80) {
   1.570 +                            *dst = pixels[datab >> 7];
   1.571 +                        }
   1.572 +                        maskb <<= 1;
   1.573 +                        datab <<= 1;
   1.574 +                        dst++;
   1.575 +                    }
   1.576 +                }
   1.577 +                dst += dstskip;
   1.578 +            }
   1.579 +        }
   1.580 +        break;
   1.581 +    }
   1.582 +}
   1.583 +
   1.584 +static void
   1.585 +SDL_DrawCursorSlow(SDL_Surface * screen, SDL_Rect * area)
   1.586 +{
   1.587 +    const Uint32 pixels[2] = { 0xFFFFFF, 0x000000 };
   1.588 +    int h;
   1.589 +    int x, minx, maxx;
   1.590 +    Uint8 *data, datab = 0;
   1.591 +    Uint8 *mask, maskb = 0;
   1.592 +    Uint8 *dst;
   1.593 +    int dstbpp, dstskip;
   1.594 +
   1.595 +    data = SDL_cursor->data + area->y * SDL_cursor->area.w / 8;
   1.596 +    mask = SDL_cursor->mask + area->y * SDL_cursor->area.w / 8;
   1.597 +    dstbpp = screen->format->BytesPerPixel;
   1.598 +    dst = (Uint8 *) screen->pixels +
   1.599 +        (SDL_cursor->area.y + area->y) * screen->pitch +
   1.600 +        SDL_cursor->area.x * dstbpp;
   1.601 +    dstskip = screen->pitch - SDL_cursor->area.w * dstbpp;
   1.602 +
   1.603 +    minx = area->x;
   1.604 +    maxx = area->x + area->w;
   1.605 +    if (screen->format->BytesPerPixel == 1) {
   1.606 +        if (palette_changed) {
   1.607 +            pixels8[0] = (Uint8) SDL_MapRGB(screen->format, 255, 255, 255);
   1.608 +            pixels8[1] = (Uint8) SDL_MapRGB(screen->format, 0, 0, 0);
   1.609 +            palette_changed = 0;
   1.610 +        }
   1.611 +        for (h = area->h; h; h--) {
   1.612 +            for (x = 0; x < SDL_cursor->area.w; ++x) {
   1.613 +                if ((x % 8) == 0) {
   1.614 +                    maskb = *mask++;
   1.615 +                    datab = *data++;
   1.616 +                }
   1.617 +                if ((x >= minx) && (x < maxx)) {
   1.618 +                    if (maskb & 0x80) {
   1.619 +                        SDL_memset(dst, pixels8[datab >> 7], dstbpp);
   1.620 +                    }
   1.621 +                }
   1.622 +                maskb <<= 1;
   1.623 +                datab <<= 1;
   1.624 +                dst += dstbpp;
   1.625 +            }
   1.626 +            dst += dstskip;
   1.627 +        }
   1.628 +    } else {
   1.629 +        for (h = area->h; h; h--) {
   1.630 +            for (x = 0; x < SDL_cursor->area.w; ++x) {
   1.631 +                if ((x % 8) == 0) {
   1.632 +                    maskb = *mask++;
   1.633 +                    datab = *data++;
   1.634 +                }
   1.635 +                if ((x >= minx) && (x < maxx)) {
   1.636 +                    if (maskb & 0x80) {
   1.637 +                        SDL_memset(dst, pixels[datab >> 7], dstbpp);
   1.638 +                    }
   1.639 +                }
   1.640 +                maskb <<= 1;
   1.641 +                datab <<= 1;
   1.642 +                dst += dstbpp;
   1.643 +            }
   1.644 +            dst += dstskip;
   1.645 +        }
   1.646 +    }
   1.647 +}
   1.648 +
   1.649 +/* This handles the ugly work of converting the saved cursor background from
   1.650 +   the pixel format of the shadow surface to that of the video surface.
   1.651 +   This is only necessary when blitting from a shadow surface of a different
   1.652 +   pixel format than the video surface, and using a software rendered cursor.
   1.653 +*/
   1.654 +static void
   1.655 +SDL_ConvertCursorSave(SDL_Surface * screen, int w, int h)
   1.656 +{
   1.657 +    SDL_VideoDevice *_this = SDL_GetVideoDevice();
   1.658 +    SDL_BlitInfo info;
   1.659 +    SDL_loblit RunBlit;
   1.660 +
   1.661 +    /* Make sure we can steal the blit mapping */
   1.662 +    if (screen->map->dst != SDL_VideoSurface) {
   1.663 +        return;
   1.664 +    }
   1.665 +
   1.666 +    /* Set up the blit information */
   1.667 +    info.s_pixels = SDL_cursor->save[1];
   1.668 +    info.s_width = w;
   1.669 +    info.s_height = h;
   1.670 +    info.s_skip = 0;
   1.671 +    info.d_pixels = SDL_cursor->save[0];
   1.672 +    info.d_width = w;
   1.673 +    info.d_height = h;
   1.674 +    info.d_skip = 0;
   1.675 +    info.aux_data = screen->map->sw_data->aux_data;
   1.676 +    info.src = screen->format;
   1.677 +    info.table = screen->map->table;
   1.678 +    info.dst = SDL_VideoSurface->format;
   1.679 +    RunBlit = screen->map->sw_data->blit;
   1.680 +
   1.681 +    /* Run the actual software blit */
   1.682 +    RunBlit(&info);
   1.683 +}
   1.684 +
   1.685 +void
   1.686 +SDL_DrawCursorNoLock(SDL_Surface * screen)
   1.687 +{
   1.688 +    SDL_VideoDevice *_this = SDL_GetVideoDevice();
   1.689 +    SDL_Rect area;
   1.690 +
   1.691 +    /* Get the mouse rectangle, clipped to the screen */
   1.692 +    SDL_MouseRect(&area);
   1.693 +    if ((area.w == 0) || (area.h == 0)) {
   1.694 +        return;
   1.695 +    }
   1.696 +
   1.697 +    /* Copy mouse background */
   1.698 +    {
   1.699 +        int w, h, screenbpp;
   1.700 +        Uint8 *src, *dst;
   1.701 +
   1.702 +        /* Set up the copy pointers */
   1.703 +        screenbpp = screen->format->BytesPerPixel;
   1.704 +        if ((screen == SDL_VideoSurface) ||
   1.705 +            FORMAT_EQUAL(screen->format, SDL_VideoSurface->format)) {
   1.706 +            dst = SDL_cursor->save[0];
   1.707 +        } else {
   1.708 +            dst = SDL_cursor->save[1];
   1.709 +        }
   1.710 +        src = (Uint8 *) screen->pixels + area.y * screen->pitch +
   1.711 +            area.x * screenbpp;
   1.712 +
   1.713 +        /* Perform the copy */
   1.714 +        w = area.w * screenbpp;
   1.715 +        h = area.h;
   1.716 +        while (h--) {
   1.717 +            SDL_memcpy(dst, src, w);
   1.718 +            dst += w;
   1.719 +            src += screen->pitch;
   1.720 +        }
   1.721 +    }
   1.722 +
   1.723 +    /* Draw the mouse cursor */
   1.724 +    area.x -= SDL_cursor->area.x;
   1.725 +    area.y -= SDL_cursor->area.y;
   1.726 +    if ((area.x == 0) && (area.w == SDL_cursor->area.w)) {
   1.727 +        SDL_DrawCursorFast(screen, &area);
   1.728 +    } else {
   1.729 +        SDL_DrawCursorSlow(screen, &area);
   1.730 +    }
   1.731 +}
   1.732 +
   1.733 +void
   1.734 +SDL_DrawCursor(SDL_Surface * screen)
   1.735 +{
   1.736 +    /* Lock the screen if necessary */
   1.737 +    if (screen == NULL) {
   1.738 +        return;
   1.739 +    }
   1.740 +    if (SDL_MUSTLOCK(screen)) {
   1.741 +        if (SDL_LockSurface(screen) < 0) {
   1.742 +            return;
   1.743 +        }
   1.744 +    }
   1.745 +
   1.746 +    SDL_DrawCursorNoLock(screen);
   1.747 +
   1.748 +    /* Unlock the screen and update if necessary */
   1.749 +    if (SDL_MUSTLOCK(screen)) {
   1.750 +        SDL_UnlockSurface(screen);
   1.751 +    }
   1.752 +    if ((screen->flags & SDL_SCREEN_SURFACE) &&
   1.753 +        !(screen->flags & SDL_HWSURFACE)) {
   1.754 +        SDL_VideoDevice *_this = SDL_GetVideoDevice();
   1.755 +        SDL_Window *window;
   1.756 +        SDL_Rect area;
   1.757 +
   1.758 +        window = SDL_GetWindowFromSurface(screen);
   1.759 +        if (!window) {
   1.760 +            return;
   1.761 +        }
   1.762 +
   1.763 +        SDL_MouseRect(&area);
   1.764 +
   1.765 +        if (_this->UpdateWindowSurface) {
   1.766 +            _this->UpdateWindowSurface(_this, window, 1, &area);
   1.767 +        }
   1.768 +    }
   1.769 +}
   1.770 +
   1.771 +void
   1.772 +SDL_EraseCursorNoLock(SDL_Surface * screen)
   1.773 +{
   1.774 +    SDL_VideoDevice *_this = SDL_GetVideoDevice();
   1.775 +    SDL_Window *window;
   1.776 +    SDL_Rect area;
   1.777 +
   1.778 +    /* Get the window associated with the surface */
   1.779 +    window = SDL_GetWindowFromSurface(screen);
   1.780 +    if (!window || !window->surface) {
   1.781 +        return;
   1.782 +    }
   1.783 +
   1.784 +    /* Get the mouse rectangle, clipped to the screen */
   1.785 +    SDL_MouseRect(&area);
   1.786 +    if ((area.w == 0) || (area.h == 0)) {
   1.787 +        return;
   1.788 +    }
   1.789 +
   1.790 +    /* Copy mouse background */
   1.791 +    {
   1.792 +        int w, h, screenbpp;
   1.793 +        Uint8 *src, *dst;
   1.794 +
   1.795 +        /* Set up the copy pointers */
   1.796 +        screenbpp = screen->format->BytesPerPixel;
   1.797 +        if ((screen->flags & SDL_SCREEN_SURFACE) ||
   1.798 +            FORMAT_EQUAL(screen->format, window->surface->format)) {
   1.799 +            src = SDL_cursor->save[0];
   1.800 +        } else {
   1.801 +            src = SDL_cursor->save[1];
   1.802 +        }
   1.803 +        dst = (Uint8 *) screen->pixels + area.y * screen->pitch +
   1.804 +            area.x * screenbpp;
   1.805 +
   1.806 +        /* Perform the copy */
   1.807 +        w = area.w * screenbpp;
   1.808 +        h = area.h;
   1.809 +        while (h--) {
   1.810 +            SDL_memcpy(dst, src, w);
   1.811 +            src += w;
   1.812 +            dst += screen->pitch;
   1.813 +        }
   1.814 +
   1.815 +        /* Perform pixel conversion on cursor background */
   1.816 +        if (src > SDL_cursor->save[1]) {
   1.817 +            SDL_ConvertCursorSave(screen, area.w, area.h);
   1.818 +        }
   1.819 +    }
   1.820 +}
   1.821 +
   1.822 +void
   1.823 +SDL_EraseCursor(SDL_Surface * screen)
   1.824 +{
   1.825 +    /* Lock the screen if necessary */
   1.826 +    if (screen == NULL) {
   1.827 +        return;
   1.828 +    }
   1.829 +    if (SDL_MUSTLOCK(screen)) {
   1.830 +        if (SDL_LockSurface(screen) < 0) {
   1.831 +            return;
   1.832 +        }
   1.833 +    }
   1.834 +
   1.835 +    SDL_EraseCursorNoLock(screen);
   1.836 +
   1.837 +    /* Unlock the screen and update if necessary */
   1.838 +    if (SDL_MUSTLOCK(screen)) {
   1.839 +        SDL_UnlockSurface(screen);
   1.840 +    }
   1.841 +    if ((screen->flags & SDL_SCREEN_SURFACE) &&
   1.842 +        !(screen->flags & SDL_HWSURFACE)) {
   1.843 +        SDL_VideoDevice *_this = SDL_GetVideoDevice();
   1.844 +        SDL_Window *window;
   1.845 +        SDL_Rect area;
   1.846 +
   1.847 +        window = SDL_GetWindowFromSurface(screen);
   1.848 +        if (!window) {
   1.849 +            return;
   1.850 +        }
   1.851 +
   1.852 +        SDL_MouseRect(&area);
   1.853 +
   1.854 +        if (_this->UpdateWindowSurface) {
   1.855 +            _this->UpdateWindowSurface(_this, window, 1, &area);
   1.856 +        }
   1.857 +    }
   1.858 +}
   1.859 +
   1.860 +/* Reset the cursor on video mode change
   1.861 +   FIXME:  Keep track of all cursors, and reset them all.
   1.862 + */
   1.863 +void
   1.864 +SDL_ResetCursor(void)
   1.865 +{
   1.866 +    int savelen;
   1.867 +
   1.868 +    if (SDL_cursor) {
   1.869 +        savelen = SDL_cursor->area.w * 4 * SDL_cursor->area.h;
   1.870 +        SDL_cursor->area.x = 0;
   1.871 +        SDL_cursor->area.y = 0;
   1.872 +        SDL_memset(SDL_cursor->save[0], 0, savelen);
   1.873 +    }
   1.874 +}
   1.875 +
   1.876  /* vi: set ts=4 sw=4 expandtab: */