0
\$\begingroup\$

Game architecture is as follows:

// main.cpp

main() loads data from .csv/.json files.
main() creates an Application class instance as app and passes it data.
main() runs app.genesis();

// Application.cpp

Application::genesis() creates an sfml window
Application::genesis() creates a Menu class : public Screen as screen

// main.cpp

main() runs app.loop()

// Application.cpp

Application::loop() calls screen.step();

// Menu.cpp

Menu::step() calls draw()/render();

How do I either call Application::draw()/render() from Menu::draw()/render() or in which way should I pass screen (Menu) the window to draw to?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

How do I either call Application::draw()/render() from Menu::draw()/render() or in which way should I pass screen (Menu) the window to draw to?

If Application::draw() is a member function, you will need to give a pointer or reference to the Application to the Menu instances. This is typically done in the constructor, especially in the case of a reference:

struct Menu {
  Application& m_application; // The reference.

  Menu::Menu(Application& application)
  : m_application(application) { // Store it.
  }

  void Menu::draw() {
     m_application.draw(); // Use it.
  }
};

If you don't need the reference stored for the lifetime of Menu, you can also pass the reference to Menu::draw() instead.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .