irrwowview

Tutorial 6: 2D Graphics

Tutorial 6: 2D Graphics
本教程演示如何在IrrLicht引擎中实现2d图形。教程演示了如何绘制图像,基于关键色(keycolor)的精灵,透明区域和变化的字体。如果你想用这个引擎制作2d游戏或者绘制一个很cool的覆盖在3d游戏上的用户界面,

The program which is described here will look like this:

 

Lets start!

一如往常,代码包含了头文件,使用irr命名空间,并告诉链接器要链接的*.lib

#include <irrlicht.h>
#include <iostream>

using namespace irr;

#pragma comment(lib, "Irrlicht.lib")

 

              

首先,让用户选择要使用的驱动类型,然后启动引擎,设置窗口标题,并获取一个指向IVideoDriver对象的指针。

int main()
{
  // let user select driver type
  video::E_DRIVER_TYPE driverType;

  printf("Please select the driver you want for this example:\n"\
                    " (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.5\n"\
                    " (d) Software Renderer\n (e) Apfelbaum Software Renderer\n"\
                    " (f) NullDevice\n (otherKey) exit\n\n");

 


   char i;
   std::cin >> i;

   switch(i)
   {
      case 'a': driverType = video::EDT_DIRECT3D9;break;
      case 'b': driverType = video::EDT_DIRECT3D8;break;
      case 'c': driverType = video::EDT_OPENGL;   break;
      case 'd': driverType = video::EDT_SOFTWARE; break;
      case 'e': driverType = video::EDT_SOFTWARE2;break;
      case 'f': driverType = video::EDT_NULL;     break;
      default: return 0;
   }     

   // create device

  IrrlichtDevice *device = createDevice(driverType,

 

         core::dimension2d<s32>(512, 384));

  if (device == 0)

 

      return 1;

 

 
  device->setWindowCaption(L"Irrlicht Engine - 2D Graphics Demo");

  video::IVideoDriver* driver = device->getVideoDriver();

这个例子的所有2d图形都放在一个2ddemo.bmp的纹理里。因为要画基于关键色的精灵,所在要在引擎中加载设置关键色(keycolor)为透明的纹理。在例子中,我们不用直接告诉引擎关键色(keycolor)的值,只要说“喂,IrrLicht引擎,我想用坐标(00)的颜色值作为关键色。”。另外,还可以调用‘driver->makeColorKeyTexture(images, video::SColor(0,0,0,0))’语句设置黑色为透明色。请注意,makeColorKeyTexture()函数只是创建基于关键色的alpha通道。

video::ITexture* images = driver->getTexture("../../media/2ddemo.bmp");
driver->makeColorKeyTexture(images, core::position2d<s32>(0,0));

可以绘制加载两种不同普遍字体的文本。而这里第一个字体使用了引擎默认的字体,第二个字体才使用额外的字体。
接着定义两个矩形,分别用来指定纹理中两个小鬼的位置。

gui::IGUIFont* font = device->getGUIEnvironment()->getBuiltInFont();
gui::IGUIFont* font2 = device->getGUIEnvironment()->getFont(

 

  "../../media/fonthaettenschweiler.bmp");

core::rect<s32> imp1(349,15,385,78);

 

core::rect<s32> imp2(387,15,423,78);

一切准备好了,现在我们可以绘制循环的beginSceneendScene之间了。虽然在这个例子中只实现了2d图形的绘制,但要在3d图形中使用也是完美无问题的。可以试一下用场景管理器绘制3d顶点和场景。

while(device->run() && driver)
{
          if (device->isWindowActive())
          {
                    u32 time = device->getTimer()->getTime();
                    driver->beginScene(true, true, video::SColor(0,120,102,136));

 

首先,我们绘制三个根据关键色设置了alpha通道的精灵。最后一个参数设置是否使用alpha通道绘制。倒数第二个参数是设置精灵能够显示的颜色。全白则精灵使用纹理的颜色。而第三个精灵根据时间改变能够显示的颜色。

// draw fire & dragons background world
driver->draw2DImage(images, core::position2d<s32>(50,50),
          core::rect<s32>(0,0,342,224), 0,
          video::SColor(255,255,255,255), true);

// draw flying imp

 

driver->draw2DImage(images, core::position2d<s32>(164,125),

 

  (time/500 % 2) ? imp1 : imp2, 0,

 

   video::SColor(255,255,255,255), true);

// draw second flying imp with colorcylce

 

driver->draw2DImage(images, core::position2d<s32>(270,105),

 

  (time/500 % 2) ? imp1 : imp2, 0,

 

  video::SColor(255,(time) % 255,255,255), true);

绘制文本实在是太简单了。应该能自己看懂代码的。

// draw some text
if (font)
          font->draw(L"This is some text.",
                    core::rect<s32>(130,10,300,50),
                    video::SColor(255,255,255,255));

// draw some other text

 

if (font2)

 

   font2->draw(L"This is some other text.",

 

       core::rect<s32>(130,20,300,60),

 

       video::SColor(255,time % 255,time % 255,255));

最后,我们绘制IrrLicht引擎的标志(没有设置精灵显示的颜色和不使用alpha通道)并把鼠标光标改为半透明的2d矩形。

    // draw logo
    driver->draw2DImage(images, core::position2d<s32>(10,10),
                                         core::rect<s32>(354,87,442,118));

    // draw transparent rect under cursor

 

    core::position2d<s32> m = device->getCursorControl()->getPosition();

 

    driver->draw2DRectangle(video::SColor(100,255,255,255),

 

          core::rect<s32>(m.X-20, m.Y-20, m.X+20, m.Y+20));

    driver->endScene();

 

  }

 

}

就这样吧。我相信是完全无难度的

   device->drop();

 

   return 0;

 

}

 

 

posted on 2008-07-04 11:04 shjy 阅读(238) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理