【发布时间】: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