Created
February 1, 2026 04:51
-
-
Save maxgoren/11f6374e0be5d966f85689c7ee82f12e to your computer and use it in GitHub Desktop.
sfml skeleton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <vector> | |
| #include <queue> | |
| #include <SFML/Graphics.hpp> | |
| using namespace std; | |
| /* | |
| SFML App Skeleton | |
| compile with | |
| g++ app.cpp -o app -lsfml-window -lsfml-graphics -lsfml-system | |
| */ | |
| enum Direction { | |
| North, South, East, West | |
| }; | |
| class Entity { | |
| private: | |
| int x; | |
| int y; | |
| sf::Color color; | |
| Direction heading; | |
| public: | |
| Entity(int xp = 5, int yp = 5, sf::Color c = sf::Color::Green) : x(xp), y(yp), color(c) { } | |
| pair<int,int> coords() { | |
| return make_pair(x, y); | |
| } | |
| Direction facing() { | |
| return heading; | |
| } | |
| void move(int dx, int dy) { | |
| x += dx; | |
| y += dy; | |
| if (dx != 0) { | |
| if (dx == 1) heading = East; | |
| else if (dx == -1) heading = West; | |
| } | |
| if (dy != 0) { | |
| if (dy == 1) heading = South; | |
| else if (dy == -1) heading = North; | |
| } | |
| } | |
| void render(sf::RenderTexture* texture) { | |
| sf::RectangleShape rect; | |
| rect.setSize(sf::Vector2f(20, 20)); | |
| rect.setFillColor(color); | |
| rect.setPosition(x*10, y*10); | |
| texture->draw(rect); | |
| texture->display(); | |
| } | |
| }; | |
| class Point { | |
| private: | |
| sf::Color color; | |
| int x; | |
| int y; | |
| int cost; | |
| public: | |
| Point(int X, int Y, sf::Color c) : x(X), y(Y), color(c) { } | |
| Point() { | |
| x = 0; | |
| y = 0; | |
| color = sf::Color::Red; | |
| } | |
| Point& setX(int X) { | |
| x = X; | |
| return *this; | |
| } | |
| Point& setY(int Y) { | |
| y = Y; | |
| return *this; | |
| } | |
| Point& setColor(sf::Color col) { | |
| color = col; | |
| return *this; | |
| } | |
| Point& setCost(int cst) { | |
| cost = cst; | |
| return *this; | |
| } | |
| pair<int,int> getCoords() { | |
| return make_pair(x,y); | |
| } | |
| int getX() { | |
| return x; | |
| } | |
| int getY() { | |
| return y; | |
| } | |
| int getCost() { | |
| return cost; | |
| } | |
| sf::Color getColor() { | |
| return color; | |
| } | |
| void render(sf::RenderTexture* texture) { | |
| sf::RectangleShape rect; | |
| rect.setSize(sf::Vector2f(20, 20)); | |
| rect.setFillColor(color); | |
| rect.setPosition(x*10, y*10); | |
| texture->draw(rect); | |
| texture->display(); | |
| } | |
| }; | |
| class App { | |
| private: | |
| int width; | |
| int height; | |
| sf::RenderWindow* window; | |
| sf::RenderTexture* texture; | |
| Entity ent; | |
| vector<Point> spots; | |
| void populate() { | |
| sf::Color colors[] = { sf::Color::Red, sf::Color::Blue, sf::Color::White, sf::Color::Yellow, sf::Color::Magenta}; | |
| int xs[] = {3, 11, 6, 4, 5, 8, 1, 7, 9, 14, 10, 16, 15, 13, 3, 12}; | |
| int ys[] = {9, 1, 8, 3, 15, 11, 6, 4, 7, 5, 13, 14, 2, 16, 12, 10}; | |
| for (int i = 0; i < 15; i++) { | |
| spots.push_back(Point(xs[i]*5, ys[i]*2, colors[i % 5])); | |
| } | |
| } | |
| bool initWindow() { | |
| sf::Event event; | |
| window = new sf::RenderWindow(sf::VideoMode(width, height), "Dipset"); | |
| texture = new sf::RenderTexture(); | |
| texture->create(width, height); | |
| window->setFramerateLimit(60); | |
| return window->isOpen(); | |
| } | |
| void eventLoop() { | |
| sf::Event event; | |
| populate(); | |
| while (window->isOpen()) { | |
| if (window->pollEvent(event)) { | |
| handleEvent(event); | |
| } | |
| render(); | |
| } | |
| } | |
| void handleEvent(sf::Event& event) { | |
| if (event.type == sf::Event::Closed) { | |
| window->close(); | |
| return; | |
| } | |
| if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Right)) { | |
| ent.move(1, 0); | |
| } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Left)) { | |
| ent.move(-1, 0); | |
| } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Up)) { | |
| ent.move(0, -1); | |
| } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Down)) { | |
| ent.move(0, 1); | |
| } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Q)) { | |
| window->close(); | |
| } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Escape)) { | |
| window->close(); | |
| } | |
| } | |
| void render() { | |
| window->clear(); | |
| texture->clear(sf::Color::Black); | |
| ent.render(texture); | |
| for (Point & e : spots) { | |
| e.render(texture); | |
| } | |
| sf::Sprite sp(texture->getTexture()); | |
| window->draw(sp); | |
| window->display(); | |
| } | |
| public: | |
| App(int maxw = 1000, int maxh = 500) { | |
| width = maxw; | |
| height = maxh; | |
| } | |
| void start() { | |
| if (initWindow()) { | |
| eventLoop(); | |
| } else { | |
| cout<<"An error occured during initialization."<<endl; | |
| } | |
| } | |
| }; | |
| int main() { | |
| srand(time(NULL)); | |
| App app; | |
| app.start(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment