简体   繁体   中英

How to decouple SDL_Texture from SDL_Renderer?

I have a sprite class which uses an SDL_Texture over an SDL_Surface in lieu of perfomance. The class looks something like this:

class Sprite
{
    public:
        ...

    private:
        SDL_Texture *m_texture;
        SDL_Rect m_blitRect;

        int m_x;
        int m_y;
};

Now the problem is when creating an SDL_Texture you also need an SDL_Renderer object as well as an SDL_Surface to create it. Normally this would look something like this:

SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);

The SDL surface can be created and disposed of while creating the texture however, the problem is, I want to be able to have a rendering object that isn't global. I want a decoupled framework and I don't know how to achieve that in this instance. My first thought was to make a renderer class and have it as a constructor argument but that cannot happen as it would complicate things down the road and cannot be an option. Are there any other ways to decouple the two?

If you want to decouple your Texture from the renderer and surface parameter, I think your best best is to actually delay the call to the SDL_CreateTextureFromSurface until you actually get these arguments.

That way, your Texture texture constructor will not use/need these arguments.

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