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

Author Topic: Drawing a class object independently of creation  (Read 3098 times)

0 Members and 2 Guests are viewing this topic.

Bogdan

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Drawing a class object independently of creation
« on: March 13, 2020, 09:45:30 pm »
Hi,
I'm struggling with drawing a class object, which is to be created during runtime by hitting key A.

Creating is no problem, but how to draw it as it is not created yet. With vectors it's no problem, but how would you handle this case? As I understand it I have to forward declare the object somehow, but how?

#include <SFML/Graphics.hpp>
#include <iostream>


class UserInterface
{
private:
        sf::Texture uitexture;
        sf::Sprite uisprite;

public:
        UserInterface(std::string loadFromFileString, float x, float y)
        {
                this->uitexture.loadFromFile(loadFromFileString);
                this->uisprite.setTexture(uitexture);
                this->uisprite.setPosition(x, y);
        }

        ~UserInterface()
        {
                std::cout << "deleted" << std::endl;
        }

        void draw(sf::RenderTarget& target)
        {
                target.draw(uisprite);
        }
};


int main()
{
        sf::RenderWindow window(sf::VideoMode(800, 500), "Map", sf::Style::Close);

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        case sf::Event::KeyPressed:
                                if (event.key.code == sf::Keyboard::A)
                                {
                                        UserInterface* uio1 = new UserInterface("ui.png", 100, 100);
                                }
                                break;
                        }
                }
               
                window.clear();
                uio1.draw(window);           //   how to draw that?
                window.display();
        }
}

« Last Edit: March 14, 2020, 01:15:43 pm by Bogdan »

Fx8qkaoy

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: Drawing a class object before creation
« Reply #1 on: March 14, 2020, 12:18:22 pm »
Hi,
I'm struggling with drawing a class object, which is to be created during runtime by hitting key A.

Creating is no problem, but how to draw it as it is not created yet. With vectors it's no problem, but how would you handle this case? As I understand it I have to forward declare the object somehow, but how?

#include <SFML/Graphics.hpp>
#include <iostream>


class UserInterface
{
private:
        sf::Texture uitexture;
        sf::Sprite uisprite;

public:
        UserInterface(std::string loadFromFileString, float x, float y)
        {
                this->uitexture.loadFromFile(loadFromFileString);
                this->uisprite.setTexture(uitexture);
                this->uisprite.setPosition(x, y);
        }

        ~UserInterface()
        {
                std::cout << "deleted" << std::endl;
        }

        void draw(sf::RenderTarget& target)
        {
                target.draw(uisprite);
        }
};


int main()
{
        sf::RenderWindow window(sf::VideoMode(800, 500), "Map", sf::Style::Close);

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        case sf::Event::KeyPressed:
                                if (event.key.code == sf::Keyboard::A)
                                {
                                        UserInterface* uio1 = new UserInterface("ui.png", 100, 100);
                                }
                                break;
                        }
                }
               
                window.clear();
                uio1.draw(window);           //   how to draw that?
                window.display();
        }
}

U indeed have to forward declare it, but it has nothing to do with forward declaration concept, so what u need is just the fact that u need to declare a variable before u can use it. Logic right? U declare ur variable inside an "if" statement. When the "if" statement ends, "uio1" will be deleted (the pointer, not the underlying obj) because it goes "out of scope", which means that when u do "uio1.draw(window)", "uio1" does not exist. U should read more about C++, or do tutorials, or ask ur tutor / teacher if u have one, since this is a simple concept and the already given information shall point u in the right direction. Try to research the problem and come back with questions

Bogdan

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Drawing a class object before creation
« Reply #2 on: March 14, 2020, 01:02:21 pm »
I know that logically I have to create something before using it, but do I always have to use global containers in cases like this?

Is there some way to draw the object only if it exists? (bools don't help here) Can you point me to an example where it is shown how a forward declaration of a class object in a case like this looks like.  With variables and functions it's easy.....but....

There are no similar examples on the internet for what I like to do. My searches always lead to examples, which don't help. Certainly it's some specific syntax. If I had found it, I wouldn't have asked my question in the first place.


Fx8qkaoy

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: Drawing a class object before creation
« Reply #3 on: March 14, 2020, 02:01:24 pm »
I know that logically I have to create something before using it, but do I always have to use global containers in cases like this?

Is there some way to draw the object only if it exists? (bools don't help here) Can you point me to an example where it is shown how a forward declaration of a class object in a case like this looks like.  With variables and functions it's easy.....but....

There are no similar examples on the internet for what I like to do. My searches always lead to examples, which don't help. Certainly it's some specific syntax. If I had found it, I wouldn't have asked my question in the first place.

As I said before, "forward declaration" is a completely different concept, and most probably won't lead to desired results. Class objects are variables themselves. This is a solution to ur code (example):

Code: [Select]
UserInterface* uio1 = nullptr; // Declare the variable before using it
while(window.isOpen()) {
    // When pressing A
    uio1 = new UserInterface("ui.png", 100, 100);

    // When pressing B (u edited the post as I remember)
    delete uio1;
    uio1 = nullptr;

    // When drawing
    if(uio1) // Aka is not nullptr
        uio1->draw(window);
}

Probably the problem u faced is that when u declare it, an obj is made instantly. That's true, and to go around this I made use of pointers