Is my main.cpp good or did I over do it?

Started by
14 comments, last by Cygon 18 years, 4 months ago
How's my main.cpp file? I think it's good, but maybe it's just too short?

#include "CGameManager.h"

int main(int argc, char *argv[])
{
    CGameManager gMainGame;

    if (!gMainGame.GetStatus())
        return gMainGame.GetStatus();

    while (gMainGame.Handle())
        gMainGame.Render();

    return gMainGame.GetStatus();
}

Advertisement
Haha, brilliant! I'd hate to see what CGameManager.h looked like though.
Frankly I wouldn't allocate CGameManager on the stack like that. That's my personal taste.

Also depending on the return value of Handle() probably isn't the best way to determine if your game is still running. Probably would want something more specific like CGameManager::IsRunning().

What's the 'g' in gMainGame? Clearly, it's a local not a global which is what I would expect the 'g' to mean.

Obviously you have a lot of initialization in your constructor. I don't like that pattern; most other people don't either.
What type of game are you making? Also, I think you are right. It does look a bit short for the main game file. Add more detail to it or tell me info on the game, then I can be of help.
I notice that your main loop is just:
gMainGame.Render();

To me that is a bit misleading because to me Render is "Render Graphics". This does not mention anything about where:

Input is handled.
Sound is handled.
Game AI/logic is handled.
Networking ( if in use ) is handled.


I assume all this handled in the Render function but a better name might be Tick or Step.
Quote:Original post by Undeadlnsanity
Haha, brilliant! I'd hate to see what CGameManager.h looked like though.


CGameManager.h: 32 lines with #ifndef ... #define ... #endif.

This is supposed to be a ping pong game, but one just started. It's splitted into 11 files along with main.cpp, since it has an SDL wrapper. It's input, events, init', deint', and sprite. CGameManager just handles stuff because I don't like main(...). *cough* She and I had problems. *cough*

The engine is supposed to be somewhat 'reusable' next time, so I don't have to write sloppy stuff.

There isn't "ALOT" initialization in CGameManager::CGameManager. Just:
CGameManager::CGameManager(): m_SCREEN_W(640), m_SCREEN_H(480), m_SCREEN_BPP(32), m_Status(true){    if (!m_MainApp.Init(SDL_INIT_VIDEO))    {        m_Status = false;        return;    }    if (!m_MainApp.InitWindow(m_SCREEN_H, m_SCREEN_W, m_SCREEN_BPP, SDL_HWSURFACE | SDL_DOUBLEBUF))    {        m_Status = false;        return;    }    SDL_WM_SetCaption("Crazy Pong!  ~Agi~", NULL);}


So, any other ideas?

P.S. 'g' for 'game'. I didn't know other people recognized 'g' as 'global'.
Quote:Original post by acraig
I notice that your main loop is just:
gMainGame.Render();

To me that is a bit misleading because to me Render is "Render Graphics". This does not mention anything about where:

Input is handled.
Sound is handled.
Game AI/logic is handled.
Networking ( if in use ) is handled.


I assume all this handled in the Render function but a better name might be Tick or Step.


Noo... Not exactly. What's gMainGame.Handle() in there for? [smile]

EDIT: Handle(...), not Run(...) ;)!

EDIT: It's EDIT, not DIT ;)))!
Why not move all that initialisation out of the constructor and into something like CGameManager::Initialise()? That way you could return true or false directly, and you wouldn't need the extra status variable.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Quote:Original post by superpig
Why not move all that initialisation out of the constructor and into something like CGameManager::Initialise()? That way you could return true or false directly, and you wouldn't need the extra status variable.


A fake init() function? That's what the constructor is for, initialization! And I need m_Status to return in main, so I don't return any ugly 0's. Plus I can use m_Status as a check, too.
Quote:Original post by agi_shi
Quote:Original post by superpig
Why not move all that initialisation out of the constructor and into something like CGameManager::Initialise()? That way you could return true or false directly, and you wouldn't need the extra status variable.


A fake init() function? That's what the constructor is for, initialization! And I need m_Status to return in main, so I don't return any ugly 0's. Plus I can use m_Status as a check, too.


The constructor is more used to Init your attributes (not sure of the english word) to their starting values. e.g m_bGameRunning = true;

The SDL Initialisation should be in a function because this way it can return if the Init was succesfull or no.

This topic is closed to new replies.

Advertisement