zoukankan      html  css  js  c++  java
  • [原创]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编辑器哦,这样就不用我们自己算坐标了,用起来就更方便了.好了,看看我们的成果吧.
     
  • 相关阅读:
    深入理解JavaScript的闭包特性 如何给循环中的对象添加事件
    兼容低版本浏览器的getElementByClassName方法
    印象深刻的bug
    pyinstaller将python编写的打卡程序打包成exe
    自动化环境搭建遇到问题
    VS2010带不出System.Data.OracleClient这个引用的解决方案
    迭代与列表生成式、生成器
    Python函数
    Python基础
    python+Selenium 环境搭建
  • 原文地址:https://www.cnblogs.com/flysnow/p/457827.html
Copyright © 2011-2022 走看看