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

Author Topic: How to create and manipulate a pixelarray?  (Read 2007 times)

0 Members and 1 Guest are viewing this topic.

smash

  • Newbie
  • *
  • Posts: 6
    • View Profile
How to create and manipulate a pixelarray?
« 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:

Code: [Select]
sf::Uint8 *pixels = new sf::Uint8[800 * 600 * 4]

that should be right.

And now, I want to access it:

Code: [Select]
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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
How to create and manipulate a pixelarray?
« Reply #1 on: August 03, 2011, 08:05:44 am »
The formula is wrong. It should be:
Code: [Select]
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;
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to create and manipulate a pixelarray?
« Reply #2 on: August 03, 2011, 10:06:46 am »
Quote from: "smash"
that should be right.
Yes, but why don't you use STL containers? They free you from the burden of manual memory management.
Code: [Select]
std::vector<sf::Uint8> pixels(800 * 600 * 4);
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

smash

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: How to create and manipulate a pixelarray?
« Reply #3 on: August 03, 2011, 01:18:17 pm »
Quote from: "Laurent"
The formula is wrong. It should be:
Code: [Select]
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:

Code: [Select]

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.


Quote from: "Nexus"
Quote from: "smash"
that should be right.
Yes, but why don't you use STL containers? They free you from the burden of manual memory management.
Code: [Select]
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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
How to create and manipulate a pixelarray?
« Reply #4 on: August 03, 2011, 10:02:23 pm »
Laurent Gomila - SFML developer