How do I organize the "modules" of my code?
Members - Reputation: 126
Posted 24 February 2012 - 03:06 PM
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.
Members - Reputation: 1008
Posted 24 February 2012 - 03:14 PM
Then have a classes that use that class to render to the screen, like a mesh renderer, terrain renderer, gui renderer, text renderer...
Members - Reputation: 425
Posted 24 February 2012 - 04:37 PM
Members - Reputation: 5815
Posted 25 February 2012 - 06:28 AM
Members - Reputation: 103
Posted 27 February 2012 - 12:31 PM