How to use classes

Discussion about everything. New games, 3d math, development tips...
Post Reply
Limb
Posts: 3
Joined: Fri May 14, 2004 12:44 am

How to use classes

Post by Limb »

Hey,

Well, i seem to have stumbled on a little trouble here. I need some help with classes. I thought you could use a seperate .cpp and .h file to setup a class (.h to do the defines and .cpp to code the functions), then just compile and your main file would recognise the class. Am I doing something wrong or can you not do this at all?

(I'm using MS Visual C++ .Net)

Thanks,
-Limb
-Limb
Unarekin
Posts: 60
Joined: Thu Apr 22, 2004 11:02 pm

Post by Unarekin »

For your other files to be able to use the class, they'll need to #include the class's header (.h) file, as well.
Limb
Posts: 3
Joined: Fri May 14, 2004 12:44 am

Post by Limb »

Thanks, got it working but now i need some more help. Im getting 2 weird errors that i dont quite understand.... heres my code:

Main.cpp

Code: Select all

#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace	scene;
using namespace video;
using namespace	io;
using namespace gui;

//ISceneNode* node = 0;

#include "eventReciever.h"

int main()
{
	MyEventReciever* reciever;
	
	IrrlichtDevice *device = createDevice(video::EDT_DIRECTX9,
								core::dimension2d<s32>(800, 600), 32, false, false, &reciever); [ERROR 2]

	IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();
	IGUIEnvironment* guienv = device->getGUIEnvironment();
	IGUIStaticText* gLocation = guienv->addStaticText(L"Location", rect<s32>(0,0,100,100), false, true, 0, 1);

	device->getFileSystem()->addZipFileArchive("Media/map-20kdm2.pk3");

	IAnimatedMesh* mesh = smgr->getMesh("20kdm2.bsp");

	if(mesh)
		node = smgr->addOctTreeSceneNode(mesh->getMesh(0));

	if(node)
		node->setPosition(vector3df(-1300, -144,-1429));

	scene::ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS(); 
	
	IAnimatedMesh* weaponMesh = smgr->getMesh("Media/armgun.3DS");
	IAnimatedMeshSceneNode* weaponNode = smgr->addAnimatedMeshSceneNode( weaponMesh, camera); 
	weaponNode->setMaterialFlag(EMF_LIGHTING, false);

//weaponNode->setScale(core::vector3df(6,6,6)); 
   weaponNode->setPosition(core::vector3df(-15,-15,10)); 
   weaponNode->setRotation(core::vector3df(90,180,0)); 
   weaponNode->setMaterialFlag(EMF_WIREFRAME, true);

   // The final setup putting the camera in the level 
   // where it best suits the start of gameplay. 
//  camera->setPosition(camera->getPosition()); */

	device->getCursorControl()->setVisible(false);

    int lastFPS = -1;

	while(device->run())
	{
		wchar_t tmp[1024];
		swprintf(tmp, 1024, L"Location: %d", camera->getPosition());

		gLocation->setText(tmp);
		
		driver->beginScene(true, true, SColor(0, 100, 100, 100));
		smgr->drawAll();
		guienv->drawAll();
		driver->endScene();

		swprintf(tmp, 1024, L"Location: %d", camera->getAbsolutePosition());

		gLocation->setText(tmp);

		int fps = driver->getFPS();

		if(lastFPS != fps)
		{

			swprintf(tmp, 1024, L"Quake 3 Map Example - Irrlich Engine (FPS: %d)", fps);
			
			device->setWindowCaption(tmp);
			lastFPS = fps;
		}
	}

	device->drop;

	return 0;
}
eventReciever.cpp

Code: Select all

#include "eventReciever.h"

bool MyEventReciever::OnEvent(SEvent event)
{
		if(node != 0 && event.EventType == EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown)
		{
			switch(event.KeyInput.Key)
			{
			case KEY_KEY_W:
			case KEY_KEY_S:
				{
					vector3df v = node->getPosition; [ERROR 1]
					v.Y += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;
					node->setPosition(v);
				}
				return true;
			}
		}
};
eventReciever.h

Code: Select all

#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace	scene;
using namespace video;
using namespace	io;
using namespace gui;

ISceneNode* node = 0;

class MyEventReciever : public IEventReceiver
{
public:

	virtual bool MyEventReciever::OnEvent(SEvent event);
};
ERROR 1: c:\Irrlicht\Learning Game\eventReciever.cpp(12): error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'irr::core::vector3d<T>'
with
[
T=irr::f32
]


ERROR 2: c:\Irrlicht\Learning Game\main - q3 map.cpp(18): error C2664: 'irr::createDevice' : cannot convert parameter 6 from 'MyEventReciever **__w64 ' to 'irr::IEventReceiver *'


Again, your help is appreciated.

Thanks,
-Limb
-Limb
Tyn
Posts: 932
Joined: Thu Nov 20, 2003 7:53 pm
Location: England
Contact:

Post by Tyn »

Wrong type of pointer m8

Code: Select all

MyEventReciever* reciever;
should be

Code: Select all

MyEventReciever reciever;
and you didn't call the function properly:

Code: Select all

node->getPosition
should be

Code: Select all

node->getPosition()
My advice would be to read a big C++ book before trying to start a project in C++, it seems you don't quite get the rules of the language.
Limb
Posts: 3
Joined: Fri May 14, 2004 12:44 am

Post by Limb »

I get the language, just really dont remember the whole classes and pointers thing (I learned about them, but never used them much until now so I have forgotten about them, heh). Guess I should read up on them again :? and I'm always forgetting those stupid ()'s :roll:

Well, Thanks for the help. Time to go read up on my classes.

-Limb
-Limb
Phreak
Posts: 25
Joined: Mon May 10, 2004 2:01 pm

Post by Phreak »

The parenthesis are really annoying in .NET. Back in the old VB6 they weren't required (not sure about VC++ 6, never used it), but now in Vb.NET all types of errors show up just for not adding them.

*slaps Visual Studio*

Phreak
If you don't succeed, redefine success!
Post Reply