GPWiki.org
GPWiki.org
It is currently Tue May 21, 2013 3:40 pm

All times are UTC




Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: Sun Sep 09, 2012 11:50 am 
Cubic Contributor
User avatar

Joined: Sun Aug 05, 2012 9:32 pm
Posts: 71
Hi guys, I am trying to learn game programming (I am an experienced programmer so don't worry about game maker wanabe), But I am always encountering a problem when it comes to Title screen.
I am capable of creating all the game logic and HUD, but I am never sure how to approach Title screen at the beginning of the game (including options menu). What I am asking you is how do you guys approach creating title and option screen? Do you start with the menu first or do you include it later on? Do you use the same game loop or a different loop just for the menu? How do you display different pages of the menu and then go back?

If you have any problem understanding what I am asking just let tell me and I try to make things clearer for you (as English is not my first language)

Thanks!

_________________
Did you ever wonder, how time works?


Top
 Profile  
 
PostPosted: Sun Sep 09, 2012 12:29 pm 
Harmlessness does no harm
User avatar

Joined: Tue Sep 14, 2004 8:37 pm
Posts: 3807
Location: Ferriday, LA, US
I tend to do it as a game state. The game loop would simply update/render based on the current game state, so if you have your states as objects, you could do stuff like:

Code:
GameState splash;
GameState title;
GameState play;

gameLoop.state = splash;


And:

Code:
gameLoop.update(time);  // calls gameLoop.state.update()
gameLoop.render(surface); // calls gameLoop.state.render()


In the case above, each GameState would need at least two functions: update() and render(). Then, when your game loop is running, you are able to specify which update/render functions to use, based on the game state. This way, you can simply do your title screen and such just as you would the game play sequences, and just swap out the game state objects and let them take over.

_________________
What most people don't understand about "enlightenment" is that it is not an end-goal; but where you find yourself just before taking a new "first step."


Top
 Profile  
 
PostPosted: Sun Sep 09, 2012 1:39 pm 
Grand Optimizer
User avatar

Joined: Mon Jan 17, 2005 6:01 pm
Posts: 352
Location: Canada
I agree with Mugai, I do something very similar:

Code:
string GameState;


Then, in my main loop, I do something like this:

Code:
switch (GameState)
{
    case: "MainMenu"
    ExecuteMainMenuLoop();
    break;

    case: "MainGameLoop"
    ExecuteMainGameLoop();
    break;
}


and so on.

_________________
"None are more hopelessly enslaved than those who falsely believe they are free."
"It is no measure of health to be well adjusted to a profoundly sick society."
"Hope is the first step on the road to dissapointment." -Jonah Orion
http://tankzgame.blogspot.com


Top
 Profile  
 
PostPosted: Sun Sep 09, 2012 2:38 pm 
Cubic Contributor
User avatar

Joined: Sun Aug 05, 2012 9:32 pm
Posts: 71
Mugai: what kind of object the GameState is? is it passing functions to the gameLoop or is it rather an enumeration value or a string as in Menelikis example?

Meneliki: thats a nice and clean idea but doesn't the extra switch function cause drop in framerate?


Thanks guys, looking back at my current solution for the title screen, its a real mess! I can't believe I didn't think of these in the first place.

_________________
Did you ever wonder, how time works?


Top
 Profile  
 
PostPosted: Sun Sep 09, 2012 8:02 pm 
Harmlessness does no harm
User avatar

Joined: Tue Sep 14, 2004 8:37 pm
Posts: 3807
Location: Ferriday, LA, US
Hazarth wrote:
Mugai: what kind of object the GameState is? is it passing functions to the gameLoop or is it rather an enumeration value or a string as in Menelikis example?

It would be a class you would define yourself, assuming you are coding in a language that supports object-oriented programming. A couple of examples:

Code:
// Java
// Note: Syntax probably not 100% correct; been a LONG time since I've seriously used Java.

public class GameState {
    // declare variables necessary to the state
    // examples: Menu choices; input handlers; etc.

    public function update(time) {
        // update the objects in this state
        // examples: animated sprites, timed effects
    }

    public function render(surface) {
        // draw to the screen/buffer
    }

    // other functions for the class. Suggestions include "initialize", "pause", etc.
}

// Then, when setting the game up, initialize your states...
GameState_Splash splash = new GameState_Splash();    // in real life, the variables would be extended from the GameState class
GameState_Title title = new GameState_Title();
GameState_Play play = new GameState_Play();

GameLoop mainLoop = new GameLoop();
mainLoop.setState(splash);    // let's start with the splash state, like most people do
mainLoop.run();    // start the loop

// .....

// In the game loop...

public function setState(GameState state) {
    this.state = state;
}

public function update(time) {
    this.state.update(time);
}

public function render(surface) {
    this.state.render(surface);
}


That's just the way I do it. Meneliki's example works fine as well, if you break the code down into manageable chunks. With his example, you would skip using classes altogether, and simply define the functions that handle the game state.

Any help? :)

