Sign in to follow this  

Why a "Game" class?

This topic is 1375 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

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

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

Hello, I just looked in my engine, I forgot i have a DataLoader class now.

 

So i just do like this :

 

DataLoader load;

 

load.SetFolder( "foldername" );

load.Texture( true , "name" ); // true is for global or false = each level

 

The DataLoader remembers the name, so it wont load twice.

 

Now with this i only have one global , and that is DataLoader load;

I,m hoping to get voted up now for once!

 

@Ravyne : Saying that i am uneducated, but not giving a better solution ?

Share this post


Link to post
Share on other sites

Now with this i only have one global , and that is DataLoader load;

I,m hoping to get voted up now for once!

You don't typically get voted up for making your own problems better. Or even voted down for making yourself problems.

You get voted down for encouraging others to adopt your mistakes.

It should come as no surprise then, that helping others to avoid the mistakes you've made or that you know are mistakes is the way to get voted up.

@Ravyne : Saying that i am uneducated, but not giving a better solution ?

Better solutions are well known. Abuse of global is the first thing covered in any software engineering course or book. Even most introductory programming classes or books will get around to mentioning in their first half.

But somehow you missed this information, and instead glibly recommended against best practices and then boasted how good you must be to not have experienced any of its known problems.

I'm probably coming of harshly, but I'm just being frank. Everyone is welcome here but don't expect to be awarded points for mere participation. When you offer up advice, that advice will be judged. So before you offer it, consider whether it is good advice and whether you are actually qualified to judge whether it us or not. If you want to share something that there's no consensus on, present it as opinion rather than fact. And if you want to share something that is contrary to best practices, qualify when the problems can be ignored.

For example, if you had said something like "In small, throw-away projects under a couple thousand lines long, I like to just use global when I'm coding from the hip." is an entirely different answer than "I think globals are great!" and it probably wouldn't have gotten any down-votes at all.

Share this post


Link to post
Share on other sites

In my engine the Game class holds the state machine, hig perf timer, and the fixed step loop, feeded by the timer down the state machine.

When doing a game, I create a GameName class, derived from both Game and Window (Window is a wrapper for win32 windows).

In that class I put the other engine main systems, like sprite renderer, sound, networking. And overwrite the wndprocedure function from the Window class, for handling resizing /input etc. ( I initialize the systems on WM_CRATE).

 

My message loop is handled manually, I dont know how to make it part of the engine. I place the Game::FixedStepLoop() myself on the main, and just need to load the first state to start the game. Till now all states I create grab a pointer to the GameName class on initialization, to access the main systems.

Edited by Icebone1000

Share this post


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