Sign in to follow this  

Apparently my SDL drawing code is inefficient

This topic is 1440 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay so I've never used SDL before and I'm just learning it now. I looked up some code on the internet and created a texture class that I use to draw. Here it is:

    /** Texture */
    Texture::Texture(string file)
    {
        SDL_Texture * newTexture = NULL;
        SDL_Surface * loadedSurface = IMG_Load(file.c_str());
        
        if (!loadedSurface)
            printf("Unable to load image %s! SDL_image Error: %s\n", file.c_str(), IMG_GetError());
        else
        {
            newTexture = SDL_CreateTextureFromSurface(SDLDevice::Instance()->GetRenderer(), loadedSurface);
            if (!newTexture)
                printf("Unable to create texture from %s! SDL Error: %s\n", file.c_str(), SDL_GetError());
            else
            {
                _width = loadedSurface->w;
                _height = loadedSurface->h;
            }
            
            
            SDL_FreeSurface(loadedSurface);
        }

        _texture = newTexture;
    }
    Texture::~Texture()
    {
        if (_texture)
            SDL_DestroyTexture(_texture);
    }
    
    int Texture::GetWidth()
    {
        return _width;
    }
    int Texture::GetHeight()
    {
        return _height;
    }
    
    void Texture::SetColour(Uint8 red, Uint8 green, Uint8 blue)
    {
        SDL_SetTextureColorMod(_texture, red, green, blue);
    }
    void Texture::SetBlendMode(SDL_BlendMode blend)
    {
        SDL_SetTextureBlendMode(_texture, blend);
    }
    void Texture::SetAlpha(Uint8 alpha)
    {
        SDL_SetTextureAlphaMod(_texture, alpha);
    }
    
    void Texture::Render(int x, int y, SDL_Rect * clip, double angle, SDL_Point * center, SDL_RendererFlip flip)
    {
        SDL_Rect renderQuad = {x, y, _width, _height};
        
        if (clip)
        {
            renderQuad.w = clip->w;
            renderQuad.h = clip->h;
        }

        SDL_RenderCopyEx(SDLDevice::Instance()->GetRenderer(), _texture, clip, &renderQuad, angle, center, flip);
    }

Apparently the 'SDL_RenderCopyEx' call is pretty inefficient, but seeing as I don't yet know how all of this really fits together, I ask if there is a better way to do this while keeping the same general interface, as it's what I'm using for my game.

 

Thanks

Share this post


Link to post
Share on other sites

If you're using the original SDL, try SDL2 since it has hardware acceleration and tons of other performance optimizations. Or you could use SFML or OpenGL (with SDL for non-graphics aspects is fine) or something else. Sticking with original SDL for graphics is possible, but I think you'd save time learning something else with orders of magnitude better performance instead of fighting for performance every step of the way.

 

If you're already using SDL2, can you explain what you're trying to do (update animated sprites on static background?), what performance levels you require (how many updates do you need to support per frame?), and what performance levels you're currently seeing on a release build?

Share this post


Link to post
Share on other sites

Use SDL_RenderCopyEx only when you need to apply transformations to your image. Otherwise, use SDL_RenderCopy as it's quicker. I recommend overloading your Texture::Render() function to achieve this.

Share this post


Link to post
Share on other sites

This topic is 1440 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this