Separated sources for my functions

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
zapakitul
Posts: 9
Joined: Sun Mar 22, 2009 7:04 pm

Separated sources for my functions

Post by zapakitul »

I'm trying to make a few functions and classes an put them in separated files, and then include them and call then when ever i need. Thing is, i do not understand what am i doing wrong. Here's an example:
Main.cpp

Code: Select all

#include "header.cpp"


int main()
{
	

	CreateWindow();

	guienv->addStaticText(L"Hello World! This is the Irrlicht Software renderer!",
		rect<s32>(10,10,260,22), true);


	IAnimatedMesh* mesh = smgr->getMesh("../../media/sydney.md2");
	if (!mesh)
		return 1;
	IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh );


	if (node)
	{
		node->setMaterialFlag(EMF_LIGHTING, false);
		node->setMD2Animation(scene::EMAT_STAND);
		node->setMaterialTexture( 0, driver->getTexture("../../media/sydney.bmp") );
	}


	smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));


	while(device->run())
	{

		driver->beginScene(true, true, SColor(255,100,101,140));

		smgr->drawAll();
		guienv->drawAll();

		driver->endScene();
	}


	device->drop();

	return 0;
}

Header.h

Code: Select all

#ifndef _HEADER_H_
#define _HEADER_H_

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

int CreateWindow();

#endif _HEADER_H_
Header.cpp

Code: Select all

#include <irrlicht.h>
#include "header.h"

#pragma once

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


int CreateWindow()
{
			IrrlichtDevice *device =
#ifdef _IRR_OSX_PLATFORM_
		createDevice( video::EDT_OPENGL, dimension2d<s32>(1024, 768), 16,
			false, false, false, 0);
#else
		createDevice( video::EDT_SOFTWARE, dimension2d<s32>(1024, 768), 16,
			false, false, false, 0);
#endif
	if (!device)
		return 1;

		device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo");

	IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();
	IGUIEnvironment* guienv = device->getGUIEnvironment();
}
When i try to compile it, it gives me the following errors:

Code: Select all

1>main.cpp
1>d:\swift\main.cpp(10) : error C2065: 'guienv' : undeclared identifier
1>d:\swift\main.cpp(10) : error C2227: left of '->addStaticText' must point to class/struct/union/generic type
1>        type is ''unknown-type''
1>d:\swift\main.cpp(14) : error C2065: 'smgr' : undeclared identifier
1>d:\swift\main.cpp(14) : error C2227: left of '->getMesh' must point to class/struct/union/generic type
1>        type is ''unknown-type''
1>d:\swift\main.cpp(17) : error C2065: 'smgr' : undeclared identifier
1>d:\swift\main.cpp(17) : error C2227: left of '->addAnimatedMeshSceneNode' must point to class/struct/union/generic type
1>        type is ''unknown-type''
1>d:\swift\main.cpp(24) : error C2065: 'driver' : undeclared identifier
1>d:\swift\main.cpp(24) : error C2227: left of '->getTexture' must point to class/struct/union/generic type
1>        type is ''unknown-type''
1>d:\swift\main.cpp(28) : error C2065: 'smgr' : undeclared identifier
1>d:\swift\main.cpp(28) : error C2227: left of '->addCameraSceneNode' must point to class/struct/union/generic type
1>        type is ''unknown-type''
1>d:\swift\main.cpp(31) : error C2065: 'device' : undeclared identifier
1>d:\swift\main.cpp(31) : error C2227: left of '->run' must point to class/struct/union/generic type
1>        type is ''unknown-type''
1>d:\swift\main.cpp(31) : fatal error C1903: unable to recover from previous error(s); stopping compilation
1>Build log was saved at "file://d:\Swift\Debug\BuildLog.htm"
1>Swift - 13 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

First: You have to include the header file. No the source file. So in your main.cpp it must be

Code: Select all

#include "header.h"
Second: guienv, smgr, etc. are local variables for the function CreateWindow(). You cannot use them outside this function because they don't exist outside. This is why you get these errors.

You could either make these variables globals or let CreateWindow() return a pointer to the created device.
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
teamAlpha
Posts: 68
Joined: Sun Mar 08, 2009 4:14 pm

Post by teamAlpha »

^ What he/she said , plus you'll need to return a structure that will contain information about the device.

Eg :

Code: Select all

struct IrrInitResult
{
   IVideoDriver* driver;
   ISceneManager* smgr;
   IGUIEnvironment* guienv; 

   IrrInitResult():driver(NULL),smgr(NULL),guienv(NULL){}
};


IrrInitResult createDevice( args??? )
{
     IrrInitResult r;

     r.driver = get driver .. etc

     return r;

}

... inside main :

IrrInitResult sys = createDevice();
sys.driver->do something();

:)
Alpha::AppleBoy
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

He ^^

I don't see the need for a structure. You can get the pointer to the gui, scene, and driver through the device.
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
Post Reply