src/video/SDL_surface.c
changeset 2266 e61ad15a205f
parent 2260 202ddfd1cfb1
child 2267 c785543d1843
     1.1 --- a/src/video/SDL_surface.c	Fri Aug 17 06:58:20 2007 +0000
     1.2 +++ b/src/video/SDL_surface.c	Sat Aug 18 01:44:21 2007 +0000
     1.3 @@ -212,153 +212,251 @@
     1.4      return 0;
     1.5  }
     1.6  
     1.7 -/*
     1.8 - * Set the color key in a blittable surface
     1.9 - */
    1.10 -int
    1.11 -SDL_SetColorKey(SDL_Surface * surface, Uint32 flag, Uint32 key)
    1.12 +int SDL_SetSurfaceRLE(SDL_Surface *surface, int flag)
    1.13  {
    1.14 -    /* Sanity check the flag as it gets passed in */
    1.15 -    if (flag & SDL_SRCCOLORKEY) {
    1.16 -        if (flag & (SDL_RLEACCEL | SDL_RLEACCELOK)) {
    1.17 -            flag = (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
    1.18 -        } else {
    1.19 -            flag = SDL_SRCCOLORKEY;
    1.20 -        }
    1.21 -    } else {
    1.22 -        flag = 0;
    1.23 -    }
    1.24 +    Uint32 flags;
    1.25  
    1.26 -    /* Optimize away operations that don't change anything */
    1.27 -    if ((flag == (surface->flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK))) &&
    1.28 -        (key == surface->map->ckey)) {
    1.29 -        return (0);
    1.30 -    }
    1.31 -
    1.32 -    /* UnRLE surfaces before we change the colorkey */
    1.33 -    if (surface->flags & SDL_RLEACCEL) {
    1.34 -        SDL_UnRLESurface(surface, 1);
    1.35 +    if (!surface) {
    1.36 +        return -1;
    1.37      }
    1.38  
    1.39      if (flag) {
    1.40 -        surface->flags |= SDL_SRCCOLORKEY;
    1.41 -        surface->map->ckey = key;
    1.42 -        if (flag & SDL_RLEACCELOK) {
    1.43 -            surface->flags |= SDL_RLEACCELOK;
    1.44 -        } else {
    1.45 -            surface->flags &= ~SDL_RLEACCELOK;
    1.46 -        }
    1.47 +        surface->flags |= SDL_RLEACCELOK;
    1.48      } else {
    1.49 -        surface->flags &= ~(SDL_SRCCOLORKEY | SDL_RLEACCELOK);
    1.50 -        surface->map->ckey = 0;
    1.51 +        surface->flags &= ~SDL_RLEACCELOK;
    1.52      }
    1.53 -    SDL_InvalidateMap(surface->map);
    1.54 -    return (0);
    1.55 -}
    1.56 -
    1.57 -/* This function sets the alpha channel of a surface */
    1.58 -int
    1.59 -SDL_SetAlpha(SDL_Surface * surface, Uint32 flag, Uint8 value)
    1.60 -{
    1.61 -    Uint32 oldflags = surface->flags;
    1.62 -    Uint32 oldalpha = (surface->map->cmod >> 24);
    1.63 -
    1.64 -    /* Sanity check the flag as it gets passed in */
    1.65 -    if (flag & SDL_SRCALPHA) {
    1.66 -        if (flag & (SDL_RLEACCEL | SDL_RLEACCELOK)) {
    1.67 -            flag = (SDL_SRCALPHA | SDL_RLEACCELOK);
    1.68 -        } else {
    1.69 -            flag = SDL_SRCALPHA;
    1.70 -        }
    1.71 -    } else {
    1.72 -        flag = 0;
    1.73 -    }
    1.74 -
    1.75 -    /* Optimize away operations that don't change anything */
    1.76 -    if ((flag == (surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK))) &&
    1.77 -        (!flag || value == oldalpha)) {
    1.78 -        return (0);
    1.79 -    }
    1.80 -
    1.81 -    if (!(flag & SDL_RLEACCELOK) && (surface->flags & SDL_RLEACCEL))
    1.82 -        SDL_UnRLESurface(surface, 1);
    1.83 -
    1.84 -    if (flag) {
    1.85 -        surface->flags |= SDL_SRCALPHA;
    1.86 -        surface->map->cmod &= 0x00FFFFFF;
    1.87 -        surface->map->cmod |= ((Uint32) value << 24);
    1.88 -        if (flag & SDL_RLEACCELOK) {
    1.89 -            surface->flags |= SDL_RLEACCELOK;
    1.90 -        } else {
    1.91 -            surface->flags &= ~SDL_RLEACCELOK;
    1.92 -        }
    1.93 -    } else {
    1.94 -        surface->flags &= ~SDL_SRCALPHA;
    1.95 -        surface->map->cmod |= 0xFF000000;
    1.96 -    }
    1.97 -    /*
    1.98 -     * The representation for software surfaces is independent of
    1.99 -     * per-surface alpha, so no need to invalidate the blit mapping
   1.100 -     * if just the alpha value was changed. (If either is 255, we still
   1.101 -     * need to invalidate.)
   1.102 -     */
   1.103 -    if (oldflags != surface->flags
   1.104 -        || (((oldalpha + 1) ^ (value + 1)) & 0x100)) {
   1.105 +    if (surface->flags != flags) {
   1.106          SDL_InvalidateMap(surface->map);
   1.107      }
   1.108 -    return (0);
   1.109 -}
   1.110 -
   1.111 -int
   1.112 -SDL_SetAlphaChannel(SDL_Surface * surface, Uint8 value)
   1.113 -{
   1.114 -    int row, col;
   1.115 -    int offset;
   1.116 -    Uint8 *buf;
   1.117 -
   1.118 -    if ((surface->format->Amask != 0xFF000000) &&
   1.119 -        (surface->format->Amask != 0x000000FF)) {
   1.120 -        SDL_SetError("Unsupported surface alpha mask format");
   1.121 -        return -1;
   1.122 -    }
   1.123 -#if SDL_BYTEORDER == SDL_LIL_ENDIAN
   1.124 -    if (surface->format->Amask == 0xFF000000) {
   1.125 -        offset = 3;
   1.126 -    } else {
   1.127 -        offset = 0;
   1.128 -    }
   1.129 -#else
   1.130 -    if (surface->format->Amask == 0xFF000000) {
   1.131 -        offset = 0;
   1.132 -    } else {
   1.133 -        offset = 3;
   1.134 -    }
   1.135 -#endif /* Byte ordering */
   1.136 -
   1.137 -    /* Quickly set the alpha channel of an RGBA or ARGB surface */
   1.138 -    if (SDL_MUSTLOCK(surface)) {
   1.139 -        if (SDL_LockSurface(surface) < 0) {
   1.140 -            return -1;
   1.141 -        }
   1.142 -    }
   1.143 -    row = surface->h;
   1.144 -    while (row--) {
   1.145 -        col = surface->w;
   1.146 -        buf = (Uint8 *) surface->pixels + row * surface->pitch + offset;
   1.147 -        while (col--) {
   1.148 -            *buf = value;
   1.149 -            buf += 4;
   1.150 -        }
   1.151 -    }
   1.152 -    if (SDL_MUSTLOCK(surface)) {
   1.153 -        SDL_UnlockSurface(surface);
   1.154 -    }
   1.155      return 0;
   1.156  }
   1.157  
   1.158 -/*
   1.159 - * Set the clipping rectangle for a blittable surface
   1.160 - */
   1.161 +int SDL_SetColorKey(SDL_Surface *surface, Uint32 flag, Uint32 key)
   1.162 +{
   1.163 +    int flags;
   1.164 +
   1.165 +    if (!surface) {
   1.166 +        return -1;
   1.167 +    }
   1.168 +
   1.169 +    if (flag & SDL_RLEACCEL) {
   1.170 +        SDL_SetSurfaceRLE(surface, 1);
   1.171 +    }
   1.172 +
   1.173 +    flags = surface->map->info.flags;
   1.174 +    if (flag) {
   1.175 +        surface->map->info.flags |= SDL_COPY_COLORKEY;
   1.176 +        surface->map->info.colorkey = key;
   1.177 +    } else {
   1.178 +        surface->map->info.flags &= ~SDL_COPY_COLORKEY;
   1.179 +    }
   1.180 +    if (surface->map->info.flags != flags) {
   1.181 +        SDL_InvalidateMap(surface->map);
   1.182 +    }
   1.183 +    return 0;
   1.184 +}
   1.185 +
   1.186 +int SDL_SetSurfaceColorMod(SDL_Surface *surface, Uint8 r, Uint8 g, Uint8 b)
   1.187 +{
   1.188 +    int flags;
   1.189 +
   1.190 +    if (!surface) {
   1.191 +        return -1;
   1.192 +    }
   1.193 +
   1.194 +    surface->map->info.r = r;
   1.195 +    surface->map->info.g = g;
   1.196 +    surface->map->info.b = b;
   1.197 +
   1.198 +    flags = surface->map->info.flags;
   1.199 +    if (r != 0xFF || g != 0xFF || b != 0xFF) {
   1.200 +        surface->map->info.flags |= SDL_COPY_MODULATE_COLOR;
   1.201 +    } else {
   1.202 +        surface->map->info.flags &= ~SDL_COPY_MODULATE_COLOR;
   1.203 +    }
   1.204 +    if (surface->map->info.flags != flags) {
   1.205 +        SDL_InvalidateMap(surface->map);
   1.206 +    }
   1.207 +    return 0;
   1.208 +}
   1.209 +
   1.210 +
   1.211 +int SDL_GetSurfaceColorMod(SDL_Surface *surface, Uint8 * r, Uint8 * g, Uint8 * b)
   1.212 +{
   1.213 +    if (!surface) {
   1.214 +        return -1;
   1.215 +    }
   1.216 +
   1.217 +    if (r) {
   1.218 +        *r = surface->map->info.r;
   1.219 +    }
   1.220 +    if (g) {
   1.221 +        *g = surface->map->info.g;
   1.222 +    }
   1.223 +    if (b) {
   1.224 +        *b = surface->map->info.b;
   1.225 +    }
   1.226 +    return 0;
   1.227 +}
   1.228 +
   1.229 +int SDL_SetSurfaceAlphaMod(SDL_Surface *surface, Uint8 alpha)
   1.230 +{
   1.231 +    int flags;
   1.232 +
   1.233 +    if (!surface) {
   1.234 +        return -1;
   1.235 +    }
   1.236 +
   1.237 +    surface->map->info.a = alpha;
   1.238 +
   1.239 +    flags = surface->map->info.flags;
   1.240 +    if (alpha != 0xFF) {
   1.241 +        surface->map->info.flags |= SDL_COPY_MODULATE_ALPHA;
   1.242 +    } else {
   1.243 +        surface->map->info.flags &= ~SDL_COPY_MODULATE_ALPHA;
   1.244 +    }
   1.245 +    if (surface->map->info.flags != flags) {
   1.246 +        SDL_InvalidateMap(surface->map);
   1.247 +    }
   1.248 +    return 0;
   1.249 +}
   1.250 +
   1.251 +int SDL_GetSurfaceAlphaMod(SDL_Surface *surface, Uint8 * alpha)
   1.252 +{
   1.253 +    if (!surface) {
   1.254 +        return -1;
   1.255 +    }
   1.256 +
   1.257 +    if (alpha) {
   1.258 +        *alpha = surface->map->info.a;
   1.259 +    }
   1.260 +    return 0;
   1.261 +}
   1.262 +
   1.263 +int SDL_SetSurfaceBlendMode(SDL_Surface *surface, int blendMode)
   1.264 +{
   1.265 +    int flags, status;
   1.266 +
   1.267 +    if (!surface) {
   1.268 +        return -1;
   1.269 +    }
   1.270 +
   1.271 +    status = 0;
   1.272 +    flags = surface->map->info.flags;
   1.273 +    surface->map->info.flags &= ~(SDL_COPY_MASK|SDL_COPY_BLEND|SDL_COPY_ADD|SDL_COPY_MOD);
   1.274 +    switch (blendMode) {
   1.275 +    case SDL_TEXTUREBLENDMODE_NONE:
   1.276 +        break;
   1.277 +    case SDL_TEXTUREBLENDMODE_MASK:
   1.278 +        surface->map->info.flags |= SDL_COPY_MASK;
   1.279 +        break;
   1.280 +    case SDL_TEXTUREBLENDMODE_BLEND:
   1.281 +        surface->map->info.flags |= SDL_COPY_BLEND;
   1.282 +        break;
   1.283 +    case SDL_TEXTUREBLENDMODE_ADD:
   1.284 +        surface->map->info.flags |= SDL_COPY_ADD;
   1.285 +        break;
   1.286 +    case SDL_TEXTUREBLENDMODE_MOD:
   1.287 +        surface->map->info.flags |= SDL_COPY_MOD;
   1.288 +        break;
   1.289 +    default:
   1.290 +        SDL_Unsupported();
   1.291 +        status = -1;
   1.292 +        break;
   1.293 +    }
   1.294 +
   1.295 +    if (surface->map->info.flags != flags) {
   1.296 +        SDL_InvalidateMap(surface->map);
   1.297 +    }
   1.298 +    return status;
   1.299 +}
   1.300 +
   1.301 +int SDL_GetSurfaceBlendMode(SDL_Surface *surface, int *blendMode)
   1.302 +{
   1.303 +    if (!surface) {
   1.304 +        return -1;
   1.305 +    }
   1.306 +
   1.307 +    if (!blendMode) {
   1.308 +        return 0;
   1.309 +    }
   1.310 +
   1.311 +    switch(surface->map->info.flags & (SDL_COPY_MASK|SDL_COPY_BLEND|SDL_COPY_ADD|SDL_COPY_MOD)) {
   1.312 +    case SDL_COPY_MASK:
   1.313 +        *blendMode = SDL_TEXTUREBLENDMODE_MASK:
   1.314 +        break;
   1.315 +    case SDL_COPY_BLEND:
   1.316 +        *blendMode = SDL_TEXTUREBLENDMODE_BLEND:
   1.317 +        break;
   1.318 +    case SDL_COPY_ADD:
   1.319 +        *blendMode = SDL_TEXTUREBLENDMODE_ADD:
   1.320 +        break;
   1.321 +    case SDL_COPY_MOD:
   1.322 +        *blendMode = SDL_TEXTUREBLENDMODE_MOD:
   1.323 +        break;
   1.324 +    default:
   1.325 +        *blendMode = SDL_TEXTUREBLENDMODE_NONE:
   1.326 +        break;
   1.327 +    }
   1.328 +    return 0;
   1.329 +}
   1.330 +
   1.331 +int SDL_SetSurfaceScaleMode(SDL_Surface *surface, int scaleMode)
   1.332 +{
   1.333 +    int flags, status;
   1.334 +
   1.335 +    if (!surface) {
   1.336 +        return -1;
   1.337 +    }
   1.338 +
   1.339 +    status = 0;
   1.340 +    flags = surface->map->info.flags;
   1.341 +    surface->map->info.flags &= ~(SDL_COPY_NEAREST);
   1.342 +    switch (scaleMode) {
   1.343 +    case SDL_TEXTURESCALEMODE_NONE:
   1.344 +        break;
   1.345 +    case SDL_TEXTURESCALEMODE_FAST:
   1.346 +        surface->map->info.flags |= SDL_COPY_NEAREST;
   1.347 +        break;
   1.348 +    case SDL_TEXTURESCALEMODE_SLOW:
   1.349 +    case SDL_TEXTURESCALEMODE_BEST:
   1.350 +        SDL_Unsupported();
   1.351 +        surface->map->info.flags |= SDL_COPY_NEAREST;
   1.352 +        status = -1;
   1.353 +        break;
   1.354 +    default:
   1.355 +        SDL_Unsupported();
   1.356 +        status = -1;
   1.357 +        break;
   1.358 +    }
   1.359 +
   1.360 +    if (surface->map->info.flags != flags) {
   1.361 +        SDL_InvalidateMap(surface->map);
   1.362 +    }
   1.363 +    return status;
   1.364 +}
   1.365 +
   1.366 +int SDL_GetSurfaceScaleMode(SDL_Surface *surface, int *scaleMode)
   1.367 +{
   1.368 +    if (!surface) {
   1.369 +        return -1;
   1.370 +    }
   1.371 +
   1.372 +    if (!scaleMode) {
   1.373 +        return 0;
   1.374 +    }
   1.375 +
   1.376 +    switch(surface->map->info.flags & (SDL_COPY_LINEAR)) {
   1.377 +    case SDL_COPY_LINEAR:
   1.378 +        *scaleMode = SDL_TEXTURESCALEMODE_FAST:
   1.379 +        break;
   1.380 +    default:
   1.381 +        *scaleMode = SDL_TEXTURESCALEMODE_NONE:
   1.382 +        break;
   1.383 +    }
   1.384 +    return 0;
   1.385 +}
   1.386 +
   1.387  SDL_bool
   1.388  SDL_SetClipRect(SDL_Surface * surface, const SDL_Rect * rect)
   1.389  {
   1.390 @@ -557,9 +655,7 @@
   1.391                     SDL_PixelFormat * format, Uint32 flags)
   1.392  {
   1.393      SDL_Surface *convert;
   1.394 -    Uint32 colorkey = 0;
   1.395 -    Uint8 alpha = 0;
   1.396 -    Uint32 surface_flags;
   1.397 +    Uint32 copy_flags;
   1.398      SDL_Rect bounds;
   1.399  
   1.400      /* Check for empty destination palette! (results in empty image) */
   1.401 @@ -578,8 +674,7 @@
   1.402      }
   1.403  
   1.404      /* Create a new surface with the desired format */
   1.405 -    convert = SDL_CreateRGBSurface(flags,
   1.406 -                                   surface->w, surface->h,
   1.407 +    convert = SDL_CreateRGBSurface(0, surface->w, surface->h,
   1.408                                     format->BitsPerPixel, format->Rmask,
   1.409                                     format->Gmask, format->Bmask,
   1.410                                     format->Amask);
   1.411 @@ -595,26 +690,9 @@
   1.412          convert->format->palette->ncolors = format->palette->ncolors;
   1.413      }
   1.414  
   1.415 -    /* Save the original surface color key and alpha */
   1.416 -    surface_flags = surface->flags;
   1.417 -    if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
   1.418 -        /* Convert colourkeyed surfaces to RGBA if requested */
   1.419 -        if ((flags & SDL_SRCCOLORKEY) != SDL_SRCCOLORKEY && format->Amask) {
   1.420 -            surface_flags &= ~SDL_SRCCOLORKEY;
   1.421 -        } else {
   1.422 -            colorkey = surface->map->ckey;
   1.423 -            SDL_SetColorKey(surface, 0, 0);
   1.424 -        }
   1.425 -    }
   1.426 -    if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
   1.427 -        /* Copy over the alpha channel to RGBA if requested */
   1.428 -        if (format->Amask) {
   1.429 -            surface->flags &= ~SDL_SRCALPHA;
   1.430 -        } else {
   1.431 -            alpha = (Uint8) (surface->map->cmod >> 24);
   1.432 -            SDL_SetAlpha(surface, 0, 0);
   1.433 -        }
   1.434 -    }
   1.435 +    /* Save the original copy flags */
   1.436 +    copy_flags = surface->map->info.flags;
   1.437 +    surface->map->info.flags = 0;
   1.438  
   1.439      /* Copy over the image data */
   1.440      bounds.x = 0;
   1.441 @@ -624,30 +702,25 @@
   1.442      SDL_LowerBlit(surface, &bounds, convert, &bounds);
   1.443  
   1.444      /* Clean up the original surface, and update converted surface */
   1.445 -    if (convert != NULL) {
   1.446 -        SDL_SetClipRect(convert, &surface->clip_rect);
   1.447 +    SDL_SetClipRect(convert, &surface->clip_rect);
   1.448 +    if (copy_flags & SDL_COPY_COLORKEY) {
   1.449 +        Uint8 keyR, keyG, keyB, keyA;
   1.450 +
   1.451 +        SDL_GetRGBA(colorkey, surface->format, &keyR, &keyG, &keyB, &keyA);
   1.452 +        SDL_SetColorKey(convert, 1,
   1.453 +                        SDL_MapRGBA(convert->format, keyR, keyG, keyB, keyA));
   1.454      }
   1.455 -    if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
   1.456 -        Uint32 cflags = surface_flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
   1.457 -        if (convert != NULL) {
   1.458 -            Uint8 keyR, keyG, keyB;
   1.459 +    convert->map->info.r = surface->map->info.r;
   1.460 +    convert->map->info.g = surface->map->info.g;
   1.461 +    convert->map->info.b = surface->map->info.b;
   1.462 +    convert->map->info.a = surface->map->info.a;
   1.463 +    convert->map->info.flags = copy_flags;
   1.464 +    surface->map->info.flags = copy_flags;
   1.465  
   1.466 -            SDL_GetRGB(colorkey, surface->format, &keyR, &keyG, &keyB);
   1.467 -            SDL_SetColorKey(convert, cflags | (flags & SDL_RLEACCELOK),
   1.468 -                            SDL_MapRGB(convert->format, keyR, keyG, keyB));
   1.469 -        }
   1.470 -        SDL_SetColorKey(surface, cflags, colorkey);
   1.471 -    }
   1.472 -    if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
   1.473 -        Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
   1.474 -        if (convert != NULL) {
   1.475 -            SDL_SetAlpha(convert, aflags | (flags & SDL_RLEACCELOK), alpha);
   1.476 -        }
   1.477 -        if (format->Amask) {
   1.478 -            surface->flags |= SDL_SRCALPHA;
   1.479 -        } else {
   1.480 -            SDL_SetAlpha(surface, aflags, alpha);
   1.481 -        }
   1.482 +    /* Enable alpha blending by default if the new surface has an
   1.483 +     * alpha channel or alpha modulation */
   1.484 +    if (format->Amask || (copy_flags & SDL_COPY_MODULATE_ALPHA)) {
   1.485 +        SDL_SetSurfaceBlendMode(convert, SDL_TEXTUREBLENDMODE_BLEND);
   1.486      }
   1.487  
   1.488      /* We're ready to go! */