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

Author Topic: Tile system  (Read 1820 times)

0 Members and 2 Guests are viewing this topic.

guptarohan

  • Newbie
  • *
  • Posts: 2
    • View Profile
Tile system
« on: December 10, 2013, 03:15:16 pm »
Hello there,
Question:
how does view function work with an array based tile engine map? Can I have one large tile map or do I need many small maps. Will I be rotate the view or use the viewport functions?

Explanation:
I am making a small 2d rpg with sfml. My tile engine is based on the vertex array example
http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php
I was planning on using sf::view to scroll the six different 'areas' of my single large tilemap(which is 6 times the screen). I wanted to use views like this
eg. 'view.move(200,200)' to scroll the area to area 2 when the house intersects with the player.
When I do this nothing happens! I think this may be because the vertex array style does not render off screen. I think a solution would be to make many small tilemaps and switch them out, but that would not allow me to manipulate other view functions.


#include <SFML/Graphics.hpp>  //includes
#include <SFML/System.hpp>
#include <iostream>
#include <vector>

class TileMap : public sf::Drawable, public sf::Transformable // see link, vertex array tile engine
{
public:

    bool 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
        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;
    }

private:

    virtual void 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);
    }

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

int main(){

std::vector< std::vector<int> > map(4, std::vector<int>(4));  // Define the map


 const int level[] =
    {
0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 9, 7, 7, 7, 7, 7, 7, 7, 10, 10, //numbers stand for tiles (see key)
0, 2, 0, 0, 3, 0, 1, 0, 0, 0, 7, 7, 7, 7, 7, 7, 7, 7, 10, 10,
0, 2, 0, 1, 1, 1, 1, 0, 0, 0, 7, 7, 7, 7, 8, 7, 7, 7, 10, 10,
0, 2, 0, 1, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7, 7, 7, 10, 10,
0, 2, 0, 1, 0, 0, 0, 3, 0, 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
0, 2, 0, 1, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
4, 2, 4, 4, 11, 4, 4, 4, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4, 2, 4, 4, 4, 4, 4, 4, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4, 2, 4, 11, 4, 11, 4, 4, 2, 4, 0, 5, 5, 5, 5, 5, 0, 2, 2, 2,
4, 2, 4, 4, 4, 4, 4, 4, 2, 4, 0, 5, 5, 5, 5, 5, 0, 2, 1, 1,
4, 2, 2, 2, 2, 2, 2, 2, 2, 4, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 3, 0, 0, 0, 0, 6, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

if  (player.getGlobalBounds().intersects(house.getGlobalBounds() ) ){    // if player intersects with house
    intersects = true; //set intersects to true
    view.move(200, 200);  // use the View.move() function to scroll the map to 'area' 2 (house)
}

window.draw(map);  //draws and displays map
window.display();

}
 
Thanks, Rohan(I am new to SFML and the forums)
« Last Edit: December 10, 2013, 06:19:23 pm by guptarohan »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Tile system
« Reply #1 on: December 10, 2013, 03:47:44 pm »
"It seems not to work" is not a problem description, because we don't know what you expect to happen, what is happening and what you've already tried.

It's also best to provide a minimal and complete example, instead of some badly formatted code snippets. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Tile system
« Reply #2 on: December 10, 2013, 06:36:15 pm »