I've advanced quite well in OpenGL and in graphics. However I lack a lot of experiences.

I have had this question in my mind since the beginning of my venture, how do I create UI or Menu for my game?

I imagine I will create a menu just like creating a 2D game in OpenGL.

  • 2
    \$\begingroup\$ What have you tried so far, based on OpenGL game development tutorials you've researched? \$\endgroup\$ – DMGregory Feb 12 at 18:51
  • \$\begingroup\$ @DMGregory Since I don't want to waste time on working wrong methods, I always ask my questions before I try something. \$\endgroup\$ – J.G Feb 12 at 19:00
  • \$\begingroup\$ I've tried nothing related to ui or menu rendering \$\endgroup\$ – J.G Feb 12 at 19:00
  • \$\begingroup\$ I am not really sure what would be a wrong method in this case (as long as we rule out the obvious things that are wrong because they don't accomplish the goal of creating a GUI or a menu for games) \$\endgroup\$ – htmlcoderexe Feb 12 at 19:05
  • 1
    \$\begingroup\$ So the first place you asked was Google, right? And you worked through several tutorials to gain the benefits of the wealth of documented knowledge already out there? What's the next gap we can help you fill, what one question have you not yet found an answer to in your research? \$\endgroup\$ – DMGregory Feb 12 at 20:54

Option 1: turn back now and find and tweak a ready made solution, there are tons of GUI libraries for every language and framework.

Option 2: roll your own for the sake of rolling your own. Continue reading!

If you intend to have anything more complex than a health/score display, you will want to do it in multiple "layers".

Start with the bottom layer - the lowest level. Create some classes and routines that draw the various graphic elements (drawButton(X, Y, text, state)). Create the next layer - GUI elements themselves, which then use your drawing functions to draw themselves appropriately. Let these both have a common ancestor - a Control class which then handles things like passing mouse clicks through to its child Controls or firing actions when clicked. Create your Window class that also derives from Control, but has its own extra functionality like dragging and closing, if you need windows in your GUI.

Finally create the top layer, the Window/GUI manager. This will then take mouse and key input, keep track of what's in focus and what's where, draw windows in the right order, all the things, so that in the end you will perhaps end up with 2-3 methods that are actively used on game runtime, not counting initialisation and death.

By layering your abstraction like this you can make changes easily exactly where you need them - I made the mistake of having it all flat before and gave up after realising that now that I want buttons to highlight when mouse over I had to change like 30 places which each defined its own button.

As usual I will drop the most recent version of what I have, most of it is readily reusable but it is not really finished and probably MonoGame specific. The higher leve stuff, however, gives you a good idea of the layered principles:


Here, I have two lower levels, one that deals with creating all the appropriate geometry that's later handed to the graphics card, and one that requests the creation of said geometry to actually render graphical elements.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.