Page 1 of 2

Using a function...

Posted: Wed Jun 14, 2006 8:23 pm
by mR.haHN
Hi again,
I´ve got a problem with using a function to make my code more structured.

now this is (a part) of my code:

Code: Select all

void RTS_camera()
{
  position2d<f32> p = device->getCursorControl()->getRelativePosition();
                        
   if(p.X <= 0.1)
          {
           vector3df pos = camera->getPosition();
           pos.Z = pos.Z + 5;
          camera->setPosition(pos);
            }
}
So now I call RTS_camera() in my while loop. The program starts, but as soon as I move the cursor to the left side, the program crashes and it sais "Demo.exe has detected a problem and needs to be shut down"(or so, my Windows is German :wink: )

Have I done anything wrong or what is the matter for this?


EDIT: I must say that if I copy the code in the while loop directly, it works.

Posted: Wed Jun 14, 2006 8:31 pm
by Baal Cadar
Errm, there is this little thingie called "debugger". How about using it?
If I had to guess, I'd say either device or camera or both are uninitialised.
But you are the only one, who can see if this is true: with using the debugger.

Posted: Wed Jun 14, 2006 8:36 pm
by mR.haHN
Eeerrrrm ( :wink: ) the debugger is definitly activated.....and the camera IS initialisated, because if I start my program, I see my Terrain, but as soon as I want to move the camera through this function this error occurs.

Posted: Wed Jun 14, 2006 8:40 pm
by TheGameMaker
maybe, that the device is initalised, but the function doesn´t know that??
may it be, that the device is is set in the mainloop??
could you show the whole code??

Posted: Wed Jun 14, 2006 8:41 pm
by hybrid
:lol: There a difference between 'using a debugger' and 'compiling in debug mode' where only the first will definitely help and you did the latter.
The error you encounter is simply a crash, and in almost 101% of the situations this is due to a null pointer usage just as Baal Cadar said.
But just in case you have the debug mode enabled: Press the button that says 'Debug application' - that'll start the debugger :wink:

Posted: Wed Jun 14, 2006 8:41 pm
by Baal Cadar
So still: Why not use the debugger then? Modern debuggers have the ability to let you step through the code, instruction by instruction. Use it!
What's the stack trace?

Posted: Wed Jun 14, 2006 8:54 pm
by mR.haHN
OK, I just tried to use the debugger (I use Dev-C++ :D ) and it opens the irrlicht window, and then stops. If I then click on "On step forward" the programm just crashes, but without error message, it just doesnt react anymore and I have to close it with the task manager.

Game Maker: Yes, it was like that, but I just put this:

Code: Select all

MyEventReceiver receiver;

IrrlichtDevice* device = createDevice(video::EDT_OPENGL, core::dimension2d<s32>(640,480),32, false, true,false, &receiver);
between the event receiver and the function (receiver upwards, function downwards). And still the same error occurs. :'(

Posted: Wed Jun 14, 2006 8:57 pm
by Baal Cadar
It stops where?

Posted: Wed Jun 14, 2006 8:59 pm
by TheGameMaker
than, would you be so kind, and show us the whole text??

*edit*

you dont use comments like //thisisanicecomment\\ or??
cause \\ comment the next line of code without showing it in blue...
(dev c++)

Posted: Wed Jun 14, 2006 9:01 pm
by mR.haHN
It stops when the loading part is ready (loading of the terrain heightmap and so on)

Oh miracle, the Debugger talks to me :lol: I just waited a bit while the program was stuck, and now the debugger says: "Your programm has caused an access violation" and this line is marked:

Code: Select all

vector3df pos = camera->getPosition();

EDIT: You want me to post the whole code? Ok then:

Code: Select all

#include <irrlicht.h>
#include <iostream.h>







using namespace irr;

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

#pragma comment(lib, "Irrlicht.lib")

IAnimatedMeshSceneNode* node = 0;
IrrlichtDevice* device = 0;
ICameraSceneNode* camera =0;

float mwheel;




class MyEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(SEvent event)
	{
		
	switch(event.EventType)
            {
                case EET_MOUSE_INPUT_EVENT:
                {
                    switch(event.MouseInput.Event)
                    {
                        case EMIE_MOUSE_WHEEL:
                        {
                           mwheel = event.MouseInput.Wheel; 
                            }
                            }
                            }
                            } 
		
		
		

		return false;
	}
};

