SFML community forums
Help => Graphics => Topic started by: GamDev 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:
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};
};
#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" ?
-
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.
-
ok, then I need to create one more VertexArray ?
-
You can't do that with a single array.
Why not?
-
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?
-
Ah, I thought you were talking about the VertexArray. Indeed it would require more information in addition to the level array.
-
using a 2d c style array
what is 2d C++ style array ?
-
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) ;)
-
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.
-
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.