Last active
October 29, 2019 23:46
-
-
Save KGrzeg/4ae107793af6f859ef412d4a1a12df12 to your computer and use it in GitHub Desktop.
C++ SFML std::Vector<sf::Drawable> Przykład
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 <SFML/Graphics.hpp> | |
| #include <vector> | |
| //własny obiekt - może być dowolny od pojedynczego kształtu, | |
| //po całę warsty, albo obiekt gry, albo gracz razem z obsługą | |
| //swojej logiki | |
| //*musi dziedziczyć po sf::Drawable | |
| class MojKsztalt : public sf::Drawable | |
| { | |
| public: | |
| MojKsztalt(float dlugosc) | |
| { | |
| myshape.setPointCount(8); | |
| myshape.setPoint(0, sf::Vector2f(0.f, 0.f)); | |
| myshape.setPoint(1, sf::Vector2f(0.f, -40.f)); | |
| myshape.setPoint(2, sf::Vector2f(30.f, -40.f)); | |
| myshape.setPoint(3, sf::Vector2f(30.f, -dlugosc)); | |
| myshape.setPoint(4, sf::Vector2f(70.f, -dlugosc)); | |
| myshape.setPoint(5, sf::Vector2f(70.f, -40.f)); | |
| myshape.setPoint(6, sf::Vector2f(100.f, -40.f)); | |
| myshape.setPoint(7, sf::Vector2f(100.f, 0.f)); | |
| myshape.setFillColor(sf::Color::Magenta); | |
| } | |
| void setPosition(float x, float y) | |
| { | |
| myshape.setPosition(x, y); | |
| } | |
| //każdy obiekt dziedziczący po sf::Drawable musi nadpisywać tę funkcję | |
| //target to w naszym przypadku sf::RenderWindow i tak go używamy | |
| void draw(sf::RenderTarget& target, sf::RenderStates states) const override | |
| { | |
| target.draw(myshape); | |
| }; | |
| private: | |
| sf::ConvexShape myshape; | |
| }; | |
| int main(int t_argc, char* t_argv[]) | |
| { | |
| //vectory przechowujące wskaźniki na obiekty drawable | |
| std::vector<sf::Drawable*> background; | |
| std::vector<sf::Drawable*> foreground; | |
| //trochę obiektów do narysowania | |
| sf::RectangleShape rect1(sf::Vector2f(100, 100)); | |
| sf::CircleShape circ(150); | |
| //od C++11 można użyć auto jako typ, gdy kompilator może się domyślić typu | |
| //tutaj typ to sf::RectangleShape, bo kopiujemy obiekty | |
| auto rect2 = rect1; | |
| auto rect3 = rect1; | |
| auto rect4 = rect1; | |
| auto rect5 = rect1; | |
| auto rect6 = rect1; | |
| rect1.setFillColor(sf::Color::Blue); | |
| rect2.setFillColor(sf::Color::Green); | |
| rect3.setFillColor(sf::Color::White); | |
| rect4.setFillColor(sf::Color::Yellow); | |
| rect5.setFillColor(sf::Color::Red); | |
| circ.setFillColor(sf::Color::Cyan); | |
| rect1.setPosition(0.f, 0.f); | |
| rect2.setPosition(25.f, 50.f); | |
| rect3.setPosition(150.f, 50.f); | |
| rect4.setPosition(200.f, 150.f); | |
| rect5.setPosition(200.f, 300.f); | |
| circ.setPosition(225.f, 75.f); | |
| //kolejność dodawania określa kolejność rysowania | |
| background.push_back(&rect1); | |
| background.push_back(&rect2); | |
| background.push_back(&rect3); | |
| background.push_back(&rect4); | |
| background.push_back(&circ); //<-- koło pomiędzy czerwonym a żółtym kwadratem | |
| background.push_back(&rect5); | |
| sf::CircleShape circ1(45); | |
| auto circ2 = circ1; | |
| auto circ3 = circ1; | |
| auto circ4 = circ1; | |
| auto circ5 = circ1; | |
| MojKsztalt qtax1(100); | |
| MojKsztalt qtax2(150); | |
| circ1.setFillColor(sf::Color::White); | |
| circ2.setFillColor(sf::Color::Green); | |
| circ3.setFillColor(sf::Color::Yellow); | |
| circ4.setFillColor(sf::Color::Blue); | |
| circ5.setFillColor(sf::Color::Red); | |
| circ1.setPosition(0.f, 0.f); | |
| circ2.setPosition(150.f, 100.f); | |
| circ3.setPosition(100.f, 125.f); | |
| circ4.setPosition(300.f, 50.f); | |
| circ5.setPosition(100.f, 300.f); | |
| qtax1.setPosition(30.f, 400.f); | |
| qtax2.setPosition(140.f, 400.f); | |
| foreground.push_back(&circ1); | |
| foreground.push_back(&circ2); | |
| foreground.push_back(&circ3); | |
| foreground.push_back(&circ4); | |
| foreground.push_back(&circ5); | |
| foreground.push_back(&qtax1); | |
| foreground.push_back(&qtax2); | |
| //utwórz okno | |
| sf::RenderWindow window(sf::VideoMode(400, 400), "Milo"); | |
| while (window.isOpen()) | |
| { | |
| sf::Event event; | |
| while (window.pollEvent(event)) | |
| { | |
| if (event.type == sf::Event::Closed) | |
| window.close(); | |
| } | |
| window.clear(); | |
| //kolejność rysowania w ramach jednej warstwy zależy od kolejnośći | |
| //dodawania obiektów | |
| for (auto value : background) | |
| { | |
| //value to wskaźnik | |
| //*value to obiekt | |
| window.draw(*value); | |
| } | |
| //warstwa foreground zawsze będzie na wierzchu warstwy background | |
| for (auto value : foreground) | |
| { | |
| window.draw(*value); | |
| } | |
| window.display(); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment