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

Author Topic: Trying to adapt the TileMap example to a class  (Read 2716 times)

0 Members and 1 Guest are viewing this topic.

BruceJohnJennerLawso

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • My Code on Github
Trying to adapt the TileMap example to a class
« on: February 15, 2015, 08:52:30 pm »
Okay, so I am trying to adapt the TileMap example class found here:

http://www.sfml-dev.org/tutorials/2.2/graphics-vertex-array.php

for use in my own project.

I created a quick tileset png file:



And quickly rewrote the class in a header and cpp file:

class Planetary_surface: public sf::Drawable, public sf::Transformable
{       public:
        bool load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height);
        private:
        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;

    sf::VertexArray m_vertices;
    sf::Texture m_tileset;
};

and cpp

bool Planetary_surface::load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height)
{       // load the tileset texture
        if(!m_tileset.loadFromFile(tileset))
        {       return false;
        }
        // resize the vertex array to fit the level size
        m_vertices.setPrimitiveType(sf::Quads);
        m_vertices.resize(width * height * 4);
        // populate the vertex array, with one quad per tile
        // why is it 4 though...
        for(unsigned int i = 0; i < width; ++i)
        {       for(unsigned int j = 0; j < height; ++j)
                {       // get the current tile number
                        int tileNumber = tiles[i+(j*width)];
                        // find its position in the tileset texture
                        int tu = tileNumber % (m_tileset.getSize().x / tileSize.x);
                        int tv = tileNumber / (m_tileset.getSize().x / tileSize.x);
                        // get a pointer to the current tile's quad
                        sf::Vertex* quad = &m_vertices[(i + j * width) * 4];
                        // define its 4 corners
                        quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
                        quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
                        quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
                        quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);
                        // define its 4 texture coordinates
                        quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
                        quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
                        quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
                        quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
                }
                return true;
        }
}


void Planetary_surface::draw(sf::RenderTarget& target, sf::RenderStates states) const
{       // apply the transform
        states.transform *= getTransform();
        // apply the tileset texture
        states.texture = &m_tileset;
        // draw the vertex array
        target.draw(m_vertices, states);
}

which seems true to the original code

But when I run it, I get this as an output:



Which has the first column drawn properly, but not the rest.

When I copy the example class as it was defined in the original document (just one big class declaration) into the main cpp and use it, it renders everything properly as expected, but when I use the class that I defined in the header/cpp file pair, I keep getting the issue with only the first column being drawn. I cant figure out how the heck that is happening.
Quote
The computer is mightier than the pen, the sword, and usually the programmer.

Rhimlock

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Trying to adapt the TileMap example to a class
« Reply #1 on: February 16, 2015, 02:10:03 pm »
I would say that you should check your parameters for Planetary_surface::load.

Looks like your width might be set to 1.

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: Trying to adapt the TileMap example to a class
« Reply #2 on: February 16, 2015, 06:34:06 pm »
I see it too.  You're not shifting your X position over along with the Y Position.  You're doing it just fine for going up and down but need to implement the X shift as well. :)
I have many ideas but need the help of others to find way to make use of them.

BruceJohnJennerLawso

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • My Code on Github
Re: Trying to adapt the TileMap example to a class
« Reply #3 on: February 17, 2015, 01:18:04 am »
Yeah, I see it now, the return call is one loop too high  ::)

works perfectly now  8)
Quote
The computer is mightier than the pen, the sword, and usually the programmer.

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: Trying to adapt the TileMap example to a class
« Reply #4 on: February 17, 2015, 03:31:49 am »
lol I can't believe all 3 of us didn't see that. ??? I was thinking it was an issue with the loop not wanting to move by calculations.  Well it was but not because of them.  If we'd looked a little harder we'd have all seen it.
I have many ideas but need the help of others to find way to make use of them.

 

anything