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 - zarro110

Pages: [1]
1
General / Re: Memory usage problem
« on: February 20, 2022, 05:33:06 pm »
I am running it on Windows 10 and am programming with Visual Studio 2019. I dynamically linked SMFL following the tutorial on the site and everything works. This is what happens with a new project, using only the code posted above:



it does this both in debug and release. I use SFML-2.5.1  C++ 15 (2017) - 32-bit which I downloaded two days ago.

2
General / Memory usage problem
« on: February 19, 2022, 06:18:23 pm »
Im am new to SFML and have made a simple program to draw a grid of squares and I was trying to measure performance. When I run this I can see the memory usage climb to above a gigabyte before dropping and rising again. What I don't understand is why this much memory is being allocated. It happens somewhere in the draw function. Do I have to free the Vertexarray in some way? If yes, how? (resizing to 0 doesn't help)


#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <vector>
#include <chrono>

class World {
public:
        std::vector<std::vector<int>> map;
        sf::Vector2i worldSize;
        std::vector<sf::Color> colors;

        World(int width, int height) {
                worldSize = sf::Vector2i(width, height);

                for (int x = 0; x < worldSize.x; x++) {
                        std::vector<int> temp;
                        for (int y = 0; y < worldSize.y; y++) {
                                temp.push_back(0);
                        }
                        map.push_back(temp);
                }
                colors.push_back(sf::Color::White);
                colors.push_back(sf::Color::Black);
        }

        void draw(sf::RenderWindow* screen) {
                sf::Vector2u screenSize = screen->getSize();
                sf::View view = screen->getView();
                sf::Vector2f viewCenter = view.getCenter();
                sf::Vector2f viewSize = view.getSize();

                float tileSize = 1;

                int x0 = 0;
                if ((viewCenter.x - (viewSize.x / 2)) > x0)
                        x0 = (viewCenter.x - (viewSize.x / 2));

                int x1 = worldSize.x;
                if ((viewCenter.x + (viewSize.x / 2)) + 1 < x1)
                        x1 = (viewCenter.x + (viewSize.x / 2)) + 1;

                int y0 = 0;
                if ((viewCenter.y - (viewSize.y / 2)) > y0)
                        y0 = (viewCenter.y - (viewSize.y / 2));

                int y1 = worldSize.x;
                if ((viewCenter.y + (viewSize.y / 2)) + 1< y1)
                        y1 = (viewCenter.y + (viewSize.y / 2)) + 1;

                int xSize = x1 - x0;
                int ySize = y1 - y0;
               
                if (xSize > 0 && ySize > 0) {
                        sf::VertexArray vertices;
                        vertices.setPrimitiveType(sf::Quads);
                        vertices.resize(xSize * ySize * 4);


                        for (int x = x0; x < x1; x += 1) {
                                for (int y = y0; y < y1; y += 1) {
                                        int current = (((x - x0) * ySize) + (y - y0)) * 4;
                                        vertices[current + 0].position = sf::Vector2f(x * tileSize, y * tileSize);
                                        vertices[current + 1].position = sf::Vector2f((x + 1) * tileSize, y * tileSize);
                                        vertices[current + 2].position = sf::Vector2f((x + 1) * tileSize, (y + 1) * tileSize);
                                        vertices[current + 3].position = sf::Vector2f(x * tileSize, (y + 1) * tileSize);


                                        vertices[current + 0].color = colors[map[x][y]];
                                        vertices[current + 1].color = colors[map[x][y]];
                                        vertices[current + 2].color = colors[map[x][y]];
                                        vertices[current + 3].color = colors[map[x][y]];
                                }
                        }

                        screen->draw(vertices);
                        vertices.resize(0);
                }
        }
};

int main()
{

        sf::Vector2f screenSize(500, 500);

        World myWorld(124, 124);

        sf::RenderWindow window(sf::VideoMode(screenSize.x, screenSize.y), "tileworld");
        window.setView(sf::View(sf::Vector2f(myWorld.worldSize) * 0.5f , sf::Vector2f(myWorld.worldSize) * (myWorld.worldSize.x / screenSize.x)));

        auto start = std::chrono::high_resolution_clock::now();
        for (int i = 0; i < 100000; i++) {
                myWorld.draw(&window);
                std::cout << i << "\r";
        }
        auto stop = std::chrono::high_resolution_clock::now();
        std::cout << std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count()/100000 << std::endl;

        return 0;
}

 

Pages: [1]