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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - NonMysteriousGuy

Pages: [1]
1
General / 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;
};
 


2
Graphics / Getting weird tile
« on: March 13, 2020, 02:47:06 pm »
(Sorry for my English, im asian)
I follow the code on how to make tile map https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php
And get
#include"SFML\Graphics.hpp"
#include<iostream>

using namespace sf;

class TileMap : public Drawable
{
        VertexArray vertices;
        Texture tileset;
public :
        bool load(std::string tilesetName, Vector2u tileSize, const int *tiles/*pass in the draw map*/, unsigned int width/* in tiles*/, unsigned int heigth/* in tiles*/)
        {
                //accessing the tileset
                if (!tileset.loadFromFile(tilesetName))
                {
                        std::cout << "Error";
                        return false;
                }
                       
                //resize the vertex array to the current map
                vertices.resize(width*heigth * 4);
                vertices.setPrimitiveType(Quads);

                //Adding a quad for each tile
                for (int x = 0; x < width; x++)
                {
                        for (int y = 0; y < heigth; y++)
                        {
                                //Get the current tile number
                                int tileID = tiles[x + y*width];
                                //find it postion in the tileset texture
                                int TextureU = tileID % (tileset.getSize().x - tileSize.x);
                                int TextureV = tileID / (tileset.getSize().x - tileSize.x);

                                //Get a pointer to the current tile quad
                                Vertex* quad = &vertices[(x + y*width) * 4];

                                //set the postion of it for corner
                                quad[0].position = Vector2f(x * tileSize.x, y * tileSize.y);
                                quad[1].position = Vector2f((x+1) * tileSize.x, y * tileSize.y);
                                quad[2].position = Vector2f((x + 1) * tileSize.x,(y+1) * tileSize.y);
                                quad[3].position = Vector2f(x * tileSize.x, (y + 1) * tileSize.y);

                                quad[0].texCoords = Vector2f(TextureU*tileSize.x, TextureV*tileSize.y);
                                quad[0].texCoords = Vector2f((TextureU+1)*tileSize.x, TextureV*tileSize.y);
                                quad[0].texCoords = Vector2f((TextureU + 1) * tileSize.x, (TextureV + 1)*tileSize.y);
                                quad[0].texCoords = Vector2f(TextureU*tileSize.x, (TextureV + 1)*tileSize.y);

                                /*if (tileID == 0)
                                {
                                        quad[0].color = Color::Green;
                                        quad[1].color = Color::Green;
                                        quad[2].color = Color::Green;
                                        quad[3].color = Color::Green;
                                }
                                else if (tileID == 1)
                                {
                                        quad[0].color = Color::Blue;
                                        quad[1].color = Color::Blue;
                                        quad[2].color = Color::Blue;
                                        quad[3].color = Color::Blue;
                                }
                                else if (tileID == 3)
                                {
                                        quad[0].color = Color::Black;
                                        quad[1].color = Color::Black;
                                        quad[2].color = Color::Black;
                                        quad[3].color = Color::Black;
                                }
                                else
                                {
                                        quad[0].color = Color::Green;
                                        quad[1].color = Color::Green;
                                        quad[2].color = Color::Green;
                                        quad[3].color = Color::Green;
                                }*/

                        }
                }

                return true;
        }

        virtual void draw(RenderTarget &target, RenderStates state = RenderStates::Default) const // state should have no transform
        {
                //apply the tileset
                state.texture = &tileset;
                //draw the vertex array
                target.draw(vertices, state);
        }
};

int main()
{
       

       
       
       

       
        //quad[0].position = Vector2f(40, 40);
        //quad[1].position = Vector2f(140, 40);
        //quad[2].position = Vector2f(140, 140);
        //quad[3].position = Vector2f(40, 140);

        //quad[4].position = Vector2f(200, 200);
        //quad[5].position = Vector2f(300,200);
        //quad[6].position = Vector2f(300,300);
        //quad[7].position = Vector2f(200, 300);

        // define the level with an array of tile indices
        const int level[] =
        {
                0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
                1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3,
                0, 1, 0, 0, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 0, 0,
                0, 1, 1, 0, 3, 3, 3, 0, 0, 0, 1, 1, 1, 2, 0, 0,
                0, 0, 1, 0, 3, 0, 2, 2, 0, 0, 1, 1, 1, 1, 2, 0,
                2, 0, 1, 0, 3, 0, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1,
                0, 0, 1, 0, 3, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1,
        };

        // create the tilemap from the level definition
        TileMap map;
        if (!map.load("C:/Users/Administrator/Documents/Visual Studio 2013/Projects/SimplePlatformer/SimplePlatformer/tileset.png", sf::Vector2u(32, 32), level, 16, 8))
        {
                std::cout << "Error";
                return -1;
        }
               
       

        RenderWindow win(VideoMode(512,256), "Test", Style::Close | Style::Titlebar);
        Event ev;

        while (win.isOpen())
        {
                while (win.pollEvent(ev))
                if (ev.type == Event::Closed) win.close();

                win.clear();

                win.draw(map);

                win.display();
        }

        return 0;
}

Pages: [1]