简体   繁体   中英

SDL2 doesn't render multiple viewports

I'm working through Lazy Foo's SDL2 tutorials and stuck trying to render multiple viewports . The idea is to load a PNG as a texture, create SDL_Rect structs for 3 different viewports (top left, top right, bottom), then copy the texture onto each viewport, and finally render all 3 viewports. My code is only rendering the first viewport.

I've tried changing the order - in every case, whichever viewport is defined first is the only one that renders.

I also tried changing the viewport dimensions, in case overlap was the issue - same problem.

The only question I found about multiple viewports didn't point me in the right direction. But it did start me thinking - my code is written in C, the tutorial in C++. Although I think I'm translating everything correctly (the other lessons work fine), maybe I'm missing something obvious here?

I'm compiling with CFLAGS = -Wall -Wextra -pedantic -std=c99 - no warnings or errors.

Edit: I tried rendering filled rectangles instead of a loaded PNG, but the issue is the same - only the first one renders.

Here's my code:

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480

int init_renderer();
int load_media();
SDL_Texture *load_texture(char *);
void close_renderer();

SDL_Window *g_window = NULL;
SDL_Renderer *g_renderer = NULL;
SDL_Texture *g_texture = NULL;

int main()
{
  if (init_renderer() != 1) {
    return -1;
  }

  if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) {
    printf("Warning: Linear texture filtering not enabled!\n");
  }

  if (load_media() != 1) {
    return -1;
  }

  int quit = 0;
  SDL_Event e;
  while (quit != 1) {
    while (SDL_PollEvent(&e) != 0) {
      if (e.type == SDL_QUIT || e.key.keysym.sym == SDLK_q) {
        quit = 1;
      }
    }

    SDL_SetRenderDrawColor(g_renderer, 0xff, 0xff, 0xff, 0xff);
    SDL_RenderClear(g_renderer);

    SDL_Rect top_left_vp;
    top_left_vp.x = 0;
    top_left_vp.y = 0;
    top_left_vp.w = SCREEN_WIDTH / 2;
    top_left_vp.h = SCREEN_HEIGHT / 2;
    SDL_RenderSetViewport(g_renderer, &top_left_vp);
    SDL_RenderCopy(g_renderer, g_texture, NULL, NULL);

    SDL_Rect top_right_vp;
    top_right_vp.x = SCREEN_WIDTH / 2;
    top_right_vp.y = 0;
    top_right_vp.w = SCREEN_WIDTH / 2;
    top_right_vp.h = SCREEN_HEIGHT / 2;
    SDL_RenderSetViewport(g_renderer, &top_right_vp);
    SDL_RenderCopy(g_renderer, g_texture, NULL, NULL);

    SDL_Rect bottom_vp;
    bottom_vp.x = 0;
    bottom_vp.y = SCREEN_HEIGHT / 2;
    bottom_vp.w = SCREEN_WIDTH;
    bottom_vp.h = SCREEN_HEIGHT / 2;
    SDL_RenderSetViewport(g_renderer, &bottom_vp);
    SDL_RenderCopy(g_renderer, g_texture, NULL, NULL);

    SDL_RenderPresent(g_renderer);
  }

  close_renderer();

  return 0;
}

int init_renderer()
{
  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    printf("Failed to initialize SDL. Error: %s\n", SDL_GetError());
    return 0;
  }

  g_window = SDL_CreateWindow("SDL Tuts",
                              SDL_WINDOWPOS_UNDEFINED,
                              SDL_WINDOWPOS_UNDEFINED,
                              SCREEN_WIDTH,
                              SCREEN_HEIGHT,
                              SDL_WINDOW_SHOWN);

  if (g_window == NULL) {
    printf("Failed to create window. Error: %s\n", SDL_GetError());
    return 0;
  }

  g_renderer = SDL_CreateRenderer(g_window, -1, SDL_RENDERER_ACCELERATED);
  if (g_renderer == NULL) {
    printf("Failed to create renderer. Error: %s\n", SDL_GetError());
    return 0;
  }

  SDL_SetRenderDrawColor(g_renderer, 0x29, 0xAB, 0x87, 0xFF);

  int img_flags = IMG_INIT_PNG;
  if (!(IMG_Init(img_flags) & img_flags)) {
    printf("Failed to initialize SDL Image. SDL_Image Error: %s\n", IMG_GetError());
    return 0;
  }

  return 1;
}

int load_media()
{
  g_texture = load_texture("assets/texture.png");
  if (g_texture == NULL) {
    return 0;
  }

  return 1;
}

SDL_Texture *load_texture(char *path)
{
  SDL_Texture *new_texture = NULL;
  SDL_Surface *loaded_surface = IMG_Load(path);
  if (loaded_surface == NULL) {
    printf("Failed to load image. SDL_Image Error: %s\n", IMG_GetError());
  } else {
    new_texture = SDL_CreateTextureFromSurface(g_renderer, loaded_surface);
    if (new_texture == NULL) {
      printf("Failed to create texture from %s. Error: %s\n", path, SDL_GetError());
    }

    SDL_FreeSurface(loaded_surface);
  }

  return new_texture;
}

void close_renderer()
{
  SDL_DestroyTexture(g_texture);
  g_texture = NULL;

  SDL_DestroyRenderer(g_renderer);
  SDL_DestroyWindow(g_window);
  g_renderer = NULL;
  g_window = NULL;

  IMG_Quit();
  SDL_Quit();
}

To check for more errors, you should see what the calls to SDL_RenderSetViewPort are returning. According to the docs , it returns an int, which is 0 on success or a negative int if there's an error. It's entirely possible that something deeper in SDL is having a fit.

Additional pro tips: you can define the SDL_Rect structs outside of your while loop to save a miniscule margin of processing power. And to spare yourself some typing, you can initialize a SDL_Rect using a list constructor instead of manually punching each property. EG:

SDL_Rect top_left_vp;
top_left_vp.x = 0;
top_left_vp.y = 0;
top_left_vp.w = SCREEN_WIDTH / 2;
top_left_vp.h = SCREEN_HEIGHT / 2;

is more simply

SDL_Rect top_left_vp = {
    0,
    0,
    SCREEN_WIDTH / 2,
    SCREEN_HEIGHT / 2
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM