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

Author Topic: Getting weird tile  (Read 964 times)

0 Members and 1 Guest are viewing this topic.

NonMysteriousGuy

  • Newbie
  • *
  • Posts: 2
    • View Profile
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;
}

Afrodeity

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Getting weird tile
« Reply #1 on: March 14, 2020, 09:50:00 pm »
You are writing each quad's texture coordinates to the same corner of the quad.