设置窗口大小后,SDL不会渲染到完整窗口

时间:2016-12-03 09:23:43

标签: c++ sdl sdl-2

以下是我使用SDL加载图片的步骤。

  1. 创建窗口并使用默认大小进行渲染,例如100x100
  2. 使用IMG_LoadTexture
  3. 从图像创建纹理
  4. 查询纹理信息并获取纹理维度,例如800x600,然后使用SDL_SetWindowSize调整大小以达到所需的大小
  5. 执行渲染
  6. 结果是SDL仅在创建窗口时呈现给100x100区域,另一部分不呈现。

    如何在SDL_SetWindowSize之后将SDL渲染到完整窗口?

    以下是我的示例代码

    #include <iostream>
    #include <SDL_image.h>
    #include "SDL.h"
    
    int main(int argc, char *argv[])
    {
    SDL_Window* sdlWindow;
    SDL_Renderer* sdlRenderer;
    SDL_CreateWindowAndRenderer(100, 100, 0, &sdlWindow, &sdlRenderer);
    
    IMG_Init(IMG_INIT_JPG);
    
    SDL_Texture* sdlTexture = IMG_LoadTexture(sdlRenderer, "test-wallpaper-800x600.jpg");
    
    Uint32 format;
    int access, w, h;
    if (SDL_QueryTexture(sdlTexture, &format, &access, &w, &h) == 0) {
        SDL_SetWindowSize(sdlWindow, w, h);
        SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
        SDL_RenderSetLogicalSize(sdlRenderer, w, h);
    }
    
    SDL_RenderClear(sdlRenderer);
    SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);
    SDL_RenderPresent(sdlRenderer);
    
    SDL_Event sdlEvent;
    while (SDL_WaitEvent(&sdlEvent)) {
        if (sdlEvent.type == SDL_WINDOWEVENT
            && sdlEvent.window.event == SDL_WINDOWEVENT_CLOSE) {
            break;
        }
        if (sdlEvent.type == SDL_KEYDOWN
                && sdlEvent.key.keysym.sym == SDLK_ESCAPE) {
            break;
        }
    }
    
    SDL_DestroyRenderer(sdlRenderer);
    SDL_DestroyWindow(sdlWindow);
    return 0;
    }
    

1 个答案:

答案 0 :(得分:0)

您没有看到更新的渲染,因为您只在帧循环之前渲染一次。要解决此问题,请将渲染移动到帧循环中,如下所示:

SDL_Event sdlEvent;
while (SDL_WaitEvent(&sdlEvent))
{
    if (sdlEvent.type == SDL_WINDOWEVENT
        && sdlEvent.window.event == SDL_WINDOWEVENT_CLOSE) {
        break;
    }
    if (sdlEvent.type == SDL_KEYDOWN
            && sdlEvent.key.keysym.sym == SDLK_ESCAPE) {
        break;
    }

    // Render here
    SDL_RenderClear(sdlRenderer);
    SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);
    SDL_RenderPresent(sdlRenderer);
}