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

Author Topic: Weird Behaviour with tilemap  (Read 1427 times)

0 Members and 1 Guest are viewing this topic.

NonMysteriousGuy

  • Newbie
  • *
  • Posts: 2
    • View Profile
Weird Behaviour with tilemap
« on: July 12, 2020, 05:49:41 am »
So im making a tilemap and rendering it to the scene . But it got really weird and mess up
I cant find what is wrong with it . Help!

This is the main code ( run in main.cpp)
private:
        //display properties
        int nTileWidth;

        const static int nTileVisY = 16;
        const static int nTileVisX = 16;

        //level properties
        const static int nLevelWidth = 32;
        const static int nLevelHeigth = 32;
        std::string sLevel;
        std::fstream stream;

        //camera
        View camera;

        //the tilemap
        TileMap tilemap;

        //the draw map
        int defMap[nLevelWidth * nLevelHeigth];
        int drawMap[(nTileVisX+1) * (nTileVisY+1)];

        void Create() override
        {
                std::cout << "Create 1 \n";
                stream.open("map.txt");

                //the level
                sLevel += "................................";
                sLevel += "................................";
                sLevel += "................................";
                sLevel += "................................";
                sLevel += "................................";
                sLevel += ".......#######.........#######..";
                sLevel += "......##..............##........";
                sLevel += "################################";
                sLevel += "................................";
                sLevel += "................................";
                sLevel += "................................";
                sLevel += "................................";
                sLevel += "................................";
                sLevel += "................................";
                sLevel += "................................";
                sLevel += "................................";
                sLevel += "................................";
                sLevel += "................................";
                sLevel += "................................";
                sLevel += "................................";
                sLevel += "................................";
                sLevel += ".......#######.........#######..";
                sLevel += "......##..............##........";
                sLevel += "################################";
                sLevel += "................................";
                sLevel += "................................";
                sLevel += "................................";
                sLevel += "................................";
                sLevel += "................................";
                sLevel += ".......#######.........#######..";
                sLevel += "......##..............##........";
                sLevel += "################################";


                //calculate the tile width
                nTileWidth = ((int)VideoMode::getDesktopMode().height - 100.f) / nTileVisY;
               
                //resize the window
                window->create(VideoMode(nTileWidth * nTileVisX, nTileWidth * nTileVisY), "Platformer2", Style::Close | Style::Titlebar);

                //resize the camera
                camera.reset(FloatRect(0.f, 0.f, nTileWidth * nTileVisX, nTileVisY * nTileWidth));
                window->setView(camera);

                //open the tileset
                if (!tilemap.setTileSet("tileset.png"))
                {
                        std::cout << "Cannot find tileset.png \n";
                }

                //set the size of tilemap
                tilemap.setSize(nLevelWidth, nLevelHeigth);

                //create the defMap
                for (int i = 0; i < nLevelWidth * nLevelHeigth; i++)
                {
                        if (sLevel[i] == '.')
                                defMap[i] = 2;
                        else if (sLevel[i] == '#')
                                defMap[i] = 1;
                }

               
                tilemap.create(Vector2u(nTileWidth, nTileWidth), defMap, nLevelWidth, nLevelHeigth, Vector2u(16, 16), Vector2f(0.f, 0.f));

        }

void Draw() override
        {
               
                //clamp the camera to the map
                //bottom bound
                if (camera.getCenter().y - camera.getSize().y / 2.f > nLevelHeigth * nTileWidth - nTileVisY * nTileWidth)
                        camera.setCenter(camera.getCenter().x, (int)(nLevelHeigth * nTileWidth - nTileVisY * nTileWidth + camera.getSize().y / 2.f));
                //top bound
                else if (camera.getCenter().y - camera.getSize().y / 2.f < 0.f)
                        camera.setCenter(camera.getCenter().x, (int)(camera.getSize().y / 2.f));
                //left bound
                if (camera.getCenter().x - camera.getSize().x / 2.f < 0.f)
                        camera.setCenter((int)(camera.getSize().x / 2.f), camera.getCenter().y);
                //rigth bound
                else if (camera.getCenter().x - camera.getSize().x / 2.f > nLevelWidth * nTileWidth - nTileVisX * nTileWidth)
                        camera.setCenter((int)(nLevelWidth * nTileWidth - nTileVisX * nTileWidth + camera.getSize().x / 2.f), camera.getCenter().y);
               

                //calculate the top left tile that is visible
                int fOffsetX = camera.getCenter().x / nTileWidth - (float)nTileVisX / 2.f;
                int fOffsetY = camera.getCenter().y / nTileWidth - (float)nTileVisY / 2.f;

                //create the drawMap
                for (int x = 0; x < nTileVisX; x++)
                {
                        for (int y = 0; y <= nTileVisY; y++)
                        {
                               
                                drawMap[(x + y * nTileVisX)] = defMap[((x + (int)fOffsetX) + ((y + (int)fOffsetY) * nLevelWidth))];
                                //std::cout << (x + y * nTileVisX) << " " << ((x + (int)fOffsetX) + ((y + (int)fOffsetY) * nLevelWidth)) << "\n";
                        }
                }

                /*Time t = sf::seconds(100000.f);
                sf::sleep(t);*/

               
                tilemap.create(Vector2u(nTileWidth, nTileWidth), drawMap, nTileVisX + 1  , nTileVisY + 1, Vector2u(16, 16), Vector2f(fOffsetX, fOffsetY));
               
               
               
                if (Keyboard::isKeyPressed(Keyboard::E))
                {
                        for (int y = 0; y <= nTileVisY; y++)
                        {
                                for (int x = 0 ; x <= nTileVisX; x++)
                                {
                                        if (drawMap[x + y*nTileVisX] == 1)
                                                std::cout << "#";
                                        else
                                        {
                                                std::cout << ".";
                                        }
                                }
                                std::cout << "\n";
                        }
                        std::cout << "End \n";
                        Time t = sf::seconds(0.5f);
                        sf::sleep(t);

                }


               
                window->setView(camera);
                window->clear();
               
                window->draw(tilemap);
                window->display();

                tilemap.clear();
        }
 

