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

Author Topic: I try to load tiles form png file but it takes time so long.  (Read 3552 times)

0 Members and 1 Guest are viewing this topic.

aratnon

  • Newbie
  • *
  • Posts: 24
    • View Profile
I try to load tiles form png file but it takes time so long.
« on: August 08, 2013, 11:30:28 am »
int i = 0;
        for(int y = 0; y < 17; ++y)
        {
                for(int x = 0; x < 20; ++x)
                {
                         if(!platformTexture[i].loadFromFile("Resources/Tileset/Tilesets.png", sf::IntRect(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT)))
                                std::cout << "error..." << std::endl;
                       
                         ++i;
                }
        }
       

The png file size is 640*640 and tile size is 32*32
Loding tiles from this file takes time about 6 secs.
What is wrong with my code?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: I try to load tiles form png file but it takes time so long.
« Reply #1 on: August 08, 2013, 11:42:39 am »
What's wrong in your code is that:
- you load a PNG file 340 times
- you create 340 textures

The right solution would be:
- load the PNG file once, in a single texture
- use Sprite::setTextureRect to assign a different texture area to each tile
Laurent Gomila - SFML developer

aratnon

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: I try to load tiles form png file but it takes time so long.
« Reply #2 on: August 08, 2013, 07:34:05 pm »
It is good to have 340 textures because my render method is this
for(auto i = 0; i < TOTAL_PLATFORM; ++i)
        {
                window.draw(platformArray_test[i], &platformManager.getPlatform(i));

        }

platformArray_test = array of vertex array
and platformManager.getPlatform(i) returns a texture

Sqasher

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: I try to load tiles form png file but it takes time so long.
« Reply #3 on: August 08, 2013, 08:35:35 pm »
Why would you store 340 textures in memory if they are exactly the same? 339 of them are basicly useless.

You could use a map and load the texture only once. If it doesn't exist you load it. If it does, you just return a pointer to the texture.

G.

  • Hero Member
  • *****
  • Posts: 1590
    • View Profile
Re: I try to load tiles form png file but it takes time so long.
« Reply #4 on: August 08, 2013, 08:58:28 pm »
It is good to have 340 textures
So good that you're complaining about its slowness...

Use one texture and set texCoords of your vertices in your vertex array.
You could even reduce your vertex array draw calls to one instead of TOTAL_PLATFORM times.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: I try to load tiles form png file but it takes time so long.
« Reply #5 on: August 08, 2013, 10:22:09 pm »
Quote
It is good to have 340 textures because [...]
If you know better than us how to use SFML, then why are you here asking for help? :P
Laurent Gomila - SFML developer

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: I try to load tiles form png file but it takes time so long.
« Reply #6 on: August 08, 2013, 10:26:51 pm »
If you know better than us how to use SFML, then why are you here asking for help? :P
He is pointing out what bad job you did, 6 seconds to open a file stream, decode png and send it to gl? Just 340 times? :P
Back to C++ gamedev with SFML in May 2023

aratnon

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: I try to load tiles form png file but it takes time so long.
« Reply #7 on: August 09, 2013, 05:18:54 am »
Oh, Sorry I mistype it. Actually I wanted to ask you about this

Is it good to have 340 textures because my render method is this?

I'm really sorry  :-[
« Last Edit: August 09, 2013, 05:20:32 am by aratnon »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: I try to load tiles form png file but it takes time so long.
« Reply #8 on: August 09, 2013, 12:06:12 pm »
It's never good to have 340 copies of the same texture when you can have all 340 sprites use 1 texture.

Besides, all you have to do is change your render method to:
for(auto i = 0; i < TOTAL_PLATFORM; ++i)
    {
        window.draw(platformArray_test[i], &platformManager.getPlatform());

    }
 
without the i in getPlatform. Just have getPlatform() return the one texture that you're using for every platform.

Also, consider renaming the method to "getPlatformTexture()" for clarity.

Ivan

  • Newbie
  • *
  • Posts: 32
  • IT geek
    • View Profile
Re: I try to load tiles form png file but it takes time so long.
« Reply #9 on: August 09, 2013, 12:56:40 pm »
I recommend you take a look at great tutorials of Coding Made Easy

Tutorial Loading Tile maps [easy]
SFML 2.0 Tutorials