SFML像素操作 - 改变窗口中单个字符的大小

时间:2018-05-31 21:28:24

标签: c++ sfml

有没有办法改变窗口中单个像素的大小,因为我宁愿使用8x8像素而不是1x1的单个控制台字符。

1 个答案:

答案 0 :(得分:1)

如果您要扩展(或缩小)窗口中的所有内容,最简单的方法是通过调用sf::View来更改窗口sf::RenderWindow::setView()

sf::RenderWindow myWindow(sf::VideoMode(640, 480), "Test");

// Define a view that's 320x240 units (i.e. pixels) in size and is centered at (160, 120)
sf::View myView(sf::Vector2f(160, 120), sf::Vector2f(320, 240));

// Apply the view
myWindow.setView(myView);

运行此代码后,呈现给窗口的所有内容都将被放大2倍(因为您在640x480内显示320x240)。以类似的方式,您可以实现8的缩放因子,例如通过制作窗口(800,800)并将视图设置为(100,100)。

但是,根据您呈现的内容,您不会获得像素化的外观。

如果您想创建具有锐化/放大像素的像素艺术品,我建议您只需将场景渲染为所需分辨率的渲染纹理,然后将该纹理渲染到实际窗口,进行放大(并禁用过滤):

#include <SFML/Graphics.hpp>

int main(int argc, char **argv) {
    sf::RenderWindow window(sf::VideoMode(800, 600), "Scaling", sf::Style::Default);

    // Set the view to upscale
    // Use 1/8th of the window's size
    sf::View view(sf::Vector2f(50, 37.5f), sf::Vector2f(100, 75));
    window.setView(view);

    // Let's create a render texture as buffer
    // (I'm skipping error checking for simplicity)
    sf::RenderTexture buffer;
    buffer.create(100, 75); // Again 1/8th of the window size

    sf::Sprite bufferSprite(buffer.getTexture());

    // Something to draw
    sf::CircleShape circle(20, 6);
    circle.setFillColor(sf::Color::Transparent);
    circle.setOutlineColor(sf::Color::Black);
    circle.setOutlineThickness(2);
    circle.setOrigin(20, 20); // Move origin to center

    // Let's use a simple sf::Clock to animate a bit
    sf::Clock timer;

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            switch (event.type) {
            case sf::Event::Closed:
                window.close();
                break;
            }
        }

        // Rotate the circle
        circle.setRotation(timer.getElapsedTime().asMilliseconds() / 10);

        // Clear the scene in the render texture
        buffer.clear(sf::Color(64, 127, 192));

        // Draw our circle
        circle.setPosition(25, 37.5f); // Despite the float offset, the result will be pixel accurate!
        buffer.draw(circle);

        // Finish the render texture drawing
        buffer.display();

        // Draw the render texture's contents
        window.draw(bufferSprite);

        // Let's render the upscaled shape for comparison
        circle.setPosition(75, 37.5f);
        window.draw(circle);

        // Display the results
        window.display();
    }

    return 0;
}

一旦编译完毕,这将导致两个旋转六面&#34;圆圈#34;一个像素化(通过渲染纹理&#39;分辨率)和一个锐利(由于简单的升级):

Example program running.