_________________
What most people don't understand about "enlightenment" is that it is not an end-goal; but where you find yourself just before taking a new "first step."


Top
 Profile  
 
PostPosted: Sun Sep 09, 2012 11:03 pm 
Cubic Contributor
User avatar

Joined: Sun Aug 05, 2012 9:32 pm
Posts: 71
Mugai wrote:
Hazarth wrote:
Mugai: what kind of object the GameState is? is it passing functions to the gameLoop or is it rather an enumeration value or a string as in Menelikis example?

It would be a class you would define yourself, assuming you are coding in a language that supports object-oriented programming. A couple of examples:

Code:
// Java
// Note: Syntax probably not 100% correct; been a LONG time since I've seriously used Java.

public class GameState {
    // declare variables necessary to the state
    // examples: Menu choices; input handlers; etc.

    public function update(time) {
        // update the objects in this state
        // examples: animated sprites, timed effects
    }

    public function render(surface) {
        // draw to the screen/buffer
    }

    // other functions for the class. Suggestions include "initialize", "pause", etc.
}

// Then, when setting the game up, initialize your states...
GameState_Splash splash = new GameState_Splash();    // in real life, the variables would be extended from the GameState class
GameState_Title title = new GameState_Title();
GameState_Play play = new GameState_Play();

GameLoop mainLoop = new GameLoop();
mainLoop.setState(splash);    // let's start with the splash state, like most people do
mainLoop.run();    // start the loop

// .....

// In the game loop...

public function setState(GameState state) {
    this.state = state;
}

public function update(time) {
    this.state.update(time);
}

public function render(surface) {
    this.state.render(surface);
}


That's just the way I do it. Meneliki's example works fine as well, if you break the code down into manageable chunks. With his example, you would skip using classes altogether, and simply define the functions that handle the game state.

Any help? :)



Yes, Thank you, I mostly understand, But how do you use 1 class to contain 3 different logics for the update function? Its obvious I don't want to, lets say, button event from the menu screen to fire while in game. Do I simply create 3 childs of the parent gameState class, or is there a way to overload a class somehow? Btw, I am using C++ for this.

If any of the moderators could delete the post above I would be grateful, I didn't notice that I wasn't logged in at that time!

_________________
Did you ever wonder, how time works?


Top
 Profile  
 
PostPosted: Mon Sep 10, 2012 12:49 am 
Harmlessness does no harm
User avatar

Joined: Tue Sep 14, 2004 8:37 pm
Posts: 3807
Location: Ferriday, LA, US
Hazarth wrote:
Yes, Thank you, I mostly understand, But how do you use 1 class to contain 3 different logics for the update function? Its obvious I don't want to, lets say, button event from the menu screen to fire while in game. Do I simply create 3 childs of the parent gameState class, or is there a way to overload a class somehow?


Yes, you would want a separate class for each state, and each of those would be derived from a base class (e.g. GameState).

