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

Author Topic: sf::Image -> SetPixel() and LoadFromPixels() both slow??  (Read 3095 times)

0 Members and 1 Guest are viewing this topic.

smash

  • Newbie
  • *
  • Posts: 6
    • View Profile
sf::Image -> SetPixel() and LoadFromPixels() both slow??
« on: August 03, 2011, 08:13:12 pm »
Hello,

I got a problem.

I want to code Langton's Ant: http://en.wikipedia.org/wiki/Langton%27s_ant

with 800x600 Pixels
I got a Map with 200x200 Fields (each 4x3 Pixels)
So if the Ant change on field, there are 12 pixel which have to be changed.

1. I tried it with 200x200 shapes (one field one shape) -> very slow if I have to draw every 200x200 (40.000) shapes after every Clear().
If I dont do the Clear and do every step only one draw of the changed shape it's real fast, but with black background... :/

2. then I tried to do it via sf::Image SetPixel(), 12 times for one field. -> not as slow as the shape method but still slow

3. now I'm trying to do it via sf::Image LoadFromPixels(), which is as slow as the SetPixel() way.
There are 48 assignments every step (12 pixel * 4 (rgba)) that's done really really fast, but the LoadFromPixels() method needs very much time, unfortunately...

here the Code for Method 2. and 3.:
No.2 the SetPixel() way is not active) just No.3 is active


Code: [Select]
void Grid::SetFieldPixel(int xMap, int yMap, sf::Color color)
{
    int xCoord = xMap *4;
    int yCoord = yMap *3;

    for(int i=0; i < 4; i++)
    {
        for(int j=0; j < 3; j++)
        {
            //GridImage.SetPixel(xMap*4+i, yMap*3+j, color);

            int pixel = ((xCoord+i) + 800 * (yCoord+j)) *4;

            if(color == sf::Color::Black)
            {
                pixels[ pixel +0] = 0;
                pixels[ pixel +1] = 0;
                pixels[ pixel +2] = 0;
            }
            else
            {
                pixels[ pixel +0] = 255;
                pixels[ pixel +1] = 255;
                pixels[ pixel +2] = 255;
            }

            pixels[((xMap*4+i)+ 800 * (yMap*3+j))*4 +3] = 255;
        }
    }
}


Code: [Select]
void Grid::Draw(void)
{
    sf::Sprite Sprite1;
    GridImage.LoadFromPixels(800, 600, pixels);
    Sprite1.SetImage(GridImage);
    gApp->Draw(Sprite1);
}



Have you any Idea to draw it (much more) faster??

Haikarainen

  • Guest
sf::Image -> SetPixel() and LoadFromPixels() both slow??
« Reply #1 on: August 03, 2011, 09:47:26 pm »
Do this in your draw session:
1. Draw whiteness
2. Draw a grid(loaded from file, or generated at init, optional)
3. Draw all black squares:

Create a black rect
Iterate thru all the 4x3 "blocks"
If state is black, draw the rect at current position.

 

anything