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

Author Topic: Tiled map same sprites but diferent position  (Read 2494 times)

0 Members and 1 Guest are viewing this topic.

demix226

  • Newbie
  • *
  • Posts: 2
    • View Profile
Tiled map same sprites but diferent position
« on: September 08, 2015, 03:12:21 am »
Hello SFML Community!.

I hope you excuse my bad english.

im working on a tiled based game and i have a 40 x 40 tiled map. Some tiles have the same graphics like another tile.

Mi question is:
should i create different sprites for each tile (talking about the same graphics tiles) with different positions??, or should i create one sprite for each graphics and change the position of the sprites in each reander??.

Example:

RenderWindow rWindow(VideoMode(500, 500), "Test");
Texture t1;
t1.loadFromFile("MyTextureFile");

Sprite s1(t1);
Sprite s2(t1);

s1.setPosition(50, 50);
s2.setPosition(100, 100);

while(true) {
      rWindow.clear();
      rWindow.draw(s1);
      rWindow.draw(s2);
      rWindow.display();
}

 

or


RenderWindow rWindow(VideoMode(500, 500), "Test");
Texture t1;
t1.loadFromFile("MyTextureFile");

Sprite s1(t1);

while(true) {
      rWindow.clear();
      s1.setPosition(50, 50);
      rWindow.draw(s1);
      s1.setPosition(100, 100);
      rWindow.draw(s1);
      rWindow.display();
}

 

Thanks =D

Brax

  • Newbie
  • *
  • Posts: 39
  • Wannabe C++ Game Developer
    • View Profile
Re: Tiled map same sprites but diferent position
« Reply #1 on: September 08, 2015, 04:20:35 am »
If I understood you correctly, you want to make a 40x40 tiled map with sprites, therefore this the way to go:
Quote
should i create different sprites for each tile (talking about the same graphics tiles) with different positions??

Of course, there are other ways to make a tilled map, such as, using sf::VertexArrays, not sprites, but they are a bit tricky to use.
So if you are just starting out with SFML, the sprites should be enough.  ;D

If you wanted to ask "Why the second option isn't good?", it isn't good because you always need to keep rendering separated from rest of the code, not to mix anything with it. Doing so is a bad code design and almost always causes trouble in more serious projects. It should do only:

// Positions, sizes, colors, textures, ect... are always updated before rendering:

void Render()
{
    window.clear(sf::Color::Black);
    window.draw(sprite1);   // You can also apply transformations using "window.draw(sprite1, transformations)", but that is another story :)
    window.draw(sprite2);  
     // Or how many things you want to draw.
    window.display();
}
 

Also note that this is too a valid approach:

// Let's say we have a class (or classes) called World that has function for rendering.
// positions, textures, ect... are updated somewhere.
void World::render(sf::RenderWindow& window)   //   Passing a sf::RenderWindow by reference (&) is very important!
{
   window.draw(sprite1);
   window.draw(sprite2);
   // ect.
}

void render()
{
   window.clear();
   mWorld.render(window); // mWorld is an object of World class;
   window.display()

}
 
« Last Edit: September 08, 2015, 04:23:58 am by Brax »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Tiled map same sprites but diferent position
« Reply #2 on: September 08, 2015, 01:39:54 pm »
I agree. As a rule, it's best to not have any positions etc. in the rendering section, so the first option would be best: one sprite per tile and then draw all the sprites during the render (clear, draw, display).

As mentioned by Brax, there is another way, which is more advanced but better overall (including performance), and that is using a single sf::VertexArray. There is a TileMap example which does just this already in its tutorial. You should read the entire tutorial, especially as using vertex arrays allows for much more flexibility in custom graphical entities.

// Let's say we have a class (or classes) called World that has function for rendering.
// positions, textures, ect... are updated somewhere.
void World::render(sf::RenderWindow& window)   //   Passing a sf::RenderWindow by reference (&) is very important!
{
   window.draw(sprite1);
   window.draw(sprite2);
   // ect.
}

void render()
{
   window.clear();
   mWorld.render(window); // mWorld is an object of World class;
   window.display()
}
 
You could also make "mWorld" inherit from sf::Drawable and then it would look like this:
void World::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
   target.draw(sprite1);
   target.draw(sprite2);
}

void render()
{
   window.clear();
   window.draw(mWorld); // mWorld is an object of World class, which inherits from sf::Drawable
   window.display();
}
;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

demix226

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Tiled map same sprites but diferent position
« Reply #3 on: September 08, 2015, 02:07:49 pm »
Hey really thanks, option 1 is the correct.

I saw the axample. They have al the Tiles in the same texture. I want to create a multi tileset map. For each map tile i have to create 1 sprites (10x10 map = 100 sprites) + 1 sprite for each enemy (same texture enemy), etc...

Thanks for all.

 

anything