Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[SFML 2.X] Marching Squares - Zaokrąglone kafelki mapy

Ostatnio zmodyfikowano 2024-10-04 23:33
Autor Wiadomość
tBane
Temat założony przez niniejszego użytkownika
[SFML 2.X] Marching Squares - Zaokrąglone kafelki mapy
» 2024-10-04 16:43:17
Witam.
Programuję grę od dłuższego czasu i napisałem już kod do generowania terenu. Jednak chciałbym urozmaicić trochę mapę i dodać do niej zaokrąglone tilesy. Jak to zrobić w SFML 2.X ?
Zamieszczam poniżej kod, który mam obecnie:

C/C++
class Terrain
    : public sf::Drawable
    , public sf::Transformable
{
public:
   
short width, height;
   
sf::Vector2i coords;
   
   
sf::VertexArray vertexes;
   
sf::Texture tileset;
   
   
std::vector < short > tiles;
   
   
Terrain( short x, short y, short width, short height ) {
       
       
coords.x = x;
       
coords.y = y;
       
       
this->width = width;
       
this->height = height;
       
       
tileset = sf::Texture();
       
tileset = * getTexture( "tiles/0_tileset" )->texture;
       
       
       
       
vertexes.setPrimitiveType( sf::Triangles );
       
vertexes.resize( width * height * 6 ); // widthMap * heightMap * TwoTrianglesVertices
       
       
tiles.resize( width * height );
       
       
short coord_x, coord_y;
       
       
// TERRAIN - GRASS
       
for( short y = 0; y < height; y++ )
       
for( short x = 0; x < width; x++ ) {
           
           
sf::Vertex * triangles = & vertexes[( y * width + x ) * 6 ];
           
           
coord_x =( coords.x + x );
           
coord_y =( coords.y + y );
           
           
triangles[ 0 ].position = sf::Vector2f( coord_x * tileSide, coord_y * tileSide );
           
triangles[ 1 ].position = sf::Vector2f(( coord_x + 1 ) * tileSide, coord_y * tileSide );
           
triangles[ 2 ].position = sf::Vector2f( coord_x * tileSide,( coord_y + 1 ) * tileSide );
           
triangles[ 3 ].position = sf::Vector2f( coord_x * tileSide,( coord_y + 1 ) * tileSide );
           
triangles[ 4 ].position = sf::Vector2f(( coord_x + 1 ) * tileSide, coord_y * tileSide );
           
triangles[ 5 ].position = sf::Vector2f(( coord_x + 1 ) * tileSide,( coord_y + 1 ) * tileSide );
           
           
edit( x, y, 0 );
       
}
       
       
    }
   
   
void edit( short x, short y, short value ) {
       
       
if( x < 0 || x >= width || y < 0 || y >= height )
           
 return;
       
       
if( value > 3 || value < 0 )
           
 return;
       
       
tiles[ y * width + x ] = value;
       
       
short global_x = coords.x + x;
       
short global_y = coords.y + y;
       
       
sf::Vertex * triangles = & vertexes[( y * width + x ) * 6 ];
       
       
short tu =( short( global_x * tileSide ) % 64 ) +( value * 64 );
       
short tv =( short( global_y * tileSide ) % 64 );
       
       
//cout << "tu: " << tu << ", tv: " << tv << "\n";
       
       
triangles[ 0 ].texCoords = sf::Vector2f( tu, tv );
       
triangles[ 1 ].texCoords = sf::Vector2f( tu + tileSide, tv );
       
triangles[ 2 ].texCoords = sf::Vector2f( tu, tv + tileSide );
       
triangles[ 3 ].texCoords = sf::Vector2f( tu, tv + tileSide );
       
triangles[ 4 ].texCoords = sf::Vector2f( tu + tileSide, tv );
       
triangles[ 5 ].texCoords = sf::Vector2f( tu + tileSide, tv + tileSide );
   
}
   
private:
   
   
virtual void draw( sf::RenderTarget & target, sf::RenderStates states ) const
   
{
       
       
// draw tiles - terrain
       
states.transform *= getTransform();
       
states.texture = & tileset; // TO-DO
       
target.draw( vertexes, states );
   
}
}
;
P-181674
pekfos
» 2024-10-04 20:08:48
Chcesz zaokrąglić kafle, czy mieć przejścia między różnymi kaflami? To niezupełnie jest to samo. W naturze przejście między piaskiem i trawą, nieważne proste czy zaokrąglone, nie jest wyraźną granicą. Z tego względu zwykle robi się dedykowane kafle dla przejść miedzy typami terenu.
P-181675
tBane
Temat założony przez niniejszego użytkownika
» 2024-10-04 22:17:13
Potrzebuję zaokrąglić kafle.
P-181676
pekfos
» 2024-10-04 23:33:46
Zrób mapę kafelkową w siatce przesuniętej o pół kafla względem tego co chcesz uzyskać. Przejście na środku kafla w tej siatce wychodzi na granicy dwóch kafli w docelowej siatce. Kafel narysuj na podstawie typów terenów jakie masz na rogach.
P-181677
« 1 »
  Strona 1 z 1