#include <iostream> #include <irrlicht.h> using namespace irr; #ifdef _IRR_WINDOWS_ #pragma comment(lib, "irrlicht.lib") //#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup") #endif int 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;//默认true driver->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; }