Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用SFML&;获取要打印到屏幕上的像素;C++;_C++_Sfml_Pixel_Fstream_Points - Fatal编程技术网

C++ 使用SFML&;获取要打印到屏幕上的像素;C++;

C++ 使用SFML&;获取要打印到屏幕上的像素;C++;,c++,sfml,pixel,fstream,points,C++,Sfml,Pixel,Fstream,Points,我的主要目标是转换文本文件(numbers.txt)中的一组数字对,并将这些数字中的每一个指定为像素坐标。然后我需要在每一个坐标上打印一个像素,最终形成一幅由单个像素组成的美国图片 现在,我所做的只是尝试使用SFML将任何颜色的像素打印到屏幕上。也许如果我能做到这一点,我可以以某种方式将存储在numbers.txt中的数字转换为坐标,每个坐标上都有一个像素 这是我到目前为止的代码 #include <iostream> #include <fstream> // the

我的主要目标是转换文本文件(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;
}
#包括
#包括//fstream类
#包括
#include//将有助于使用字符串
使用名称空间std;
int main()
{
弦线;
int宽度、高度;
fstream-myFile;
myFile.open(“resources/numbers.txt”);
如果(我的文件)
{
库特宽度>>高度;
//在屏幕上打印一个黑色窗口
sf::RenderWindow窗口(sf::VideoMode(宽度、高度),“地图”);
sf::纹理;
雪碧雪碧;
雪碧。塞特维特(纹理);
while(window.isOpen())
{
sf::事件;
while(window.pollEvent(事件))
{
开关(事件类型)
{
案例sf::事件::已结束:
window.close();
打破
}
}
window.clear();
//窗口。绘制(精灵);
//*************在这里画精灵****************
//创建一个新顶点
sf:顶点;
//定位
顶点位置=sf::向量2f(10.f,50.f);
//定色
vertex.color=sf::color::红色;
//设置其纹理坐标
texCoords=sf::Vector2f(100.f,100.f);
sf::顶点阵列像素(sf::点);
窗口绘制(像素);
//显示窗口
window.display();
}
返回0;
}
<>这确实编译得很好,但是只显示一个黑屏给用户。我需要在黑屏上得到一个像素。我对SFML和C++很陌生,在SFML主页上查到了点和像素,但是只得到了这么远。你们有人能帮助吗?它是超级赞赏!


一切都很好。

在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
,其中颜色由作为参数传递给该函数的RGBs值创建

但是你必须把这些像素存储在某个地方

因此,让我们创建窗口和一些容器来保存像素,例如
std::vector
并添加一些像素:

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));
在尝试显示之前,请确保添加更多像素。在高分辨率屏幕上,单个像素可能不可见

编辑:您的代码也可以工作,但忘记将
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
,否则您可能不会注意到屏幕上的单个像素

希望有帮助

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);