I need help debugging my program.

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
Stu L Tissimus
Posts: 48
Joined: Thu Jul 15, 2004 8:45 pm
Location: Manhattan
Contact:

I need help debugging my program.

Post by Stu L Tissimus »

I need a little help debugging my program. This is my first real experience debugging, so.... Meh. Anyway, Dev-C++ says that the problem is an access violation/segmentation fault, in the "receiver = new MyEventReceiver;" line in VideoCore.cpp.

When I previewed the message, all the code tags after the first one didn't work, so it might not appear correctly.

main.cpp

Code: Select all

#include <iostream>
#include <fstream>
#include <vector>

#include "MyEventReceiver.h"
#include "GameCore.h"

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

using namespace irr;

int main() {
    /*
    GameCore MyCore;
    MyCore.InitDevice();
    IrrlichtDevice *device = MyCore.device;
    device->getSceneManager();
    video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();
	
    device->getFileSystem()->addZipFileArchive("map-20kdm2.pk3");
	
	scene::IAnimatedMesh* mesh = smgr->getMesh("20kdm2.bsp");
	scene::IAnimatedMeshSceneNode* node;
	scene::IAnimatedMesh* mesh2 = smgr->getMesh("Soldier9.3ds");
	scene::IAnimatedMeshSceneNode* node2;
	
	scene::ISceneNode* camera = smgr->addCameraSceneNodeFPS();
	scene::ISceneNode* camera2 = smgr->addCameraSceneNodeFPS();
	int hello = MyCore.receiver.xplane;

	if (mesh)
	    node = smgr->addAnimatedMeshSceneNode(mesh, camera, -1, core::vector3df(10, 0, 0));
	    node2 = smgr->addAnimatedMeshSceneNode(mesh2, camera, -1, core::vector3df(500, 0, 0));}
	int lastFPS = -1;


	while(device->run())
	{
        driver->beginScene ( true, true, video::SColor ( 0, 0, 0, 0 ) );
        smgr->drawAll ( );
        driver->endScene ( ); 
		int fps = driver->getFPS();
		if (lastFPS != fps)
		{
			core::stringw str = L"Irrlicht Engine - Vertex and pixel shader example [";
			str += driver->getName();
			str += "] FPS:";
			str += fps;

			device->setWindowCaption(str.c_str());
			lastFPS = fps;
		}
		
		
	}
	device->drop();
	*/
	GameCore* myCore;
	myCore->videoCore->Init();
	int hello;
	cin >> hello;
	if (hello = 1) {
	    return 0;}
	
}


MyEventReceiver.h

Code: Select all

#ifndef __MYEVENTRECEIVER_H__
#define __MYEVENTRECEIVER_H__
#include "Irrlicht.h"
//#include "GameCore.h"
#include <iostream>

using namespace std;
using namespace irr;

const int MOUSE_BUTTON_CODE_COUNT = 3;

class MyEventReceiver : public IEventReceiver {
private:
   core::vector2di m_mouseLocation;
   bool            m_mouseMoved;
   f32             m_mouseWheelValue;
   bool            m_key_buffer[KEY_KEY_CODES_COUNT];
   bool            m_key_buffer_toggle[KEY_KEY_CODES_COUNT];
   bool            m_mouse_buffer[MOUSE_BUTTON_CODE_COUNT];
   bool            m_mouse_buffer_toggle[MOUSE_BUTTON_CODE_COUNT]; 
   //scene::IAnimatedMeshSceneNode* currentNode;
    


public:         
    /*
    void setCurrentNode(scene::IAnimatedMeshSceneNode* setCurrentNodeNode) {
        currentNode = setCurrentNodeNode; 
    }    
    */
    
    virtual bool OnEvent(SEvent event) {
    switch ( event.EventType ) {
        
       case EET_KEY_INPUT_EVENT: {
             m_key_buffer[event.KeyInput.Key] = event.KeyInput.PressedDown;
             
             switch (event.KeyInput.Key) {
                 //case KEY_KEY_W: cout << "whoamg its W, lol" << endl; break;
                 case 65: cout << "y helo thar A" << endl; break;
                 case 83: cout << "O_O S" << endl; break;
                 case 68: cout << "I'm D. D for YOU!" << endl; break;

                 case KEY_KEY_W: 
                    cout << "W" << endl;
             }
                 
             return true; }
             
       case EET_MOUSE_INPUT_EVENT:{
             if ( event.MouseInput.Event == EMIE_MOUSE_WHEEL )
                m_mouseWheelValue = event.MouseInput.Wheel;
    
             if ( event.MouseInput.Event == EMIE_MOUSE_MOVED ) {
                m_mouseLocation.X = event.MouseInput.X;
                m_mouseLocation.Y = event.MouseInput.Y;
                m_mouseMoved = true; }
    
             if ( event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN )
                m_mouse_buffer[EMIE_LMOUSE_PRESSED_DOWN] = true;
             else if ( event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP )
                m_mouse_buffer[EMIE_LMOUSE_PRESSED_DOWN] = false;
    
             if ( event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN  )
                m_mouse_buffer[EMIE_RMOUSE_PRESSED_DOWN ] = true;
             else if ( event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP )
                m_mouse_buffer[EMIE_RMOUSE_PRESSED_DOWN] = false;
    
             if ( event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN  )
                m_mouse_buffer[EMIE_MMOUSE_PRESSED_DOWN ] = true;
             else if ( event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP )
                m_mouse_buffer[EMIE_MMOUSE_PRESSED_DOWN] = false;
    
             return true;
          }
       } 

      return false;
   }
};


