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

Author Topic: Resolution independent rendering  (Read 9393 times)

0 Members and 1 Guest are viewing this topic.

fr1ar

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Resolution independent rendering
« on: July 09, 2014, 12:40:42 pm »
Is it possible to separate rendering resolution and the window size?
For example, I may only want to render at 400x300 resolution, but I want my window size to be 800x600.
If so, how is this done?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Resolution independent rendering
« Reply #1 on: July 09, 2014, 12:53:24 pm »
With a view.

sf::FloatRect area(0, 0, 400, 300);
window.setView(sf::View(area));

Then no matter how big or small your window is, it will always show this area of 400x300 units. Note that this is already the case by default, it's just that the size of the default view is the initial size of the window.
Laurent Gomila - SFML developer

fr1ar

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Resolution independent rendering
« Reply #2 on: July 09, 2014, 01:52:51 pm »
Let me show you small example:

#include <SFML/Graphics.hpp>

using namespace sf;

int main()
{
        Vector2f windowSize(800, 600);
        Vector2f resolution(100, 75);

        RenderWindow window;
        window.create(VideoMode(windowSize.x, windowSize.y, 32), "test", Style::Default);

        Texture texture;
        texture.loadFromFile("pic.png");
       
        Vector2u textureSize = texture.getSize();
        Vector2f textureScale(windowSize.x / textureSize.x, windowSize.y / textureSize.y);

        Sprite sprite;
        sprite.setTexture(texture);
        sprite.setScale(textureScale.x * resolution.x / windowSize.x, textureScale.y * resolution.y / windowSize.y);

        FloatRect area(0, 0, resolution.x, resolution.y);
        window.setView(View(area));

        while (window.isOpen())
        {
                Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == Event::Closed) window.close();
                        if (event.type == Event::KeyPressed && event.key.code == Keyboard::Escape) window.close();
                }

                window.clear();
                window.draw(sprite);
                window.display();
        }
        return 0;
}

By changing the value of the variable "resolution" I expect that the result will be different. But it's always the same (test1.png)
I thought that it would be something like this (test2.png) for resolution 100x75.


« Last Edit: July 09, 2014, 02:01:53 pm by fr1ar »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Resolution independent rendering
« Reply #3 on: July 09, 2014, 03:23:31 pm »
No. The view specifies an area of the 2D world to show. It has no knowledge of pixels. What you draw is transformed to pixels (rasterized) only when it is mapped to a render-target.

To get a pixelated result, you need to change the size of the render-target. For example, you can render to a smaller intermediate sf::RenderTexture.
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Resolution independent rendering
« Reply #4 on: July 09, 2014, 10:18:03 pm »
sf::RenderTexture.
The tutorial code should be:
const sf::Texture& texture = renderTexture.getTexture();
instead of:
sf::Texture& texture = renderTexture.getTexture();
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Resolution independent rendering
« Reply #5 on: July 09, 2014, 10:25:53 pm »
sf::RenderTexture.
The tutorial code should be:
const sf::Texture& texture = renderTexture.getTexture();
instead of:
sf::Texture& texture = renderTexture.getTexture();
And exactly what difference does that make in the context of his problem? None. That's how much. So what's the point?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Resolution independent rendering
« Reply #6 on: July 10, 2014, 01:23:15 am »
And exactly what difference does that make in the context of his problem? None. That's how much. So what's the point?
It makes quite a big difference if he's expected to use the tutorials to use a technique suggested to fix his problem.
I found I was interested in this subject too and looked it up. In my compiler, it won't even compile without const.
I would've thought people would want the tutorials to be okay since everyone is constantly directed toward them. I thought I was being helpful...
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Resolution independent rendering
« Reply #7 on: July 10, 2014, 08:07:20 am »
Quote
And exactly what difference does that make in the context of his problem? None. That's how much. So what's the point?
Don't be so rude. This is a mistake, it had to be reported. So what? You wanted him to create a new thread in the "SFML website" forum instead? Who cares? ;)
Laurent Gomila - SFML developer

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Resolution independent rendering
« Reply #8 on: July 10, 2014, 08:30:33 am »
I didn't mean to be rude. I expressed myself badly. I'm sorry.
I didn't mean to imply that reporting the problem was not helpful, merely that it didn't belong in this thread. Reading my comment again I can see that the way I wrote it really was rude.

@Golden Eagle
I apologize.

