SFML community forums

Help => Graphics => Topic started by: moyk on October 13, 2020, 09:54:40 am

Title: Changing Color of an object a lot of times per frame is bad? [SOLVED]
Post by: moyk on October 13, 2020, 09:54:40 am
Hi! So I'm a java developer, trying to slowly rip off the garbage collection training wheels by moving to some languages closer to C, so forgive me If I'm doing something incredibly stupid. I'm trying to write a fluid dynamics simulation (Google Mike Ash fluid dynamics, he wrote this crazy article detailing the process)

Long story short; I'm trying to draw the cells to the screen with their density being the alpha channel of the color like this:

   sf::RenderWindow window(sf::VideoMode(N*SCALE, N*SCALE), "Fluid");
   while (window.isOpen()) {
      sf::Event event;
      while (window.pollEvent(event)) {
         if (event.type == sf::Event::Closed) window.close();
      }

      step(fs);

      window.clear(sf::Color::Black);
      sf::RectangleShape square(sf::Vector2f(SCALE, SCALE));
      square.setFillColor(sf::Color(125, 0, 255));

      int x = 0;
      int y = 0;
      for (float d : fs.density) {
         square.setPosition(x*SCALE,y*SCALE);
         float alpha = d * 255;
         square.setFillColor(sf::Color(125, 0, 255, alpha));
         window.draw(square);
         x++;
         if (x % N == 0) {
            x = 0;
            y++;
         }
      }
      
      window.display();

changing the color of the square for some reason results in that crash "exited with code -1073741571" that normally happens when the window gets destroyed in some ungraceful fashion. I should note that that array is 65536 (256^2) elements big, so I'm sure I should probably be doing something completely differently here, I'm just at a loss as to what.
Title: Re: Changing Color of an object a lot of times per frame is bad?
Post by: fallahn on October 13, 2020, 02:06:13 pm
I can't say for sure what causes the crash, but you should probably not be processing your drawables between window.clear() and wndow.display(). The array is also VERY large to be calling draw() on for each square - this is usually a source for a bottleneck and keeping the number of times you call draw() to a minimum is preferable. To do this you could look at using a vertex array https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php

With this you can first process all your geometry outside of the clear/draw/display loop into maybe one or two vertex arrays, and then just draw those.
Title: Re: Changing Color of an object a lot of times per frame is bad?
Post by: eXpl0it3r on October 14, 2020, 05:59:40 pm
The error code translates into 0xC00000FD which is connected to a stack overflow error. Try using a std::vector instead of an array, so the data is dynamically allocated on the heap instead of the stack.
Title: Re: Changing Color of an object a lot of times per frame is bad?
Post by: moyk on October 15, 2020, 03:46:08 pm
The array is also VERY large to be calling draw() on for each square - this is usually a source for a bottleneck and keeping the number of times you call draw() to a minimum is preferable. To do this you could look at using a vertex array https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php

Thank you! That's exactly what I needed!

Now if only I could figure out what's wrong with the rest of my code...
Title: Re: Changing Color of an object a lot of times per frame is bad?
Post by: moyk on October 15, 2020, 04:11:50 pm
The error code translates into 0xC00000FD which is connected to a stack overflow error. Try using a std::vector instead of an array, so the data is dynamically allocated on the heap instead of the stack.

Good to know! I think maybe that explains some of the warnings visual studio is giving me lmao