Hazarth wrote:
Btw, I am using C++ for this.


You may want to look into functors (also called "function objects", I believe). C++ is considerably less forgiving when it comes to type compatibility and memory management, so the solution above may not work without some significant adjustment. There should be some material on functors in the wiki here.

Hazarth wrote:
If any of the moderators could delete the post above I would be grateful, I didn't notice that I wasn't logged in at that time!


Done. :)

_________________
What most people don't understand about "enlightenment" is that it is not an end-goal; but where you find yourself just before taking a new "first step."


Top
 Profile  
 
PostPosted: Mon Sep 10, 2012 2:06 pm 
Cubic Contributor
User avatar

Joined: Sun Aug 05, 2012 9:32 pm
Posts: 71
Quote:
Yes, you would want a separate class for each state, and each of those would be derived from a base class (e.g. GameState).

Thanks a lot! This really helps me out :) I never actually used inheritance or similar methods in C++ for classes (As I never found a reason to use them), This might be a good time and reason to learn it properly so, thats a plus indeed.

Quote:
C++ is considerably less forgiving when it comes to type compatibility and memory management


Yes, I noticed that when I ported to C++ from VB .NET Which was much easier to use as it is more high level language. I thought it would be a good idea to learn a low level language like C++ as well, It is really much easier when I already know few other languages and how the logic works (been working in VB for about 5-6 years). Its fun to find or create alternative ways of doing the same things in Low level language, although it can be really hard and (depressive?) at times :D !


anyway, thank you guys, You've been a great help. :thumbs

_________________
Did you ever wonder, how time works?


Top
 Profile  
 
PostPosted: Tue Sep 11, 2012 6:09 pm 
Bytewise

Joined: Sun Oct 16, 2011 3:09 pm
Posts: 277
Location: Here (where else?)
Hazarth wrote:
I never actually used inheritance or similar methods in C++ for classes (As I never found a reason to use them), This might be a good time and reason to learn it properly so, thats a plus indeed.
Indeed :)

With C++, make sure you declare the base destructor virtual (
Code:
virtual ~GameState()
), and also all base class methods you want to override in a derived class.
Last but not least, call the baseclass constructor in the constructor of the derived class:

Code:
GameStateSplash::GameStateSplash() : GameState() { ... }

_________________
My project: Messing about in FreeRCT, dev blog, and IRC #freerct at oftc.net


Top
 Profile  
 
PostPosted: Tue Sep 11, 2012 7:10 pm 
Cubic Contributor
User avatar

Joined: Sun Aug 05, 2012 9:32 pm
Posts: 71
Alberth wrote:
Hazarth wrote:
I never actually used inheritance or similar methods in C++ for classes (As I never found a reason to use them), This might be a good time and reason to learn it properly so, thats a plus indeed.
Indeed :)

With C++, make sure you declare the base destructor virtual (
Code:
virtual ~GameState()
), and also all base class methods you want to override in a derived class.
Last but not least, call the baseclass constructor in the constructor of the derived class:

Code:
GameStateSplash::GameStateSplash() : GameState() { ... }


Yes, indeed I do it like that, but i am not exactly sure what Virtual destructor means, I learned to use it from examples and such, but I never really understood what does it change, the "virtual" thing I mean.

_________________
Did you ever wonder, how time works?


Top
 Profile  
 
PostPosted: Wed Sep 12, 2012 9:54 am 
Shake'n'Baker

Joined: Sun May 27, 2012 6:01 pm
Posts: 62
Hazarth wrote:
Yes, indeed I do it like that, but i am not exactly sure what Virtual destructor means, I learned to use it from examples and such, but I never really understood what does it change, the "virtual" thing I mean.


It allows a method to be overridden by an inheriting class. Its a key component in polymophism.

If you use a polymophic delete, you must have a virtual deconstructor in order to fully remove/cleanup the object.

ANIMAL *bob = new FISH;
delete bob; <- This is a polymophic delete, because it is cast to a different type.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group