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

Author Topic: [Solved]Tile map in C#  (Read 8843 times)

0 Members and 2 Guests are viewing this topic.

delio

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
[Solved]Tile map in C#
« on: April 05, 2014, 03:35:44 pm »
class TileMap : Drawable
        {

            public bool load(string tileset, SFML.Window.Vector2u tileSize, int[] tiles, uint width, uint height)
            {

                // resize the vertex array to fit the level size
                // m_vertices.PrimitiveType(Quads);
                m_vertices.Resize(width * height * 4);

                // populate the vertex array, with one quad per tile
                for (uint i = 0; i < width; ++i)
                {
                    for (uint j = 0; j < height; ++j)
                    {
                        // get the current tile number
                        int tileNumber = tiles[i + j * width];

                        // find its position in the tileset texture
                        long tu = tileNumber % (m_tileset.Size.X / tileSize.X);
                        long tv = tileNumber / (m_tileset.Size.X / tileSize.X);

                        // get a pointer to the current tile's quad
                        uint index = (i + j * width) * 4;

                        // define its 4 corners
                        m_vertices[index + 0] = new Vertex(new Vector2f(i * tileSize.X, j * tileSize.Y), new Vector2f(tu * tileSize.X, tv * tileSize.Y));
                        m_vertices[index + 1] = new Vertex(new Vector2f((i + 1) * tileSize.X, j * tileSize.Y), new Vector2f((tu + 1) * tileSize.X, tv * tileSize.Y));
                        m_vertices[index + 2] = new Vertex(new Vector2f((i + 1) * tileSize.X, (j + 1) * tileSize.Y), new Vector2f((tu + 1) * tileSize.X, (tv + 1) * tileSize.Y));
                        m_vertices[index + 3] = new Vertex(new Vector2f(i * tileSize.X, (j + 1) * tileSize.Y), new Vector2f(tu * tileSize.X, (tv + 1) * tileSize.Y));
                    }
                }

                return true;
            }

            void Drawable.Draw(SFML.Graphics.RenderTarget target, SFML.Graphics.RenderStates states)
            {
                // apply the transform
                //states.Transform *= getTransform();

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

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

            public SFML.Graphics.Texture m_tileset = new SFML.Graphics.Texture(@"d:/tileset.png");
            private SFML.Graphics.VertexArray m_vertices = new SFML.Graphics.VertexArray();

        }
This is my code
And problem is It display as dot, not areas.
like in the picture

What is the mistake?
Thanks
« Last Edit: April 07, 2014, 04:49:07 pm by delio »

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: Tile map in C#
« Reply #1 on: April 06, 2014, 06:31:22 pm »
Noticed that most of the loading functions are in the constructors in C#.  Also in some cases you'll need to use properties in place of getters and setters you'd use in C++.  Since I'm not working with tilemaps right now I really can't help much with this.
I have many ideas but need the help of others to find way to make use of them.

delio

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Re: Tile map in C#
« Reply #2 on: April 07, 2014, 03:34:55 pm »
Noticed that most of the loading functions are in the constructors in C#.  Also in some cases you'll need to use properties in place of getters and setters you'd use in C++.  Since I'm not working with tilemaps right now I really can't help much with this.
Now I can loading tileset.
But problem is my program display as dot, not areas.
« Last Edit: April 07, 2014, 03:46:50 pm by delio »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Tile map in C#[Updated]
« Reply #3 on: April 07, 2014, 03:58:30 pm »
You don't define the primitive type of your VertexArray to Quads, you use the default one.
And the doc says : "The default primitive type is sf::Points."

delio

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Re: Tile map in C#[Updated]
« Reply #4 on: April 07, 2014, 04:00:39 pm »
Hi,
I have no idea how can I set it?
m_vertices.PrimitiveType(SFML.Graphics.PrimitiveType.Quads);
This gives error "Cannot use like this method"
« Last Edit: April 07, 2014, 04:06:54 pm by delio »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Tile map in C#[Updated]
« Reply #5 on: April 07, 2014, 04:09:19 pm »
What did I tell you about properties....  :)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

delio

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Re: Tile map in C#[Updated]
« Reply #6 on: April 07, 2014, 04:10:26 pm »
What did I tell you about properties....  :)

Okay :-[
m_vertices.PrimitiveType = SFML.Graphics.PrimitiveType.Quads;

Thanks for every answer  :)

 

anything