[SDL] Re: Stretching

Marco Salvi nao at libero.it
Wed Aug 25 09:50:08 PDT 1999


Hello Stuart

On 24-Ago-99, you wrote:

> John Garrison wrote:
>> 
>> Ryan Wahle wrote:
>> 
>>> Hi,
>> 
>> Does anyone here know how sdl stores a bitmap in memory? the reason I
>> ask
>> is because i want to impletment stretching of bitmaps for my game, and
>> was
>> just wondering how I could get the sdl_surface and mess around with the
>> bitmap data so i can stretch it.
>> 
>> thanks. see ya
> 
> you can scale-blit using somthing like this, untested, just-written code
> 
> (For 16bit)
> 
> void scale_blit( SDL_Surface *src, SDL_Surface *dst, float xscl, float
> yscl ) {
>     Uint16 *srcptr = (Uint16 *)src->pixels;
>     Uint16 *dstptr = (Uint16 *)dst->pixels;
> 
>     int srcX=0, srcY=0;
> 
>     for( int y=0; y < dst->h; y++ ) {
>         for( int x=0; x < dst->w; x++ ) {
>             *dstptr = *srcptr;
>             dstptr++;
>             if( (int)(x / xscl) > srcX ) {
>                 srcptr+=(int)(x/xscl) - srcX;
>                 srcX = (int)(x / xscl );
>             }
>         }
>         if( (int)(y / yscl) > srcY ) {
>             srcptr+= ((int)(y / yscl) - srcY)*src->w - srcX;
>             srcY = (int)(y / yscl);
>         }
>     }
> }


Umh..make floating point calcs in a blit routine
is not suitable for a slow processor.
I think that some fixed point calculation should be much faster.


#define fxp 16


void scale_blit (SDL_Surface *src, SDL_Surface *dst)
{
    

    UInt16 *srcptr = (UInt16 *)src->pixels;
    UInt16 *dstptr = (UInt16 *)dst->pixels;
    UInt16 *ptry,*ptrx;

    int dx = (((int)(src->w))<<fxp)/(int)dst->w;
    int dy = (((int)(src->h))<<fxp)/(int)dst->h;

    int x=0;
    int y=0;

    
    for ( int j=0; j < dst->h ; j++);
    {

        ptry = (y>>fxp)*src->w*sizeof(UInt16) + srcptr;
        y = y+dy;
        
        for ( int i=0; i < dst->w; i++);
        {
            ptrx = ptry + x>>fxp;    
            *dstptr = *ptrx;
            dstptr++;
            x = x+dx:
        }
    }
}
   
                        
I have not tested it but should works :)
One can increase precision modifying fxp, anyway 16 bits
shoould be enough.
It's obvius that works only with 16 bit video modes.






More information about the SDL mailing list