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

Author Topic: sf::Texture to sf::RectangleShape  (Read 2162 times)

0 Members and 1 Guest are viewing this topic.

ProttisSFML

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
sf::Texture to sf::RectangleShape
« on: December 07, 2012, 02:53:56 pm »
Hi, im new to the forums:)

Im working on a tower defence and as of now im writing the tiled map code, this takes a map.txt with numbers in it and draw the map from that.

What im having troubles with is how i can use textures together with shapes!

Here is the current code for that:
void DrawMap(sf::RenderWindow &Window)
{
        sf::RectangleShape rect;
        rect.setSize(sf::Vector2f(BLOCKSIZE,BLOCKSIZE));
        sf::Texture rectTexture;
        for(int i = 0; i < mapSizeX; i++)
        {
                for(int j = 0; j < mapSizeY; j++)
                {
                        if(mapFile[i][j] == 0) //if the value of the map is 0 then we draw this
                                rectTexture.loadFromFile("Dirt.png");
                                        //set texture of the rectangle 0
                        else if(mapFile[i][j] == 1)//if the value of the map is 1 then we draw this
                                rectTexture.loadFromFile("Grass.png");//set texture of the rectangle 1
                //we can add more statements if we want more nunmbers for our map
               
               
                        rect.setPosition(i * BLOCKSIZE, j * BLOCKSIZE);
                        rect.setTexture(rectTexture); // Im getting an error here, cant convert
//from sf:: texture to sf::const texture
                        Window.draw(rect);

                }
        }
}

So do i need to change it to sprites maybe cant rectangular shapes have a texture only a color? Because I used sf::Color and that worked fine

gyscos

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Re: sf::Texture to sf::RectangleShape
« Reply #1 on: December 07, 2012, 03:01:17 pm »
Read the documentation again : setTexture takes a Texture*, that is, a pointer to a texture.
So you'd need to do :

rect.setTexture(&rectTexture);

But that's not the only problem here.
First, you shouldn't load the texture every time you're drawing the tile. Rather, you should keep the texture alive somewhere else (it's easier if you use OOP with classes and all, you could put the texture as a class member).

If you want to draw a tile map, there's a better solution : VertexArray.
First, you'll need to have only one image file containing all your possible tiles. Then, you'll tell the VertexArray which part of that texture you want to use for each tile.

The VertexArray is not trivial to use, so you'll need to read the doc carefully.
Now, some people have done it before you, and you may want to make use of that : Thor is a helper library for SFML that makes many tasks easier, including tilemaps.

ProttisSFML

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: sf::Texture to sf::RectangleShape
« Reply #2 on: December 07, 2012, 03:07:15 pm »
Thanks for the heads up there, yeah there is more to the code than this but Im currently working on the resource manager aswell I just wanted to make sure that this worked.

And i have quite alot done allready, but I will definatly look into the VertexArray. The map will not move either so its a static tilemap which only shows that one picture. I just made it like this so I can have more levels as I go.

 

anything