Skip to content

Instantly share code, notes, and snippets.

@MarioLiebisch
Created December 22, 2014 16:09
Show Gist options
  • Save MarioLiebisch/97e7cf1457a82f5c1311 to your computer and use it in GitHub Desktop.
Save MarioLiebisch/97e7cf1457a82f5c1311 to your computer and use it in GitHub Desktop.
SFML Wall Drawing Example
#include <SFML/Graphics.hpp>
#include <vector>
struct wall {
float x1, y1, x2, y2;
wall(float x1, float y1, float x2, float y2) : x1(x1), y1(y1), x2(x2), y2(y2) {}
};
int main(int argc, char **argv) {
sf::RenderWindow window(sf::VideoMode(320, 240), "Outlines Test");
// Just defining some walls
// You'd read this from a file
std::vector<wall> walldata;
// Let's just add four walls around our room
walldata.push_back(wall(10, 10, 210, 10));
walldata.push_back(wall(210, 10, 210, 110));
walldata.push_back(wall(10, 110, 210, 110));
walldata.push_back(wall(10, 10, 10, 110));
// Some wall pointing inwards
walldata.push_back(wall(60, 10, 60, 60));
sf::Color outlinecolor(sf::Color::Black);
sf::Color wallcolor(sf::Color::White);
sf::VertexArray walls(sf::Quads);
// Let's first add the outlines, since those have to be drawn first
for (unsigned int i = 0; i < walldata.size(); ++i) {
walls.append(sf::Vertex(sf::Vector2f(walldata[i].x1 - 4, walldata[i].y1 - 4), outlinecolor)); // top left corner
walls.append(sf::Vertex(sf::Vector2f(walldata[i].x2 + 4, walldata[i].y1 - 4), outlinecolor)); // top right corner
walls.append(sf::Vertex(sf::Vector2f(walldata[i].x2 + 4, walldata[i].y2 + 4), outlinecolor)); // bottom right corner
walls.append(sf::Vertex(sf::Vector2f(walldata[i].x1 - 4, walldata[i].y2 + 4), outlinecolor)); // bottom left corner
}
// And now the walls again, this time with their actual color
for (unsigned int i = 0; i < walldata.size(); ++i) {
walls.append(sf::Vertex(sf::Vector2f(walldata[i].x1 - 2, walldata[i].y1 - 2), wallcolor)); // top left corner
walls.append(sf::Vertex(sf::Vector2f(walldata[i].x2 + 2, walldata[i].y1 - 2), wallcolor)); // top right corner
walls.append(sf::Vertex(sf::Vector2f(walldata[i].x2 + 2, walldata[i].y2 + 2), wallcolor)); // bottom right corner
walls.append(sf::Vertex(sf::Vector2f(walldata[i].x1 - 2, walldata[i].y2 + 2), wallcolor)); // bottom left corner
}
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color(0, 64, 128));
window.draw(walls);
window.display();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment