SFML community forums
Help => Graphics => Topic started by: woskaz0n on November 30, 2008, 01:16:47 am
-
Hi everybody,
I tried to create a little tilemap. i thought it should be no problem, but unfortunately nothing works really well :) .
At first, my code....
//of course i don´t post everything
int TileMap[mapHoehe][mapBreite] =
{
{1,1,1},
{1,2,1},
{1,1,1},
};
for(int y = 0; y < mapHoehe; y++)
{
for(int x = 0; x < mapBreite; x++)
{
if(TileMap[y][x] == 1)
Map1 = true;
if(TileMap[y][x] == 2)
Map2 = true;
}
}
/* In the main-loop */
if(Map1 == true)
Window.Draw(Tile1);
if(Map2 == true)
Window.Draw(Tile2);
But if I start the program, just one tile is shown, instead of 9.
Thanks for your help!
-
You're only drawing 1-2 sprites. The draw call has to be in the loop.
Try something more like
const int TILE_SIZE = 128;
int TileMap[mapHoehe][mapBreite] =
{
{1,1,1},
{1,2,1},
{1,1,1},
};
Sprite TileSprite[2];
TileSprite[0].LoadFromFile ( "grass.png" );
TileSprite[1].LoadFromFile ( "dirt.png" );
// later to render the tiles
for(int y = 0; y < mapHoehe; y++)
{
for(int x = 0; x < mapBreite; x++)
{
int i;
i = TileMap[y][x];
TileSprite[i].SetPosition ( x*TILE_SIZE, y*TILE_SIZE );
Window.Draw ( TileSprite[i] );
}
}
-
Thank you, now it is working.
But, there is something in your code thant won´t work.
Sprite TileSprite[2];
TileSprite[0].LoadFromFile ( "grass.png" );
TileSprite[1].LoadFromFile ( "dirt.png" );
I know, this was just an example, but you aren´t able to load an image with an Sprite an things like
sf::Image Image;
sf::Sprite Sprite[2](Image);
couldn´t work as well.
I did it this way
for(int y = 0; y < mapHoehe; y++)
{
for(int x = 0; x < mapBreite; x++)
{
if(TileMap[y][x] == 1)
{
Tile1.SetPosition(x*TILE_SIZE, y*TILE_SIZE);
Window.Draw(Tile1);
}
if(TileMap[y][x] == 2)
{
Tile2.SetPosition(x*TILE_SIZE, y*TILE_SIZE);
Window.Draw(Tile2);
}
Nearly the same, so thank you very much! :D
-
But, there is something in your code thant won´t work.
Sprite TileSprite[2];
TileSprite[0].LoadFromFile ( "grass.png" );
TileSprite[1].LoadFromFile ( "dirt.png" );
Oh, right. That's what I get for just going from memory. Second attempt from memory:
sf::Sprite TileSprite[2];
sf::Image TileImage[2];
TileImage[0].LoadFromFile("grass.png");
TileImage[1].LoadFromFile("dirt.png");
TileSprite[0].SetImage ( &(TileImage[0]) );
TileSprite[1].SetImage ( &(TileImage[1]) );
The reason I suggest using an array instead of what you're doing is that eventually you wind up with code that looks like this:
If (Tile[X][Y] == 1 )
...
else if (Tile[X][Y] == 2 )
...
else if (Tile[X][Y] == 3 )
...
else if (Tile[X][Y] == 4 )
...
etc.
Eventually it's just not very easy to add tiles. Much easier to have an array or a vector container of some sort.