Original Post
Hi, I am mostly a novice to game development. I think my question will be best explained through an example:
So I want to use OpenGL to create graphics for my game. There is a fair amount of "initialization" code to get everything set up (shaders, vertex array objects, program objects, etc..). There is also a fair amount of code involved in "display" code, which is the actual sending of draw commands to OpenGL. And then there is the "changing of game state" that my graphics code needs to be aware of, so it knows what to render. This includes changing the positions/orientations of objects in the world, creating/destroying objects. There could also be other more technical, less game-centric "changes in state" like selecting a different shader, or notifying the graphics code that the window has been resized.
Now, it seems unreasonable to keep all this code in the same place as my main loop. The solution I am thinking of is to create a Singleton "Graphics" class that manages whatever state it needs, and deals with OpenGL-related code. It could have methods like initialize(...) and display(...) and whatever methods the main loop needs to change the state of the graphics code.
I have read that Singleton classes are like global variables, and that they should not be used unless you really need to. But with the Graphics singleton, communication seems relatively one-way (from the main loop to the graphics module), which might be relevant to this design choice. I don't see a better alternative. It would be very cumbersome for the main loop to manage the graphics state and instead rely on functions not of any class.
I imagine dealing with other "modules" will be similar to this.
So I want to use OpenGL to create graphics for my game. There is a fair amount of "initialization" code to get everything set up (shaders, vertex array objects, program objects, etc..). There is also a fair amount of code involved in "display" code, which is the actual sending of draw commands to OpenGL. And then there is the "changing of game state" that my graphics code needs to be aware of, so it knows what to render. This includes changing the positions/orientations of objects in the world, creating/destroying objects. There could also be other more technical, less game-centric "changes in state" like selecting a different shader, or notifying the graphics code that the window has been resized.
Now, it seems unreasonable to keep all this code in the same place as my main loop. The solution I am thinking of is to create a Singleton "Graphics" class that manages whatever state it needs, and deals with OpenGL-related code. It could have methods like initialize(...) and display(...) and whatever methods the main loop needs to change the state of the graphics code.
I have read that Singleton classes are like global variables, and that they should not be used unless you really need to. But with the Graphics singleton, communication seems relatively one-way (from the main loop to the graphics module), which might be relevant to this design choice. I don't see a better alternative. It would be very cumbersome for the main loop to manage the graphics state and instead rely on functions not of any class.
I imagine dealing with other "modules" will be similar to this.