0

BACKGROUD: So I am creating an application with ImGui and as it stands I have created several files that describe the functionality of each window and then include those cpp files headers to my app main.cpp, I then call a displaywindowXX function for each window in order to populate them simpler to the ImGui demo window. But I have grouped them into a UI namespace however if say I hit a button on the menu bar and I want it to call functions specific to window x and class a what is a good/standard design pattern for accomplishing this sort of basic communication/method calling.

Below or the 3 approaches I can see working in a crude way, let me know if these are bad good or if there are better alternatives.

// mainWindow.cpp ---------------

include "window1.h"
include "window2.h"
// etc

int main()
{
 // glfw window create code
 //create ImGui::Frame ....
  UI::showWindow1();
  UI::showWindow2();
}


// window1.cpp ------------------

namespace UI
{

void showWindow1() // just generates and populates the window1
{
  ImGui::Begin("window1");
  //do some stuff etc

  ImGui::End();
}

void createNewName(){ // do some code stuff }

//the rest of window1's methods

// windo2.cpp ------------------

namespace UI
{

void showWindow2()
{
  ImGui::Begin("window2)";
  static int csClicked = 0;
  if (ImGui::Button("newName"))
  {
      UI::createNewName();  //calling method from other windows file via namespace 
      csClicked++;
  }
}

method 2

// window1.cpp ------------

class window1 : public window
{ 
  window1()
  { 
     ImGui::Begin("window1");  //here is where I am starting to question this method
                               // will the constructor get call over and over again
      ImGui::End();
  } ;
  ~window1() {  };

};

// main.cpp --------------
include "window1.h"
include "window2.h"

int main() {
// glfw window code
// ImGui frame and dockspace code

// create window1 instance which creates the ImGui window in the constructor
 window1* win1 = new window1;
 window2* win2 = new window2;

/* now how could I pass the pointer to win2? do I need an event handler type of setup or 
   maybe I was thinking a vector list that holds the pointers of the window instances 
   and then just pass the list to each window after initialization so they each have a 
   local copy of the pointer list?     */
                             

method 3 using singletons

Create each window as a singleton and simply get the instance and call its specific functions/methods .

apple apple
  • 10,292
  • 2
  • 16
  • 36
MacLCM
  • 57
  • 9
  • 1
    Just as an aside since I didn't have enough time to read the whole thing (got a meeting): the Singleton pattern is often considered an anti-pattern due to misuse. If there isn't an inherent reason to make something a singleton, then I would avoid it, as it can cause difficulty in testing and coupling. – Rogue Aug 02 '22 at 15:26
  • I see no reason you make each window a class, afaict, in this case, it can be done with one class (and events). – apple apple Aug 02 '22 at 15:33
  • @Rogue I agree I was not wanting to go the singleton route as I have researched a little and found it is not the best approach and should be avoided if possible thanks. – MacLCM Aug 02 '22 at 17:05
  • @apple apple I had not thought about implementing into a single class, now do you mean a single instance or a single window class and multiple instances? If a single instance would that not be a huge class as it would contain the implementation code for all windows functionality. – MacLCM Aug 02 '22 at 17:07
  • @MacLCM sorry, now I re-read your code again, multiple class is no problem here. – apple apple Aug 02 '22 at 17:37
  • fwiw, function doesn't belong to window1 just because it's defined there, it's free function and I don't see why you cannot call it. it'd be easier to understand if you provide [mcve] – apple apple Aug 02 '22 at 17:44
  • @apple apple yes exactly it is free to access so basically I was wondering if doing it in this way being the namespace and free function is a bad idea or totally normal since I know to stay away from global's and this seems like the namespace is the only thing separating these functions from being global's, but I also wasn't sure how if I made them all classes what the best way to pass around references or pointers to the instances would be in order to access them as member of said classes – MacLCM Aug 02 '22 at 18:37

0 Answers0