【问题标题】:How to resize Window to loaded Image?如何调整窗口大小以加载图像?
【发布时间】:2017-05-10 04:15:17
【问题描述】:

有没有办法在 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);
}

【问题讨论】:

  • 如果你能指出它们会很棒,我还在学习(我的大部分代码是 C,但我需要一些 C++ 功能来让事情变得更容易)。谢谢
  • 例如,将 printf 更改为 cout 语句会添加一些简单的 C++ 功能。 (printf 在 C++ 和 C 中都是合法的)
  • 我将答案更改为更以 SDL 为中心的答案。很抱歉造成误解。
  • 不用担心,但非常感谢 :)

标签: c sdl sdl-2


【解决方案1】:

这是我修复它的方式:我所做的只是将窗口重新分配给表面'surface = SDL_GetWindowSurface(window);'

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);
}

【讨论】:

    【解决方案2】:

    这是一种用于缩放表面的 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 上还有更多示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-24
      • 2023-03-22
      • 1970-01-01
      • 2010-12-21
      相关资源
      最近更新 更多