Cross File Initialising

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
nath042
Posts: 2
Joined: Wed Jun 15, 2011 8:18 am
Location: United Kingdom

Cross File Initialising

Post by nath042 »

i have my main.cpp

Code: Select all

//all includes
#include <irrlicht.h>					//include headers
#include "IChoiceScreen.h"


using namespace irr;					//include namespace
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")	//include libs
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif

void StartGame(void);

int main()				/// INIT GAME
{

isgf::IChoiceScreen set;

    if(!set.choiceScreen())
        return -1;

    IrrlichtDevice *device=set.createDeviceFromMe();	// Loads Devices
	IVideoDriver* driver = device->getVideoDriver();	// Loads Video Driver
	ISceneManager* smgr = device->getSceneManager();	// Loads Scene Manager
	IGUIEnvironment* gui = device->getGUIEnvironment();// Loads Environment
	ISceneCollisionManager* collMan = smgr->getSceneCollisionManager(); //remember node

	StartGame();

    device->drop();
    return 0;
	}

but before the game loads the managers it has to read the other cpp file.. and it crashes because there is no managers for them yet.

Is there a way to get around this? StargGame links to a file called game.cpp

it starts like this

Code: Select all

#include <irrlicht.h>					//include headers
#include "globals.h"
#include "device.h"
#include "gui.h"
#include "levels.h"
#include "controls.h"

void StartGame(void){
	loadHarlowTown(smgr);							// loads map, camera, and collision
	dev->getCursorControl()->setVisible(false);		// Removes Cursor
	
	//game loop
	while(dev->run())
		if (dev->isWindowActive())
			{
				FunctionKeys();
				driver->beginScene(true, true, 0);
				smgr->drawAll();
				gui->drawAll();
				driver->endScene();			// End Scene
				
			//gui
				//FPS
				int fps = driver->getFPS();
				if (lastFPS != fps)
					{
						stringw str = L"CS";
						str += driver->getName();
						str += "] FPS:";
						str += fps;

						dev->setWindowCaption(str.c_str());
						lastFPS = fps;

					}
				stringw drivrtype = L"Driver Type: ";
				drivrtype += driver->getName();
				gui->addStaticText(drivrtype.c_str(),rect<s32>(10,10,260,22), true);
		ShowGUI();
			}
	dev->drop();
	return 0;
}

please someone help this has been bugging me for hours!

thanks,
nathan
[/code]
Reiko
Posts: 105
Joined: Sun Aug 16, 2009 7:06 am
Location: Australia

Post by Reiko »

Hmm, I feel like there's something missing.

In your main function, you got your 5 variables (device, driver, smgr, gui, collMan). But these exist only within the scope of the main function.

Then in StartGame() the first line uses "smgr", but there's no smgr in this scope, so it shouldn't even compile. I'm guessing you also have an "smgr" in globals.h or device.h?

Either way, even if you do, all the real pointers are still back in your main(), so if you have another smgr etc somewhere else, those wouldn't have been initialized.

There are a couple of approaches you can take.

One example would be to make StartGame take IrrlichtDevice* as a parameter, and call it like StartGame(device) and then set your other pointers using that.

Or you can move the device creating into StartGame somewhere.

Or you can move those 5 variables from main() outside of the function (so that they become global variables) and then in the other file, outside of the function, but after you include irrlicht.h, you can use extern for each, eg. "extern IrrlichtDevice* device;" and that would also give you access to them.

But basically, the reason its crashing is because StartGame() (and loadHarlowTown(), etc) doesn't have the pointer to the device cause only main() has it.
RageD
Posts: 34
Joined: Mon Jun 13, 2005 2:34 pm

Post by RageD »

This code looks very C-style; not that it is wrong (to have C-style code), I would recommend using data structures to hold your data. If you're going to do something like this, I generally use (what I call) a "GameManager." Basically, it's the glue to everything (well, I initialize all my objects in the structure rather than passing scoped variables around).

In any case, Reiko is right I think as long as your code compiles.

Here is an example:

Code: Select all

#ifndef __GAMEMANAGER_H_
#define __GAMEMANAGER_H_

#include <irrlicht.h>

// For brevity, I am breaking encapsulation
class GameManager
{
public:
  GameManager()
  {
      // Initialize everything
  }
  virtual ~GameManager(){}

  void start()
  {
     // Put your game loop here
  }

private:
  // I tend not to use namespaces in headers; it can lead to problems
  irr::IrrlichtDevice * device;
  irr::video::IVideoDriver * driver;
  irr::scene::ISceneManager * smgr;
  irr::gui::IGUIEnvironment * gui;
  irr::scene::ISceneCollisionManager * collMan;
};

#endif // __GAMEMANAGER_H_
main.cpp

Code: Select all

#include "GameManager.h"

int main()
{
    GameManager game;
    game.start();
    return 0;
}
This is merely a suggestion, but may be a good starting point. I use different physics, gui, etc., so I also have "sub-managers" such as "PhysicsManager" and "GUIManager" depending on whether or not I am using the default Irrlicht things.

Good luck :)

-RageD
DuF = (del)F.u
Microsonic Development
nath042
Posts: 2
Joined: Wed Jun 15, 2011 8:18 am
Location: United Kingdom

Post by nath042 »

wow thanks guys ive never had so much support on a forum before, im going to try to the class method and ill report back on my progress, thanks alot!!
Post Reply