【问题标题】:Getting a Pixel to print to the screen using SFML & C++使用 SFML 和 C++ 将像素打印到屏幕上
【发布时间】:2018-10-28 04:15:15
【问题描述】:

我的主要目标是从文本文件 (numbers.txt) 转换成对的数字,并将这些数字中的每一个分配为像素坐标。然后我需要在每个坐标处打印一个像素,最终将形成仅由单个像素组成的美国图片。

现在,我正在做的只是尝试使用 SFML 将任何颜色的任何像素打印到屏幕上。也许如果我能做到这一点,我可以以某种方式获取存储在 numbers.txt 中的数字并将它们转换为每个像素都有一个像素的坐标。

这是我目前的代码...

#include <iostream>
#include <fstream> // the fstream class
#include <SFML/Graphics.hpp>

#include <string> // will help to use strings

using namespace std;

int main()
{

    string line;
    int width, height;
    fstream myFile;
    myFile.open("resources/numbers.txt");

    if (myFile)
    {
        cout << "This file is opened\n";
    }
    else
        return EXIT_FAILURE;

    cout << "Enter 2 integer values";
    cin >> width >> height;

    // Print a black window to the screen

    sf::RenderWindow window(sf::VideoMode(width, height), "MAP");

    sf::Texture texture;

    sf::Sprite sprite;
    sprite.setTexture(texture);

    while (window.isOpen())
    {
        sf::Event event;

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

                    break;
            }
        }

        window.clear();

        //window.draw(sprite);

        //*************DRAW THE SPRITE HERE****************
        // create a new vertex
        sf::Vertex vertex;

        // set its position
        vertex.position = sf::Vector2f(10.f, 50.f);

        // set its color
        vertex.color = sf::Color::Red;

        // set its texture coordinates
        vertex.texCoords = sf::Vector2f(100.f, 100.f);

        sf::VertexArray pixel(sf::Points);

        window.draw(pixel);

        //display window
        window.display();

    }
    return 0;
}

这确实编译得很好。但只向用户显示黑屏。我需要以某种方式在那个黑屏上获得一个像素。我对 SFML 和 C++ 非常陌生,并在 SFML 主页上查找了有关点和像素的信息,但仅此而已。你们中的任何人都可以帮忙吗?非常感谢!

一切顺利。

【问题讨论】:

    标签: c++ sfml pixel fstream points


    【解决方案1】:

    SFML 中有几种方法可以在屏幕上绘制单个像素。第一个是您尝试对sf::VertexArray 执行的操作。 在我看来,使用sf::RectangleShape 的方法更简单、更直观,您可以在其中创建 1x1 大小的矩形,我将向您展示这个。

    首先,我们可以添加有用的函数,在给定的位置和颜色创建“像素”。

    sf::RectangleShape addPixel(sf::Vector2f position, sf::Uint8 red, sf::Uint8 green, sf::Uint8 blue)
    {
        sf::RectangleShape pixel;
        pixel.setSize({ 1.f, 1.f });
        pixel.setFillColor({ red, green, blue });
        pixel.setPosition(position);
        return pixel;
    }
    

    该函数将返回 sf::RectangleShape,并使用从 RGB 值创建的颜色作为参数传递给该函数。

    但是您必须将这些像素存储在某个地方。

    所以让我们创建窗口,以及一些容器来保存我们的像素,例如std::vector&lt;sf::RectangleShape&gt; 并添加一些像素:

    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
    std::vector<sf::RectangleShape> pixels;
    pixels.push_back(addPixel({ 100, 100 }, 255, 0, 0));
    pixels.push_back(addPixel({ 101, 100 }, 255, 255, 0));
    pixels.push_back(addPixel({ 102, 100 }, 0, 0, 0));
    pixels.push_back(addPixel({ 103, 100 }, 255, 0, 255));
    

    最后创建主循环,在每次迭代中绘制所有像素。

    while (window.isOpen())
    {
        window.clear();
        for (const auto& pixel : pixels)
        {
            window.draw(pixel);
        }
        window.display();
    }
    

    确保在尝试显示之前添加更多像素。在高分辨率屏幕上,单个像素可能是不可见的。

    编辑:您的代码也可以工作,但您忘记将 sf::Vertex 添加到 sf::VertexArray。 你应该做的就是:

    sf::Vertex vertex1;
    vertex1.position = sf::Vector2f(10.f, 50.f);
    vertex1.color = sf::Color::Red;
    sf::VertexArray pixel;
    pixel.append(vertex1); // That's what you forgot.
    window.draw(pixel);
    

    如上,使用这种方法还可以将更多的sf::Vertex 彼此相邻添加,否则您可能不会注意到屏幕上的单个像素。

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-14
      • 2022-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-08
      • 2013-05-28
      相关资源
      最近更新 更多