Although I know about the Irrlicht engine many years now, recently I decided to play with it. Because I think that I have good nowledge of the wxWidgets library I decided to built a project as a starting point for my needs.
I am sure that there are very good and experienced coders here. I believe that the following sources will help some of you to start with these wonderfull libraries:
The header file
Code: Select all
#include <irrlicht.h>
#include <wx/wx.h>
typedef irr::SIrrlichtCreationParameters wxIrrCreateParam;
typedef irr::video::E_DRIVER_TYPE wxIrrDriverType;
typedef irr::IrrlichtDevice wxIrrDevice;
typedef irr::scene::ISceneManager wxIrrSceneManager;
typedef irr::video::IVideoDriver wxIrrVideoDriver;
typedef irr::scene::ICameraSceneNode wxIrrCamera;
typedef irr::scene::ISceneNode wxIrrNode;
typedef irr::video::SColor wxIrrColor;
typedef irr::core::dimension2d<irr::s32> wxIrrDimension2D;
typedef irr::core::vector3df wxIrrVector3DFloat;
typedef irr::s32 wxIrrS32;
typedef irr::video::E_MATERIAL_FLAG wxIrrMaterialFlag;
class CIrrApp;
class CIrrDevice;
class CIrrWindow;
class CIrrFrame;
int MyCube=1;
enum
{
ID_Quit = 1,
ID_About,
};
class CIrrApp : public wxApp
{
public:
virtual bool OnInit(void);
};
class CIrrDevice
{
public:
CIrrDevice(CIrrWindow *window, wxIrrDriverType type=irr::video::EDT_OPENGL, bool bResizeable=true);
~CIrrDevice();
inline wxIrrDevice *GetHandle(void) { return irrDevice; }
inline wxIrrSceneManager *GetSceneManager(void) { return irrDevice ? irrDevice->getSceneManager() : NULL; }
inline wxIrrVideoDriver *GetVideoDriver(void) { return irrDevice ? irrDevice->getVideoDriver() : NULL; }
wxIrrCamera *AddCamera(wxIrrNode *parent=0, wxIrrVector3DFloat &position=wxIrrVector3DFloat(0.0f,0.0f,0.0f), wxIrrVector3DFloat &lookat=wxIrrVector3DFloat(0.0f,0.0f,1.0f), wxIrrS32 id=1);
protected:
wxIrrDevice *irrDevice;
};
class CIrrWindow : public wxWindow
{
public:
CIrrWindow(wxWindow *parent, wxWindowID id, long style=wxTAB_TRAVERSAL);
~CIrrWindow();
void OnPaint(wxPaintEvent &event);
void OnSize(wxSizeEvent &event);
void OnTimer(wxTimerEvent &event);
void OnSceneUpdate(void);
inline wxIrrSceneManager *GetSceneManager(void) { return irrDevice ? irrDevice->GetSceneManager() : NULL; }
inline wxIrrVideoDriver *GetVideoDriver(void) { return irrDevice ? irrDevice->GetVideoDriver() : NULL; }
inline void SetDevice(CIrrDevice *irrdevice) { irrDevice = irrdevice; }
inline void SetCamera(wxIrrCamera *irrcam) { irrCameraCurrent = irrcam; }
wxIrrCamera *AddCamera(wxIrrNode *parent=0, wxIrrVector3DFloat &position=wxIrrVector3DFloat(0.0f,0.0f,0.0f), wxIrrVector3DFloat &lookat=wxIrrVector3DFloat(0.0f,0.0f,1.0f), wxIrrS32 id=1, bool bsetcurrent=true);
DECLARE_EVENT_TABLE()
protected:
CIrrDevice *irrDevice;
wxIrrCamera *irrCameraCurrent;
private:
float fRatio;
wxTimer m_Timer;
};
class CIrrFrame : public wxFrame
{
public:
CIrrFrame(const wxString &title, const wxPoint &pos, const wxSize &size, long style=wxDEFAULT_FRAME_STYLE);
void OnQuit(wxCommandEvent &event);
void OnAbout(wxCommandEvent &event);
DECLARE_EVENT_TABLE()
protected:
CIrrDevice *device3D;
wxIrrCamera *irrCam;
CIrrWindow *window3D;
};
Code: Select all
#include "wxIrrHello.h"
IMPLEMENT_APP(CIrrApp)
bool CIrrApp::OnInit()
{
CIrrFrame *frame = new CIrrFrame( _T("wxIrrlicht: Hello World"), wxPoint(50,50), wxSize(450,340) );
frame->Show(TRUE);
SetTopWindow(frame);
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////
CIrrDevice::CIrrDevice(CIrrWindow *window, wxIrrDriverType type, bool bResizeable)
{
wxIrrCreateParam param;
param.WindowId = reinterpret_cast<void *>( (HWND)window->GetHandle() );
param.DriverType = type;
irrDevice = irr::createDeviceEx(param);
window->SetDevice( this );
if ( irrDevice )
irrDevice->setResizeAble( bResizeable );
}
CIrrDevice::~CIrrDevice()
{
if ( irrDevice )
{
irrDevice->closeDevice();
irrDevice->drop();
}
}
wxIrrCamera *CIrrDevice::AddCamera(wxIrrNode *parent, wxIrrVector3DFloat &position, wxIrrVector3DFloat &lookat, wxIrrS32 id)
{
wxIrrSceneManager *irrSceneMgr=GetSceneManager();
if (irrSceneMgr)
return (wxIrrCamera *)irrSceneMgr->addCameraSceneNode( parent, position, lookat );
else
return NULL;
}
///////////////////////////////////////////////////////////////////////////////
BEGIN_EVENT_TABLE(CIrrWindow, wxWindow)
EVT_PAINT(CIrrWindow::OnPaint)
EVT_SIZE(CIrrWindow::OnSize)
EVT_TIMER( wxID_ANY, CIrrWindow::OnTimer)
END_EVENT_TABLE()
CIrrWindow::CIrrWindow(wxWindow *parent, wxWindowID id, long style) : wxWindow( parent, id, wxDefaultPosition, wxDefaultSize, style ), m_Timer(this)
{
irrDevice = NULL;
irrCameraCurrent = NULL;
m_Timer.Start(1000/60);
}
CIrrWindow::~CIrrWindow()
{
}
void CIrrWindow::OnPaint(wxPaintEvent &event)
{
wxPaintDC dc;
if ( irrDevice )
{
wxIrrVideoDriver *driver=irrDevice->GetVideoDriver();
wxIrrSceneManager *scenemgr=irrDevice->GetSceneManager();
if ( driver )
{
driver->beginScene(true, true, wxIrrColor(0,0,0,0) );
if ( scenemgr )
scenemgr->drawAll();
driver->endScene();
}
}
event.Skip();
}
void CIrrWindow::OnSize(wxSizeEvent &event)
{
float w=GetClientSize().GetWidth();
float h=GetClientSize().GetHeight();
fRatio = (float)w/(float)h;
if ( irrDevice )
{
wxIrrVideoDriver *driver=irrDevice->GetVideoDriver();
if ( driver )
{
driver->OnResize( wxIrrDimension2D(w, h) );
if ( irrCameraCurrent )
irrCameraCurrent->setAspectRatio( fRatio );
if ( !m_Timer.IsRunning() )
Update();
}
}
event.Skip();
}
void CIrrWindow::OnTimer(wxTimerEvent &event)
{
OnSceneUpdate();
Refresh( false );
event.Skip();
}
//
// Do the update of your scene here
//
void CIrrWindow::OnSceneUpdate(void)
{
wxIrrSceneManager *scene=GetSceneManager();
if ( scene )
{
wxIrrNode *cube=scene->getSceneNodeFromId( MyCube );
if (cube)
{
wxIrrVector3DFloat yaw=cube->getRotation();
cube->setRotation( wxIrrVector3DFloat(0.0f, yaw.Y+1.0f, 0.0f) );
}
}
}
wxIrrCamera *CIrrWindow::AddCamera(wxIrrNode *parent, wxIrrVector3DFloat &position, wxIrrVector3DFloat &lookat, wxIrrS32 id, bool bsetcurrent)
{
wxIrrCamera *newcamera=NULL;
if (irrDevice)
{
newcamera = irrDevice->AddCamera(parent, position, lookat, id);
if ( bsetcurrent )
irrCameraCurrent = newcamera;
}
return newcamera;
}
///////////////////////////////////////////////////////////////////////////////
BEGIN_EVENT_TABLE(CIrrFrame, wxFrame)
EVT_MENU(ID_Quit, CIrrFrame::OnQuit)
EVT_MENU(ID_About, CIrrFrame::OnAbout)
END_EVENT_TABLE()
CIrrFrame::CIrrFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style) : wxFrame((wxFrame *)NULL, -1, title, pos, size, style)
{
this->SetSizeHints( wxDefaultSize, wxDefaultSize );
wxBoxSizer* bSizer1;
bSizer1 = new wxBoxSizer( wxVERTICAL );
window3D = new CIrrWindow( this, wxID_ANY );
bSizer1->Add( window3D, 1, wxEXPAND, 5 );
this->SetSizer( bSizer1 );
this->Layout();
wxMenu *menuFile = new wxMenu;
menuFile->Append( ID_About, _T("&About...") );
menuFile->AppendSeparator();
menuFile->Append( ID_Quit, _T("E&xit") );
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, _T("&File") );
SetMenuBar( menuBar );
CreateStatusBar();
SetStatusText( _T("Welcome to wxIrrlich!") );
device3D = new CIrrDevice( window3D );
window3D->AddCamera();
wxIrrNode *cube=window3D->GetSceneManager()->addCubeSceneNode( 1.0f, 0, MyCube );
cube->setMaterialTexture(0, window3D->GetVideoDriver()->getTexture("../media/crate.jpg"));
cube->setMaterialFlag( irr::video::EMF_LIGHTING, false );
cube->setPosition( wxIrrVector3DFloat(0.0f, 0.0f, 3.0f) );
}
void CIrrFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(TRUE);
}
void CIrrFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox( _T("This is a wxIrrlicht Hello world sample"), _T("About Hello World"), wxOK|wxICON_INFORMATION, this );
}