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

Author Topic: Multiple instances of a class  (Read 5449 times)

0 Members and 2 Guests are viewing this topic.

Red RevYol

  • Newbie
  • *
  • Posts: 6
    • View Profile
Multiple instances of a class
« on: October 18, 2013, 11:25:25 pm »
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

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Multiple instances of a class
« Reply #1 on: October 18, 2013, 11:28:31 pm »
You declare multiple instances of it?  The same way you already declared one instance?

It sounds like you're asking questions about basic C++ OOP constructs, so you should probably go read a basic C++ book or tutorial.  Maybe this one: http://www.cplusplus.com/doc/tutorial/classes/

Red RevYol

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Multiple instances of a class
« Reply #2 on: October 19, 2013, 12:02:39 am »
I meant I want to have multiple instances that is based off of user input. As an example, if I want to have a user press the mouse button, one instance is added to that class and it shows on the screen where the mouse pointer is at.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Multiple instances of a class
« Reply #3 on: October 19, 2013, 12:24:12 am »
So create an instance when the user presses the mouse button and set its position to the mouse position?

I'm not sure what's stopping you from just writing the code to do exactly that.  The SFML tutorials cover everything you need to know about mouse input to do this.

Red RevYol

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Multiple instances of a class
« Reply #4 on: October 19, 2013, 01:43:01 am »
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

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Multiple instances of a class
« Reply #5 on: October 19, 2013, 01:59:54 am »
So read the error.  It probably tells you exactly why it won't compile.

Also, by putting it inside the event handler you're creating temporary objects whose memory never gets freed and only get drawn exactly once while the button is held down.  There's no way that's the behavior you want.

It looks like you need to back away from libraries like SFML and just study C++ by itself for a while.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Multiple instances of a class
« Reply #6 on: October 19, 2013, 02:32:14 am »
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++.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Multiple instances of a class
« Reply #7 on: October 19, 2013, 10:15:13 am »
I totally agree with what binary1248 said about new and delete. In modern C++, there is hardly ever a reason to use manual memory management.

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.
That's not true; rvalue objects can be modified, but it's not allowed to store a pointer or (non-const lvalue) reference to them. Furthermore, C++ distinguishes between scalar rvalues (such as int) which can indeed not be modified, and class-type rvalues.

C++11 move semantics rely heavily on the modification of rvalue objects, in order to transfer resources from one object to another. Rvalue references are not directly related to move itself, they merely represent an indirection that binds rvalues, but not lvalues.
« Last Edit: October 19, 2013, 10:20:12 am by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Red RevYol

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Multiple instances of a class
« Reply #8 on: October 19, 2013, 10:36:36 am »
To get this straight, I barely know Java. I am using SFML to get out of using a console based program to learn C++ which gets boring after a while. I am also new to programming. Looks like I will need to look at rvalue, lvalue, and dynamic memory.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Multiple instances of a class
« Reply #9 on: October 19, 2013, 11:06:44 am »
I referred to the syntax style as being Java-like, not your past experience in any programming language. Java made some really horrible decisions when it came to these issues and I can't help but be reminded of Java when I see code written in conformance with their language specification.

I am using SFML to get out of using a console based program to learn C++ which gets boring after a while. I am also new to programming.
This really isn't a good idea, considering you still don't know how to use pointers. Working with the console can become monotonous after a while if you are used to seeing things other than text on your screen, but it is still the fastest way to test and learn about language concepts. You need to be able to rule out errors that you could have made when something doesn't work out as expected, and it is easier to do this when using the console instead of SFML. If you use SFML to do things, you will always have to double check whether the mistake you made was in your non-SFML-specific code or the SFML-specific code, so in that sense, it is less efficient to learn basic C++ using SFML.

You don't have to directly start with the difference between lvalues and rvalues and how that matters for you, but some knowledge on dynamic memory is good to have. The basics that you would start to learn about regardless of programming language you will have to learn for C++ as well. You should start with those language-independent concepts first and move on to the C++-specific concepts after that.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Red RevYol

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Multiple instances of a class
« Reply #10 on: October 20, 2013, 12:17:05 am »
I'll take your word for that, but visuals helps me learn and it helps me understand the concept more clear.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Multiple instances of a class
« Reply #11 on: October 20, 2013, 12:20:13 am »
Text in a terminal is still visual.  It's also far, FAR simpler than real 2D/3D graphics.  Testing and debugging real graphics programs is orders of magnitude more complicated than anything text based, which makes it terrible for learning the basics of a language.  If you want graphics that badly at your level, the only sane option would be a curses library.

Red RevYol

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Multiple instances of a class
« Reply #12 on: October 20, 2013, 06:50:16 am »
Everyone learns differently, and I tend to be on the visual side of that aspect - using pictures to help explain. Learning by using text is not visual, but it sometimes does.

I also did not say I wanted to do graphics so early at my level. I always want to do video game design with computer science in any way possible. I've also been studying C++ for over a year or so; school was slowing me down for a while, and now I am in college coming back to learning again.

Well, anyway, you can close this topic. It seems like this conversation is not going anywhere.

 

anything