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

Author Topic: Changing Color of an object a lot of times per frame is bad? [SOLVED]  (Read 2542 times)

0 Members and 1 Guest are viewing this topic.

moyk

  • Newbie
  • *
  • Posts: 3
    • View Profile
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.
« Last Edit: October 15, 2020, 04:12:24 pm by moyk »

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Changing Color of an object a lot of times per frame is bad?
« Reply #1 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Changing Color of an object a lot of times per frame is bad?
« Reply #2 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

moyk

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Changing Color of an object a lot of times per frame is bad?
« Reply #3 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...

moyk

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Changing Color of an object a lot of times per frame is bad?
« Reply #4 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