I think I should write implementation for my Circle class, but I'm not sure and I don't know, how to transfer Color as function parameter in main bcz compiler doesn't work with sf::Color::Red
or just Red
as function parameter in main function
#include <SFML/Graphics.hpp>
using namespace sf;
const int APPLICATION_WIDTH = 400;
const int APPLICATION_HEIGHT = 300;
class Figure : public sf::Shape{
protected:
double m_x0 = 0, m_y0 = 0;
float m_angle = 0;
double m_scale;
public:
virtual ~Figure() {}
void setCoordX(double x0) { m_x0 = x0; }
void setCoordY(double y0) { m_y0 = y0; }
void setAngle(float angle) { m_angle = angle; }
void setScale(double scale) { m_scale = scale; }
double getCoordX() { return m_x0; }
double getCoordY() { return m_y0; }
float getAngle() { return m_angle; }
double getScale() { return m_scale; }
virtual void drawFigure(sf::RenderWindow& w) = 0;
//virtual void moveFigure(sf::RenderWindow& w, const double vx, const double vy) = 0;
void hideFigure(sf::RenderWindow& w);
virtual void rotateFigure(sf::RenderWindow& w) = 0;
//virtual void scaleFigure(const double vx, const double vy) = 0;
};
void Figure::rotateFigure(sf::RenderWindow& w) {
sf::Shape::rotate(m_angle);
}
void Figure::hideFigure(sf::RenderWindow& w) {
sf::Shape::setFillColor(sf::Color::Transparent);
}
class Circle : public Figure {
private:
CircleShape m_obj;
//double m_x1 , m_y1;
double m_radius = 1;
Vector2f getPoint(std::size_t index) const override {
return m_obj.getPoint(index);
}
std::size_t getPointCount() const override {
return m_obj.getPointCount();
}
public:
//void setCoordX1(double x1) { m_x1 = x1; }
void setRad(double r) { m_radius = r; }
double getRad() { return m_obj.getRadius(); }
double getCenterX() { return m_obj.getRadius(); }
double getCenterY() { return m_obj.getRadius(); }
void drawFigure(sf::RenderWindow& w);
//void moveFigure(sf::RenderWindow& w, const double vx, const double vy);
//void hideFigure(sf::RenderWindow& w) override;
void rotateFigure(sf::RenderWindow& w) override;
};
void Circle::drawFigure(sf::RenderWindow& w) {
m_obj.setRadius(m_radius);
m_obj.setPosition(m_x0, m_y0);
w.draw(m_obj);
}
//void Circle::hideFigure(sf::RenderWindow &w) {
// m_obj.setFillColor(sf::Color::Transparent);
//}
void Circle::rotateFigure(sf::RenderWindow& w) {
//m_obj.setFillColor(sf::Color::Magenta); // if I'll paint it here, it works
m_obj.rotate(m_angle);
}
int main()
{
RenderWindow window(VideoMode(APPLICATION_WIDTH, APPLICATION_HEIGHT), "Lab 6 using SFML");
Circle a, b, c, d;
a.setFillColor(sf::Color::Red);
b.setFillColor(sf::Color::Green);
//c.setFillColor(sf::Color::Blue);
//d.setFillColor(sf::Color::Magenta);
a.setRad(32);
a.setCoordX(50); a.setCoordY(34);
b.setRad(16);
b.setCoordX(10); b.setCoordY(34);
b.setAngle(45);
b.rotateFigure(window);
//b.hideFigure(window);
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
window.close();
}
//sf::CircleShape circle;
//circle.setRadius(50);
//circle.setFillColor(sf::Color::Green);
//circle.setPosition((APPLICATION_WIDTH - (circle.getRadius()*2))/2.0,
// (APPLICATION_HEIGHT - (circle.getRadius()*2))/2.0);
//circle.move(30, 30);
//window.draw(circle);
a.drawFigure(window);
b.drawFigure(window);
window.display();
}
return 0;
}
sf::Color is a class that can be passed as a parameter. You can read how sf::Color works in the documentation: https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Color.php
Example:
#include <SFML/Graphics.hpp>
class MyCircle
{
public:
MyCircle()
{
mCircle.setRadius(50.f);
}
void setColor(sf::Color color)
{
mCircle.setFillColor(color);
}
void draw(sf::RenderWindow& window)
{
window.draw(mCircle);
}
private:
sf::CircleShape mCircle;
};
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
MyCircle circle;
circle.setColor(sf::Color::Red);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
circle.draw(window);
window.display();
}
return 0;
}