#endif // __MYEVENTRECEIVER_H__


VideoCore.h

Code: Select all

#ifndef __VIDEOCORE_H__
#define __VIDEOCORE_H__

#include "Irrlicht.h"
#include "MyEventReceiver.h"
using namespace irr;

class VideoCore
{
    private:
           IrrlichtDevice*         device;
           scene::ISceneManager*   smgr;
           video::IVideoDriver*    driver;
           gui::IGUIEnvironment*   guiEnvironment;
           MyEventReceiver*        receiver;
           
           video::E_DRIVER_TYPE    deviceType;
           bool                    fullscreen;
           core::dimension2d<s32>  windowSize;
           
           
           
    public:
           VideoCore();
          ~VideoCore();
           bool Init();
                   
           IrrlichtDevice*         GetDevice         ( ) { return device; }
           video::IVideoDriver*    GetVideoDriver    ( ) { return driver; }
           scene::ISceneManager*   GetSceneManager   ( ) { return smgr; }
           gui::IGUIEnvironment*   GetGUIEnvironment ( ) { return guiEnvironment; }
           MyEventReceiver*        GetReceiver       ( ) { return receiver; }
           
           core::rect<s32>         GetViewPort       ( ) { return driver->getViewPort ( ); }
           int                     GetViewPortWidth  ( ) { return driver->getViewPort ( ).getWidth ( ); }
           int                     GetViewPortHeight ( ) { return driver->getViewPort ( ).getHeight ( ); }
           
           void SetDeviceType      ( video::E_DRIVER_TYPE deviceType )   { deviceType = deviceType; }
           void SetWindowSize      ( core::dimension2d<s32> windowSize ) { windowSize = windowSize; }
           void SetFullscreen      ( bool fullscreen )                   { fullscreen = fullscreen; }
};


#endif


VideoCore.cpp

Code: Select all

#include "VideoCore.h"
#include "MyEventReceiver.h"
#include "Irrlicht.h"
using namespace irr;

// VideoCore Constructor
VideoCore::VideoCore ( ) 
{
    device =         NULL;
    smgr =           NULL;
    driver =         NULL;
    guiEnvironment = NULL;
    receiver =       NULL;
    
    deviceType =     video::EDT_OPENGL;
    fullscreen =     false;
    windowSize =     core::dimension2d<s32> ( 1024, 768 ); 
}

// VideoCore Destructor 
VideoCore::~VideoCore ( )
{
   if ( receiver )
      delete receiver;
}

// VideoCore Initialization
bool VideoCore::Init()
{
    receiver = new MyEventReceiver;
    device = createDevice(deviceType, windowSize, 32, fullscreen, false, false, receiver);
    
    if ( device )
    {
        smgr = device->getSceneManager ( );
        driver = device->getVideoDriver ( );
        guiEnvironment = device->getGUIEnvironment ( );
        return true;
    }
    
    return false;    
}    


GameCore.h

Code: Select all

#ifndef __GAMECORE_H__
#define __GAMECORE_H__

#include "Irrlicht.h"
#include <iostream>
#include "VideoCore.h"
using namespace std;
using namespace irr;

class GameCore {
        public:
            VideoCore* videoCore;
};

#endif
Laptop: P-M 1.6GHz|1024MB PC2800|Mob.Radeon9700Pro|60GB

Desktop: A-XP 2400+|512MB PC3200|Radeon 9700 Pro|180GB
Stu L Tissimus
Posts: 48
Joined: Thu Jul 15, 2004 8:45 pm
Location: Manhattan
Contact:

Post by Stu L Tissimus »

Bump - FOR GREAT JUSTICE!
Laptop: P-M 1.6GHz|1024MB PC2800|Mob.Radeon9700Pro|60GB

Desktop: A-XP 2400+|512MB PC3200|Radeon 9700 Pro|180GB
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

try changing the line

GameCore* myCore;

to

GameCore* myCore = new GameCore;

in main.cpp
Stu L Tissimus
Posts: 48
Joined: Thu Jul 15, 2004 8:45 pm
Location: Manhattan
Contact:

Post by Stu L Tissimus »

Nope ;_;

Good try, though.
Laptop: P-M 1.6GHz|1024MB PC2800|Mob.Radeon9700Pro|60GB

Desktop: A-XP 2400+|512MB PC3200|Radeon 9700 Pro|180GB
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

Well, was trying to get you to see the other problem, but, you also have to create the VideoCore object before calling init. GameCore only holds a pointer to the VideoCore object, so do this in main.cpp -

Code: Select all

GameCore* myCore = new GameCore ( );
myCore->videoCore = new VideoCore ( );
myCore->videoCore->Init ( ); 

int hello; 
cin >> hello; 

if (hello = 1)
{
     return 0;
}
That'll make it work, but I would set it up so that the GameCore class allocates and manages it's own videoCore object. If you don't understand what I mean, ask away! :)

Good work on getting that framework setup, you're on a roll! :D
Post Reply