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

Author Topic: [SOLVED] Tiling a sprite  (Read 5105 times)

0 Members and 1 Guest are viewing this topic.

hach-que

  • Newbie
  • *
  • Posts: 6
    • MSN Messenger - hachque@roket-productions.com
    • AOL Instant Messenger - hachque
    • Yahoo Instant Messenger - hachque
    • View Profile
    • http://www.roket-enterprises.com/
[SOLVED] Tiling a sprite
« on: November 14, 2009, 07:21:08 am »
I want to tile a background sprite, but unfortunately using two for loops (C++) is extremely slow.  I heard there was some OpenGL attribute that would automatically tile a sprite or something like that.  What's the most effective way of tiling a background?

SOLUTION: Tile the sprite into a large image at the start, and then draw the large image in the main loop.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[SOLVED] Tiling a sprite
« Reply #1 on: November 14, 2009, 12:43:02 pm »
There's nothing in SFML that helps to achieve this. However in SFML 2 it is much faster.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Tiling a sprite
« Reply #2 on: November 14, 2009, 02:06:02 pm »
Quote from: "hach-que"
I want to tile a background sprite, but unfortunately using two for loops (C++) is extremely slow.
Is it really? Maybe you use them wrongly. Do you only draw the tiles on the screen? How many tiles have to be drawn each frame?

I have never had problems with performance (I have been using tiles as well, but probably less than you). I think, some of your loop code could express more...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

hach-que

  • Newbie
  • *
  • Posts: 6
    • MSN Messenger - hachque@roket-productions.com
    • AOL Instant Messenger - hachque
    • Yahoo Instant Messenger - hachque
    • View Profile
    • http://www.roket-enterprises.com/
Re: Tiling a sprite
« Reply #3 on: November 15, 2009, 01:36:40 am »
Quote from: "Nexus"
Quote from: "hach-que"
I want to tile a background sprite, but unfortunately using two for loops (C++) is extremely slow.
Is it really? Maybe you use them wrongly. Do you only draw the tiles on the screen? How many tiles have to be drawn each frame?

I have never had problems with performance (I have been using tiles as well, but probably less than you). I think, some of your loop code could express more...


The code went something like this (I've since replaced it with a mechanism that tiles the screen onto another sprite the size of the screen resolution when the application starts):

Code: [Select]
for (a = 0; a < App.GetWidth(); a += 95)
{
  for (b = 0; b < App.GetHeight(); b += 95)
  {
    BGImage.SetPosition(a,b);
    App.Draw(BGImage);
  }
}

Jack Flower

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Tiling a sprite
« Reply #4 on: November 15, 2009, 12:56:02 pm »
Hello,

Try draw (for…) you tile once, before loop. By example, I draw my tile in sf::Image object (buffer). When my surface is complete, I join sf::Image (with draw tile) to sf::Sprite object and draw this in loop.

By Example:

Code: [Select]
sf::Image Buffer(640,640);
...

for (int i=0;i<10;i++)
{
 for (int j=0;j<10;j++)
  {
TileMap[i][j].SetTileContainer(&TileSet); //My tileset TileMap[i][j].SetPosition(
i*(float)TileMap[i][j].GetTileContainer()->GetTileWidth(0),
j*(float)TileMap[i][j].GetTileContainer()->GetTileHeight(0)); TileMap[i][j].SetTile(tab_mapa[j][i]);

Buffer.Copy(
*TileMap[i][j].GetImage()
,(int)TileMap[i][j].GetPosition().x
,(int)TileMap[i][j].GetPosition().y
,TileMap[i][j].GetSubRect()
     );
     }
}

sf::Sprite Map_01;//Sprite part #1
sf::Sprite Map_02;//Sprite part #2
sf::Sprite Map_03;//Sprite part #3
sf::Sprite Map_04;//Sprite part #4

Map_01.SetImage(Buffer);
Map_01.SetPosition(0.0f,0.0f);
Map_02.SetImage(Buffer);
Map_02.SetPosition(640.0f,0.0f);
Map_03.SetImage(Buffer);
Map_03.SetPosition(0.0f,640.0f);
Map_04.SetImage(Buffer);
Map_04.SetPosition(640.0f,640.0f);

