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

Author Topic: Switching from image to textures.  (Read 2948 times)

0 Members and 2 Guests are viewing this topic.

OutlawLee

  • Jr. Member
  • **
  • Posts: 50
  • This is my personal text. Dont read it.
    • View Profile
    • Email
Switching from image to textures.
« 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
« Last Edit: May 09, 2012, 04:18:21 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Switching from image to textures.
« Reply #1 on: May 09, 2012, 02:54:12 pm »
You can either:
  • do all your image manipulation as before with sf::Image, and then call texture.loadFromImage(image)
  • you can create an empty texture (texture.create) and copy images to it with texture.update
Laurent Gomila - SFML developer

OutlawLee

  • Jr. Member
  • **
  • Posts: 50
  • This is my personal text. Dont read it.
    • View Profile
    • Email
Re: Switching from image to textures.
« Reply #2 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Switching from image to textures.
« Reply #3 on: May 09, 2012, 03:34:57 pm »
But are you copying tiles from a big tileset into individual small textures?
Laurent Gomila - SFML developer

OutlawLee

  • Jr. Member
  • **
  • Posts: 50
  • This is my personal text. Dont read it.
    • View Profile
    • Email
Re: Switching from image to textures.
« Reply #4 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));
 

« Last Edit: May 09, 2012, 04:18:29 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Switching from image to textures.
« Reply #5 on: May 09, 2012, 04:18:59 pm »
You didn't answer to my question, and the call to create is useless.
Laurent Gomila - SFML developer

OutlawLee

  • Jr. Member
  • **
  • Posts: 50
  • This is my personal text. Dont read it.
    • View Profile
    • Email
Re: Switching from image to textures.
« Reply #6 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.
« Last Edit: May 09, 2012, 04:23:52 pm by OutlawLee »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Switching from image to textures.
« Reply #7 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.
Laurent Gomila - SFML developer

OutlawLee

  • Jr. Member
  • **
  • Posts: 50
  • This is my personal text. Dont read it.
    • View Profile
    • Email
Re: Switching from image to textures.
« Reply #8 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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Switching from image to textures.
« Reply #9 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.
Laurent Gomila - SFML developer