Jump to content
  • Advertisement
Sign in to follow this  
Zotoaster

Apparently my SDL drawing code is inefficient

This topic is 1784 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
Advertisement

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!