Advertisement Jump to content
Sign in to follow this  
cozzie

Why a "Game" class?

This topic is 1742 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

Everytime I see some game's source code (just a sneak peek), I see al class "Game" (or CGame).
Was just wondering, in my own main application I declare objects for input, io, audio, 3d scene etc. Global in the cpp file.

Would the game class be having all of these as a member, so you need only one "game" objects? (and also put your other stuff in there what otherwise would be global in your cpp main file)

Share this post


Link to post
Share on other sites
Advertisement

Using a game class has a number of other potential advantages. Firstly, it can allow you to benefit from inheritance and polymorphism. If you're utilising an underlying framework, that framework might provide a Game class which can be extended, thereby handling much of the lower-level stuff. Certainly this can be handled using a number of separate systems, but there's certainly scope for conveniently handling timing values and calls to update the input system through a base class.

 

In the same way, you can benefit from polymorphism. In the Heron Framework, which I've developed and used for a number of years, there are multiple extensions to the game class that provide more specific functionality and, more importantly, there are different versions for different platforms. Once again, there are other ways to approach these issues but this is one convenient one.

 

There's also the possibility to consider scope for multiple 'games.' In Heron, there's a 'GameMode' class which provides the functionality of a game loop but is managed by a game. This effectively allows multiple 'Game'-type classes to be created, loaded, unloaded and run. In this manner separate components can be loaded separately and unloaded when not required, though this is usually handled in other ways. More often, I use this class to create a single game class in a portable library.

 

A final way in which multiple 'Games' might be employed are in tools. While not games, a number of the tools I have created employ Game classes to manage components that utilise the graphic device and may also employ the input system or content pipeline. In such cases it is beneficial to separate the game from the remainder of the application, and also sometimes necessary to create multiple games and run them simultaneously.

 

These are some of the reasons I, personally, choose to use game classes. Like rip-off, I also have an aversion to globals. That said, there are many other valid approaches to the problem, such as utilising a graph of systems and components. 

Share this post


Link to post
Share on other sites

I love globals, so you only have to declare them once, also not passing in functions, just use external in the header file.

Next to game class i also have demomovie class, mainmenu, briefing, options, credits, etc etc.

greetings

Share this post


Link to post
Share on other sites

Thanks for all replies.

I'm gonna make a game class for sure, but first I'll probably have to make a basic design on the 'whole game'. So my decisions on which classes should be part of the 'game class' are logical. For example my audio and input class will not change during runtime, so they wouldn't need to be reloaded (as part of a 'new game').

Share this post


Link to post
Share on other sites

Hello Frob,

I got voted down for these reasons about globals ?, i feel special now that i can handle it then.

Once the engine works like this, there is no more of these problems.

 

Anyways are using globals also slower ?

 

The point for me is that i have all data / pointers in 1 file ( ok 2 files ( cpp & h ) )

i like to make many games, so i dont have to hunt everything down when cleaning the engine.

Its realy comfortable, professors can discuss another decade about it, but it wont convince me.

 

greetings

Edited by the incredible smoker

Share this post


Link to post
Share on other sites
Interesting reactions and input, thanks.
I'll go draw out my high over design and decide where to do what with the game class/ globals.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!