void RTS_camera()
{
       position2d<f32> p = device->getCursorControl()->getRelativePosition();
                        
                        if(p.X <= 0.1)
                        {
                               vector3df pos = camera->getPosition();
                               pos.Z = pos.Z + 5;
                               camera->setPosition(pos);
                               
                              
                        }
                        if(p.X >= 0.9)
                        {
                               vector3df pos = camera->getPosition();
                               pos.Z = pos.Z - 5;
                               camera->setPosition(pos);
                        }
                        
                        if(p.Y <= 0.1)
                        {
                               vector3df pos = camera->getPosition();
                               pos.X = pos.X + 5;
                               camera->setPosition(pos);
                               
                              
                        }
                        if(p.Y >= 0.9)
                        {
                               vector3df pos = camera->getPosition();
                               pos.X = pos.X - 5;
                               camera->setPosition(pos);
                        }
                        
                        if(mwheel == 1)
                        {      vector3df pos = camera->getPosition();
                               pos.Y = pos.Y - 50;
                               camera->setPosition(pos);
                               mwheel = 0;
                        }
                        
                        if(mwheel == -1)
                        {      vector3df pos = camera->getPosition();
                               pos.Y = pos.Y + 50;
                               camera->setPosition(pos);
                               mwheel = 0;
                        }
                        }




int main()
{

    MyEventReceiver receiver;  
    
    

    device = createDevice(video::EDT_OPENGL, core::dimension2d<s32>(640,480),32, false, true,false, &receiver);
    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();
    
    
    
    ICameraSceneNode *camera;
    camera = smgr->addCameraSceneNodeFPS (0, 100, 500);
    camera->setInputReceiverEnabled (false); 
    

	camera->setPosition(core::vector3df(0,1000,0));
	camera->setRotation(vector3df(90.0f,0.0f,0.0f));
	camera->setFarValue(12000.0f);
    
    
    scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode("media/terrain-heightmap.bmp");

	terrain->setScale(core::vector3df(40, 4.4f, 40));
	terrain->setMaterialFlag(video::EMF_LIGHTING, false);

	terrain->setMaterialTexture(0, driver->getTexture("media/terrain-texture.jpg"));
	terrain->setMaterialTexture(1, driver->getTexture("media/detailmap3.jpg"));
	
	terrain->setMaterialType(video::EMT_DETAIL_MAP);

	terrain->scaleTexture(1.0f, 20.0f);
                       
    
    while(device->run())
    {      
                              
                        
                       
                        RTS_camera();
                         
                         
                        
                        driver->beginScene(true,true,SColor(255,0,0,140));
                        smgr->drawAll();
                        device->getGUIEnvironment()->drawAll();
                        driver->endScene();
                        
                        
}
device->drop();

	return 0;
}


Posted: Wed Jun 14, 2006 9:05 pm
by Baal Cadar
This means camera is uninitialised. Step through your code. the code you pasted looks made-up, not like the code you're actually using. Please only post the actual code you're running not some mock-up. It is extremely frustrating to remote debug such.

If this is indeed the real code: Don't. Don't use top-level functions with global variables. This begs for making errors.

Posted: Wed Jun 14, 2006 9:10 pm
by Baal Cadar
Indeed. You have two variables camera here. One local camera to the main function and one global one. The local one overlaps (Klingt doof. Das Deutsche Wort ist "überdeckt") the global one.

You have a local camera in main, which has nothing to do with the global one.
This is hideous style and you now experience one of the myriads of reasons to not use globals.

Assign the newly created camera to the global camera variable and not the local one.

Posted: Wed Jun 14, 2006 9:11 pm
by hybrid
The problem is that camera is used twice. One is the global variable, also used in the EventReceiver, and never initialised. Second one in main proc is initialised, but is not propagated to the event receiver.

Posted: Wed Jun 14, 2006 9:13 pm
by mR.haHN
Uhm, do you mean that I shouldnt declare my vars in initialization, but in the main function itself?

Anyway, I tried to initalizate my camera before the RTS_camera function. I just put the part from the bottom in main up where at the moment is camera=0. But he tells me that he expects a con- or destructor or a type conversion. Well actually I did use a constructor: ICameraSceneNode*....

Posted: Wed Jun 14, 2006 9:16 pm
by mR.haHN
Ahhh....that was the problem.... ok now it works, thanks for your patience
with me... :wink: