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.


Topics - danikaze

Pages: [1]
1
Graphics / Just wondering (performance)
« on: April 03, 2013, 03:28:50 pm »
Even if there are several classes to draw tile maps, I wanted to test 3 options and see the difference of performance:
  • filling a map using Vertex
  • pregenerate an image and use it as chunk
  • use vertexArray

As I thought, just using vertex was going to be slow, so I thought about pre-rendering bigger chunks and using them to save draw() calls.

What was my surprise when I saw the test results (debug mode)...

Filling a 800x600 window, with 32x32 tiles (and a simple chunk of 800x600)
  • filling a map using Vertex (475 calls): 4706 ms
  • pregenerated image and used as chunk (1 call): 4442ms
  • vertexArray (1 call to draw): 57 ms

Why the big difference between using only 1 800x600 image and calling once to draw but using 1900 vertices?
My common sense says that [1 draw / 800x600px / 4 vertices] should be faster than [1 draw / 800x600px / 1900 vertices]

In release mode the results are quite different...
  • filling a map using Vertex (475 calls): 544 ms
  • pregenerated image and used as chunk (1 call): 7ms
  • vertexArray (1 call to draw): 5399 ms

But again, I don't know why the vertexArray (1 call / 1900 vertices) is much slower than the Vertex call (475 calls / 1900 vertices)

Here is the code of the test, if you wanna try...
        sf::Clock clock;

        // prepare vertices[4]
        sf::Texture* tilesTexture = ResourceManager::getTexture("tiles.png");
        sf::Vertex vertices1[4];
        vertices1[0].position = sf::Vector2f(0, 0);
        vertices1[1].position = sf::Vector2f(31, 0);
        vertices1[2].position = sf::Vector2f(31, 31);
        vertices1[3].position = sf::Vector2f(0, 31);

        vertices1[0].texCoords = sf::Vector2f(0, 0);
        vertices1[1].texCoords = sf::Vector2f(31, 0);
        vertices1[2].texCoords = sf::Vector2f(31, 31);
        vertices1[3].texCoords = sf::Vector2f(0, 31);

        sf::RenderStates VertexStates;
        VertexStates.texture = tilesTexture;

        // test vertices[4]
        clock.restart();
        for(int x=0; x<25; x++)
        {
                for(int y=0; y<19; y++)
                {
                        vertices1[0].position = sf::Vector2f(32*x, 32*y);
                        vertices1[1].position = sf::Vector2f(32*(x+1), 32*y);
                        vertices1[2].position = sf::Vector2f(32*(x+1), 32*(y+1));
                        vertices1[3].position = sf::Vector2f(32*x, 32*(y+1));
                        window.draw(vertices1, 4, sf::Quads, VertexStates);
                }
        }
        cout << "vertex[4]: " << clock.getElapsedTime().asMicroseconds() << endl;

        // prepare 1 draw with a 800x600 image
        sf::Texture* fondo = ResourceManager::getTexture("bg800x600.png");
        vertices1[0].position = sf::Vector2f(0, 0);
        vertices1[1].position = sf::Vector2f(799, 0);
        vertices1[2].position = sf::Vector2f(799, 599);
        vertices1[3].position = sf::Vector2f(0, 599);

        vertices1[0].texCoords = sf::Vector2f(0, 0);
        vertices1[1].texCoords = sf::Vector2f(799, 0);
        vertices1[2].texCoords = sf::Vector2f(799, 599);
        vertices1[3].texCoords = sf::Vector2f(0, 599);

        // test the image chunk
        clock.restart();
        VertexStates.texture = fondo;
        window.draw(vertices1, 4, sf::Quads, VertexStates);
        cout << "chunk800x600: " << clock.getElapsedTime().asMicroseconds() << endl;

        // prepare the VertexArray call
        VertexStates.texture = tilesTexture;
        sf::VertexArray vertices2(sf::Quads, 25*19*4);
        for(int x=0; x<25; x++)
        {
                for(int y=0; y<19; y++)
                {
                        vertices2[(x + y*25)*4].position = sf::Vector2f(32*x, 32*y);
                        vertices2[(x + y*25)*4+1].position = sf::Vector2f(32*(x+1), 32*y);
                        vertices2[(x + y*25)*4+2].position = sf::Vector2f(32*(x+1), 32*(y+1));
                        vertices2[(x + y*25)*4+3].position = sf::Vector2f(32*x, 32*(y+1));

                        int i = rand()%9;
                        int j = rand()%9;
                        vertices2[(x + y*25)*4].texCoords = sf::Vector2f(i*32, 32*j);
                        vertices2[(x + y*25)*4+1].texCoords = sf::Vector2f(32*(i+1), 32*j);
                        vertices2[(x + y*25)*4+2].texCoords = sf::Vector2f(32*(i+1), 32*(j+1));
                        vertices2[(x + y*25)*4+3].texCoords = sf::Vector2f(32*i, 32*(j+1));
                }
        }

        // test the VertexArray call
        clock.restart();
        window.draw(vertices2, VertexStates);
        cout << "vertexArray: " << clock.getElapsedTime().asMicroseconds() << endl;

 

