有没有更好的方法在 SFML 中绘制多个点?
Is there a better way to draw multiple points in SFML?
我试图用随机颜色在 800 x 800 window 上绘制每个像素,并且我使用 sf::Vertex
绘制像素,但是当我 运行 程序, 它很慢。有没有更好的方法来绘制多个点更快,使用更少的内存?这是我写的代码:
#include <SFML/Graphics.hpp>
#include <ctime>
#include <random>
int main() {
// Window
sf::RenderWindow window{ sf::VideoMode{ 800u, 800u }, "Views" };
sf::Clock clock;
static std::mt19937 randomEngine{ static_cast<unsigned int>(std::time(nullptr)) };
std::uniform_int_distribution<int> randDist{ 0, 255 };
// Main game loop
while (window.isOpen()) {
// Event loop
sf::Event sfmlEvent;
while (window.pollEvent(sfmlEvent)) {
if (sfmlEvent.type == sf::Event::Closed) {
window.close();
}
}
// Draw
for (float i = 0; i < window.getSize().x; ++i) {
for (float j = 0; j < window.getSize().y; ++j) {
uint8_t r = randDist(randomEngine);
uint8_t g = randDist(randomEngine);
uint8_t b = randDist(randomEngine);
sf::Vertex pixel{ { i, j }, { r, g, b } };
window.draw(&pixel, 1, sf::Points);
}
}
// Display
window.display();
}
}
感谢其中一条评论,我现在知道如何解决这个问题了。
我可以使用 sf::Image
而不是使用一堆 sf::Vertecies
。我所做的是创建了一个 window 大小的图像并将其全黑。然后我不绘制每个单独的点,而是将图像中的每个像素设置为随机颜色。然后,在退出 for 循环后,我将图像加载到纹理中并将该纹理设置为我绘制到 window.
上的精灵
这是更新后的代码:
#include <SFML/Graphics.hpp>
#include <ctime>
#include <random>
int main() {
// Window
sf::RenderWindow window{ sf::VideoMode{ 800u, 800u }, "Views" };
sf::Clock clock;
static std::mt19937 randomEngine{ static_cast<unsigned int>(std::time(nullptr)) };
std::uniform_int_distribution<int> randDist{ 0, 255 };
sf::Image image;
image.create(window.getSize().x, window.getSize().y, sf::Color::Black);
sf::Texture texture;
sf::Sprite sprite;
// Main game loop
while (window.isOpen()) {
// Event loop
sf::Event sfmlEvent;
while (window.pollEvent(sfmlEvent)) {
if (sfmlEvent.type == sf::Event::Closed) {
window.close();
}
}
// Draw
for (float i = 0; i < window.getSize().x; ++i) {
for (float j = 0; j < window.getSize().y; ++j) {
uint8_t r = randDist(randomEngine);
uint8_t g = randDist(randomEngine);
uint8_t b = randDist(randomEngine);
image.setPixel(i, j, { r, g, b });
}
}
texture.loadFromImage(image);
sprite.setTexture(texture);
window.draw(sprite);
// Display
window.display();
}
}
我试图用随机颜色在 800 x 800 window 上绘制每个像素,并且我使用 sf::Vertex
绘制像素,但是当我 运行 程序, 它很慢。有没有更好的方法来绘制多个点更快,使用更少的内存?这是我写的代码:
#include <SFML/Graphics.hpp>
#include <ctime>
#include <random>
int main() {
// Window
sf::RenderWindow window{ sf::VideoMode{ 800u, 800u }, "Views" };
sf::Clock clock;
static std::mt19937 randomEngine{ static_cast<unsigned int>(std::time(nullptr)) };
std::uniform_int_distribution<int> randDist{ 0, 255 };
// Main game loop
while (window.isOpen()) {
// Event loop
sf::Event sfmlEvent;
while (window.pollEvent(sfmlEvent)) {
if (sfmlEvent.type == sf::Event::Closed) {
window.close();
}
}
// Draw
for (float i = 0; i < window.getSize().x; ++i) {
for (float j = 0; j < window.getSize().y; ++j) {
uint8_t r = randDist(randomEngine);
uint8_t g = randDist(randomEngine);
uint8_t b = randDist(randomEngine);
sf::Vertex pixel{ { i, j }, { r, g, b } };
window.draw(&pixel, 1, sf::Points);
}
}
// Display
window.display();
}
}
感谢其中一条评论,我现在知道如何解决这个问题了。
我可以使用 sf::Image
而不是使用一堆 sf::Vertecies
。我所做的是创建了一个 window 大小的图像并将其全黑。然后我不绘制每个单独的点,而是将图像中的每个像素设置为随机颜色。然后,在退出 for 循环后,我将图像加载到纹理中并将该纹理设置为我绘制到 window.
这是更新后的代码:
#include <SFML/Graphics.hpp>
#include <ctime>
#include <random>
int main() {
// Window
sf::RenderWindow window{ sf::VideoMode{ 800u, 800u }, "Views" };
sf::Clock clock;
static std::mt19937 randomEngine{ static_cast<unsigned int>(std::time(nullptr)) };
std::uniform_int_distribution<int> randDist{ 0, 255 };
sf::Image image;
image.create(window.getSize().x, window.getSize().y, sf::Color::Black);
sf::Texture texture;
sf::Sprite sprite;
// Main game loop
while (window.isOpen()) {
// Event loop
sf::Event sfmlEvent;
while (window.pollEvent(sfmlEvent)) {
if (sfmlEvent.type == sf::Event::Closed) {
window.close();
}
}
// Draw
for (float i = 0; i < window.getSize().x; ++i) {
for (float j = 0; j < window.getSize().y; ++j) {
uint8_t r = randDist(randomEngine);
uint8_t g = randDist(randomEngine);
uint8_t b = randDist(randomEngine);
image.setPixel(i, j, { r, g, b });
}
}
texture.loadFromImage(image);
sprite.setTexture(texture);
window.draw(sprite);
// Display
window.display();
}
}