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

Author Topic: [SOLVED]I have problem in a 2d vector of class Tile  (Read 1255 times)

0 Members and 1 Guest are viewing this topic.

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
[SOLVED]I have problem in a 2d vector of class Tile
« on: July 02, 2013, 03:59:56 pm »
Hello Guys I am going to implement dijktra's algorithm and I really need to show up some walkable and unwalkable tiles on screen. I am having a great problem doing this because, every time I draw the tile nothing appears. I suspected somehow that the implementation I made in loading that vector is wrong and that it loses reference as soon the loop ends. I badly need help on this one..

This is how I populate the grid with walkable tiles as the default tileset
    shared_ptr< Tile > tmp;

    for( int y = 0; y < grid_height; y++ )
    {
        tset.push_back( vector< shared_ptr< Tile > >() );

        for( int i = 0; i < grid_width; i++ )
        {
            tmp.reset( new Tile( "walkable.png" , 50 ) );
            tset[ y ].push_back( tmp );
        }
    }
 

this is how I draw it.

void Grid::populateGrid()
{
    int dist_y = 0;
    int dist_x = 0;

    for( int y = 0; y < grid_height; y++ )
    {
        for( int i = 0; i < grid_width; i++ )
        {
            tset[ y ][ i ].get()->setTexture( wTile );
            tset[ y ][ i ].get()->setPosition( dist_x , dist_y );
            dist_x += tilesize;
        }

        dist_x = 0;
        dist_y += tilesize;
    }
}
 
« Last Edit: July 02, 2013, 05:29:50 pm by magneonx »

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: I have problem in a 2d vector of class Tile
« Reply #1 on: July 02, 2013, 04:31:40 pm »
There are a lot of things that we don't know, like Tile, or what is tset... but when you say «this is how I draw it», where is the actual "draw" in your code?
SFML / OS X developer

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Re: [SOLVED]I have problem in a 2d vector of class Tile
« Reply #2 on: July 02, 2013, 05:29:18 pm »
Great!!! Thanks for replying. I have finally made it to work. My code is kinda long now, although its just a prototype for pathfinding.

As I painstakingly find the root cause of this problem, I have made a minute flaw, I just declared a local sf::Texture to the scope where I actually push Tiles back to the vector. Thats why it loses reference to it as soon as the function ends, and of course it show nothing on the screen.

I have made that texture as a member variable for the class Tile and it works great now.
Thank you very much!

 

anything