BTW bg800x600.png is a 800x600 image, and tiles.png a 256x256 image.

2
Feature requests / Texture::Texture explicit
« on: December 17, 2012, 08:57:27 pm »
Since it's a expensive resource to load, why not make its constructor explicit to avoid errors.

I was programming a resource manager (which returns a sf::Texture&) and expended about 15 minutes to notice this error:

sf::Texture texture = ResourceManager::getTexture("file.png");
sf::Sprite sprite(texture);
ResourceManager::freeTexture("file.png");

// sprite still displaying even if the Resource Manager texture was freed
 

The reason of the sprite working fine is because sf::Texture has a non-explicit copy constructor. So it was alocating twice the memory required for an image.

The intended behaviour was to free the texture and no image should be displayed

The correct code for this is:

sf::Texture& texture = ResourceManager::getTexture("file.png");
 

I know everything is right, but it would be better if that triggers an error.

If you want to copy a texture still would be possible with the following code:
sf::Texture& texture(ResourceManager::getTexture("file.png"));
 

Same with other expensive resources, as Image, etc.

3
Graphics / [SOLVED] Multiple textures vs bigger single texture
« on: December 01, 2012, 10:57:33 pm »
I was wondering (and couldn't find any info in the forum :S), which way is better (talking about performance):
- load 1 texture for each image
- load 1 bigger texture containing all images (and then use a portion of it when loading sprites)

Is there any difference?
The question came to me viewing some directX (?, not sure) code, where all images are loaded into just one texture and then used from them. Probably to manage graphic cards things...

Thanks in advance!

4
Graphics / [SOLVED] How to do (composed) transparency? shaders?
« on: September 22, 2012, 08:39:19 pm »
Hello!

I've searched for this question in this forum but I couldn't find it so, I want to know how to apply a % of transparency to several sprites/images at the same time.

Let me explain this:

Think about Photoshop (or gimp if you like). While the alpha channel of each sprite is like the opacity of each layer in Photoshop, and it's easily modificable, what I want to do is change the opacity of a group/folder of layers in Photoshop. That is, draw several images and then and only then apply the opacity value.

Here's an example:
Think of a playable character composed by 3 images (head/body/feet).
Let's say I do (pseudo-code)
player.alpha(60%)
I want to show this:


But applying alpha to each image, it would draw this:


So my question is, how to do this? Do I need to play with OpenGL (I never touched it and I don't know how) or there's a way in SFML?

Thanks!

[attachment deleted by admin]

5
Graphics / Text width?
« on: September 06, 2012, 01:45:07 am »
Hello there!

I'm new in SFML (I come from allegro/gosu/others) and I wonder if there's any way to know the width of a text.

I noticed there isn't any method such as float Text::width() that returns the size in pixels of the Text string.

Why do I need this? Well, to draw my text into boxes (with lines limited by a certain number of pixels) and have a custom format system (to change style, color...) which will draw the text by parts.

So is there (or will be in the 2.0) any function to know that?

Thank you in advance!

Pages: [1]
anything