First i'm truly sorry for the overhead here, but it looks like my only way out.
I'm trying to code tetris as a first project to learn better C++ and of course irrlicht Now i've come to the point where i want to write code to move my blocks left and right. When i use the eventreceiver, i get a segmentation fault, even when only moving the cursor inside the game window, and i really have no clue why! I already commented all my functional game code so only the structure of the program is left, but still it crashes.
One this is for sure, it's happening when MyEventreciever is called (for example when moving the mouse inside the game window). I'm running it on Ubuntu linux btw, but i don't think it has anything to do with that (i ran other irrlicht event receiver programs just fine)
What is the problem here?? Here's my code in the shortest way possible. (It does compile).
main
-------
Code: Select all
#include "playfieldMgr.h"
int main()
{
PlayfieldMgr playfieldMgr;
while(playfieldMgr.getDevice()->run())
{
if (playfieldMgr.getDevice()->isWindowActive())
{
playfieldMgr.update();
}
}
playfieldMgr.~PlayfieldMgr();
}
---------------------
Code: Select all
#ifndef PLAYFIELDMGR_H_INCLUDED
#define PLAYFIELDMGR_H_INCLUDED
#include <irrlicht.h>
#include "eventreceiver.h"
class PlayfieldMgr{
private:
irr::IrrlichtDevice *device;
irr::video::IVideoDriver* driver;
irr::scene::ISceneManager* smgr;
public:
PlayfieldMgr();
~PlayfieldMgr();
void update();
IrrlichtDevice* getDevice();
};
#endif
------------------------
Code: Select all
#include "playfieldMgr.h"
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
PlayfieldMgr::PlayfieldMgr()
{
device = createDevice( video::EDT_SOFTWARE, dimension2d<s32>(640, 480), 16, false, false, false);
MyEventReceiver receiver;
device->setEventReceiver(&receiver);
driver = device->getVideoDriver();
smgr = device->getSceneManager();
smgr->addCameraSceneNode(0, core::vector3df(50,100,-200), core::vector3df(50,100,0));
}
PlayfieldMgr::~PlayfieldMgr()
{
device->drop();
}
void PlayfieldMgr::update()
{
driver->beginScene(true, true, SColor(100,100,100,100));
smgr->drawAll();
driver->endScene();
}
IrrlichtDevice* PlayfieldMgr::getDevice()
{
return device;
}
----------------------
Code: Select all
#ifndef EVENTRECEIVER_H_INCLUDED
#define EVENTRECEIVER_H_INCLUDED
#include <irrlicht.h>
using namespace irr;
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(const SEvent&);
};
#endif
-------------------------
Code: Select all
#include "eventreceiver.h"
#include <iostream>
bool MyEventReceiver::OnEvent(const SEvent& event)
{
if (event.EventType == irr::EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown)
{
switch(event.KeyInput.Key)
{
case KEY_LEFT:
std::cout << "left\n";
case KEY_RIGHT:
std::cout << "right\n";
default:
std::cout << "smth else\n";
}
return true;
}
return false;
}