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 :-)

Kopiuj
void loadTexture(std::ifstream& file) {
cout << size.x << " " << size.y << "\n";
short walls_height = 3;
float tiles_rows = 16;
float tiles_columns = 16;
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 color_inside = sf::Color(128, 24, 24);
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);
sf::Image wall;
wall.create(32, 32, sf::Color::Transparent);
wall = getTexture("walls/wooden_wall")->texture->copyToImage();
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);
}
}
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);
}
}
}
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);
sf::RenderStates rstate(getTexture("tiles/tile_5_highlands")->texture);
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);
}
}
for (float x = 1; x < tiles_columns; x += 2) {
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);
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);
}
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);
}
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);
}
}
for (float x = 1; x < tiles_columns; x += 2) {
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);
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);
}
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);
}
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);
}
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);
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);
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);
sf::Texture* tex = new sf::Texture();
tex->loadFromImage(house_image);
sprite = sf::Sprite();
sprite.setTexture(*tex);
sprite.setOrigin(tex->getSize().x / 2, tex->getSize().y);
sprite.setPosition(position);
}
0.5f
do którejś współrzędnej w osi Y. Bez względu na to co jest problemem, tak czy siak trzeba go naprawić, aby ten dach ładnie wyglądał, tak czysto piksel artowo. ;)