Irrlicht引擎学习笔记(14)--Splitscreen

说明:

分屏技术
这个例子展示内容:
将视口分为4部分,三个显示fixed camera和一个用户控制camera
详情请看源码及其注释.

源码及注释:

/*
*18.分屏技术
*这个例子展示内容:
*将视口分为4部分,三个显示fixed camera和一个用户控制camera
*/
#include <iostream>
#include <irrlicht.h>
#include "driverChoice.h"

using namespace irr;


#ifdef _IRR_WINDOWS_
#pragma comment(lib, "irrlicht.lib")
//#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif

/*
*1.宏定义,全局变量
*/
//Resolution
const int ResX = 800;
const int ResY = 600;
const bool fullScreen = false;

bool SplitScreen = true;//标记,开关分屏效果
//声明四个照相机
scene::ICameraSceneNode* camera[4] = { 0, 0, 0, 0 };//最后一个是FPScamera

/**
*2.事件处理
*通过S键控制分屏效果的开关
*注意:消息传递与处理,特别是FPS相机移动的输入消息
*/
class MyEventReceiver :public IEventReceiver
{
public:
virtual bool OnEvent(const SEvent& event)
{
if (event.EventType == irr::EET_KEY_INPUT_EVENT&&
event.KeyInput.Key == KEY_KEY_S&&event.KeyInput.PressedDown)
{
SplitScreen = !SplitScreen;
return true;
}

//其他消息交给camera FPS处理
if (camera[3])
return camera[3]->OnEvent(event);
return false;
}
};

int main(int argc, char** argv)
{
video::E_DRIVER_TYPE driverType = driverChoiceConsole();
IrrlichtDevice *device =
createDevice(driverType, core::dimension2d<u32>(ResX,ResY), 32,
false, true, false, 0);
if (!device)
return 1;
device->setWindowCaption(L"quake3map");

video::IVideoDriver *driver = device->getVideoDriver();
scene::ISceneManager *smgr = device->getSceneManager();
gui::IGUIEnvironment *guiev = device->getGUIEnvironment();
//3.准备 
scene::IAnimatedMesh* model = smgr->getMesh("../media/sydney.md2");
if (!model)
return 1;
scene::IAnimatedMeshSceneNode* model_node = smgr->addAnimatedMeshSceneNode(model);
if (model_node)
{
//设置
video::ITexture* texture = driver->getTexture("../media/sydney.bmp");
model_node->setMaterialTexture(0, texture);
model_node->setMD2Animation(scene::EMAT_RUN);
model_node->setMaterialFlag(video::EMF_LIGHTING, false);
}
device->getFileSystem()->addZipFileArchive("../media/map-20kdm2.pk3");
scene::IAnimatedMesh* map = smgr->getMesh("20kdm2.bsp");
if (map)
{
scene::ISceneNode* map_node = smgr->addOctreeSceneNode(map->getMesh(0));
map_node->setPosition(core::vector3df(-850, -220, -850));
}
//2.创建摄像机
//分别为:前,上,侧,FPS
camera[0] = smgr->addCameraSceneNode(0, core::vector3df(50, 0, 0), core::vector3df(0, 0, 0));
camera[1] = smgr->addCameraSceneNode(0, core::vector3df(0, 50, 0), core::vector3df(0, 0, 0));
camera[2] = smgr->addCameraSceneNode(0, core::vector3df(0, 0, 50), core::vector3df(0, 0, 0));
camera[3] = smgr->addCameraSceneNodeFPS();
if (camera[3])
{
camera[3]->setPosition(core::vector3df(-50, 0, 50));
camera[3]->setTarget(core::vector3df(0, 0, 0));
device->getCursorControl()->setVisible(false);
}
/*
*3.重点部分:
*每帧需要渲染4次,
*额外的3此渲染是0-2的摄像机的内容
*步骤:
*设置整个场景的视口
* 开始新的场景(清屏)
*
* 为分屏重复三次渲染
* 设置分屏视口
* 激活相应的照相机并连接到视口
* 渲染
*
*如果有GUI:
* 设置视口为整个屏幕
* 显示GUI
*结束场景
*/
int lastFPS = -1;
while (device->run())
{
driver->setViewPort(core::rect<s32>(0, 0, ResX, ResY));
driver->beginScene(true, true, video::SColor(255, 100, 101, 140));

if (SplitScreen)
{
smgr->setActiveCamera(camera[0]);
driver->setViewPort(core::rect<s32>(0, 0, ResX / 2, ResY / 2));
smgr->drawAll();
smgr->setActiveCamera(camera[1]);
driver->setViewPort(core::rect<s32>(ResX / 2, 0, ResX, ResY / 2));
smgr->drawAll();
smgr->setActiveCamera(camera[2]);
driver->setViewPort(core::rect<s32>(0, ResY / 2, ResX / 2, ResY ));
smgr->drawAll();
driver->setViewPort(core::rect<s32>(ResX / 2, ResY / 2, ResX, ResY));
}
smgr->setActiveCamera(camera[3]);
smgr->drawAll();
guiev->drawAll();

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;
}


你可能感兴趣的:(Irrlicht)