Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Having a hard time drawing a private sf::RectangleShape?  (Read 1660 times)

0 Members and 1 Guest are viewing this topic.

AndreeU17

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Having a hard time drawing a private sf::RectangleShape?
« on: February 14, 2015, 11:21:13 pm »
I'm trying to recreate another pong game. Why you might ask, well because as i learn C++ i'm learning better design, etc. I don't practice C++ regularly but more like once every 2 weeks because of school and work! So i would like some help and perhaps no hard comments. I've managed to make it work before but can't seem to remember! So this is what i'm doing:

1) created a class named Entities that contains a constructor with specific parameters properties for sf::RectangleShape, sf::Circle, etc.
2) I then created 4 variables of data type sf::RectangleShape, sf::Circle, etc. as private member variables.
3) i created 4 getFunctions that will return the variables.

Entities.h
#include <SFML/Graphics.hpp>
#include "ResourcePath.hpp"

class Entities
{
public:
    sf::RectangleShape rect;
    sf::CircleShape circle;
    sf::Text text;
    sf::Font font;
   
public:
    Entities();
    Entities(sf::Vector2f size, sf::Vector2f position, sf::Color color);
    Entities(int radius, sf::Vector2f position, sf::Color color);
    Entities(std::string message, int charSize, sf::Vector2f position, sf::Font font, sf::Color color);
   
    sf::RectangleShape getRectangle();
    sf::CircleShape getCircle();
    sf::Text getText();
    sf::Font getFont();
   
   
};
#endif /* defined(__PongClone__entities__) */
 

Entities.cpp

#include "entities.h"

Entities::Entities()
{
    font.loadFromFile(resourcePath() + "");
}

Entities::Entities(sf::Vector2f size, sf::Vector2f position, sf::Color color)
{
    rect.setSize(size);
    rect.setPosition(position);
    rect.setFillColor(color);
}
Entities::Entities(int radius, sf::Vector2f position, sf::Color color)
{
    circle.setRadius(radius);
    circle.setPosition(position);
    circle.setFillColor(color);
}
Entities::Entities(std::string message, int charSize, sf::Vector2f position, sf::Font font, sf::Color color)
{
    text.setString(message);
    text.setCharacterSize(charSize);
    text.setPosition(position);
    text.setFont(font);
    text.setColor(color);
}

 sf::RectangleShape Entities::getRectangle()
{
    return rect;
}
sf::CircleShape Entities::getCircle()
{
    return circle;
}
sf::Text Entities::getText()
{
    return text;
}

sf::Font Entities::getFont()
{
    return font;
}
 

Now i have another class which will create specific objects. For example, i created a leftpaddle object like this:
Entities leftPaddle(Entities(sf::Vector2f(100,100), sf::Vector2f(200,200), sf::Color::White));

I've got all that working but when i try to draw the object i get and error like this:
"invalid use of non-static data member 'rect'"

ive tried
window->draw(getRectangle().leftPaddle);

but none of it works, also sorry for the wrong programming lingo!

Thanks for anyone willing to help and for your time spent!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Having a hard time drawing a private sf::RectangleShape?
« Reply #1 on: February 15, 2015, 01:00:07 am »
You might want to go over the official tutorials, they contain some valuable information.
Additionally you might want to look up sf::Drawable in the documentation, since that would probably make things a lot easier.

I've got all that working but when i try to draw the object i get and error like this:
"invalid use of non-static data member 'rect'"

ive tried
window->draw(getRectangle().leftPaddle);

but none of it works, also sorry for the wrong programming lingo!
You need to provide the actual code that generates the error and no some version of the code and some collection of errors you received.
At best for yourself and us, you create a minimal and compilable example. That way you narrow down the problem and might find it yourself and if you don't we get a very small code base that we can compile and test. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/