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

Author Topic: How to best handle Textures  (Read 11492 times)

0 Members and 1 Guest are viewing this topic.

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: How to best handle Textures
« Reply #15 on: December 30, 2012, 02:09:18 pm »
Quote
Interestingly, a month ago you have expressed the exact opposite of your current statement ;)
I'm not saying binding is light, I'm saying switching sprite's texture to another one and proceeding with drawing everything as before is.

The point is that they probably already draw something in between their huge animations so it doesn't matter if animation's texture id is 20, 30 or 666, because the last catched texture is 1000 from something entirely different so they have to rebind anyway.
Like this:
#include <iostream>
#include <SFML/Graphics.hpp>

int main()
{
        sf::RenderWindow app(sf::VideoMode(768,768),"app");
        sf::Texture background,things[2];
        int index=0,framecount=0;
        sf::Uint64 timecount=0;
        {
                sf::Image image;
                image.create(1024,1024,sf::Color::White);
                background.loadFromImage(image);
                image.create(1024,1024,sf::Color::Red);
                things[0].loadFromImage(image);
                image.create(1024,1024,sf::Color::Blue);
                things[1].loadFromImage(image);
        }
        sf::Sprite back(background);
        sf::Sprite thing(things[0]);
        sf::Clock clock;
        sf::Event eve;
        while(app.isOpen())
        {
                while(app.pollEvent(eve))
                {
                        //switch texture, single ptr swap
                        if(eve.type==sf::Event::KeyPressed) thing.setTexture(things[index=!index]);

                        else if(eve.type==sf::Event::Closed) app.close();
                }
                app.clear();
                app.draw(back);//1!=2, 1!=3, bind id 1
                app.draw(thing);//2!=1, 3!=1 , bind id 2 or id 3
                app.display();
                std::cout<<"Avg:"<<(timecount+=clock.getElapsedTime().asMicroseconds())/(++framecount)<<std::endl;
                std::cout<<"This frame:"<<clock.restart().asMicroseconds()<<std::endl;
        }
}
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How to best handle Textures
« Reply #16 on: December 30, 2012, 03:49:55 pm »
Quote
The point is that they probably already draw something in between their huge animations so it doesn't matter if animation's texture id is 20, 30 or 666, because the last catched texture is 1000 from something entirely different so they have to rebind anyway.
You're right, and this is something that is often overlooked in this kind of discussions.
Laurent Gomila - SFML developer

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: How to best handle Textures
« Reply #17 on: December 30, 2012, 03:51:51 pm »
Quote
You're right, and this is something that is often overlooked in this kind of discussions.
??? Didn't you two just overlook that?
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How to best handle Textures
« Reply #18 on: December 30, 2012, 03:59:13 pm »
I didn't say anything, except that texture switch is expensive. Which is true ;)
Laurent Gomila - SFML developer

ZenonSeth

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: How to best handle Textures
« Reply #19 on: January 03, 2013, 05:04:23 am »
I didn't say anything, except that texture switch is expensive. Which is true ;)
I was looking through these forums, and this topic seems to be quite related to what I was wondering - so I'm going to ask here instead of making a new thread.

If I draw 50 sf::Sprite in immediate succession (i.e. nothing drawn in between them) would the drawing be faster if all 50 were set from the same texture (but potentially different texture rectangles), than if all 50 had individual textures?

I'm aware in pure OpenGL the above would be faster due to not having to re-bind textures for drawing, but I'm not 100% familiar with all the code in SFML (especially 2.0 RC), so I'd thought I'd ask directly.
Thanks.

pdinklag

  • Sr. Member
  • ****
  • Posts: 330
  • JSFML Developer
    • View Profile
    • JSFML Website
Re: How to best handle Textures
« Reply #20 on: January 03, 2013, 05:09:48 am »
If I draw 50 sf::Sprite in immediate succession (i.e. nothing drawn in between them) would the drawing be faster if all 50 were set from the same texture (but potentially different texture rectangles), than if all 50 had individual textures?

I'm aware in pure OpenGL the above would be faster due to not having to re-bind textures for drawing, but I'm not 100% familiar with all the code in SFML (especially 2.0 RC), so I'd thought I'd ask directly.
Thanks.
Yes.
SFML binding a texture should not be alot different from what you'd do in raw OpenGL code.
JSFML - The Java binding to SFML.

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: How to best handle Textures
« Reply #21 on: January 03, 2013, 06:28:19 am »
I am making my own sprite (and their container with many shortcut functions and access to each sprite, texture rect and transformations both global and individual via brackets) for those cases. If you want I can give you what I have so far, it's not tested yet, but it could give you ideas if you want to increase performances by reducing the amount of draw calls in sprites with the same texture.

Edit: Actually I've already coded most of it and it should be functional, I just haven't tested it.
« Last Edit: January 03, 2013, 06:30:47 am by masskiller »
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

ZenonSeth

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: How to best handle Textures
« Reply #22 on: January 03, 2013, 02:07:43 pm »
Yes.
SFML binding a texture should not be alot different from what you'd do in raw OpenGL code.
I'm just going to assume you're more familiar with sfml than I, but it's certainly no stretch of the imagination that it works like that. Thanks.

Edit: Actually I've already coded most of it and it should be functional, I just haven't tested it.
Heh, reminds me of the quote from Donald Knuth - "Beware of bugs in the above code; I have only proved it correct, not tried it."
But no thanks, really sf::Sprite does 99.9% of the things I need, and the one thing that's slightly bugging me about it - the lack of setSize - is easily circumvented with setScale.