[原创]IrrLicht的GUI使用
生活随笔
收集整理的這篇文章主要介紹了
[原创]IrrLicht的GUI使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
irrlicht有自己的ui系統,不用再去找其他的ui系統來掛載了.下面介紹一下irrlicht UI系統的基本使用方法.我用一個hello world的工程來講解.因為代碼量并不多,就將所有的代碼都貼出來了. #include <windows.h>
#include <irrlicht.h> //頭函數,因為將調用GetWindowsDirectory()函數獲得系統目錄所以包含了<windows.h> using namespace irr; using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui; //名字空間,不用每次都用irr::等來聲明 IGUIEditBox *edtName = 0;
IGUIButton *pbtn = 0;
CGUITTFont *pFont;
IGUIListBox* listbox = 0;
IGUISkin* skin;
IrrlichtDevice *Device; //用到的幾個全局變量,CGUITTFont是從IGUIFont派生出的類,非irrlicht自帶.用于中文支持.不用中文的話,定義為IGUIFont即可 class MyEventReceiver : public IEventReceiver
{
public:
?virtual bool OnEvent(SEvent event)
?{
??if (event.EventType == EET_GUI_EVENT)
??{
???s32 id = event.GUIEvent.Caller->getID();
???switch(event.GUIEvent.EventType)
???{???
???case EGET_BUTTON_CLICKED://這里只處理了按鈕點擊的消息
????if (id == 101)
????{?????
?????listbox->addItem(edtName->getText()); //點發送按鈕時,把editbox里的內容加到listbox中;
?????return true;
????}???
????break;???
???}
??}
??return false;
?}
}; //自定義的消息處理類,重載了OnEvent()函數,demo將用這個類來出理消息.注意后面會調用一個setEventReceiver()函數來設定消息處理類. int main()
{ Device =createDevice( video::EDT_DIRECT3D9, dimension2d<s32>(640,480), 16,false, false, false, 0); //main函數開始,創設備. Device->setWindowCaption(L"鬼火引擎,第一個例子"); IVideoDriver* driver = Device->getVideoDriver();
ISceneManager* smgr = Device->getSceneManager();
IGUIEnvironment* guienv = Device->getGUIEnvironment();
smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0)); //添加了一個攝像機,位置和觀察點分別為(0,30,-40),(0,5,0). MyEventReceiver receiver;
?Device->setEventReceiver(&receiver); //指定消息處理類 skin = guienv->getSkin();
c8 tmp[512];
GetWindowsDirectory(tmp,511);
strcat(tmp,"\\fonts\\simhei.ttf");
pFont = (CGUITTFont *)guienv->getFont(tmp,15);//獲得ttf字體
skin->setFont(pFont);//設置字體 //得到系統目錄fonts下的simhei.ttf字體,并設置為當前使用的字體. edtName = guienv->addEditBox(L"歲月無聲",rect<s32>(350,400,530,430));
edtName->setOverrideColor(SColor(0,0,0,255)); //添加一個EditBox,并將字體顏色設成藍色.也可以這樣調用類指定自己的字體: edtName->setOverrideFont(pFont),pFont要另行加載. pbtn = guienv->addButton(rect<s32>(540,400,590,430), 0, 101, L"發送"); listbox = guienv->addListBox(rect<s32>(350, 300, 590, 380)); //添加了一個按鈕和一個列表框 IAnimatedMesh* mesh = smgr->getMesh("../../media/sydney.md2");
?IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh ); if (node)
?{
??node->setMaterialFlag(EMF_LIGHTING, false);
??node->setMD2Animation ( scene::EMAT_STAND );
??node->setMaterialTexture( 0, driver->getTexture("../../media/sydney.bmp") );
?} //場景太單調了,還是留下原來的這個md2 "美女"模型 while(Device->run())
?{
??/*
??Anything can be drawn between a beginScene() and an endScene()
??call. The beginScene clears the screen with a color and also the
??depth buffer if wanted. Then we let the Scene Manager and the
??GUI Environment draw their content. With the endScene() call
??everything is presented on the screen.
??*/
??driver->beginScene(true, true, SColor(255,100,102,140)); smgr->drawAll();
??guienv->drawAll();
??driver->endScene();
?} //游戲循環 Device->drop(); return 0;
} //main函數結束. 呵呵,挺簡單的吧,irrlicht好像還有ui編輯器哦,這樣就不用我們自己算坐標了,用起來就更方便了.好了,看看我們的成果吧.
#include <irrlicht.h> //頭函數,因為將調用GetWindowsDirectory()函數獲得系統目錄所以包含了<windows.h> using namespace irr; using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui; //名字空間,不用每次都用irr::等來聲明 IGUIEditBox *edtName = 0;
IGUIButton *pbtn = 0;
CGUITTFont *pFont;
IGUIListBox* listbox = 0;
IGUISkin* skin;
IrrlichtDevice *Device; //用到的幾個全局變量,CGUITTFont是從IGUIFont派生出的類,非irrlicht自帶.用于中文支持.不用中文的話,定義為IGUIFont即可 class MyEventReceiver : public IEventReceiver
{
public:
?virtual bool OnEvent(SEvent event)
?{
??if (event.EventType == EET_GUI_EVENT)
??{
???s32 id = event.GUIEvent.Caller->getID();
???switch(event.GUIEvent.EventType)
???{???
???case EGET_BUTTON_CLICKED://這里只處理了按鈕點擊的消息
????if (id == 101)
????{?????
?????listbox->addItem(edtName->getText()); //點發送按鈕時,把editbox里的內容加到listbox中;
?????return true;
????}???
????break;???
???}
??}
??return false;
?}
}; //自定義的消息處理類,重載了OnEvent()函數,demo將用這個類來出理消息.注意后面會調用一個setEventReceiver()函數來設定消息處理類. int main()
{ Device =createDevice( video::EDT_DIRECT3D9, dimension2d<s32>(640,480), 16,false, false, false, 0); //main函數開始,創設備. Device->setWindowCaption(L"鬼火引擎,第一個例子"); IVideoDriver* driver = Device->getVideoDriver();
ISceneManager* smgr = Device->getSceneManager();
IGUIEnvironment* guienv = Device->getGUIEnvironment();
smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0)); //添加了一個攝像機,位置和觀察點分別為(0,30,-40),(0,5,0). MyEventReceiver receiver;
?Device->setEventReceiver(&receiver); //指定消息處理類 skin = guienv->getSkin();
c8 tmp[512];
GetWindowsDirectory(tmp,511);
strcat(tmp,"\\fonts\\simhei.ttf");
pFont = (CGUITTFont *)guienv->getFont(tmp,15);//獲得ttf字體
skin->setFont(pFont);//設置字體 //得到系統目錄fonts下的simhei.ttf字體,并設置為當前使用的字體. edtName = guienv->addEditBox(L"歲月無聲",rect<s32>(350,400,530,430));
edtName->setOverrideColor(SColor(0,0,0,255)); //添加一個EditBox,并將字體顏色設成藍色.也可以這樣調用類指定自己的字體: edtName->setOverrideFont(pFont),pFont要另行加載. pbtn = guienv->addButton(rect<s32>(540,400,590,430), 0, 101, L"發送"); listbox = guienv->addListBox(rect<s32>(350, 300, 590, 380)); //添加了一個按鈕和一個列表框 IAnimatedMesh* mesh = smgr->getMesh("../../media/sydney.md2");
?IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh ); if (node)
?{
??node->setMaterialFlag(EMF_LIGHTING, false);
??node->setMD2Animation ( scene::EMAT_STAND );
??node->setMaterialTexture( 0, driver->getTexture("../../media/sydney.bmp") );
?} //場景太單調了,還是留下原來的這個md2 "美女"模型 while(Device->run())
?{
??/*
??Anything can be drawn between a beginScene() and an endScene()
??call. The beginScene clears the screen with a color and also the
??depth buffer if wanted. Then we let the Scene Manager and the
??GUI Environment draw their content. With the endScene() call
??everything is presented on the screen.
??*/
??driver->beginScene(true, true, SColor(255,100,102,140)); smgr->drawAll();
??guienv->drawAll();
??driver->endScene();
?} //游戲循環 Device->drop(); return 0;
} //main函數結束. 呵呵,挺簡單的吧,irrlicht好像還有ui編輯器哦,這樣就不用我們自己算坐標了,用起來就更方便了.好了,看看我們的成果吧.
轉載于:https://www.cnblogs.com/flysnow/archive/2006/07/23/457827.html
總結
以上是生活随笔為你收集整理的[原创]IrrLicht的GUI使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 黄健翔让赛场上出现大个中文字
- 下一篇: Atlas学习手记(9):异步调用Pag