Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <SFML/System.hpp>
- #include <vector>
- #include <cmath>
- using namespace sf;
- int main()
- {
- RenderWindow window{{1024, 768}, ""};
- View view{{0, 0, 1024, 768}};
- std::vector<RectangleShape> rects;
- while (window.isOpen())
- {
- Event event;
- while (window.pollEvent(event))
- {
- switch (event.type)
- {
- case Event::Closed:
- window.close();
- break;
- case Event::Resized:
- {
- float width = static_cast<float>(event.size.width);
- float height = static_cast<float>(event.size.height);
- view.reset({0.0f, 0.0f, width, height});
- break;
- }
- default:
- break;
- }
- }
- Vector2f offset;
- if (Keyboard::isKeyPressed(Keyboard::Left))
- {
- offset.x += -2.0f;
- }
- if (Keyboard::isKeyPressed(Keyboard::Right))
- {
- offset.x += 2.0f;
- }
- if (Keyboard::isKeyPressed(Keyboard::Down))
- {
- offset.y += 2.0f;
- }
- if (Keyboard::isKeyPressed(Keyboard::Up))
- {
- offset.y += -2.0f;
- }
- view.move(offset);
- if (Mouse::isButtonPressed(Mouse::Left))
- {
- auto pixelPos = Mouse::getPosition(window);
- auto coordPos = window.mapPixelToCoords(pixelPos);
- int x = static_cast<int>(std::trunc(coordPos.x));
- int y = static_cast<int>(std::trunc(coordPos.y));
- x -= x % 48;
- y -= y % 48;
- coordPos.x = static_cast<float>(x);
- coordPos.y = static_cast<float>(y);
- RectangleShape rect{{48.0f, 48.0f}};
- rect.setPosition(coordPos);
- rects.push_back(std::move(rect));
- }
- window.clear();
- window.setView(view);
- for (const auto &it: rects) window.draw(it);
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement