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

Author Topic: Not all tiles from TileMap are shown  (Read 2396 times)

0 Members and 1 Guest are viewing this topic.

woskaz0n

  • Newbie
  • *
  • Posts: 14
    • View Profile
Not all tiles from TileMap are shown
« 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....
Code: [Select]

//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!
Realism is what makes games suck. - Wouter van Oortmerssen

jdindia

  • Newbie
  • *
  • Posts: 13
    • View Profile
Not all tiles from TileMap are shown
« Reply #1 on: November 30, 2008, 01:40:26 am »
You're only drawing 1-2 sprites.  The draw call has to be in the loop.

Try something more like

Code: [Select]


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] );
   }
}


woskaz0n

  • Newbie
  • *
  • Posts: 14
    • View Profile
Not all tiles from TileMap are shown
« Reply #2 on: November 30, 2008, 03:51:50 pm »
Thank you, now it is working.

But, there is something in your code thant won´t work.

Code: [Select]

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
Code: [Select]

sf::Image Image;
sf::Sprite Sprite[2](Image);

couldn´t work as well.

I did it this way
Code: [Select]

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
Realism is what makes games suck. - Wouter van Oortmerssen

jdindia

  • Newbie
  • *
  • Posts: 13
    • View Profile
Not all tiles from TileMap are shown
« Reply #3 on: November 30, 2008, 08:49:20 pm »
Quote from: "woskaz0n"

But, there is something in your code thant won´t work.

Code: [Select]

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:

Code: [Select]

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:

Code: [Select]

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.