How do you set up a menuing system (clickable buttons) in C++/directx?
I'm lead to believe that function pointers are going to be needed to assign functions to the images that will be used as buttons somehow, but I can't find any decent resources on this.
How can you set up a button menu in C++? For example, having buttons for save, load, exit, new.
edit: I am not worried about portability for this example. I开发者_JAVA百科'd rather do it myself than use a predefined framework. It will only be run on Windows machines.
You will need to write yourself a bunch of GUI classes which draw the GUI interface (buttons, text) as well as some basic "collision detection" to detect if the mouse was clicked inside your button when the user presses the mouse button.
You don't necessarily have to use function pointers, they are more of a convenience. The below example is just as capable.
I would instead write a button class that had position, width, height, etc then you could write a method that returned if the mouse was clicked, not requiring any hard coding which you should avoid at all costs.
If you want to implement a very basic and non-flexible GUI you could detect mouse clicks like this):
if ( mouseWasClicked == true )
{
if (( mouseX > startButton.minX && mouseX < startButton.maxX ) &&
( mouseY > startButton.minY && startButton.maxY < mouseY) )
{
loadGame();
}
}
That said I'd recommend using a proper library for a GUI system such as CEGUI which is open source and works with Rendering engines like Ogre and others. CEGUI is also portable and much more lightweight than Qt. It is recommended if you are programming a game.
Don't reinvent the wheel unless you're trying to learn how the low level works, use something that is proven.
You basically have to do all of your own drawing, mouse tracking, etc. There are samples in the DirectX SDK itself that show an entire windowing framework, with controls.
However, this can be dramatically simplified by using something like CEGUI. Crazy Eddie's GUI provides an entire framework, ready to go and skinnable, for doing heads up displays and controls, including buttons, menus, etc., in DirectX.
That is not a language feature as your answer implies -- you are now in the realm of GUIs and framework which are often non-portable.
So with that I'd recommend Qt which is cross-platform, mature, well-documented and mature. There are other options, and there are numerous FAQs to guide you.
精彩评论