SFML community forums

Help => Graphics => Topic started by: OutlawLee on May 09, 2012, 02:17:12 pm

Title: Switching from image to textures.
Post by: OutlawLee on May 09, 2012, 02:17:12 pm
I want to load a  texture onto a texture. (before i loaded a image and a image with copy)

or should i can i make image onto a texture ? which is better ?
btw this is my TextureManager.cpp (changed from ImageManager.cpp)


I used to have:

sf::Image tileset;
sf::Image tileImage;
tileImage.Copy(tileset, 0, 0, sf::IntRect(x * tileSize, y * tileSize, frames * tileSize, tileSize), true);

But now i switched my code all to sfml 2.0 and i dont know the function that copies in sfml 2.0.

i want something like this:
sf::Image tileset;
sf::Texture tileTexture;
tileTexture.Copy(tileset, 0, 0, sf::IntRect(x * tileSize, y * tileSize, frames * tileSize, tileSize), true);

edit:

I searched a bit, and i think i could use loadFromImage but im not sure how. An example with the code above would be appriciated.

edit2:

Maybe this ? but im lacking the 0,0 position, maybe texture.update ?
tileTexture.loadFromImage(tileset, sf::IntRect(x * tileSize, y * tileSize, frames * tileSize, tileSize));
 

edit3:

I managed to fix the errors. Can someone tell if this code does like the past one ?

sf::Image tileset;

sf::Texture tileTexture;
tileTexture.create(tileSize, tileSize);
tileTexture.loadFromImage(tileset, sf::IntRect(x * tileSize, y * tileSize, frames * tileSize, tileSize));
tileTexture.update(tileset, 0, 0);
 

I have the correct tileSize declaration. i dont get any errors when compile.

ty
Title: Re: Switching from image to textures.
Post by: Laurent on May 09, 2012, 02:54:12 pm
You can either:
Title: Re: Switching from image to textures.
Post by: OutlawLee on May 09, 2012, 03:04:36 pm
I think i did what you said in your second option right ?

Create a new texture and load the image inside it.
Title: Re: Switching from image to textures.
Post by: Laurent on May 09, 2012, 03:34:57 pm
But are you copying tiles from a big tileset into individual small textures?
Title: Re: Switching from image to textures.
Post by: OutlawLee on May 09, 2012, 04:02:11 pm
Everything works properly if i comment:
tileTexture.update(tileset, 0, 0);

right now the code is:

sf::Texture tileTexture;
tileTexture.create(tileSize, tileSize);
tileTexture.loadFromImage(tileset, sf::IntRect(x * tileSize, y * tileSize, frames * tileSize, tileSize));
 

Title: Re: Switching from image to textures.
Post by: Laurent on May 09, 2012, 04:18:59 pm
You didn't answer to my question, and the call to create is useless.
Title: Re: Switching from image to textures.
Post by: OutlawLee on May 09, 2012, 04:20:24 pm
Ups, i edited the message and removed it.

Yes, im loading a big one and then cliping it.
Title: Re: Switching from image to textures.
Post by: Laurent on May 09, 2012, 04:34:38 pm
Quote
Yes, im loading a big one and then cliping it.
Why? This is the worst strategy if you want to get good performances. Switching textures is an expensive operation for the graphics card, having everything in the same texture is more optimized. That's the reason why the sf::Sprite class has a TextureRect property.
Title: Re: Switching from image to textures.
Post by: OutlawLee on May 09, 2012, 04:44:25 pm
Oh loading once big pic into a texture and then splitting it into sprites is pretty logical.

argh, im such a noob :D need to learn classes and arrays to make it properly-

Laurent, is this
http://en.sfml-dev.org/forums/index.php?topic=3023.0
system good ? I will jsut edit it for sfml 2,0
Title: Re: Switching from image to textures.
Post by: Laurent on May 09, 2012, 05:56:38 pm
Quote
Laurent, is this
http://en.sfml-dev.org/forums/index.php?topic=3023.0
system good ?
I have no idea.

But probably not: SFML 2 provides an optimized way of creating large batches of static geometry (with vertex arrays); tilemaps are a typical use case for this.