Trying to make an app with several irrlicht 1.1 windows.
It worked well with the D3D driver, but with the openGL driver the second window is empty en show the desktop.
#ifndef QIRRLICHTWIDGET_H
#define QIRRLICHTWIDGET_H
#include <qwidget.h>
#include <irrlicht.h>
#include <qfont.h>
class QPaintEvent;
class QIrrlichtWidget : public QWidget
{
Q_OBJECT
public:
irr::IrrlichtDevice* getIrrlichtDevice();
void setDriverType( irr::video::E_DRIVER_TYPE driver );
QIrrlichtWidget( QWidget *parent=0 );
~QIrrlichtWidget();
void init();
public slots:
void autoUpdateIrrlicht( irr::IrrlichtDevice* device );
signals:
void updateIrrlicht( irr::IrrlichtDevice* device );
protected:
irr::video::E_DRIVER_TYPE driverType;
virtual void paintEvent( QPaintEvent* event );
void sendKeyEventToIrrlicht( QKeyEvent* event, bool pressedDown );
virtual void keyPressEvent( QKeyEvent* event );
virtual void keyReleaseEvent( QKeyEvent* event );
void sendMouseEventToIrrlicht( QMouseEvent* event, bool pressedDown );
virtual void mousePressEvent( QMouseEvent* event );
virtual void mouseReleaseEvent( QMouseEvent* event );
virtual void wheelEvent( QWheelEvent* event );
virtual void timerEvent( QTimerEvent* event );
virtual void resizeEvent( QResizeEvent* event );
private:
irr::IrrlichtDevice* device;
};
#endif // QIRRLICHTWIDGET_H
#include "qirrlichtwidget.h"
#include <irrlicht.h>
#include <line2d.h>
#include <qpalette.h>
#include <QKeyEvent>
#include <QMouseEvent>
#include <qpainter.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
QIrrlichtWidget::QIrrlichtWidget( QWidget *parent )
: QWidget(parent)
{
// Wait for the init() call
device = 0;
// Default to Open GL
driverType = irr::video::EDT_OPENGL;
}
QIrrlichtWidget::~QIrrlichtWidget()
{
if ( device != 0 )
{
device->closeDevice();
device->run();
device->drop();
}
}
void QIrrlichtWidget::init()
{
// Don't initialize more than once!
if ( device != 0 ) return;
irr::SIrrlichtCreationParameters params;
params.DriverType = driverType;
params.WindowId = (irr::s32)winId();
params.WindowSize.Width = width();
params.WindowSize.Height = height();
device = irr::createDeviceEx( params );
// Irrlicht will clear the canvas for us, so tell Qt not to do it
setAttribute( Qt::WA_OpaquePaintEvent );
connect( this, SIGNAL(updateIrrlicht(irr::IrrlichtDevice*)),
this, SLOT(autoUpdateIrrlicht(irr::IrrlichtDevice*)) );
startTimer(0);
}
irr::IrrlichtDevice* QIrrlichtWidget::getIrrlichtDevice()
{
return device;
}
void QIrrlichtWidget::paintEvent( QPaintEvent* event )
{
if ( device != 0 )
{
emit updateIrrlicht( device );
}
}
void QIrrlichtWidget::timerEvent( QTimerEvent* event )
{
if ( device != 0 )
{
emit updateIrrlicht( device );
}
event->accept();
}
void QIrrlichtWidget::resizeEvent( QResizeEvent* event )
{
if ( device != 0 )
{
irr::core::dimension2d<int> size;
size.Width = event->size().width();
size.Height = event->size().height();
device->getVideoDriver()->OnResize( size );
irr::scene::ICameraSceneNode *cam = device->getSceneManager()->getActiveCamera();
if ( cam != 0 )
{
cam->setAspectRatio( size.Height / size.Width );
}
}
QWidget::resizeEvent(event);
}
void QIrrlichtWidget::autoUpdateIrrlicht( irr::IrrlichtDevice* device )
{
device->getTimer()->tick();
irr::video::SColor color (0,0,0,0);
device->getVideoDriver()->beginScene( true, true, color );
device->getSceneManager()->drawAll();
device->getGUIEnvironment()->drawAll();
device->getVideoDriver()->endScene();
}
struct SIrrlichtKey
{
irr::EKEY_CODE code;
wchar_t ch;
};
SIrrlichtKey convertToIrrlichtKey( int key )
{
SIrrlichtKey irrKey;
irrKey.code = (irr::EKEY_CODE)(0);
irrKey.ch = (wchar_t)(0);
// Letters A..Z and numbers 0..9 are mapped directly
if ( (key >= Qt::Key_A && key <= Qt::Key_Z) || (key >= Qt::Key_0 && key <= Qt::Key_9) )
{
irrKey.code = (irr::EKEY_CODE)( key );
irrKey.ch = (wchar_t)( key );
}
else
// Dang, map keys individually
switch( key )
{
case Qt::Key_Up:
irrKey.code = irr::KEY_UP;
break;
case Qt::Key_Down:
irrKey.code = irr::KEY_DOWN;
break;
case Qt::Key_Left:
irrKey.code = irr::KEY_LEFT;
break;
case Qt::Key_Right:
irrKey.code = irr::KEY_RIGHT;
break;
}
return irrKey;
}
void QIrrlichtWidget::sendKeyEventToIrrlicht( QKeyEvent* event, bool pressedDown )
{
irr::SEvent irrEvent;
irrEvent.EventType = irr::EET_KEY_INPUT_EVENT;
SIrrlichtKey irrKey = convertToIrrlichtKey( event->key() );
if ( irrKey.code == 0 ) return; // Could not find a match for this key
irrEvent.KeyInput.Key = irrKey.code;
irrEvent.KeyInput.Control = ((event->modifiers() & Qt::ControlModifier) != 0);
irrEvent.KeyInput.Shift = ((event->modifiers() & Qt::ShiftModifier) != 0);
irrEvent.KeyInput.Char = irrKey.ch;
irrEvent.KeyInput.PressedDown = pressedDown;
device->postEventFromUser( irrEvent );
}
void QIrrlichtWidget::keyPressEvent( QKeyEvent* event )
{
if ( device != 0 )
{
sendKeyEventToIrrlicht( event, true );
}
event->ignore();
}
void QIrrlichtWidget::keyReleaseEvent( QKeyEvent* event )
{
if ( device != 0 )
{
sendKeyEventToIrrlicht( event, false );
}
event->ignore();
}
void QIrrlichtWidget::sendMouseEventToIrrlicht( QMouseEvent* event, bool pressedDown )
{
irr::SEvent irrEvent;
irrEvent.EventType = irr::EET_MOUSE_INPUT_EVENT;
switch ( event->button() )
{
case Qt::LeftButton:
irrEvent.MouseInput.Event = pressedDown? irr::EMIE_LMOUSE_PRESSED_DOWN:irr::EMIE_LMOUSE_LEFT_UP;
break;
case Qt::MidButton:
irrEvent.MouseInput.Event = pressedDown? irr::EMIE_MMOUSE_PRESSED_DOWN:irr::EMIE_MMOUSE_LEFT_UP;
break;
case Qt::RightButton:
irrEvent.MouseInput.Event = pressedDown? irr::EMIE_RMOUSE_PRESSED_DOWN:irr::EMIE_RMOUSE_LEFT_UP;
break;
default:
return; // Cannot handle this mouse event
}
irrEvent.MouseInput.X = event->x();
irrEvent.MouseInput.Y = event->y();
irrEvent.MouseInput.Wheel = 0.0f; // Zero is better than undefined
device->postEventFromUser( irrEvent );
}
void QIrrlichtWidget::mousePressEvent( QMouseEvent* event )
{
if ( device != 0 )
{
sendMouseEventToIrrlicht( event, true );
}
event->ignore();
}
void QIrrlichtWidget::mouseReleaseEvent( QMouseEvent* event )
{
if ( device != 0 )
{
sendMouseEventToIrrlicht( event, false );
}
event->ignore();
}
void QIrrlichtWidget::wheelEvent( QWheelEvent* event )
{
if ( device != 0 && event->orientation() == Qt::Vertical )
{
irr::SEvent irrEvent;
irrEvent.EventType = irr::EET_MOUSE_INPUT_EVENT;
irrEvent.MouseInput.Event = irr::EMIE_MOUSE_WHEEL;
irrEvent.MouseInput.X = 0; // We don't know these,
irrEvent.MouseInput.Y = 0; // but better zero them instead of letting them be undefined
irrEvent.MouseInput.Wheel = event->delta() / 120.0f;
device->postEventFromUser( irrEvent );
}
event->ignore();
}
#include <qstring.h>
#include <qapplication.h>
#include <qpushbutton.h>
#include <QVBoxLayout>
#include "qirrlichtwidget.h"
#include <irrlicht.h>
using namespace irr;
void setupIrrlicht( IrrlichtDevice* device )
{
// Get the scene manager
scene::ISceneManager* manager = device->getSceneManager();
// Create a small box
scene::ISceneNode* node = manager->addTestSceneNode();
node->setMaterialFlag( video::EMF_LIGHTING, false );
// Create a camera to view the box
scene::ICameraSceneNode* cam = manager->addCameraSceneNode();
cam->setPosition( core::vector3df(100,100,0) );
cam->setTarget( core::vector3df(0,0,0) );
scene::ISceneNodeAnimator* anim = manager->createFlyCircleAnimator( core::vector3df(0,0,0), 20 );
node->addAnimator(anim);
anim->drop();
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QWidget window;
QWidget window1;
QVBoxLayout layout;
QPushButton quit("Quit");
QObject::connect( &quit, SIGNAL(clicked()), &app, SLOT(quit()) );
QIrrlichtWidget irrWidget;
QIrrlichtWidget irrWidget1;
layout.addWidget(&quit);
layout.addWidget(&irrWidget);
layout.addWidget(&irrWidget1);
window.setLayout(&layout);
window.resize(400,400);
window.show();
irrWidget.init();
irrWidget1.init();
setupIrrlicht( irrWidget.getIrrlichtDevice() );
setupIrrlicht( irrWidget1.getIrrlichtDevice() );
return app.exec();
}
wglMakeCurent(...)beginScene(...)virtual void keyPressEvent ( QKeyEvent * event )
virtual void keyReleaseEvent ( QKeyEvent * event )
virtual void mousePressEvent ( QMouseEvent * event )
virtual void mouseReleaseEvent ( QMouseEvent * event )
and
virtual void mouseMoveEvent ( QMouseEvent * event )params.WindowId = (void*)winId();undefined reference to `vtable for QIrrlichtWidget'|
QIrrlichtWidget::QIrrlichtWidget( QWidget *parent )
: QWidget(parent)
{
// Wait for the init() call
device = 0;
// Default to Open GL
driverType = irr::video::EDT_OPENGL;
}Escen wrote:Aha... I forgot to use QtWorkbench custom Makefile for CodeBlocks, compiling fine now...
That's what happens when you getting older.
Cheers

Users browsing this forum: No registered users and 1 guest