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?