SDL_Renderer won't show textures after passing through the function

大憨熊 提交于 2019-12-11 20:18:08

问题


I have a little problem with SDL_Renderer. I can't understand why it doesn't work. Let's look at this example, it works fine:

bool running = true;
SDL_Window* window = SDL_CreateWindow("ASDF", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Texture* texture = IMG_LoadTexture(renderer, "asdf.bmp");
SDL_Event event;

while(running)
{
    while(SDL_PollEvent(&event))
    {
        switch(event.type)
        {
            case SDL_KEYDOWN:
                if(event.key.keysym.sym == SDLK_ESCAPE)
                {
                    running = false;
                }
                break;
            default:
                break;
        }
    }
    SDL_RenderClear(renderer);
    SDL_RenderCopy(renderer, texture, NULL, NULL);
    SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(texture);

Then it comes to classes and when I pass the renderer through functions, it won't work anymore.

class Sprite
{
public:
    Sprite(const std::string& path) : filePath(path) {};
    ~Sprite() { SDL_DestroyTexture(tex); };
    void draw(SDL_Renderer* renderer);

private:
    const std::string& filePath;
    SDL_Texture* tex;
};

void Sprite::draw(SDL_Renderer* renderer)
{
    printf("renderer sprite = %p\n", renderer);

    tex = IMG_LoadTexture(renderer, filePath.c_str());
    SDL_RenderCopy(renderer, tex, NULL, NULL);
}

int main(int argc, char **argv)
{
    bool running = true;
    SDL_Window* window = SDL_CreateWindow("ASDF", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    SDL_Event event;

    Sprite* sprite = new Sprite("asdf.bmp");
    while(running)
    {
        printf("renderer main = %p\n", renderer);

        while(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
                case SDL_KEYDOWN:
                    if(event.key.keysym.sym == SDLK_ESCAPE)
                    {
                        running = false;
                    }
                    break;
                default:
                    break;
            }
        }
        SDL_RenderClear(renderer);
        sprite->draw(renderer);
        SDL_RenderPresent(renderer);
    }

    return 0;
}

The address of renderer is the same in main and in the draw function. I know that I'm probably making some sort of beginner's mistake here but i can't find it out.


回答1:


The texture is being loaded every frame when you call sprite->draw.

You should move the line tex = IMG_LoadTexture(renderer, filePath.c_str()); to the constructor so that it is only loaded once.

The reason for this is that the texture will not be ready for rendering in the same frame that it is loaded.

As a side note you don't appear to be cleaning up the SDL_Window or SDL_Renderer with their respective destroy functions or calling SDL_Quit although I accept this may have been omitted for submitting the code example.



来源:https://stackoverflow.com/questions/27765712/sdl-renderer-wont-show-textures-after-passing-through-the-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!