src/video/SDL_video.c
changeset 2222 926294b2bb4e
parent 2130 3ee59c43d784
child 2245 989fb86ad1ec
     1.1 --- a/src/video/SDL_video.c	Sat Aug 11 20:46:24 2007 +0000
     1.2 +++ b/src/video/SDL_video.c	Sat Aug 11 20:54:31 2007 +0000
     1.3 @@ -1532,14 +1532,11 @@
     1.4  }
     1.5  
     1.6  SDL_TextureID
     1.7 -SDL_CreateTextureFromSurface(Uint32 format, int access, SDL_Surface * surface)
     1.8 +SDL_CreateTextureFromSurface(Uint32 format, SDL_Surface * surface)
     1.9  {
    1.10      SDL_TextureID textureID;
    1.11      Uint32 surface_flags = surface->flags;
    1.12      SDL_PixelFormat *fmt = surface->format;
    1.13 -    Uint8 alpha;
    1.14 -    SDL_Rect bounds;
    1.15 -    SDL_Surface dst;
    1.16      int bpp;
    1.17      Uint32 Rmask, Gmask, Bmask, Amask;
    1.18  
    1.19 @@ -1576,23 +1573,41 @@
    1.20          }
    1.21      }
    1.22  
    1.23 -    textureID = SDL_CreateTexture(format, access, surface->w, surface->h);
    1.24 +    textureID =
    1.25 +        SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, surface->w,
    1.26 +                          surface->h);
    1.27      if (!textureID) {
    1.28          return 0;
    1.29      }
    1.30  
    1.31 -    /* Set up a destination surface for the texture update */
    1.32 -    SDL_zero(dst);
    1.33 -    dst.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
    1.34 -    if (!dst.format) {
    1.35 -        SDL_DestroyTexture(textureID);
    1.36 -        return 0;
    1.37 -    }
    1.38 -    dst.w = surface->w;
    1.39 -    dst.h = surface->h;
    1.40 -    if (SDL_LockTexture(textureID, NULL, 1, &dst.pixels, &dst.pitch) == 0) {
    1.41 -        dst.flags |= SDL_PREALLOC;
    1.42 +    if (bpp == fmt->BitsPerPixel && Rmask == fmt->Rmask && Gmask == fmt->Gmask
    1.43 +        && Bmask == fmt->Bmask && Amask == fmt->Amask) {
    1.44 +        if (SDL_MUSTLOCK(surface)) {
    1.45 +            if (SDL_LockSurface(surface) < 0) {
    1.46 +                SDL_DestroyTexture(textureID);
    1.47 +                return 0;
    1.48 +            }
    1.49 +            SDL_UpdateTexture(textureID, NULL, surface->pixels,
    1.50 +                              surface->pitch);
    1.51 +            SDL_UnlockSurface(surface);
    1.52 +        } else {
    1.53 +            SDL_UpdateTexture(textureID, NULL, surface->pixels,
    1.54 +                              surface->pitch);
    1.55 +        }
    1.56      } else {
    1.57 +        Uint8 alpha;
    1.58 +        SDL_Rect bounds;
    1.59 +        SDL_Surface dst;
    1.60 +
    1.61 +        /* Set up a destination surface for the texture update */
    1.62 +        SDL_zero(dst);
    1.63 +        dst.format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
    1.64 +        if (!dst.format) {
    1.65 +            SDL_DestroyTexture(textureID);
    1.66 +            return 0;
    1.67 +        }
    1.68 +        dst.w = surface->w;
    1.69 +        dst.h = surface->h;
    1.70          dst.pitch = SDL_CalculatePitch(&dst);
    1.71          dst.pixels = SDL_malloc(dst.h * dst.pitch);
    1.72          if (!dst.pixels) {
    1.73 @@ -1601,76 +1616,72 @@
    1.74              SDL_OutOfMemory();
    1.75              return 0;
    1.76          }
    1.77 -    }
    1.78  
    1.79 -    /* Copy the palette if any */
    1.80 -    if (SDL_ISPIXELFORMAT_INDEXED(format)) {
    1.81 -        if (fmt->palette) {
    1.82 -            SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
    1.83 -                                  fmt->palette->ncolors);
    1.84 -            SDL_SetSurfacePalette(&dst, fmt->palette);
    1.85 -        } else {
    1.86 -            dst.format->palette =
    1.87 -                SDL_AllocPalette((1 << SDL_BITSPERPIXEL(format)));
    1.88 -            if (!dst.format->palette) {
    1.89 -                SDL_DestroyTexture(textureID);
    1.90 -                SDL_FreeFormat(dst.format);
    1.91 -                return 0;
    1.92 +        /* Copy the palette if any */
    1.93 +        if (SDL_ISPIXELFORMAT_INDEXED(format)) {
    1.94 +            if (fmt->palette) {
    1.95 +                SDL_SetTexturePalette(textureID, fmt->palette->colors, 0,
    1.96 +                                      fmt->palette->ncolors);
    1.97 +                SDL_SetSurfacePalette(&dst, fmt->palette);
    1.98 +            } else {
    1.99 +                dst.format->palette =
   1.100 +                    SDL_AllocPalette((1 << SDL_BITSPERPIXEL(format)));
   1.101 +                if (!dst.format->palette) {
   1.102 +                    SDL_DestroyTexture(textureID);
   1.103 +                    SDL_FreeFormat(dst.format);
   1.104 +                    return 0;
   1.105 +                }
   1.106 +                SDL_DitherColors(dst.format->palette->colors,
   1.107 +                                 SDL_BITSPERPIXEL(format));
   1.108              }
   1.109 -            SDL_DitherColors(dst.format->palette->colors,
   1.110 -                             SDL_BITSPERPIXEL(format));
   1.111          }
   1.112 -    }
   1.113  
   1.114 -    /* Make the texture transparent if the surface has colorkey */
   1.115 -    if (surface_flags & SDL_SRCCOLORKEY) {
   1.116 -        int row;
   1.117 -        int length = dst.w * dst.format->BytesPerPixel;
   1.118 -        Uint8 *p = (Uint8 *) dst.pixels;
   1.119 -        for (row = 0; row < dst.h; ++row) {
   1.120 -            SDL_memset(p, 0, length);
   1.121 -            p += dst.pitch;
   1.122 +        /* Make the texture transparent if the surface has colorkey */
   1.123 +        if (surface_flags & SDL_SRCCOLORKEY) {
   1.124 +            int row;
   1.125 +            int length = dst.w * dst.format->BytesPerPixel;
   1.126 +            Uint8 *p = (Uint8 *) dst.pixels;
   1.127 +            for (row = 0; row < dst.h; ++row) {
   1.128 +                SDL_memset(p, 0, length);
   1.129 +                p += dst.pitch;
   1.130 +            }
   1.131          }
   1.132 -    }
   1.133  
   1.134 -    /* Copy over the alpha channel */
   1.135 -    if (surface_flags & SDL_SRCALPHA) {
   1.136 -        if (fmt->Amask) {
   1.137 -            surface->flags &= ~SDL_SRCALPHA;
   1.138 -        } else {
   1.139 -            /* FIXME: Need to make sure the texture has an alpha channel
   1.140 -             *        and copy 'alpha' into the texture alpha channel.
   1.141 -             */
   1.142 -            alpha = surface->format->alpha;
   1.143 -            SDL_SetAlpha(surface, 0, 0);
   1.144 +        /* Copy over the alpha channel */
   1.145 +        if (surface_flags & SDL_SRCALPHA) {
   1.146 +            if (fmt->Amask) {
   1.147 +                surface->flags &= ~SDL_SRCALPHA;
   1.148 +            } else {
   1.149 +                /* FIXME: Need to make sure the texture has an alpha channel
   1.150 +                 *        and copy 'alpha' into the texture alpha channel.
   1.151 +                 */
   1.152 +                alpha = surface->format->alpha;
   1.153 +                SDL_SetAlpha(surface, 0, 0);
   1.154 +            }
   1.155          }
   1.156 -    }
   1.157  
   1.158 -    /* Copy over the image data */
   1.159 -    bounds.x = 0;
   1.160 -    bounds.y = 0;
   1.161 -    bounds.w = surface->w;
   1.162 -    bounds.h = surface->h;
   1.163 -    SDL_LowerBlit(surface, &bounds, &dst, &bounds);
   1.164 +        /* Copy over the image data */
   1.165 +        bounds.x = 0;
   1.166 +        bounds.y = 0;
   1.167 +        bounds.w = surface->w;
   1.168 +        bounds.h = surface->h;
   1.169 +        SDL_LowerBlit(surface, &bounds, &dst, &bounds);
   1.170  
   1.171 -    /* Clean up the original surface */
   1.172 -    if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
   1.173 -        Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
   1.174 -        if (fmt->Amask) {
   1.175 -            surface->flags |= SDL_SRCALPHA;
   1.176 -        } else {
   1.177 -            SDL_SetAlpha(surface, aflags, alpha);
   1.178 +        /* Clean up the original surface */
   1.179 +        if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
   1.180 +            Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
   1.181 +            if (fmt->Amask) {
   1.182 +                surface->flags |= SDL_SRCALPHA;
   1.183 +            } else {
   1.184 +                SDL_SetAlpha(surface, aflags, alpha);
   1.185 +            }
   1.186          }
   1.187 -    }
   1.188  
   1.189 -    /* Update the texture */
   1.190 -    if (dst.flags & SDL_PREALLOC) {
   1.191 -        SDL_UnlockTexture(textureID);
   1.192 -    } else {
   1.193 +        /* Update the texture */
   1.194          SDL_UpdateTexture(textureID, NULL, dst.pixels, dst.pitch);
   1.195          SDL_free(dst.pixels);
   1.196 +        SDL_FreeFormat(dst.format);
   1.197      }
   1.198 -    SDL_FreeFormat(dst.format);
   1.199  
   1.200      return textureID;
   1.201  }
   1.202 @@ -1967,6 +1978,10 @@
   1.203      if (!texture) {
   1.204          return -1;
   1.205      }
   1.206 +    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
   1.207 +        SDL_SetError("SDL_LockTexture(): texture must be streaming");
   1.208 +        return -1;
   1.209 +    }
   1.210  
   1.211      renderer = texture->renderer;
   1.212      if (!renderer->LockTexture) {
   1.213 @@ -1994,6 +2009,9 @@
   1.214      if (!texture) {
   1.215          return;
   1.216      }
   1.217 +    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
   1.218 +        return;
   1.219 +    }
   1.220  
   1.221      renderer = texture->renderer;
   1.222      if (!renderer->UnlockTexture) {
   1.223 @@ -2012,6 +2030,9 @@
   1.224      if (!texture) {
   1.225          return;
   1.226      }
   1.227 +    if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
   1.228 +        return;
   1.229 +    }
   1.230  
   1.231      renderer = texture->renderer;
   1.232      if (!renderer->DirtyTexture) {