1

I am designing an immediate gui using C++ and SDL2. As I was starting to design my framework, I noticed that basically everything needs a renderer or some sort of event or other relevant information (Fonts, Themes, etc.). Does it make sense to put these is global static object that only the gui functions have access to, or just keep passing everything in.

Would this be ok? This could be the header:

struct GuiForm {
   GuiId active_id;
   GuiId hot_id;
   std::vector<Window> windows;
   Renderer* renderer;
};

Then this could be the .cpp file:

static GuiForm* gui_form;

void SomeGuiWidget() {
   GuiId current_hot_id = gui_form.hot_id;
   
   /* Whatever the widget is going to do */
}
Stra
  • 31
  • 6
  • 1
    You're probably going to be a bit more specific. I can at least not really tell what the design idea is with this limited information. In general there are most of the times better alternatives to using global variables. If you're doing it because you're new to programming and it seems easier, you should probably look into some alternatives at least. – super Jan 10 '21 at 19:47
  • Re :`Does it make sense?` - it can, but it will make it very difficult to do testing, debugging, and support multiple windows / contexts later. You should also consider a functional-style approach, wherein you could pass a `GuiContext` object around as you build a UI, not relying on any global state, possibly with operator overloading like `std::cout`. – alter_igel Jan 10 '21 at 20:15
  • @alter igel how would you use operator overloading in this case? – Stra Jan 11 '21 at 00:20
  • Depends, are you planning to make an immediate mode GUI or a retained mode GUI? – alter_igel Jan 11 '21 at 01:51
  • @alter igel Defanitaly an immediate mode gui. – Stra Jan 11 '21 at 02:43

0 Answers0