Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Creating a menu


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 mypel16000   Members   -  Reputation: 46

Like
0Likes
Like

Posted 31 October 2012 - 09:33 AM

Hi, I have been learning some programming and after a couple of months of C++ and SFML I have been able to create simple games such as pong or an animated man moving around the screen.

I have been trying to implement a game menu to my Pong game, and as I found no tutorials or help, I proceeded myself. My code was:

[source lang="cpp"]if (!inMenu){ while(Screen.isOpened()) { blablabla; }}while (inMenu){ //Drawng my menu background Screen.Clear(); Screen.Draw(text); Screen.Display(); //Getting mouse co-ordinates float mouseX = Screen.GetInput().GetMouseX(); float mouseY = Screen.GetInput().GetMouseY(); //Check if mouse is on button x-coordinates and clicked if(mouseX <= 210 && mouseX >= 38 && Screen.GetInput().IsMouseButtonDown(sf::Mouse::Left)) { //Check if its on Y co-ordinates if(mouseY <= 470 && mouseY >= 420) //exit inMenu loop and go to !inMenu one inMenu = false; } }[/source]
This is just the heart of my menu code. It is inside a while !Quit loop and inside my main function. When I run this the game stays open for about 5 secs, the button doesnt work and then the game crashes. If i take out the menu it works fine, so the rest of the code is OK.

Please, if you are going to say that I need more time with C++, don't. I'm clever enough and I can manage. I just need help to asimilate new concepts. I would like for someone to help me find the problem with this code and if possible to give me a tutorial on game Menus.

P.S: I also have a game of a man moving around the screen and I would like to make enemies and shooting. I have an animation class and a person calss to controll them, but I'm not clear on how to make different enemies or on how to make SHOOTING (most important thing I want to learn) As I said, I'm working from documentation and don't really know how to do this.

*Attached are all of the files for my PONG! game, if anyone would want to take a look.

Sponsor:

#2 Zouflain   Members   -  Reputation: 532

Like
5Likes
Like

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.

---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){
	 	 case MENU:
	 	 	 gameMenu();
	 	 	 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.

Edited by Zouflain, 31 October 2012 - 10:52 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS