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

Author Topic: Sfml drawing faster algorithm slower  (Read 2448 times)

0 Members and 1 Guest are viewing this topic.

yoyo

  • Newbie
  • *
  • Posts: 4
    • View Profile
Sfml drawing faster algorithm slower
« on: December 18, 2019, 03:42:06 pm »
Hi. (Sorry for grammar I don't use english everyday)

I have big problem. I wrote a lichen array function. The problem is with performance.

When I use 2 dimension array of rectangle shape (shape size 6x6). In my function I'm checking conditions and setting color of each rectangle. Then I draw it. So the time of draw is like 20ms and the time of iteriation 100x100 and setting colors/checking conditions  is under 1ms.

I have read that I should use VertexArray of points. So i did it. The draw time is under 0.1ms for 100x100 array and that is perfect but... iterating and swapping colors is over 25ms? No idea why I have the same amount of conditions etc. I just have one more field that I increment to 10000. So its O(1). Maybe the problem is with setting the array. For 100x100 I just set it like that in constructor. But I just do it once so I don't think so. (The Vertex is point and I add this point to VertexArray named vert, other two arrays are normal vectors)

int j = 0;
        int pom = 0;
        int pom2 = 0;
        for (int i = 0; i < 10000; i++) {
                odpornosc.push_back(0);
                flaga.push_back(true);
                if (i == 100) {
                        pom = i;
                        j++;
                        pom2 = 0;
                }
                if (pom + 100 == i) {
                        pom += 100;
                        j++;
                        pom2 = 0;
                }

                point.position = Vector2f(pom2, j);
                pom2++;
                point.color = Color::Green;
                vert.append(point);
        }
 

In my iteration function I use methods only like:

vert[pom].color = Color::Yellow;


Of course I use this function in loop while(window.isOpen())

It looks like that:

https://www.youtube.com/watch?v=XKkq8rPhtDk&feature=youtu.be


Maybe there is possibility to make 2 dimension vertex array and still draw efficiently?
« Last Edit: December 18, 2019, 03:50:51 pm by yoyo »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10800
    • View Profile
    • development blog
    • Email
Re: Sfml drawing faster algorithm slower
« Reply #1 on: December 18, 2019, 06:17:25 pm »
What do you mean with "iterating and swapping colors"?

What does the posted code represent?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

yoyo

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Sfml drawing faster algorithm slower
« Reply #2 on: December 18, 2019, 07:06:54 pm »
Ok my bad ^^

1. Every start of the program I make an VertexArray of primitve points. The code shows how I make it. I make 10k points, set color to green set position each of them that I got square of pixels (100x100). It's constructor.

2.In loop while(window.isOpen()) in other class I use the function that use this vertexArray (I declare this VertexArray once at the start of the game).

3.The function iterate through all elements of my VertexArray for(int i=0; i<VertexArray.size(); i++). So its 10k iterations.

4. In this function I check like 1-12 condition also I change like 1-8 colors. For example vert[i+1].color = Color::Red or vert[i-1].color = Color::Red.

5. Then I just turn off program. It looks like on youtube that I posted link.


The main problem is why this algorithm work faster on rectangle shapes? I dont have bigger loop or something.In two cases in my function I do the same things. Check some conditions and if they are true I increment some values(the same amount, expect one field) and change color of that poins/pixel/shape. And vertexArray its like 20x slower but drawing 20x faster. No idea why :/

I hope I make it clear :D

yoyo

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Sfml drawing faster algorithm slower
« Reply #3 on: December 18, 2019, 08:32:48 pm »
Well, im stupid. I used vectors for int and bool not a dynamic array. Now I got 800fps.

Still is there posibility to make 2 dimension vertex array without loosing efficiently on drawing? Its boring to make conditions for one dimension.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Sfml drawing faster algorithm slower
« Reply #4 on: December 18, 2019, 09:15:51 pm »
Well, im stupid. I used vectors for int and bool not a dynamic array. Now I got 800fps.
This doesn't make sense. std::vector is a dynamic array, and has the same performance as new[]. Just enable Release builds, and make sure optimizations are on and debug symbols off.

If the iteration is the bottleneck even with a Release build, you might be doing something wrong or unnecessarily expensive. Hard to say without knowing what exactly you're doing in the loop.

Also, not sure what you mean with "2D vertex array" -- are you sure you're not looking for a sf::RenderTexture? You could keep the data on the graphics card and modify it either by drawing pixels onto the texture, or by running shaders (i.e. still drawing).
« Last Edit: December 18, 2019, 09:17:46 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

yoyo

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Sfml drawing faster algorithm slower
« Reply #5 on: December 18, 2019, 09:37:45 pm »
Well maybe I missunderstand something in vectors because I'm new in c++ but I got perfromance that I needed.

About 2 dimension VectorArray.

I need VectorArray of points (All of them make big square). I have to color every pixel.For example With RectangleShape[][] I could do something like that.

for(int i=0; i<1000; i++){
for(int j=0; j<1000; j++){
if(condition)
shape[i-1][j+1].setFillColor = Color::Red;
}
}

In vectors I have to add another field to iterate and for square of pixels it looks like that

int pom = 0;
        int j = 0;
        int i = 0;
        for (; i < 1000000; i = i + 1000) {
                for (j = 0; j < 1000; j++) {
if(condition)
vertex[pom-999].color = Color::Red;
pom++;

To make it easier for 1D VertexArray its much harder to make conditions of coloring than RectangleShape 2D array. Thats why I'm asking about it.

 

anything