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

Author Topic: Drawing Pixelated Shapes  (Read 2365 times)

0 Members and 1 Guest are viewing this topic.

Lurgypai

  • Newbie
  • *
  • Posts: 6
    • View Profile
Drawing Pixelated Shapes
« on: January 11, 2018, 09:15:38 am »
I was wondering how I'd use the libraries to draw pixelly things without using a sprite, so I decided to start with a line. I tried drawing a line, using a rotated 1 pixel tall sf::RectangleShape, and then zooming into it using a view, and I got this:


Whereas what I want is more like this:


I'd also like to be able to draw circles and other shapes, and even render and rotate sprites like this if possible, but I figured I'd try with a line first to figure it out.

Its probably something simple. Regardless, I tried looking at other threads, but I wasn't sure what exactly to call this, and my searches came up with nothing. Anywho, thanks in advance for the help!
« Last Edit: January 11, 2018, 04:25:06 pm by Lurgypai »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Drawing Pixelated Shapes
« Reply #1 on: January 11, 2018, 10:00:26 am »
Seems like the images are missing in your post.
Laurent Gomila - SFML developer

Lurgypai

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Drawing Pixelated Shapes
« Reply #2 on: January 11, 2018, 04:26:00 pm »
How'd that happen? Oh well, I fixed it, apologies for the inconvenience. Images are bigger than I though, but I don't have time to shrink them, rn, sorry. Thanks for the help.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10829
    • View Profile
    • development blog
    • Email
Re: Drawing Pixelated Shapes
« Reply #3 on: January 11, 2018, 05:02:07 pm »
You csn set the anti-aliasing settings in context settings to 0.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Lurgypai

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Drawing Pixelated Shapes
« Reply #4 on: January 12, 2018, 01:01:49 am »
That window is done using 0 as the antialiasing. In a moment I will post the code.

Edit:
The code:
#include "SFML\Graphics.hpp"


int main()
{

        sf::ContextSettings contSettings{};
        contSettings.antialiasingLevel = 0;

        sf::RectangleShape rect{ sf::Vector2f{ 150, 1 } };
        rect.setPosition( 500, 500);
        rect.rotate(32);

        sf::RenderWindow window{};
        window.create(sf::VideoMode{ 1080, 720 }, "BoxT", sf::Style::Default, contSettings);
        sf::View view{};
        view.zoom(.25);
        window.setView(view);

        while (window.isOpen()) {
                window.clear(sf::Color::Black);
                window.draw(rect);
                window.display();

                sf::Event ev;
                while (window.pollEvent(ev)) {
                        if (ev.type == sf::Event::Closed) {
                                window.close();
                        }
                }
        }

    return 0;
}
 

produces:
« Last Edit: January 12, 2018, 01:47:50 am by Lurgypai »

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Drawing Pixelated Shapes
« Reply #5 on: January 12, 2018, 07:49:38 am »
You're zoomed in.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Drawing Pixelated Shapes
« Reply #6 on: January 12, 2018, 08:51:47 am »
The zoom level will never make things appear more pixelated, since OpenGL applies it before the final rasterization, which always happens at the resolution of the back buffer (in your case, 1080x720).

You need to draw to a lower-resolution target (sf::RenderTexture) and stretch the result to cover your final window. And don't forget to disable smoothing (renderTexture.setSmooth(false)).

Another solution is to use a custom shader; see the "Shaders" official example, which demonstrates a pixelated effect.
« Last Edit: January 12, 2018, 08:54:10 am by Laurent »
Laurent Gomila - SFML developer

Lurgypai

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Drawing Pixelated Shapes
« Reply #7 on: January 12, 2018, 04:18:12 pm »
Thanks. the shader is probably what I'm looking for, can't believe that I forgot those were a thing. I'll look into using a sf::RednerTexture too, but teh shaders probably what I need.

 

anything