How can I make multiple shapes using a class that has functions?
Here is my code for the class:
class baseAttributes
{
public:
//baseAttributes();
//~baseAttributes();
int getRadius;
sf::CircleShape baseOutline(int x, int y, int r)
{
sf::CircleShape baseShape(r, 100);
baseShape.setFillColor(sf::Color(0, 0, 255, 15));
baseShape.setOutlineThickness(2);
baseShape.setOutlineColor(sf::Color(28, 87, 174));
baseShape.setRadius(r);
baseShape.setPosition(x - r, y - r);
getRadius = baseShape.getRadius();
return baseShape;
}
sf::CircleShape baseShape(int x, int y, int r)
{
sf::CircleShape base;
base.setFillColor(sf::Color(0, 200, 0));
base.setRadius(r);
base.setPosition(x - r, y - r);
return base;
}
};
and my main
int main()
{
sf::ContextSettings settings;
int width = 1280;
int height = 720;
settings.antialiasingLevel = 8;
sf::RenderWindow window(sf::VideoMode(width, height), "SFML works!", sf::Style::Default, settings);
//window.setMouseCursorVisible(false);
sf::View view;
view.reset(sf::FloatRect(0, 0, width, height));
view.setCenter(0, 0);
view.setViewport(sf::FloatRect(0.f, 0.f, 1.f, 1.f));
window.setView(view);
int mouseX;
int mouseY;
baseAttributes p;
sf::CircleShape player(40, 6);
player.setFillColor(sf::Color::Magenta);
//Loads font
sf::Font OCRAStd;
if(!OCRAStd.loadFromFile("OCRAStd.otf"));
{
std::cout << "Font loaded" << std::endl;
}
sf::Text font;
font.setFont(OCRAStd);
while (window.isOpen())
{
sf::Event event;
//window.setFramerateLimit(120);
while (window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
window.close();
break;
//case sf::Keyboard::Escape:
// window.close();
// break;
}
}
//if(event.type == sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
//{
// window.close();
//}
if(event.type == sf::Event::MouseMoved)
{
mouseX = event.mouseMove.x;
mouseY = event.mouseMove.y;
}
player.setPosition(mouseX - width/2, mouseY - height/2);
//int px(0), py(0);
window.clear(sf::Color::Black);
window.draw(p.baseOutline(0, 0, 200));
//Using Pythagorean theorem to display a shape when mouse is in circle
if(pow(player.getPosition().x, 2) + pow(player.getPosition().y, 2) <= pow(p.getRadius, 2))
{
window.draw(player);
}
window.display();
}
return 0;
}
I don't know "baseAttributes p" can have multiple instances, and I don't know what to do to make multiple shapes using that class
If you were thinking something like this:
if (event.type == sf::Event::MouseButtonPressed)
{
{
if (event.mouseButton.button == sf::Mouse::Left)
{
baseAttributes *baseAttributes1 = new baseAttributes();
window.draw(baseAttributes().baseOutline(mouseX, mouseY, 200));
}
}
}
The IDE gave me an error while compiling it
baseAttributes *baseAttributes1 = new baseAttributes();
window.draw(baseAttributes().baseOutline(mouseX, mouseY, 200));
You need to get off the Java train. If there is no good reason to use new, then don't use it. Unlike Java, C++ can allocate objects without the use of new. And also unlike Java, in C++ pointer types (and reference types) are distinct from value types. You can't invoke a method on a pointer with the normal . like you can in Java. You need to dereference the pointer to get a value to invoke the method on. It would look something like (*baseAttributes1).baseOutline(...) or simpler baseAttributes1->baseOutline(...).
Also, what you are doing with baseAttributes().baseOutline(...) is illegal. You are trying to invoke a method on an rvalue or temporary object. This won't work because unless the rvalue is bound to a named variable it technically isn't addressable and hence you can't call any of it's methods. Another difference between Java and C++ is that you can omit an empty constructor parameter list when constructing objects: baseAttributes* baseAttributes1 = new baseAttributes;
Like Ixrec already said, before trying your luck with SFML, you need to get a grasp of fundamental concepts in C++. If you don't understand what you did wrong here, then SFML will be a nightmare for you and you really shouldn't try to use it any further until you are more capable in C++.