如何将窗口大小调整为加载图像?

时间:2017-05-09 19:20:09

标签: c sdl sdl-2

有没有办法在SDL中调整窗口大小以适应加载的图像大小?目前,当你调整大小时,它会复制窗口后面的内容。 这是我的加载图像功能:

void userImage(SDL_Surface *surface, SDL_Window *window)
{
    SDL_Surface *userLoadImage;
    char FileLocation[200];

    printf( "Please Enter the file location:\n" );
    fgets(FileLocation, 200, stdin );
    fflush(stdin);
    FileLocation[strcspn(FileLocation,"\n")]=0;
    char *const picturePath = FileLocation;

    userLoadImage = IMG_Load( picturePath );
    int width = userLoadImage->w; //Get the width
    int height = userLoadImage ->h;  //Get the height

    printf("image width = %d\n", width);
    printf("image width = %d\n", height);
    SDL_BlitSurface(userLoadImage, NULL, surface, NULL);
    SDL_SetWindowSize( window, width, height);
}

2 个答案:

答案 0 :(得分:0)

这是我修复它的方式:我改变的是将窗口重新分配到表面> surface = SDL_GetWindowSurface(窗口);'

void userImage(SDL_Surface *surface, SDL_Window *window)
{
    SDL_Surface *userLoadImage;
    char FileLocation[200];

    printf( "Please Enter the file location:\n" );
    fgets(FileLocation, 200, stdin );
    fflush(stdin);
    FileLocation[strcspn(FileLocation,"\n")]=0;
    char *const picturePath = FileLocation;

    userLoadImage = IMG_Load( picturePath );
    int width = userLoadImage->w; //Get the width
    int height = userLoadImage ->h;  //Get the height
    SDL_SetWindowSize( window, width, height);

    surface = SDL_GetWindowSurface(window);
    SDL_BlitSurface(userLoadImage, NULL, surface, NULL);
}

答案 1 :(得分:0)

这是一个用于缩放曲面的SDL方法,可以做你想要的:

SDL_Surface* SDL_ScaleSurface(SDL_Surface *Surface, Uint16 Width, Uint16 Height)
{
    for(Sint32 y = 0; y < Surface->h; y++) //Run across all Y pixels.
        for(Sint32 x = 0; x < Surface->w; x++) //Run across all X pixels.
            for(Sint32 o_y = 0; o_y < _stretch_factor_y; ++o_y) //Draw _stretch_factor_y pixels for each Y pixel.
                for(Sint32 o_x = 0; o_x < _stretch_factor_x; ++o_x) //Draw _stretch_factor_x pixels for each X pixel.
                    DrawPixel(_ret, static_cast<Sint32>(_stretch_factor_x * x) + o_x, 
                        static_cast<Sint32>(_stretch_factor_y * y) + o_y, ReadPixel(Surface, x, y));
}

示例,还有更多内容在 this page