窗口不显示图像SDL

时间:2018-05-14 22:41:43

标签: c++ sdl

程序没有在我创建的窗口中显示图像,也没有得到我设置的任何失败消息,这意味着值不为空。 问题是什么? 这是代码:

#include "SDL.h"
#include "SDL_image.h"
#include <iostream>

int main(int argc,char* argv[])
{
    SDL_Window *window;
    SDL_Renderer *renderer;
    SDL_Event event;
    bool quit = false;
    SDL_Surface *tmpsur = NULL;
    SDL_Texture *tex = NULL;

    SDL_Init(SDL_INIT_EVERYTHING);

    window = SDL_CreateWindow("First window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
    renderer = SDL_CreateRenderer(window, -1, 0);

    tmpsur = IMG_Load("assets/player.png");
    if (tmpsur == NULL)
    {
        std::cout << "fail" << std::endl;
    }
    tex = SDL_CreateTextureFromSurface(renderer,tmpsur);
    if (tex == NULL)
    {
        std::cout << "fail 2" << std::endl;
    }
    SDL_FreeSurface(tmpsur);

    SDL_RenderPresent(renderer);

    while (!quit)
    {
        while (SDL_PollEvent(&event) != 0)
        {
            if(event.type == SDL_QUIT)
            {
                quit = true;
            }
        }
    }

    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);
    SDL_Quit();

    return 0;
}

1 个答案:

答案 0 :(得分:1)

您需要将纹理复制到渲染目标上。在展示您的渲染器之前,您需要像这样致电SDL_RenderCopy

SDL_RenderCopy(renderer, text, nullptr, nullptr);
SDL_RenderPresent(renderer);

参数中的nullptrs将使其在所有目标(窗口)上复制纹理。