fr1ar

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Resolution independent rendering
« Reply #9 on: July 10, 2014, 09:40:31 am »
So, I've made a new example and I was surprised that the FPS is lower when it uses double buffer.
The purpose of my question is how to increase FPS by using small resolutions? Or it's impossible?

P.S. You can toggle the drawing mode by pressing "1" button:

#include <SFML/Graphics.hpp>

using namespace sf;

int main()
{
        // Settings
        Vector2f windowSize(800, 600);
        Vector2f resolution(100, 75);
        const char* imageFile = "pic.png";
        const char* fontFile = "arial.ttf";
        bool useDoubleBuffer = true;


        RenderWindow window;
        window.create(VideoMode(windowSize.x, windowSize.y, 32), "Double buffer test", Style::Default);

        RenderTexture buffer;
        buffer.create(resolution.x, resolution.y);

        Texture texture;
        texture.loadFromFile(imageFile);

        Sprite sprite(texture);

        Vector2u textureSize = texture.getSize();
        Vector2f textureScale(windowSize.x / textureSize.x, windowSize.y / textureSize.y);
        Vector2f scale(resolution.x / windowSize.x, resolution.y / windowSize.y);

        sprite.setScale(textureScale.x * scale.x, textureScale.y * scale.y);

        Font font;
        font.loadFromFile(fontFile);

        FloatRect area(0, 0, resolution.x, resolution.y);
        window.setView(View(area));

        String fps;
        float elapsedTime = 0.0f;
        Uint32 frameCounter = 0;

        Clock clock;
        while (window.isOpen())
        {
                Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == Event::Closed)
                                window.close();

                        if (event.type == Event::KeyPressed)
                        {
                                if (event.key.code == Keyboard::Escape)
                                        window.close();

                                if (event.key.code == Keyboard::Num1)
                                        useDoubleBuffer = !useDoubleBuffer;
                        }
                }

                elapsedTime += clock.restart().asSeconds();
                frameCounter++;

                if (elapsedTime >= 1.0f)
                {
                        fps = std::to_string(frameCounter) + " FPS";
                        elapsedTime = 0.0f;
                        frameCounter = 0;
                }

                Text text(fps, font, 20);
                text.setPosition(0, 0);

                window.clear();

                if (useDoubleBuffer)
                {
                        buffer.clear();
                        buffer.draw(sprite);
                        buffer.display();

                        sf::Sprite bufferSprite(buffer.getTexture());
                        window.draw(bufferSprite);
                }
                else
                {
                        window.draw(sprite);
                }

                window.draw(text);
                window.display();
        }
        return 0;
}
« Last Edit: July 10, 2014, 10:05:15 am by fr1ar »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Resolution independent rendering
« Reply #10 on: July 10, 2014, 10:33:14 am »
So the whole purpose of this is to get more FPS? If so, I'm afraid you're in the wrong direction.

So let's stop with technical considerations and please explain what you're trying to achieve.
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Resolution independent rendering
« Reply #11 on: July 11, 2014, 01:27:54 am »
@Golden Eagle
I apologize.
Accepted.
Let's all just get along  :D

To be fair, I wish it didn't need a const. I found that copying a full HD texture just to be add "setSmooth" to it was highly unproductive  :(

So the whole purpose of this is to get more FPS?
It's possible that this goal is born of the resolution changes that you see in games (usually 3D). You can reduce the number of pixels rendered to increase the speed of rendering.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

fr1ar

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Resolution independent rendering
« Reply #12 on: July 11, 2014, 08:53:54 am »
It's possible that this goal is born of the resolution changes that you see in games (usually 3D). You can reduce the number of pixels rendered to increase the speed of rendering.
That's exactly what I'm talking about. In 3D games it can increase performance if you are using small resolution. Is that statement applicable for 2D games or not?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Resolution independent rendering
« Reply #13 on: July 11, 2014, 08:59:40 am »
Performance is increased when rendering to a smaller window, or to a lower resolution full-screen. If you're rendering to a different resolution than the one you are outputting, it will need the extra buffer/resampling so I imagine it's probably only faster if you change the actual resolution, not the perceived one.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Resolution independent rendering
« Reply #14 on: July 11, 2014, 09:02:10 am »
Rendering to smaller resolutions is an optimization technique that should only be applied if a) there actually is a performance issue and b) everything else is highly optimized.
Trying to implement something like this from the start equals to premature optimization. It will make your life harder and you don't really gain anything from it.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything