• Create Account

### #ActualZouflain

Posted 31 October 2012 - 10:52 AM

To be honest, writing a menu in C++ is something of a pain, not because C++ isn't perfectly capable of doing it, but because in general a menu is something that is better handled in a script rather than be compiled. A menu will be fiddled with quite a bit over the course of an application's development, and hard coding that into the game make iteration difficult. If you're up to the task, I would suggest adding a scripting engine/virtual machine into your game. Lua is extremely lightweight and easy to get running even for a novice programmer (heck, it was my first scripting language and I've never had cause to leave it!), but very powerful and worth your consideration. It takes literally about 4 lines of to get in running. Python is a nightmare to get running for a novice, so I don't suggest it at this juncture.

If you can get LUA running, then I would suggest doing all of your GUI widgets (which is what a menu is comprised of) in Lua, and let them handle all of the "if mouseX< 98484" logic and event handling. In my games, I just pass pertinent render code from Lua to C++ so it can communicate with OpenGL - C++ itself has no idea how or what a widget does, it knows only how to pass it a generic event structure and how to get back render data. Completely rewriting a menu is a snap at this point, as is adding new widgets or changing their behavior. Also remember to compartmentalize your data into objects as it will save you quite a bit of frustration, and make this sort of thing much simpler in the long run.

---Edit---
1) Be sure you give your CPU a rest in those while loops, unless you want your game to eat as much processor as it can get its hands on in nothing more than a menu. SFML has a delay or sleep function, add that at the end of your loop. It's not necessary, but I find it to be a nice gesture for those that run multiple applications.
2) Make sure the coordinates correspond to what you're expecting. When I first started, I didn't pay attention to the orientation of coordinate systems (naively assuming they'd all use the "standard" one in math text books). The button may be working, but not in the region you think the coordinates describe.
3) Without a debugger, finding crashes is a pain. I would suggest you get familiar with a debugger. Still, if you're into hackish style coding, you can comment out every line and test the application, uncommenting line by line until you find the line that prompts the crash (or, if you're familiar with "binary searching" you can use that rather than brute force...). But get a debugger.
4) I generally avoid while loops, except the main loop (there are appropriate places for them, but I don't think a menu is one). What happens if you get a WMQUIT message during that while loop? Your program looks to windows like its not responding. There are also a host of other events that ought to be handled. Instead of a while loop, implement a finite state machine.
while (game_is_running){
switch(game_current_state){
break;
case GAME:
gamePlay();
break;
}
}
That's ugly, but an example of what I mean. No more while loop, and you can handle events before, after, or even during these function calls.

### #1Zouflain

Posted 31 October 2012 - 10:35 AM

To be honest, writing a menu in C++ is something of a pain, not because C++ isn't perfectly capable of doing it, but because in general a menu is something that is better handled in a script rather than be compiled. A menu will be fiddled with quite a bit over the course of an application's development, and hard coding that into the game make iteration difficult. If you're up to the task, I would suggest adding a scripting engine/virtual machine into your game. Lua is extremely lightweight and easy to get running even for a novice programmer (heck, it was my first scripting language and I've never had cause to leave it!), but very powerful and worth your consideration. It takes literally about 4 lines of to get in running. Python is a nightmare to get running for a novice, so I don't suggest it at this juncture.

If you can get LUA running, then I would suggest doing all of your GUI widgets (which is what a menu is comprised of) in Lua, and let them handle all of the "if mouseX< 98484" logic and event handling. In my games, I just pass pertinent render code from Lua to C++ so it can communicate with OpenGL - C++ itself has no idea how or what a widget does, it knows only how to pass it a generic event structure and how to get back render data. Completely rewriting a menu is a snap at this point, as is adding new widgets or changing their behavior. Also remember to compartmentalize your data into objects as it will save you quite a bit of frustration, and make this sort of thing much simpler in the long run.

PARTNERS