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

[C++ SFML 2.6.2] Auto-Tiling

Ostatnio zmodyfikowano 2025-03-21 22:26
Autor Wiadomość
tBane
Temat założony przez niniejszego użytkownika
[C++ SFML 2.6.2] Auto-Tiling
» 2025-03-19 22:04:05
Cześć Wszystkim!
Próbuję napisać auto-tiling. I nie wiem jak edytować dane bity. Normalnie do kafelka odnoszę się w ten sposób:

C/C++
class Terrain
    : public sf::Drawable
    , public sf::Transformable
{
public:
   
short width, height; // normal is a 16x16
   
sf::Vector2i coords; // multiply by 16x16
   
sf::VertexArray vertexes; // vertexes of tiles
   
SingleTexture * tileset; // main texture
   
std::vector < short > tiles; // tile values
    // etc..
};

void Terrain::edit( sf::Vector2f worldMousePosition, short value ) {
   
   
// edycja kafelka
   
short coord_x =( worldMousePosition.x - coords.x * 16 ) / 16;
   
short coord_y =( worldMousePosition.y - coords.y * 16 ) / 16;
   
   
if( coord_x < 0 || coord_x >= width || coord_y < 0 || coord_y >= height )
       
 return;
   
   
tiles[ coord_y * width + coord_x ] = short( value ); // chciałbym przypisywać bity
   
   
sf::Vertex * quad = & vertexes[( coord_y * width + coord_x ) * 4 ];
   
   
// edycja vertex-array
   
short tu =( short( abs( coord_x ) * tileSide ) % 64 ) +( value * 64 );
   
short tv =( short( abs( coord_y ) * tileSide ) % 64 );
   
   
//cout << "tu: " << tu << ", tv: " << tv << "\n";
   
   
   
quad[ 0 ].texCoords = sf::Vector2f( tu + 1, tv + 1 );
   
quad[ 1 ].texCoords = sf::Vector2f( tu + tileSide - 1, tv + 1 );
   
quad[ 2 ].texCoords = sf::Vector2f( tu + tileSide - 1, tv + tileSide - 1 );
   
quad[ 3 ].texCoords = sf::Vector2f( tu + 1, tv + tileSide - 1 );
   
   
}

Chciałbym przerobić tak klasę Terrain by zamiast
std::vector < short > tiles
 miała
std::vector < char > tile_bits


C/C++
class Terrain
    : public sf::Drawable
    , public sf::Transformable
{
public:
   
short width, height; // normal is a 16x16
   
sf::Vector2i coords; // multiply by 16x16
   
std::vector < char > tile_bits; // np. 0000 - 0 - czerwony, 0001 - 1 - lewy-górny szary, 0010 - 2 prawy-górny szary, 1111 - 15 - szary
   
sf::VertexArray vertexes; // vertexes of tiles
   
SingleTexture * tileset; // main texture
   
    // ... etc.
};

I taki tile_bits zawierał by dane binarne o kafelku. Tak jak na załączonym screenie. Jak odnieść się do takiego bitu ?

U mnie kafelki bazowe mają rozmiar 64x64 stąd:
C/C++
short tu =( short( abs( coord_x ) * tileSide ) % 64 ) +( value * 64 );
short tv =( short( abs( coord_y ) * tileSide ) % 64 );
P-182166
DejaVu
» 2025-03-19 22:22:45
1. Rzutuj na jakiś typ unsigned.
2. Potem wyciągaj informacje o tym bicie, który Ciebie interesuje:
C/C++
int bitNumber = 0;
bool isBitSet =(( value >> bitNumber ) & 1 ) > 0; // to wyciągnie podany bit
Możesz odczytywać bity pętlą.
P-182167
tBane
Temat założony przez niniejszego użytkownika
» 2025-03-19 22:33:46
Dobrze rozumiem?
C/C++
bool bit0 =(( value >> 0 ) & 1 ) > 0;
bool bit1 =(( value >> 1 ) & 1 ) > 0;
bool bit2 =(( value >> 2 ) & 1 ) > 0;
bool bit3 =(( value >> 3 ) & 1 ) > 0;
P-182168
DejaVu
» 2025-03-19 22:35:11
tak.
P-182169
tBane
Temat założony przez niniejszego użytkownika
» 2025-03-19 22:36:09
Dziękuje Mistrzu :-)


który bit jest zaznaczony
C/C++
short bit_x =( worldMousePosition.x - coords.x * 16 ) % 2;
short bit_y =( worldMousePosition.y - coords.y * 16 ) % 2;

edycja bitu
C/C++
value = tiles[ coord_y * width + coord_x ];
bool bit0 =(( value >> 0 ) & 1 ) > 0;
bool bit1 =(( value >> 1 ) & 1 ) > 0;
bool bit2 =(( value >> 2 ) & 1 ) > 0;
bool bit3 =(( value >> 3 ) & 1 ) > 0;
P-182170
tBane
Temat założony przez niniejszego użytkownika
» 2025-03-20 15:08:05
Mam jeszcze jedno pytanie.

Mam 10 bazowych płytek i 16 wariantów kafelków granicznych co daje łącznie 10x10x16 możliwych kombinacji kafelków. Zważywszy, że pojedynczy kafelek ma wymiary 64x64 to daje nam teksturę o rozmiarze 64x102400... Nie zmieści się to w pamięci GPU chyba. Jak więc w sfml przechowywać teksturę kafelków?

C/C++
class Terrain
    : public sf::Drawable
    , public sf::Transformable
{
public:
   
short width, height; // normal is a 16x16
   
sf::Vector2i coords; // multiply by 16x16
   
sf::VertexArray vertexes; // vertexes of tiles
   
SingleTexture * tileset; // main texture
   
std::vector < char > tiles_bits; // tiles bits
   
    /// ...
   
private:
   
   
virtual void draw( sf::RenderTarget & target, sf::RenderStates states ) const
   
{
       
// draw tiles - terrain
       
states.transform *= getTransform();
       
states.texture = & * tileset->texture; // odniesienie się do tekstury
       
target.draw( vertexes, states );
   
}
}
;
P-182171
DejaVu
» 2025-03-20 15:11:13
Jeżeli masz takie 'duże' wartości to raczej powinieneś trzymać dane w 'surowej' postaci i generować sobie tylko te, których potrzebujesz, bo i tak każdej możliwej kombinacji nie będziesz renderował.
P-182172
tBane
Temat założony przez niniejszego użytkownika
» 2025-03-20 15:26:30
w surowej postaci tzn ?
P-182173
« 1 » 2
  Strona 1 z 2 Następna strona