And in Loop…
...
App.Clear(sf::Color(255,255,255));
App.Draw(Map_01);
App.Draw(Map_02);
App.Draw(Map_03);
App.Draw(Map_04);
...


Next, I use sf::View to move “background”. This solution is only example, test…
I use SFML 1.5

Jack Flower

hach-que

  • Newbie
  • *
  • Posts: 6
    • MSN Messenger - hachque@roket-productions.com
    • AOL Instant Messenger - hachque
    • Yahoo Instant Messenger - hachque
    • View Profile
    • http://www.roket-enterprises.com/
[SOLVED] Tiling a sprite
« Reply #5 on: November 15, 2009, 12:58:06 pm »
Yeah, that's the approach I'm now taking.

CBenni::O

  • Newbie
  • *
  • Posts: 48
    • View Profile
[SOLVED] Tiling a sprite
« Reply #6 on: December 25, 2009, 04:52:14 pm »
Hy!
I had a similar Problem, I wanted to program a tile-based PacMan game, so I downloaded the SFML2.0 using the SVN, but when I tried to render using the RenderImage class, nothing was drawn, neither Flush() nor anything else I tried, helped.
When I looked at the Image in the debugger, I found out that still every single Pixel was black.

Does someone have an idea, or even better, a piece of Code which demonstates Rendering on a Sprite (in realtime!)?

bye, CBenni::O
42!
Metal will never die!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[SOLVED] Tiling a sprite
« Reply #7 on: December 25, 2009, 05:22:24 pm »
Can you show a minimal and complete piece of code that reproduces your problem?
Laurent Gomila - SFML developer

CBenni::O

  • Newbie
  • *
  • Posts: 48
    • View Profile
[SOLVED] Tiling a sprite
« Reply #8 on: December 25, 2009, 06:07:06 pm »
Well... I dont have the original code, but this is (approx.) what it did:
Code: [Select]
class CGameboard
{
public:
//[...]
void ChangeField(sf::Vector2i vWhere,int newType);

private:
//[...]
sf::Sprite* Tiles[10]; // Sprites of my Tiles (are Initialized somewhere else, and they are loaded correctly, have checked that
SGBField Fields[1024]; // The Fields of my Gameboard (32x32 Matrix)
sf::Sprite* MySprite; // Sprite that is rendered to
sf::RenderImage MyRenderImg; // RenderImage used for Rendering
};

void CGameboard::ChangeField(sf::Vector2i vWhich, int newType)
{
       Fields[vWhich.x+vWhitch.y*32].Type = newType; // Set the new Type of the Field
       Tiles[newType]->SetPosition(Fields[vWhich.x+vWhitch.y*32].Position); // Move the Sprite to the Position where they should be rendered
       MyRenderImg->Render(Tiles[newType]);
//Adding MyRenderImg->Flush() here didn't change anything
       MySprite->SetImage(MyRenderImg);
}


I hope you understand what I did

bye, CBenni::O
42!
Metal will never die!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[SOLVED] Tiling a sprite
« Reply #9 on: December 25, 2009, 06:37:52 pm »
Did you check RenderImage::IsAvailable? Do you call MyRenderImg->Display() (like for RenderWindow)?
Laurent Gomila - SFML developer

CBenni::O

  • Newbie
  • *
  • Posts: 48
    • View Profile
[SOLVED] Tiling a sprite
« Reply #10 on: December 25, 2009, 06:53:16 pm »
Quote from: "Laurent"
Did you check RenderImage::IsAvailable?

Yes, I did ;)
Quote
Do you call MyRenderImg->Display() (like for RenderWindow)?

Yes, but that didn't change anything either...

But I'm trying to recover my original program, and I will upload it as soon as possible.

bye, CBenni::O
42!
Metal will never die!

CBenni::O

  • Newbie
  • *
  • Posts: 48
    • View Profile
[SOLVED] Tiling a sprite
« Reply #11 on: December 25, 2009, 07:45:08 pm »
huh? Now it worked... Don't know what I did wrong...

Thanks anyway ;)
bye, CBenni::O
42!
Metal will never die!

 

anything