SFML community forums

Help => Graphics => Topic started by: sgehlich on August 29, 2012, 01:02:16 pm

Title: Scaled down sprite rendering
Post by: sgehlich on August 29, 2012, 01:02:16 pm
Hey,

This is the Screen class I use for rendering Sprites from a Spritesheet (given a source rect and a destination rect)

#include "Screen.hpp"
#include "ResourcePath.hpp"

Screen::Screen(sf::RenderWindow* window) {
    _mainWindow = window;
   
    // Load spritesheet
    if(!spriteSheet.loadFromFile(resourcePath() + "sprites.png")) {
        throw "We failed at loading the sprites.";
    }
   
    spriteSheet.setSmooth(true);
   
    sf::ContextSettings settings = _mainWindow->getSettings();
    printf("%d", settings.antialiasingLevel);
   
}

void Screen::render(sf::IntRect sourceRect, sf::IntRect destRect) {
    sf::Sprite sprite(spriteSheet);
    sprite.scale(1.0 / sourceRect.width * destRect.width, 1.0 / sourceRect.height * destRect.height);
    sprite.setTextureRect(sourceRect);
    sprite.setPosition(destRect.top, destRect.left);

    _mainWindow->draw(sprite);
}

void Screen::render(sf::IntRect sourceRect, sf::IntRect destRect, bool centered) {
    sf::Sprite sprite(spriteSheet);
   
    sprite.scale(1.0 / sourceRect.width * destRect.width, 1.0 / sourceRect.height * destRect.height);
    sprite.setTextureRect(sourceRect);
   
    if(centered)
        sprite.setOrigin(destRect.width / 2.0, destRect.height / 2.0);
   
    sprite.setPosition(destRect.left, destRect.top);
   
    _mainWindow->draw(sprite);
}

void Screen::clear() {
    _mainWindow->clear(sf::Color(13, 15, 30));
}

This works pretty fine when the source rect size equals the dest rect size or if the dest rect size is higher than the source rect size. But when I scale stuff down (e.g. sourceRect = 0,0,256,256  destRect = 0,0,50,50) it looks very pixelated (see Attachment).

I already tried setting sf::ContextSettings:antialiasingLevel for my renderWindow, that didn't change anything.

Is there a way to properly scale images down or is my approach of rendering sprites from a spritesheet / texture totally wrong?

[attachment deleted by admin]
Title: Re: Scaled down sprite rendering
Post by: Laurent on August 29, 2012, 01:47:53 pm
And how does it look at normal size?
Title: Re: Scaled down sprite rendering
Post by: sgehlich on August 29, 2012, 02:14:44 pm
This is how it looks like with a scale of 1.0 (256x256 pixels)

[attachment deleted by admin]
Title: Re: Scaled down sprite rendering
Post by: Laurent on August 29, 2012, 02:40:27 pm
Can you upload the image that you use, so that I can test it?
Title: Re: Scaled down sprite rendering
Post by: sgehlich on August 29, 2012, 03:10:35 pm
Sure, here you go!

http://filshmedia.net/ld24/assets/images/sprites.png
Title: Re: Scaled down sprite rendering
Post by: eXpl0it3r on August 29, 2012, 04:58:57 pm
I'd say that's normal, I mean you can take your image and scale it down to that size in a image editor and it will mostly look the same (see attachment), depends highly on the used scale algorithm though.

[attachment deleted by admin]
Title: Re: Scaled down sprite rendering
Post by: sgehlich on August 29, 2012, 08:03:16 pm
So I have to implement my own scaling algorithm, huh? Any recommendations on good and fast algorithms that are easy to implement?
Title: Re: Scaled down sprite rendering
Post by: eXpl0it3r on August 29, 2012, 10:51:12 pm
Depends, what is your aim with the game?
If you have to zoom around a lot, you could think of scaling the sprites up to a certain point and then use a diffrent resolution/texture (mipmapping).
If you just zoom once then you could implement this diffrently and use a smaller texture from the beginning or also swap them after zooming.

There's not really a solution, where you can zoom infently in an out and still have the top quality (e.g. if you walk closer to a wall in some 3D games the texture will look very bad). ;)