Search code examples
c++windowssdlsdl-2

Is there any possible way to resize an PNG image in SDL2?


I am using SDL2 and SDL_image.h. My current attempt was trying to fit the .PNG image in a SDL_Rect which only hid my image in the rectangle's area and thus did not work. I was following this tutorial to load the .PNG image. I'm looking to make the image stretch to the same size of the screen which is 640x480. This was my attempt:

...
SDL_Rect surfWindRectBC;
SDL_Rect surfWindRectCI;

SDL_Surface * screenSurf = NULL;
SDL_Surface* current = SDL_CreateRGBSurface(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0, 0, 0);
SDL_Surface * menu = IMG_Load(std::string(".\\sprites\\menu\\background.png").c_str()); 
...
...
int main() {
    surfWindRectBC.w = SCREEN_WIDTH;
    surfWindRectBC.h = SCREEN_HEIGHT;
    surfWindRectCI.w = 32;
    surfWindRectCI.h = 32;
...
...
screenSurf = SDL_GetWindowSurface(window);
current = SDL_ConvertSurface(menu, screenSurf->format,0);
SDL_FreeSurface(menu);
...
...
while (game) {
                SDL_Event event;
                Uint8 input = 0;
                
                while (SDL_PollEvent(&event)) {...}
                SDL_BlitSurface(current, &surfWindRectBC, screenSurf, &surfWindRectCI);
                SDL_UpdateWindowSurface(window);
            }
...

Solution

  • Looking at documentation maybe you should try using SDL_BlitScaled instead of SDL_BlitSurface