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

Author Topic: TileSet code problem  (Read 3777 times)

0 Members and 2 Guests are viewing this topic.

Pacis

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
TileSet code problem
« on: May 13, 2019, 11:30:44 pm »
Hi guys i've made the tilesSet from this example "https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php" in c# indeed.
    class TileSet : Entità
    {

        protected override Texture Texture
        {
            get { return Texture; }
            set { this.Texture = value; }
        }

        protected override VertexArray VertexArray
        {
            get { return VertexArray; }
            set { this.VertexArray = value; }
        }

        public bool Load(string tileSetPath, Vector2u tileSize, int[] tiles, uint width, uint height)
        {
            bool esito = false;

            Texture Texture = new Texture(tileSetPath);
            VertexArray VertexArray = new VertexArray();
            int tileIndex;
            //Carichiamo la texture del tileset
            if (Texture != null)
            {
               
                //Ridimensioniamo la grandezza del VertexArray
                VertexArray.PrimitiveType = PrimitiveType.Quads;
                VertexArray.Resize(width * height * 4);

                for (uint i = 0; i < width; ++ i)
                {
                    for (uint j = 0; j < height;++ j)
                    {

                        //Posizione nel tile fatto
                        tileIndex = tiles[i + j * width];

         
                        //Posizione nella texture del tile caricato
                        long tu = tileIndex % ((int)Texture.Size.X / (int)tileSize.X);
                        long tv = tileIndex / ((int)Texture.Size.X / (int)tileSize.X);
                       
                        //Indice alla posizione attuale del tile
                        uint posizione = (i + (j * width)) * 4;
                       
                        // define its 4 corners
                        VertexArray[posizione + 1] = new Vertex(new Vector2f(i * tileSize.X, j * tileSize.Y), new Vector2f(tu * tileSize.X, tv * tileSize.Y));
                        VertexArray[posizione + 2] = new Vertex(new Vector2f((i + 1) * tileSize.X, j * tileSize.Y), new Vector2f((tu + 1) * tileSize.X, tv * tileSize.Y));
                        VertexArray[posizione + 3] = new Vertex(new Vector2f((i + 1) * tileSize.X, (j + 1) * tileSize.Y), new Vector2f((tu + 1) * tileSize.X, (tv + 1) * tileSize.Y));
                        VertexArray[posizione + 4] = new Vertex(new Vector2f(i * tileSize.X, (j + 1) * tileSize.Y), new Vector2f(tu * tileSize.X, (tv + 1) * tileSize.Y));
                    }
                }
                Console.WriteLine("listen");
            }

            return esito;
        }
        //Sovrascrittura funzione draw
        public override void Draw(RenderTarget target, RenderStates states)
        {
            //Applichiamo la texture
            states.Texture = this.Texture;

            //disegnamo l'array dei vertex
            target.Draw(VertexArray, states);
        }

    }
}

but i get from:
//Posizione nel tile fatto
tileIndex = tiles[i + j * width];
That i go over the limit of the array i've given but i'm doing it like on the example...this is my main btw

    class Program
    {
        public static void Main(string[] args)
        {
            RenderWindow window = new RenderWindow(new VideoMode(512, 256), "Prova");

            int[] mappatura = { 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,};

            TileSet tileSet = new TileSet();

            if (!tileSet.Load("./Immagini/tileset/test.png", new Vector2u(32, 32), mappatura, 16, 8))
                Console.WriteLine("oh fuck");

            while (window.IsOpen)
            {
                // draw the map
                window.Clear();
                window.Draw(tileSet);
                window.Display();
            }
 
        }
    }
using the tile set they give to you, downloaded from https://www.sfml-dev.org/tutorials/2.5/images/graphics-vertex-array-tilemap-tileset.png
I don't even know if the problem is the line of code it self or anything else, can you guys help me? :C
(UPDATE) so if i put some prints here and there it happens to get at least .Draw and then give me error, still havent solved anything...
(UPDATE) i've moved the making of the vertexArray and the the textures file out of the load, now if i run the program it gives me a white screen with nothing and then it close it self, but if i put a print after
"VertexArray[posizione + 4] = new Vertex(new Vector2f(i * tileSize.X, (j + 1) * tileSize.Y),
 new Vector2f(tu * tileSize.X, (tv + 1) * tileSize.Y));"
it does show me my map but with the tiles all in diagonals .-.
Lol i'm an idiot i took the wrong places in the array, i've used 1 2 3 4 instead of 0 1 2 3 solved
« Last Edit: May 14, 2019, 10:37:24 am by eXpl0it3r »

 

anything