SFML community forums
Help => Graphics => Topic started by: smash on August 02, 2011, 10:45:47 pm
-
Hi guys (sry for my english :P :oops:)
I'm coding a simulation of langton's ant at the moment.
Because SetPixel is to slow, i want (i read about it) i want to code it via a pixelarray with 800x600 Pixels.
creating:
sf::Uint8 *pixels = new sf::Uint8[800 * 600 * 4]
that should be right.
And now, I want to access it:
pixels[x*800 + y +0] = 0; //red
pixels[x*800 + y +1] = 0; //green
pixels[x*800 + y +2] = 0; //blue
pixels[x*800 + y +3] = 0xff; //alpha
but I think it's not right, isn't it?
If the pixelarray is ready I load it via LoadFromPixels() into an Image and Draw it.
But it happens not the right result.
The picture is not how i expected.
Where's my fault?
Hope someone did something similar.
-
The formula is wrong. It should be:
pixels[(y * 800 + x) * 4 + 0] = red;
pixels[(y * 800 + x) * 4 + 1] = green;
pixels[(y * 800 + x) * 4 + 2] = blue;
pixels[(y * 800 + x) * 4 + 3] = alpha;
-
that should be right.
Yes, but why don't you use STL containers? They free you from the burden of manual memory management.
std::vector<sf::Uint8> pixels(800 * 600 * 4);
-
The formula is wrong. It should be:
pixels[(y * 800 + x) * 4 + 0] = red;
pixels[(y * 800 + x) * 4 + 1] = green;
pixels[(y * 800 + x) * 4 + 2] = blue;
pixels[(y * 800 + x) * 4 + 3] = alpha;
ok, thanks this works for me.
But it's also really really slow (not really faster as SetPixel()).
I got (at a resolution of 800x600) a Map with 200x200 Fields, and every Field consits of 12 Pixels (4x3)
And in every Step I change one of these Fields, draw the whole picture and display it.
Which means I change every Step 12 pixels and so 48 assignments (4 assignments per pixel), in each step.
I show you my Code:
void Grid::SetFieldPixel(int xMap, int yMap, sf::Color color)
{
for(int i=0; i < 4; i++)
{
for(int j=0; j < 3; j++)
{
//GridImage.SetPixel(xMap*4+i, yMap*3+j, color); // the "old" SetPixel() Idea...
if(color == sf::Color::Black)
{
pixels[ (((xMap*4+i) + 800 * (yMap*3+j)) *4) +0] = 0;
pixels[ (((xMap*4+i) + 800 * (yMap*3+j)) *4) +1] = 0;
pixels[ (((xMap*4+i) + 800 * (yMap*3+j)) *4) +2] = 0;
}
else
{
pixels[ (((xMap*4+i) + 800 * (yMap*3+j)) *4) +0] = 255;
pixels[ (((xMap*4+i) + 800 * (yMap*3+j)) *4) +1] = 255;
pixels[ (((xMap*4+i) + 800 * (yMap*3+j)) *4) +2] = 255;
}
pixels[((xMap*4+i)+ 800 * (yMap*3+j))*4 +3] = 255;
}
}
}
And I dont know, why it's so slow, it's ca. as fast as the SetPixel() variant.
that should be right.
Yes, but why don't you use STL containers? They free you from the burden of manual memory management.
std::vector<sf::Uint8> pixels(800 * 600 * 4);
Don't understood your intention... I knew the STL containers, but what do you mean exactly??
someone an Idea to get this faster?
-
Please don't duplicate posts.
http://www.sfml-dev.org/forum/viewtopic.php?t=5477