Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - stati30241

Pages: [1]
1
Graphics / Is there a better way to draw multiple points
« on: April 10, 2021, 07:54:09 pm »
I am trying to draw every pixel on an 800 by 800 window with a random color, and I am drawing the pixel using sf::Vertex, but when I run the program, it is very slow. Is there a better way to draw multiple points that is faster and uses less memory. Here is the code I wrote:

#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();
        }
}

 

2
Graphics / Why is this taking up so much memory
« on: November 14, 2020, 08:09:25 pm »
I was having a problem when I tried to render all the pixels in the window separately, so here a simpler version of my problem. Whenever I try to run this code, the memory usage starts growing very fast and reaches up to 365MB before stopping. Why is this taking up so much memory and how can I prevent this?
#include <SFML/Graphics.hpp>

int main() {
        sf::RenderWindow window{ sf::VideoMode{ 1024, 960 }, "Pixels", sf::Style::Close };

        while (window.isOpen()) {
                sf::Event sfmlEvent;
                while (window.pollEvent(sfmlEvent)) {
                        if (sfmlEvent.type == sf::Event::Closed) {
                                window.close();
                        }
                }

                window.clear();

                for (int i = 0; i < window.getSize().x; ++i) {
                        for (int j = 0; j < window.getSize().y; ++j) {
                                sf::RectangleShape rect{ { 1.0f, 1.0f } };
                                rect.setPosition(i, j);
                                window.draw(rect);
                        }
                }

                window.display();
        }

        return 0;
}

 

Pages: [1]