【问题标题】:Is there any possible way to resize an PNG image in SDL2?有没有办法在 SDL2 中调整 PNG 图像的大小?
【发布时间】:2021-12-31 03:07:30
【问题描述】:

我正在使用SDL2SDL_image.h。我目前的尝试是尝试将 .PNG 图像放入 SDL_Rect 中,这仅将我的图像隐藏在矩形区域中,因此不起作用。我正在关注this tutorial 来加载 .PNG 图像。我希望将图像拉伸到与屏幕相同的大小,即 640x480。这是我的尝试:

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

【问题讨论】:

  • 查看文档也许你应该尝试使用 SDL_BlitScaled 代替 od SDL_BlitSurface
  • 是的!成功了,非常感谢。也只有一个问题,你为什么评论?我可以将您的答案标记为正确的。
  • std::string(".\\sprites\\menu\\background.png").c_str() 为什么会有这么多余的东西?
  • 事实上,您可以通过这样做SDL_Surface * menu = IMG_Load(std::string(".\\sprites\\menu\\background.png").c_str()); 调用UB,因为临时std::string 的生命周期在语句的末尾结束。只需简单地做SDL_Surface * menu = IMG_Load(".\\sprites\\menu\\background.png");
  • 很抱歉,这是因为我在尝试一些东西,它一直在出错,当我发现真正的错误时,我忘记了修复那个东西,但非常感谢!

标签: c++ windows sdl sdl-2


【解决方案1】:

查看文档也许你应该尝试使用SDL_BlitScaled 而不是SDL_BlitSurface

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-19
    • 2021-04-28
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 2021-03-14
    • 1970-01-01
    相关资源
    最近更新 更多