0





9

Advertisement

Here is the steps I use SDL to load image.

  1. create window and render with default size, e.g 100x100
  2. create texture from image with IMG_LoadTexture
  3. Query texture info and get texture dimension, e.g 800x600, then use SDL_SetWindowSize to resize to the required size
  4. perform render

The result is SDL only render to the 100x100 region when the window created, the other part is not render.

How can I make SDL render to full window after SDL_SetWindowSize?

Below is my sample code

#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;
}
Question author Alijandro | Source

Advertisement


0


You don't see an updated rendering because you only render it once before the frame loop. To solve this problem, move the rendering into the frame loop, like so:

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);
}
Answer author Meyer