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

Author Topic: sf::RenderWindow reference main class availble gobally in second Class  (Read 1605 times)

0 Members and 1 Guest are viewing this topic.

utan

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Hello there all,

 I am very new to CPP, but I have a question I couldn't get it settle searching..

private:
class Game {
private:
    sf::RenderWindow window;
public:

    Game() {
        this->window.create(sf::VideoMode(600, 500), this->name, sf::Style::Close);

        this->player(this->window);// instantiated window

    }
    Player player;
};

class Player {
private:
    sf::RenderWindow w;
public:
    Player(sf::RenderWindow& window)
        : w(window)
    {

        this->w; // window reference from Game class Available globally in Player Class this->w

    }
};

int main() {
        Game game;
        return 0;
}
 

I don't want to be passing reference of window on every class method in Player class and I don't want to pass it in main.cpp and pass it as reference from a normal object to both classes because I know works using the list initiator in the constructors.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: sf::RenderWindow reference main class availble gobally in second Class
« Reply #1 on: January 25, 2021, 08:08:04 am »
Reverse the control so instead of the player operating on the render window, have your game state (or similar) use the player in combination with the window.
So instead of player.draw(window) you'd call window.draw(player), which can be achieved by deriving from sf::Drawable
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

utan

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: sf::RenderWindow reference main class availble gobally in second Class
« Reply #2 on: January 25, 2021, 09:57:30 am »
Thanks a lot,

 But is not that I want to draw the player, instead I want to have some public functions in the Player class that are dependent of the intance the window and I dont want to pass it as parameter refence to all clases that I want to create..

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: sf::RenderWindow reference main class availble gobally in second Class
« Reply #3 on: January 25, 2021, 10:38:05 am »
What do those functions depend on of the window?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

utan

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: sf::RenderWindow reference main class availble gobally in second Class
« Reply #4 on: January 25, 2021, 06:14:26 pm »
Hi,
 To get the size of the window mainly , i dont like passing reference and having a global reference of sf::window come handy i guess. I am still learning SFML.


utan

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: sf::RenderWindow reference main class availble gobally in second Class
« Reply #5 on: January 25, 2021, 06:16:23 pm »
If no doable the way i want then i will do it passing reference or passing any parameter taken from de SF::WINDOW
 Apreciate ur help