THis is the tilemap code from sfml tilemap tutorial
class TileMap : public sf::Drawable
{
public:
        bool setTileSet(const std::string& tileset)
        {
                // load the tileset texture
                if (!m_tileset.loadFromFile(tileset))
                        return false;
                return true;
        }

        void setSize(int width, int heigth)
        {
                // resize the vertex array to fit the level size
                m_vertices.setPrimitiveType(sf::Quads);
                m_vertices.resize(width * heigth * 4);
                m_width = width;
                m_heigth = heigth;
        }

        bool create(sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height, Vector2u texSize, Vector2f startPos)
        {




                // populate the vertex array, with one quad per tile

                for (int i = 0; i < width; ++i)
                for (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 TileOffsetX = 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 + startPos.x) * tileSize.x, (j + startPos.y) * tileSize.y);
                        quad[1].position = sf::Vector2f((i + 1 + startPos.x) * tileSize.x, (j + startPos.y) * tileSize.y);
                        quad[2].position = sf::Vector2f((i + 1 + startPos.x) * tileSize.x, (j + 1 + startPos.y) * tileSize.y);
                        quad[3].position = sf::Vector2f((i + startPos.x) * tileSize.x, (j + 1 + startPos.y) * tileSize.y);

                        // define its 4 texture coordinates
                        quad[0].texCoords = sf::Vector2f(tileNumber * texSize.x, 0 * texSize.y);
                        quad[1].texCoords = sf::Vector2f((tileNumber + 1) * texSize.x, 0 * texSize.y);
                        quad[2].texCoords = sf::Vector2f((tileNumber + 1) * texSize.x, 1 * texSize.y);
                        quad[3].texCoords = sf::Vector2f(tileNumber * texSize.x, 1 * texSize.y);


                }

                return true;
        }

        const Vertex &getVertex(int i)
        {
                return  m_vertices[i];
        }

        const Texture &getTexture()
        {
                return m_tileset;
        }

        void clear()
        {
                m_vertices.clear();
                m_vertices.setPrimitiveType(sf::Quads);
                m_vertices.resize(m_width * m_heigth * 4);
        }

private:

        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
        {

                // apply the tileset texture
                states.texture = &m_tileset;

                // draw the vertex array
                target.draw(m_vertices, states);
        }

        int m_width , m_heigth;
        sf::VertexArray m_vertices;
        sf::Texture m_tileset;
};
 

« Last Edit: July 12, 2020, 05:53:37 am by NonMysteriousGuy »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Weird Behaviour with tilemap
« Reply #1 on: July 12, 2020, 06:25:59 pm »
I don't think that your post describes your problem, and it is not obvious what's "weird and messed up" in your screen capture.
Laurent Gomila - SFML developer

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Weird Behaviour with tilemap
« Reply #2 on: July 13, 2020, 06:12:58 pm »
i'm also not sure of what is "weird and messed up", but it seems to be a problem with your sLevel. is that string the tilemap internally? why not an array or vector of ints? try to change that, because your problem could be an inconsistency between your sLevel x and y size and your intended x and y size.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Weird Behaviour with tilemap
« Reply #3 on: July 13, 2020, 11:32:53 pm »
I think you need to decide how you wish to use the Tilemap class. You are creating it multiple times (every frame?) and use differing values to set it up.

tilemap.create(Vector2u(nTileWidth, nTileWidth), defMap, nLevelWidth, nLevelHeigth, Vector2u(16, 16), Vector2f(0.f, 0.f));

tilemap.create(Vector2u(nTileWidth, nTileWidth), drawMap, nTileVisX + 1  , nTileVisY + 1, Vector2u(16, 16), Vector2f(fOffsetX, fOffsetY));
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*