SDL2不渲染多个视口

问题描述 投票:1回答:1

我正在研究Lazy Foo的SDL2教程,并尝试渲染多个viewports。想法是将PNG加载为纹理,为3个不同的视口(左上,右上,下)创建SDL_Rect结构,然后将纹理复制到每个视口中,最后渲染所有3个视口。我的代码仅呈现第一个视口。

我尝试过更改顺序-在每种情况下,首先定义的任何视口都是唯一呈现的视口。

[我还尝试过更改视口尺寸,以防出现重叠-同样的问题。

[我发现的关于多个视口的唯一question并没有指出正确的方向。但这确实让我开始思考-我的代码是用C编写的,是C ++的教程。尽管我认为我正在正确翻译所有内容(其他课程都可以正常工作),但也许我在这里遗漏了一些明显的东西?

我正在使用CFLAGS = -Wall -Wextra -pedantic -std=c99进行编译-没有警告或错误。

编辑:我尝试渲染填充的矩形而不是加载的PNG,但是问题是相同的-仅第一个渲染。

这是我的代码:

#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();
}
c sdl sdl-2
1个回答
0
投票

要检查更多错误,您应该查看对SDL_RenderSetViewPort的调用返回了什么。根据the docs,它返回一个int,成功则为0,如果有错误,则为负int。完全有可能在SDL中更深层次的内容适合。

其他提示:您可以在while循环之外定义SDL_Rect结构,以节省微不足道的处理能力。为了省去打字的麻烦,您可以使用列表构造函数初始化SDL_Rect,而不必手动打孔每个属性。例如:

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_Rect top_left_vp = {
    0,
    0,
    SCREEN_WIDTH / 2,
    SCREEN_HEIGHT / 2
};
© www.soinside.com 2019 - 2024. All rights reserved.