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

Author Topic: Drawing of Shape very slow?  (Read 2739 times)

0 Members and 1 Guest are viewing this topic.

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Drawing of Shape very slow?
« on: January 23, 2011, 04:35:43 am »
I'm trying to use Shape to draw random noise in 8x8 blocks but it's running at about 11FPS. This is my code:
Code: [Select]
//PIXEL_SIZE is defined to 8
size_t h = (engine::Window->GetHeight() / PIXEL_SIZE) + 1;
size_t w = (engine::Window->GetWidth() / PIXEL_SIZE) + 1;
Shape s;
for (size_t y = 0; y < h; y++) {
for (size_t x = 0; x < w; x++) {
s = Shape::Rectangle(FloatRect(float(x * PIXEL_SIZE), float(y * PIXEL_SIZE), float(PIXEL_SIZE), float(PIXEL_SIZE) ), Color(utils::Rand(5,10), utils::Rand(10,14), utils::Rand(200,255) ) );
rt.Draw(s);
}
}
rt.Display();
engine::Window->Draw( Sprite( rt.GetImage() ) );


What is a better way to draw noise like this?

In case it helps this is what it renders, albeit slowly:


If there is no way to do this with Shape, can someone give me an example of how to do this with a shader?

onEnterFrame

  • Newbie
  • *
  • Posts: 10
    • View Profile
Drawing of Shape very slow?
« Reply #1 on: January 23, 2011, 11:00:27 am »
i have never used shape before. i just picked up this library 2 days ago.

but its not the fault of shape or the library.

you are creating at least 600/8 * 400/8 = 3750 new shape objects every frame.

not to mention all the color objects and rect objects that you are copying and moving around.

and if its within an infinite loop in the function itself i dont even know what the memory stack looks like. i dont remember the fine details of allocation in C++ but it cannot be good.

the bottom line is that youre being wasteful and need to think about the best way to utilize your resources.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Drawing of Shape very slow?
« Reply #2 on: January 23, 2011, 11:08:25 am »
Filling a raw array of sf::Uint8 [width * height * 4] and then uploading it to an sf::Image with Image::LoadFromPixels will probably be faster.
Laurent Gomila - SFML developer

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Drawing of Shape very slow?
« Reply #3 on: January 23, 2011, 07:36:40 pm »
Quote from: "Laurent"
Filling a raw array of sf::Uint8 [width * height * 4] and then uploading it to an sf::Image with Image::LoadFromPixels will probably be faster.


Thanks, that works, it draws at acceptable speed even with PIXEL_SIZE set to 1 now.

 

anything