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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - smash

Pages: [1]
1
Graphics / 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??

2
Graphics / Re: How to create and manipulate a pixelarray?
« 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?

3
Graphics / 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.

4
General / Different behaviours on Linux/Windows
« on: April 05, 2010, 07:46:14 pm »
I love you :)

thank you very much!!

yeah of course it's no SFML, problem... thats the reason why I post it also at a c++ community.

and... I dont know, why I am creating the projectiles, before I shoot them... it was my first idea and it was working pretty good.

i think i will use an integer to store the no. of bullets...

thank you for your great help and this idea!


greets

5
General / Different behaviours on Linux/Windows
« on: April 05, 2010, 06:06:12 pm »
If I would know, where the error related code is, I would fix it...

On Linux it's working on Windows it's not working... I dont know which code lines have errors...


greets

6
General / Different behaviours on Linux/Windows
« on: April 05, 2010, 01:38:08 pm »
Hallo everybody :)

A started to create a game with SFML in c++, a space shooter.

Here is the Code: space-game.zip

So far I have a player (controlling with arrow-keys) weapons for this player (switching with L-Controll) an of course fireing this weapons (with space).

Later there have to be enemies and bosses, highscores etc...

I developed this game on linux (ubuntu 32 bit) and there the game is working really great on linux.

If i put this code on my Windows XP PC and build the game there the behavior of the game is different.

Instead of 50 Bullets I got only 38 there and instead of 10 Missiles I got only 4.

On both (windows an linux) I use g++ and SFML 1.5


Here is the Win32 Debug Executable (with gfx, ready to run): space-game-bin.zip
Here you could see my Problem and you are able to Debug it..


What did I wrong? Did I make a misstake with the memory and the pointers?? (if yes, why its running perfect on linux?)

I hope someone could help me :)

Pages: [1]
anything