以下是我使用SDL加载图片的步骤。
100x100
IMG_LoadTexture
800x600
,然后使用SDL_SetWindowSize
调整大小以达到所需的大小结果是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;
}
答案 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);
}