Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Scaled down sprite rendering  (Read 2827 times)

0 Members and 1 Guest are viewing this topic.

sgehlich

  • Newbie
  • *
  • Posts: 6
    • View Profile
Scaled down sprite rendering
« 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]

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Scaled down sprite rendering
« Reply #1 on: August 29, 2012, 01:47:53 pm »
And how does it look at normal size?
Laurent Gomila - SFML developer

sgehlich

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Scaled down sprite rendering
« Reply #2 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]

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Scaled down sprite rendering
« Reply #3 on: August 29, 2012, 02:40:27 pm »
Can you upload the image that you use, so that I can test it?
Laurent Gomila - SFML developer

sgehlich

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Scaled down sprite rendering
« Reply #4 on: August 29, 2012, 03:10:35 pm »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Scaled down sprite rendering
« Reply #5 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]
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

sgehlich

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Scaled down sprite rendering
« Reply #6 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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Scaled down sprite rendering
« Reply #7 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). ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/