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

[SFML 2.X] Generowanie Sprajtów Budynków z dachem dwuspadowym

Ostatnio zmodyfikowano 2024-11-09 18:16
Autor Wiadomość
pekfos
» 2024-11-09 15:42:35
Usunąłem zduplikowany post jakby co. Co do współrzędnych, może być ułatwieniem to że tekstura może być automatycznie powtarzana:
https://www.sfml-dev.org/documentation/2.6.1/classsf_1_1Texture.php#aaa87d1eff053b9d4d34a24c784a28658
Nie da się tu nic więcej powiedzieć nie wiedząc jak wygląda ta tekstura. Zakładam że masz tam teksturę drewnianej belki która się kafelkuje bez widocznego przejścia w jednej osi, powiedzmy pionowej. Wtedy Współrzędne to (0,0), (w,0), (w,h*n), (0,h*n). Dobierasz tak żeby tekstura się nie rozciągała ani nie kurczyła w widoczny sposób.
P-181861
tBane
Temat założony przez niniejszego użytkownika
» 2024-11-09 18:02:11
Ok. Dzięki :-) Teraz muszę jakoś te belki ładnie oteksturować
P-181862
tBane
Temat założony przez niniejszego użytkownika
» 2024-11-09 18:16:20
Dobra. Udało mi się zrobić automatyczne generowanie dachów i działa dla różnych rozmiarów. Załączam dodatkowo kod, może kiedyś komuś się przyda :-)
C/C++
void loadTexture( std::ifstream & file ) {
   
   
// check the size
   
cout << size.x << " " << size.y << "\n";
   
   
short walls_height = 3;
   
   
float tiles_rows = 16; //size.y / 2;
   
float tiles_columns = 16; // size.x / 2;
   
   
float tile_width = float( size.x ) / 4.0f * 32.0f / tiles_columns;
   
float tile_height = float( size.y ) / 2.0f * 32.0f / tiles_rows;
   
float tile_border = 1;
   
   
sf::Color color_outside = sf::Color::Black; //sf::Color(51, 38, 21);
   
sf::Color color_inside = sf::Color( 128, 24, 24 );
   
   
// create basic image to render house
   
sf::Image house_image;
   
house_image.create( size.x * 16 + tile_width,( walls_height + size.x / 2.0f ) * 16.0f + tiles_rows * tile_height + tile_height, sf::Color::Transparent );
   
   
// load the wall texture
   
sf::Image wall;
   
wall.create( 32, 32, sf::Color::Transparent );
   
wall = getTexture( "walls/wooden_wall" )->texture->copyToImage();
   
   
   
// DRAWING A WALLS
   
for( short y = 1; y <= walls_height; y++ ) {
       
for( short x = 0; x < size.x / 2; x++ ) {
           
house_image.copy( wall, x * 2 * 16 + tile_width / 2.0f, house_image.getSize().y - y * 2 * 16 );
       
}
    }
   
   
// DRAWING A WALLS UNDER THE ROOF
   
short height = size.x / 4;
   
short width = size.x / 2;
   
short start_x;
   
short end_x;
   
   
for( int y = 0; y <= height; y++ ) {
       
start_x = y;
       
end_x = width - y - 1;
       
       
for( int x = 0; x < width; x++ ) {
           
if( x >= start_x && x <= end_x ) {
               
house_image.copy( wall, x * 2 * 16 + tile_width / 2.0f, house_image.getSize().y -( y + walls_height + 1 ) * 2 * 16 );
           
}
        }
    }
   
   
// DRAWING A ROOF
   
sf::Color color = sf::Color::Black;
   
   
sf::Vector2f p1( 0,( walls_height - 1.0f ) * 2.0f * 16.0f );
   
sf::Vector2f p2( tile_width / 2.0f + size.x / 2.0f * 16.0f,( walls_height - 1.0f + size.x / 4.0f ) * 32.0f + tile_height / 4.0f );
   
sf::Vector2f p3( tile_width + size.x * 16.0f,( walls_height - 1.0f ) * 32.0f );
   
sf::Vector2f p4( tile_width + size.x * 16.0f,( walls_height - 1.0f ) * 32.0f + tiles_rows * tile_height );
   
sf::Vector2f p5( tile_width / 2.0f + size.x / 2.0f * 16.0f,( walls_height - 1.0f + size.x / 4.0f ) * 32.0f + tiles_rows * tile_height + tile_height / 4.0f );
   
sf::Vector2f p6( 0,( walls_height - 1.0f ) * 32.0f + tiles_rows * tile_height );
   
   
sf::VertexArray left_quad( sf::Quads, 4 );
   
left_quad[ 0 ].position = p1;
   
left_quad[ 1 ].position = p2;
   
left_quad[ 2 ].position = p5;
   
left_quad[ 3 ].position = p6;
   
   
for( short i = 0; i < 4; i++ )
       
 left_quad[ i ].color = color;
   
   
sf::VertexArray right_quad( sf::Quads, 4 );
   
right_quad[ 0 ].position = p2;
   
right_quad[ 1 ].position = p3;
   
right_quad[ 2 ].position = p4;
   
right_quad[ 3 ].position = p5;
   
   
for( short i = 0; i < 4; i++ )
       
 right_quad[ i ].color = color;
   
   
sf::RenderTexture rtex;
   
rtex.create( house_image.getSize().x, house_image.getSize().y );
   
rtex.draw( left_quad );
   
rtex.draw( right_quad );
   
   
// CREATE A TILES
   
sf::RenderStates rstate( getTexture( "tiles/tile_5_highlands" )->texture );
   
   
// LEFT SIDE OF ROOF - EVEN TILES
   
for( float x = 0; x < tiles_columns; x += 2 ) {
       
for( float y = 0; y < tiles_rows; y += 1 ) {
           
           
sf::Vector2f quad_pos( x * tile_width,( walls_height - 1.0f ) * 32.0f + y * tile_height + x * tile_width );
           
           
sf::VertexArray tile( sf::Quads, 4 );
           
tile[ 0 ].position = sf::Vector2f( quad_pos.x + tile_border, quad_pos.y + tile_border );
           
tile[ 1 ].position = sf::Vector2f( quad_pos.x + tile_width - tile_border, quad_pos.y + tile_width + tile_border );
           
tile[ 2 ].position = sf::Vector2f( quad_pos.x + tile_width - tile_border, quad_pos.y + tile_width + tile_height - tile_border );
           
tile[ 3 ].position = sf::Vector2f( quad_pos.x + tile_border, quad_pos.y + tile_height - tile_border );
           
           
tile[ 0 ].texCoords = sf::Vector2f( 0, 0 );
           
tile[ 1 ].texCoords = sf::Vector2f( tile_width, 0 );
           
tile[ 2 ].texCoords = sf::Vector2f( tile_width, tile_height );
           
tile[ 3 ].texCoords = sf::Vector2f( 0, tile_height );
           
           
rtex.draw( tile, rstate );
       
}
    }
   
   
// LEFT SIDE OF ROOF - ODD TILES
   
for( float x = 1; x < tiles_columns; x += 2 ) {
       
       
// bottom tile
       
sf::Vector2f bottom_pos( x * tile_width,( walls_height - 1.0f ) * 32.0f + x * tile_width + tile_height / 2.0f );
       
sf::VertexArray bottom_tile( sf::Quads, 4 );
       
bottom_tile[ 0 ].position = sf::Vector2f( bottom_pos.x + tile_border, bottom_pos.y - tile_height / 2.0f + tile_border );
       
bottom_tile[ 1 ].position = sf::Vector2f( bottom_pos.x + tile_width - tile_border, bottom_pos.y + tile_width - tile_height / 2.0f + tile_border );
       
bottom_tile[ 2 ].position = sf::Vector2f( bottom_pos.x + tile_width - tile_border, bottom_pos.y + tile_width - tile_border );
       
bottom_tile[ 3 ].position = sf::Vector2f( bottom_pos.x + tile_border, bottom_pos.y - tile_border );
       
       
bottom_tile[ 0 ].texCoords = sf::Vector2f( 0, 0 );
       
bottom_tile[ 1 ].texCoords = sf::Vector2f( tile_width, 0 );
       
bottom_tile[ 2 ].texCoords = sf::Vector2f( tile_width, tile_height );
       
bottom_tile[ 3 ].texCoords = sf::Vector2f( 0, tile_height );
       
       
rtex.draw( bottom_tile, rstate );
       
       
// center tiles
       
for( float y = 0; y < tiles_rows - 1; y += 1 ) {
           
           
sf::Vector2f quad_pos( x * tile_width,( walls_height - 1.0f ) * 32.0f + y * tile_height + x * tile_width + tile_height / 2.0f );
           
           
sf::VertexArray tile( sf::Quads, 4 );
           
tile[ 0 ].position = sf::Vector2f( quad_pos.x + tile_border, quad_pos.y + tile_border );
           
tile[ 1 ].position = sf::Vector2f( quad_pos.x + tile_width - tile_border, quad_pos.y + tile_width + tile_border );
           
tile[ 2 ].position = sf::Vector2f( quad_pos.x + tile_width - tile_border, quad_pos.y + tile_width + tile_height - tile_border );
           
tile[ 3 ].position = sf::Vector2f( quad_pos.x + tile_border, quad_pos.y + tile_height - tile_border );
           
           
tile[ 0 ].texCoords = sf::Vector2f( 0, 0 );
           
tile[ 1 ].texCoords = sf::Vector2f( tile_width, 0 );
           
tile[ 2 ].texCoords = sf::Vector2f( tile_width, tile_height );
           
tile[ 3 ].texCoords = sf::Vector2f( 0, tile_height );
           
           
rtex.draw( tile, rstate );
       
}
       
       
// top tile
       
sf::Vector2f top_pos( x * tile_width,( walls_height - 1.0f ) * 32.0f + tiles_rows * tile_height + x * tile_width - tile_height / 2.0f );
       
sf::VertexArray top_tile( sf::Quads, 4 );
       
top_tile[ 0 ].position = sf::Vector2f( top_pos.x + tile_border, top_pos.y + tile_border );
       
top_tile[ 1 ].position = sf::Vector2f( top_pos.x + tile_width - tile_border, top_pos.y + tile_width + tile_border );
       
top_tile[ 2 ].position = sf::Vector2f( top_pos.x + tile_width - tile_border, top_pos.y + tile_width + tile_height / 2.0f - tile_border );
       
top_tile[ 3 ].position = sf::Vector2f( top_pos.x + tile_border, top_pos.y + tile_height / 2.0f - tile_border );
       
       
top_tile[ 0 ].texCoords = sf::Vector2f( 0, 0 );
       
top_tile[ 1 ].texCoords = sf::Vector2f( tile_width, 0 );
       
top_tile[ 2 ].texCoords = sf::Vector2f( tile_width, tile_height );
       
top_tile[ 3 ].texCoords = sf::Vector2f( 0, tile_height );
       
       
rtex.draw( top_tile, rstate );
   
}
   
   
   
// RIGHT SIDE OF ROOF - EVEN TILES
   
for( float x = 0; x < tiles_columns; x += 2 ) {
       
for( float y = 0; y < tiles_rows; y += 1 ) {
           
           
sf::Vector2f quad_pos( size.x * 16.0f - x * tile_width,( walls_height - 1.0f ) * 32.0f + y * tile_height + x * tile_width );
           
sf::VertexArray tile( sf::Quads, 4 );
           
tile[ 0 ].position = sf::Vector2f( quad_pos.x + tile_border, quad_pos.y + tile_width + tile_border );
           
tile[ 1 ].position = sf::Vector2f( quad_pos.x + tile_width - tile_border, quad_pos.y + tile_border );
           
tile[ 2 ].position = sf::Vector2f( quad_pos.x + tile_width - tile_border, quad_pos.y + tile_height - tile_border );
           
tile[ 3 ].position = sf::Vector2f( quad_pos.x + tile_border, quad_pos.y + tile_width + tile_height - tile_border );
           
           
tile[ 0 ].texCoords = sf::Vector2f( 0, 0 );
           
tile[ 1 ].texCoords = sf::Vector2f( tile_width, 0 );
           
tile[ 2 ].texCoords = sf::Vector2f( tile_width, tile_height );
           
tile[ 3 ].texCoords = sf::Vector2f( 0, tile_height );
           
           
sf::RenderStates rstate( getTexture( "tiles/tile_5_highlands" )->texture );
           
rtex.draw( tile, rstate );
       
}
    }
   
   
// RIGHT SIDE OF ROOF - ODD TILES
   
for( float x = 1; x < tiles_columns; x += 2 ) {
       
       
// bottom tile
       
sf::Vector2f bottom_pos( size.x / 2.0f * 16.0f + x * tile_width,( walls_height - 1.0f + size.x / 4.0f ) * 32.0f - x * tile_width - tile_height / 2.0f );
       
sf::VertexArray bottom_tile( sf::Quads, 4 );
       
bottom_tile[ 0 ].position = sf::Vector2f( bottom_pos.x + tile_border, bottom_pos.y + tile_width + tile_border + tile_height / 2.0f );
       
bottom_tile[ 1 ].position = sf::Vector2f( bottom_pos.x + tile_width - tile_border, bottom_pos.y + tile_border + tile_height / 2.0f );
       
bottom_tile[ 2 ].position = sf::Vector2f( bottom_pos.x + tile_width - tile_border, bottom_pos.y + tile_height - tile_border );
       
bottom_tile[ 3 ].position = sf::Vector2f( bottom_pos.x + tile_border, bottom_pos.y + tile_width + tile_height - tile_border );
       
       
bottom_tile[ 0 ].texCoords = sf::Vector2f( 0, 0 );
       
bottom_tile[ 1 ].texCoords = sf::Vector2f( tile_width, 0 );
       
bottom_tile[ 2 ].texCoords = sf::Vector2f( tile_width, tile_height );
       
bottom_tile[ 3 ].texCoords = sf::Vector2f( 0, tile_height );
       
       
rtex.draw( bottom_tile, rstate );
       
       
// center tiles
       
for( float y = 0; y < tiles_rows - 1; y += 1 ) {
           
           
sf::Vector2f quad_pos( size.x * 16.0f - x * tile_width,( walls_height - 1.0f ) * 32.0f + y * tile_height + x * tile_width + tile_height / 2.0f );
           
           
sf::VertexArray tile( sf::Quads, 4 );
           
tile[ 0 ].position = sf::Vector2f( quad_pos.x + tile_border, quad_pos.y + tile_width + tile_border );
           
tile[ 1 ].position = sf::Vector2f( quad_pos.x + tile_width - tile_border, quad_pos.y + tile_border );
           
tile[ 2 ].position = sf::Vector2f( quad_pos.x + tile_width - tile_border, quad_pos.y + tile_height - tile_border );
           
tile[ 3 ].position = sf::Vector2f( quad_pos.x + tile_border, quad_pos.y + tile_width + tile_height - tile_border );
           
           
tile[ 0 ].texCoords = sf::Vector2f( 0, 0 );
           
tile[ 1 ].texCoords = sf::Vector2f( tile_width, 0 );
           
tile[ 2 ].texCoords = sf::Vector2f( tile_width, tile_height );
           
tile[ 3 ].texCoords = sf::Vector2f( 0, tile_height );
           
           
rtex.draw( tile, rstate );
       
}
       
       
// top tile
       
sf::Vector2f top_pos( size.x / 2.0f * 16.0f + x * tile_width,( walls_height - 1.0f + size.x / 4.0f ) * 32.0f + tiles_rows * tile_height - x * tile_width - tile_height / 2.0f );
       
sf::VertexArray top_tile( sf::Quads, 4 );
       
top_tile[ 0 ].position = sf::Vector2f( top_pos.x + tile_border, top_pos.y + tile_width + tile_border );
       
top_tile[ 1 ].position = sf::Vector2f( top_pos.x + tile_width - tile_border, top_pos.y + tile_border );
       
top_tile[ 2 ].position = sf::Vector2f( top_pos.x + tile_width - tile_border, top_pos.y + tile_height / 2.0f - tile_border );
       
top_tile[ 3 ].position = sf::Vector2f( top_pos.x + tile_border, top_pos.y + tile_width + tile_height / 2.0f - tile_border );
       
       
top_tile[ 0 ].texCoords = sf::Vector2f( 0, 0 );
       
top_tile[ 1 ].texCoords = sf::Vector2f( tile_width, 0 );
       
top_tile[ 2 ].texCoords = sf::Vector2f( tile_width, tile_height );
       
top_tile[ 3 ].texCoords = sf::Vector2f( 0, tile_height );
       
       
rtex.draw( top_tile, rstate );
   
}
   
   
// WOODEN BEAMS
    // main wooden beam
   
sf::VertexArray beam( sf::Quads, 4 );
   
sf::Vector2f pos;
   
   
pos.x = size.x / 2.f * 16.0f + tile_width / 2.0f;
   
pos.y =( walls_height - 1.0f + size.x / 4.0f ) * 32.0f;
   
beam[ 0 ].position = sf::Vector2f( pos.x - tile_width / 2.0f, pos.y );
   
beam[ 1 ].position = sf::Vector2f( pos.x + tile_width / 2.0f, pos.y );
   
beam[ 2 ].position = sf::Vector2f( pos.x + tile_width / 2.0f, pos.y + tiles_rows * tile_height );
   
beam[ 3 ].position = sf::Vector2f( pos.x - tile_width / 2.0f, pos.y + tiles_rows * tile_height );
   
   
for( short i = 0; i < 4; i++ )
       
 beam[ i ].color = sf::Color::Black;
   
   
rtex.draw( beam );
   
   
for( float y = 0; y < tiles_rows; y += 1 ) {
       
sf::VertexArray tile( sf::Quads, 4 );
       
tile[ 0 ].position = sf::Vector2f( size.x / 2.0f * 16.0f + tile_border,( walls_height - 1.0f + size.x / 4.0f ) * 32.0f + y * tile_height + tile_border );
       
tile[ 1 ].position = sf::Vector2f( size.x / 2.0f * 16.0f + tile_width - tile_border,( walls_height - 1.0f + size.x / 4.0f ) * 32.0f + y * tile_height + tile_border );
       
tile[ 2 ].position = sf::Vector2f( size.x / 2.0f * 16.0f + tile_width - tile_border,( walls_height - 1.0f + size.x / 4.0f ) * 32.0f + y * tile_height + tile_height - tile_border );
       
tile[ 3 ].position = sf::Vector2f( size.x / 2.0f * 16.0f + tile_border,( walls_height - 1.0f + size.x / 4.0f ) * 32.0f + y * tile_height + tile_height - tile_border );
       
       
for( short i = 0; i < 4; i++ )
           
 tile[ i ].texCoords = beam[ i ].position;
       
       
rtex.draw( tile, rstate );
   
}
   
   
// top-center quad
   
beam[ 0 ].position = sf::Vector2f( pos.x - tile_width / 2.0f, pos.y - tile_height / 4.0f );
   
beam[ 1 ].position = sf::Vector2f( pos.x + tile_width / 2.0f, pos.y - tile_height / 4.0f );
   
beam[ 2 ].position = sf::Vector2f( pos.x + tile_width / 2.0f, pos.y );
   
beam[ 3 ].position = sf::Vector2f( pos.x - tile_width / 2.0f, pos.y );
   
   
for( short i = 0; i < 4; i++ )
       
 beam[ i ].texCoords = beam[ i ].position;
   
   
rtex.draw( beam, rstate );
   
   
// top-left beam
   
beam[ 0 ].position = sf::Vector2f( pos.x - tile_width / 2.0f - tiles_columns * tile_width, pos.y - tile_height / 4.0f - size.x / 4.0f * 32.0f );
   
beam[ 1 ].position = sf::Vector2f( pos.x - tile_width / 2.0f, pos.y - tile_height / 4.0f );
   
beam[ 2 ].position = sf::Vector2f( pos.x - tile_width / 2.0f, pos.y );
   
beam[ 3 ].position = sf::Vector2f( pos.x - tile_width / 2.0f - tiles_columns * tile_width, pos.y - size.x / 4.0f * 32.0f );
   
   
for( short i = 0; i < 4; i++ )
       
 beam[ i ].texCoords = beam[ i ].position;
   
   
rtex.draw( beam, rstate );
   
   
// top-right beam
   
beam[ 0 ].position = sf::Vector2f( pos.x + tile_width / 2.0f + tiles_columns * tile_width, pos.y - tile_height / 4.0f - size.x / 4.0f * 32.0f );
   
beam[ 1 ].position = sf::Vector2f( pos.x + tile_width / 2.0f, pos.y - tile_height / 4.0f );
   
beam[ 2 ].position = sf::Vector2f( pos.x + tile_width / 2.0f, pos.y );
   
beam[ 3 ].position = sf::Vector2f( pos.x + tile_width / 2.0f + tiles_columns * tile_width, pos.y - size.x / 4.0f * 32.0f );
   
   
for( short i = 0; i < 4; i++ )
       
 beam[ i ].texCoords = beam[ i ].position;
   
   
rtex.draw( beam, rstate );
   
   
sf::Image roof_image = rtex.getTexture().copyToImage();
   
   
house_image.copy( roof_image, 0, 0, sf::IntRect( 0, 0, 0, 0 ), true );
   
   
// create main tex
   
sf::Texture * tex = new sf::Texture();
   
tex->loadFromImage( house_image );
   
   
// create the sprite
   
sprite = sf::Sprite();
   
sprite.setTexture( * tex );
   
sprite.setOrigin( tex->getSize().x / 2, tex->getSize().y );
   
sprite.setPosition( position );
   
}
P-181863
1 2 3 « 4 »
Poprzednia strona Strona 4 z 4