Irrlicht学习笔记(6)--2DGraphics

来源:互联网 发布:雅思词汇书推荐 知乎 编辑:程序博客网 时间:2024/04/27 23:35

1说明

这个例子介绍了用Irrlicht引擎绘制2D图形.
包括:
绘制图片,
基于精灵的关键色,
不同字体,
透明矩形

适用于:用Irrlicht引擎制作2D游戏,或者为3D游戏绘制个性的界面

2绘制纹理

2.1载入纹理

通过video::IVideoDriver类的方法getTexture()获得ITexture*对象的指针.
可载入格式:BMP,JPG,TGA,PCX,PNG,PSD

2.2设置关键色

(IVideoDriver类的方法)
void makeColorKeyTexture(video::ITExture* texture,
video::SColor color,//也可以是某个位置的颜色core::position2d<s32> colorKeyPixelPos
bool zeroTexels =false);

2.3绘制纹理

virtual void draw2DImage(
    const video::ITexture* texture, //要绘制的纹理
    const core::position2d<s32>& destPos,//目标位置
    const core::rect<s32>& sourceRect,//在原图片中的相对位置
     const core::rect<s32>* clipRect =0,//屏幕上的一个裁剪矩形,NULL不裁剪
    SColor color=SColor(255,255,255,255), //绘制纹理的颜色通道设置,argb
    bool useAlphaChannelOfTexture=false//是否开启透明颜色
) =0;



3字体

3.1获得引擎内置的字体


3.1.1由guie管理

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

3.1.2外部字体载入

gui::IGUIFont* font2 = guie->getFont("../media/fonthaettenschweiler.bmp");


3.2使用字体写字

使用到接口irr::IGUIFont的方法
virtual void draw(
const core::stringw& text, //文字内容
const core::rect<s32>& position,//位置范围
video::SColor color, //字体颜色
bool hcenter=false,//矩形内横向书写?
 bool vcenter=false,//纵向
const core::rect<s32>* clip=0//裁剪矩形
) = 0;
比如:
font->draw(
L"This demo shows that Irrlicht is also capable of drawing 2D graphics.",
core::rect<s32>(130, 10, 300, 50),
video::SColor(255, 255, 255, 255)
);


4透明矩形

要求:透明矩形跟随鼠标移动.
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)
);

5完整代码

#include <iostream>#include <irrlicht.h>using namespace irr;#ifdef _IRR_WINDOWS_#pragma comment(lib, "irrlicht.lib")//#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")#endifint main(int argc, char** argv){IrrlichtDevice *device =createDevice(video::EDT_OPENGL, core::dimension2d<u32>(512, 384), 16,false, true, false, 0);if (!device)return 1;device->setWindowCaption(L"2D Graphic");video::IVideoDriver *driver = device->getVideoDriver();//scene::ISceneManager *smgr = device->getSceneManager();//gui::IGUIEnvironment *guiev = device->getGUIEnvironment();//这张图片包括了这个例子要用到的图形,一个地图,一个生物的两个动作video::ITexture *images = driver->getTexture("../media/2ddemo.png");;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);//参数设置driver->getMaterial2D().TextureLayer[0].BilinearFilter = true;//默认truedriver->getMaterial2D().AntiAliasing = video::EAAM_FULL_BASIC;//smgr->addCameraSceneNode(0, core::vector3df(0, 30, -40), core::vector3df(0, 5, 0));int lastFPS = -1;while (device->run()){if (device->isWindowActive()){u32 time = device->getTimer()->getTime();driver->beginScene(true, true, video::SColor(255, 100, 101, 140));//smgr->drawAll();//guiev->drawAll();driver->draw2DImage(images, core::position2d<s32>(50, 50),core::rect<s32>(0, 0, 342, 224), 0,video::SColor(255, 255, 255, 255), true);driver->draw2DImage(images, core::position2d<s32>(164,125),(time / 500 % 2) ? imp1 : imp2, 0, video::SColor(255, 255, 255, 255), true);driver->draw2DImage(images, core::position2d<s32>(270, 105),(time / 500 % 2) ? imp1 : imp2, 0,video::SColor(255, (time) % 255, 255, 255), true);if (font)font->draw(L"This demo shows that Irrlicht is also capable of drawing 2D graphics.",core::rect<s32>(130, 10, 300, 50),video::SColor(255, 255, 255, 255));if (font2)font2->draw(L"Also mixing with 3d graphics is posible.",core::rect<s32>(130, 20, 300, 60),video::SColor(55, time%255, time % 255, 255));driver->enableMaterial2D(false);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();int fps = driver->getFPS();if (lastFPS != fps){core::stringw str = L"Campfire FX example [";str += driver->getName();str += "]FPS.",str += fps;device->setWindowCaption(str.c_str());lastFPS = fps;}}}device->drop();return 0;}


0 0