To really make use without global variables, you need a place to keep all your important pointers. For example:
If you create a game, the game should be runned in the "CGame class", and the CGame class holds the pointers to all your little monsters and maps and everything. The CGame is instanciated from main.
Code:
class CGame
{
void create()
{
// create ogres and put them in the list here.
}
void run()
{
m_bRunning = true;
while(m_bRunning){
//update all your oggres here.
for each ogre in m_OgreList{ Orge->Update() }
KillOgre(GetNextOgreFromList);
if(IsAllOgresDead()) m_bRunning = false;
}
}
void KillOgre(COgre *pOgre)
{
pOgre->DieBastard();
// remove pOgre From list
}
LinkedListWithCOgres m_OgreList;
bool m_bRunning
};
void Main()
{
CGame game;
game.create();
game.run();
}
I've keept this very very simple, and I hope you get the Idea how to use OOP to get rid of the globals and make it all nice and confy. Now, I don't say "put every pointer in CGame", because that is wrong also. You should rather use the model in KillOgre, that you pass a pointer to the function, telling it what ogre we are talking about. And the Ogre should hold its pointers to its "What-ever-stuff-the-ogre-is-using".
Another way of doing this kind of "get around globals" is to use something called singleton. But that is only usable for say a Game Engine, where you know that you'll only be using one, and more should never be created. Singletons can only have one instance at a time. This is usefull since you can get this one-and-only instance by just creating an ordinare pointer to the class. I won't explain singletons more here though, think you should manage the OOP part first!
Well I've tried to keep it simple, and this is only the basics. Probably broke a few "rules of OOP" to... Love to do that...
Where you go from this simple OOP design is up to you. I would go for polymorphism and use of interfaces, templates and more.
for example this is how the core of my engine would look (the update/moveframe part):
Code:
void CEngine::Update()
{
// pseudocode for iterating trough a linked list
for each pGameObject in GameObjectList{
pGameObject->Update();
}
}
----
meanwile, faraway in the CGameObject class
class CGameObject
{
virtual void Update() = 0;
};
What happened there? isn't that the same code as before? No, looks the same but is not. The game object is the base interface for all object I have in the game world (player, map, sword, invetory, chatwindow ...) All of these object inherit the GameObject class and implements the Update function. for example the player needs to update his position, the chat window need to update its text buffers. This way, i have only a single object to handle in the core of the engine, yet, i can update any object in the game from there...
Well I won't digg deeper from here. This topic is huuuge and I've already confused you enough I think... Since you are still working with simple classes. Ask me if you got problems.
Happy coding!