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

Author Topic: please tell me how to do it correctly (VertexArray)  (Read 3080 times)

0 Members and 1 Guest are viewing this topic.

GamDev

  • Newbie
  • *
  • Posts: 29
    • View Profile
please tell me how to do it correctly (VertexArray)
« on: July 11, 2018, 06:20:33 pm »
hi, please tell me how to do it correctly:
I have a class of a game map. Map size 100x100
inside the class of the game map there is a class DrawTileMap
I draw map like this:
Quote
class DrawTileMap : public sf::Drawable, public sf::Transformable
{
    public:
        void            load                (const Texture tileset, int ( &TileMap )[100][100]);
        void            update              (int ( &TileMap )[100][100]);
    private:
        virtual void    draw                (sf::RenderTarget& target, sf::RenderStates states) const;
        VertexArray     m_vertices;
        Texture         m_tileset;
        Vector2u        tileSize            = {64, 64};
};

Quote
#include "DrawTileMap.h"

void DrawTileMap::load(const Texture tileset, int ( &TileMap )[100][100])
{
    m_tileset = tileset;

    m_vertices.setPrimitiveType(sf::Quads);
    m_vertices.resize(100 * 100 * 4);
}

void DrawTileMap::update(int ( &TileMap )[100][100])
{
    int tu = 0;
    int tv = 0;

    for(unsigned int x = 0; x < 100; ++x)
        for(unsigned int y = 0; y < 100; ++y)
            {
                switch(TileMap[ x ] [ y ])
                    {
                        case 1: tu = 0;tv = 0;break; // Water
                        case 2: tu = 1;tv = 0;break; // Ground
                        case 3: tu = 2;tv = 0;break; // Grass

                        default:tu = 0;tv = 0;break;
                    }

                sf::Vertex* tile64 = &m_vertices[(x + y * 100) * 4];

                tile64[0].position = sf::Vector2f(x * tileSize.x, y * tileSize.y);
                tile64[1].position = sf::Vector2f((x + 1) * tileSize.x, y * tileSize.y);
                tile64[2].position = sf::Vector2f((x + 1) * tileSize.x, (y + 1) * tileSize.y);
                tile64[3].position = sf::Vector2f(x * tileSize.x, (y + 1) * tileSize.y);

                tile64[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
                tile64[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
                tile64[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
                tile64[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
            }
}

void DrawTileMap::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    states.transform *= getTransform();
    states.texture = &m_tileset;
    target.draw(m_vertices, states);
}

With this approach, I get pictures as pictured on "picture1.png"
but in my tile set there is a smooth transition from grass to earth, 3 sprite
How can I do this to get what is depicted in the "picture2.png" ?
« Last Edit: July 12, 2018, 06:13:22 am by GamDev »

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: please tell me how to do it correctly
« Reply #1 on: July 11, 2018, 08:08:08 pm »
The subject of your topic should specify the issue. "help me" is not a proper topic title.

You need to overlay your 3rd tile on top of your 1st tile. You can't do that with a single array.

GamDev

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: please tell me how to do it correctly (VertexArray)
« Reply #2 on: July 12, 2018, 06:14:06 am »
ok, then I need to create one more VertexArray ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: please tell me how to do it correctly (VertexArray)
« Reply #3 on: July 12, 2018, 06:35:14 am »
Quote
You can't do that with a single array.
Why not?
Laurent Gomila - SFML developer

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: please tell me how to do it correctly (VertexArray)
« Reply #4 on: July 12, 2018, 07:02:16 am »
Quote
You can't do that with a single array.
Why not?

Look at tileset.png. OP wants tile 3 on top of tile 1. He/She is using a 2d c style array to draw the tile map. Am I missing something?
« Last Edit: July 12, 2018, 07:03:56 am by NGM88 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: please tell me how to do it correctly (VertexArray)
« Reply #5 on: July 12, 2018, 07:55:40 am »
Ah, I thought you were talking about the VertexArray. Indeed it would require more information in addition to the level array.
Laurent Gomila - SFML developer

GamDev

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: please tell me how to do it correctly (VertexArray)
« Reply #6 on: July 12, 2018, 01:33:30 pm »
Quote
using a 2d c style array
what is 2d C++ style array ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: please tell me how to do it correctly (VertexArray)
« Reply #7 on: July 12, 2018, 01:45:17 pm »
Quote
what is 2d C++ style array ?
This: int TileMap[100][100]
... although I would rather call it a "2D C-style array" (C++ style would rather be std::array) ;)
Laurent Gomila - SFML developer

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: please tell me how to do it correctly (VertexArray)
« Reply #8 on: July 12, 2018, 04:25:58 pm »
So anyway, either make 2 tilemap arrays (1 for background, 1 for transitional tiles) OR the easier option: just add those transitional tiles to your tileset. You'll have to add a tile for each direction, but a slightly bigger tileset is easier to manage than 2 tilemaps.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: please tell me how to do it correctly (VertexArray)
« Reply #9 on: July 12, 2018, 08:03:12 pm »
Or deduce overlays from underlying tiles. In this case, for example, if you have tile 1 and tile 2 on its right, then you know that you must overlay tile 3 on tile 1, no need to get this information hard-coded somewhere. This makes it easier to modify the tileset.
Laurent Gomila - SFML developer