#include #include #include #include #include #include #include #include #include #define WINDOW_HEIGHT 720 #define WINDOW_WIDTH 1080 #define RACKET_SPEED 10.0 #define BALL_SPEED 0.3 using namespace sf; enum gameState { inGame, inSettings, inMenu }; class Button : public Drawable, Transformable { public: Button(Vector2f size, Font labelFont) : m_rect(size), m_label(labelFont) {} Button(const Button& b) : m_rect(b.m_rect), m_label(b.m_label) {} void setLabel(std::string label, Color labelColor = Color::White) { m_label.setString(label); m_label.setFillColor(labelColor); } void setFillColor(Color rectColor) { m_rect.setFillColor(rectColor); } Color getFillColor() { return m_rect.getFillColor(); } void setPosition(Vector2f position) { try { m_rect.setPosition(position); const FloatRect textRect = m_label.getGlobalBounds(); Vector2f scaleFactor(0.6 * m_rect.getSize().x / textRect.size.x, 0.6 * m_rect.getSize().y / textRect.size.y); if (scaleFactor.x < 1 or scaleFactor.y < 1) m_label.setScale(scaleFactor); m_label.setPosition({ position.x + (position.x + m_rect.getSize().x - m_label.getGlobalBounds().size.x) / 2, position.y + (position.y + m_rect.getSize().y - m_label.getGlobalBounds().size.y) / 2 }); } catch(std::exception const& e) { std::cerr << "ERREUR : " << e.what() << std::endl; } } Vector2f getPosition() { return m_rect.getPosition(); } bool contains(Vector2f point) { return m_rect.getGlobalBounds().contains(point); } private: void draw(sf::RenderTarget& target, sf::RenderStates states) const override { states.transform *= getTransform(); target.draw(m_rect, states); target.draw(m_label, states); } RectangleShape m_rect; Text m_label; }; class ButtonArray : public Drawable, Transformable { public: void addButton(const Button& b) { array.push_back(b); } size_t size() { return array.size(); } Button& operator[](size_t i) { return array[i]; } const Button& operator[](size_t i) const { return array[i]; } void pop_back() { array.pop_back(); } bool empty() const { return array.empty(); } void removeAll() { array.clear(); } void setLabelToAll(std::string label, unsigned int size, Color labelColor = Color::White) { for (Button b : array) b.setLabel(label, labelColor); } void setFillColorToAll(Color rectColor) { for (Button b : array) b.setFillColor(rectColor); } private: void draw(sf::RenderTarget& target, sf::RenderStates states) const override { states.transform *= getTransform(); for (Button b : array) target.draw(b, states); } std::vector