[SDL] Resize image?
Kenneth Bull
llubnek at gmail.com
Tue Aug 4 17:38:45 PDT 2009
2009/8/3 Bruno Adami <a_j_bruno at hotmail.com>:
> Hello!
>
> I got to do a function here that creates a copy of an image but with bigger
> size. I dont want to make it bigger (scretch), only want it to resize! I am
> using PNG images with alpha, the mouth and eyes are transparents! I made it
> with Gimp:
>
> From: http://img41.imageshack.us/img41/8241/imagemqat.png
> To: http://img178.imageshack.us/img178/4120/imagem2l.png
>
> But I got problems! The image is not how it should be, it is all white!
> Here is how I am doing it:
>
> ...
> loadedImage = IMG_Load(path); // I test to see if it loaded right, I removed
> to make it smaller
> surface = SDL_DisplayFormatAlpha(loadedImage);
> SDL_FreeSurface(loadedImage);
> surface = redImage(surface->w*2,surface->h*2,surface);
> ...
>
> SDL_Surface* redImage(int w,int h,SDL_Surface* src)
> {
> SDL_Surface* ret =
> SDL_CreateRGBSurface(src->flags,w,h,src->format->BitsPerPixel,
>
> src->format->Rmask,src->format->Gmask,src->format->Bmask,src->format->Amask);
> if (!ret)
> return src;
> SDL_BlitSurface(src,NULL,ret,NULL);
> SDL_FreeSurface(src);
> SDL_Surface* surface = SDL_DisplayFormatAlpha(ret);
> SDL_FreeSurface(ret);
> return surface;
> }
>
> I fell that I am making something wrong, tryed many things but nothing seems
> to work! Whats wrong with it?
>
try this:
SDL_Surface* loadedImage = IMG_Load(path);
SDL_PixelFormat* format = loadedImage->format;
SDL_Surface* tmp = SDL_CreateRGBSurface(
SDL_SWSURFACE | SDL_SRCALPHA,
loadedImage->w*2, loadedImage->h*2,
format->BitsPerPixel,
format->Rmask, format->Gmask, format->Bmask, format->Amask
);
SDL_FillRect(tmp, NULL, SDL_MapRGBA(tmp->format, 0, 0, 0, 0));
SDL_SetSurfaceScaleMode(loadedImage, SDL_TEXTURESCALEMODE_FAST);
SDL_SoftStretch(loadedImage, NULL, tmp, NULL);
SDL_Surface* biggerImage = SDL_DisplayFormatAlpha(tmp);
SDL_FreeSurface(tmp);
SDL_FreeSurface(loadedImage);
...
SDL_FreeSurface(biggerImage);
You should be able to trim that down a bit. If using SDL 1.2, leave
out SDL_SetSurfaceScaleMode. If using C instead of C++, declare all
variables at the start of the function.
If you can't get SDL_SoftStrech to work, either do it yourself (by
manually copying each pixel in the source surface to a 2x2 pixel
square in the destination) or use SDL_GFX or some other add on
library.
More information about the SDL
mailing list