Code: Select all
// Game.cpp: implementation of the CGame class.
//
//////////////////////////////////////////////////////////////////////
#include "Game.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CGame::CGame()
{
}
CGame::~CGame()
{
}
void CGame::run()
{
device =
createDevice(video::EDT_OPENGL, core::dimension2d<s32>(800, 600), 16, false, false, this);
driver = device->getVideoDriver();
smgr = device->getSceneManager();
//scene::ISceneManager* sm = device->getSceneManager();
env = device->getGUIEnvironment();
static irr::video::ITexture * loadbar = NULL;
driver->beginScene(true, false, irr::video::SColor(0,0,0,0));
loadbar = driver->getTexture("load.tga");
//irr::gui::IGUIFont* font = env->getBuildInFont();
//** DRAW TEXT LOADING MESSAGE
driver->draw2DImage(loadbar, core::position2d<s32>(0,00),core::rect<s32>(0,0,800,600), 0,video::SColor(255,255,255,255), false);
driver->endScene();
//**:~~~~~~~~~~~~~~~~~~~~~~~~~
device->getFileSystem()->addZipFileArchive("main.dll");
LevelMesh = smgr->getMesh("dapalace.bsp");
if (LevelMesh)
{
LevelNode = smgr->addOctTreeSceneNode(LevelMesh->getMesh(0));
}
if (LevelNode)
{
LevelNode->setPosition(core::vector3df(0,1120,-860));
mapSelector = smgr->createOctTreeTriangleSelector(LevelMesh->getMesh(0), LevelNode, 128);
LevelNode->setTriangleSelector(mapSelector);
mapSelector->drop();
//** Set up skybox
skyboxNode = smgr->addSkyBoxSceneNode(
driver->getTexture("1_up.jpg"),
driver->getTexture("1_dn.jpg"),
driver->getTexture("1_lf.jpg"),
driver->getTexture("1_rt.jpg"),
driver->getTexture("1_ft.jpg"),
driver->getTexture("1_bk.jpg"));
}
SKeyMap keyMap[8];
keyMap[0].Action = EKA_MOVE_FORWARD;
keyMap[0].KeyCode = KEY_UP;
keyMap[1].Action = EKA_MOVE_FORWARD;
keyMap[1].KeyCode = KEY_KEY_W;
keyMap[2].Action = EKA_MOVE_BACKWARD;
keyMap[2].KeyCode = KEY_DOWN;
keyMap[3].Action = EKA_MOVE_BACKWARD;
keyMap[3].KeyCode = KEY_KEY_S;
keyMap[4].Action = EKA_STRAFE_LEFT;
keyMap[4].KeyCode = KEY_LEFT;
keyMap[5].Action = EKA_STRAFE_LEFT;
keyMap[5].KeyCode = KEY_KEY_A;
keyMap[6].Action = EKA_STRAFE_RIGHT;
keyMap[6].KeyCode = KEY_RIGHT;
keyMap[7].Action = EKA_STRAFE_RIGHT;
keyMap[7].KeyCode = KEY_KEY_D;
camera = smgr->addCameraSceneNodeFPS(0,100.0f,300.0f, -1, keyMap, 8);
camera->setPosition(core::vector3df(0,0,-99));
scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
mapSelector, camera, core::vector3df(30,50,30),
core::vector3df(0,-100,0), 100.0f,
core::vector3df(0,50,0));
camera->addAnimator(anim);
anim->drop();
//** disable mouse cursor
device->getCursorControl()->setVisible(false);
// add billboard
scene::IBillboardSceneNode * bill = smgr->addBillboardSceneNode();
bill->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR );
bill->setMaterialTexture(0, driver->getTexture("particle.bmp"));
bill->setMaterialFlag(video::EMF_LIGHTING, false);
bill->setSize(core::dimension2d<f32>(20.0f, 20.0f));
//** Add alot of Kid Buu's.
video::SMaterial material, material2, material3;
material.Texture1 = driver->getTexture("buu.pcx"); //** load texture for buu
material2.Texture1 = driver->getTexture("vegeta.pcx");
material3.Texture1 = driver->getTexture("vhead.pcx");
material.Lighting = true;
material2.Lighting = true;
material3.Lighting = true;
scene::IAnimatedMesh* buu = smgr->getMesh("buu.md2"); //** load model for buu, picture a wireframe we wrap the texture(buu.pcx) around
ModelNode_1 = smgr->addAnimatedMeshSceneNode(buu);
ModelNode_1->setPosition(core::vector3df(-70,0,-90));
ModelNode_1->setMD2Animation(scene::EMAT_JUMP);
ModelNode_1->getMaterial(0) = material;
ModelNode_1 = smgr->addAnimatedMeshSceneNode(buu);
ModelNode_1->setPosition(core::vector3df(-70,0,-150));
ModelNode_1->setMD2Animation(scene::EMAT_RUN);
ModelNode_1->getMaterial(0) = material;
ModelNode_1 = smgr->addAnimatedMeshSceneNode(buu);
ModelNode_1->setPosition(core::vector3df(-70,0,-210));
ModelNode_1->setMD2Animation(scene::EMAT_ATTACK);
ModelNode_1->getMaterial(0) = material;
ModelNode_1 = smgr->addAnimatedMeshSceneNode(buu);
ModelNode_1->setPosition(core::vector3df(-70,0,-270));
ModelNode_1->setMD2Animation(scene::EMAT_WAVE);
ModelNode_1->getMaterial(0) = material;
ModelNode_1 = smgr->addAnimatedMeshSceneNode(buu);
ModelNode_1->setPosition(core::vector3df(-70,0,-320));
ModelNode_1->setMD2Animation(scene::EMAT_FLIP);
ModelNode_1->getMaterial(0) = material;
ModelNode_1 = smgr->addAnimatedMeshSceneNode(buu);
ModelNode_1->setPosition(core::vector3df(-70,0,-380));
ModelNode_1->setMD2Animation(scene::EMAT_DEATH_FALLBACK);
ModelNode_1->getMaterial(0) = material;
scene::IAnimatedMesh* vegeta = smgr->getMesh("vegeta.md2");
scene::IAnimatedMesh* vhead = smgr->getMesh("vhead.md2");
ModelNode_1 = smgr->addAnimatedMeshSceneNode(vegeta);
ModelNode_1->setPosition(core::vector3df(-70,0,-420));
ModelNode_1->setMD2Animation(scene::EMAT_FLIP);
ModelNode_1->getMaterial(0) = material2;
//** vegeta requires two models for his head and body
ModelNode_1 = smgr->addAnimatedMeshSceneNode(vhead);
ModelNode_1->setPosition(core::vector3df(-70,0,-420));
ModelNode_1->setMD2Animation(scene::EMAT_FLIP);
ModelNode_1->getMaterial(0) = material3;
driver->beginScene(true, true, 0);
core::triangle3df tri;
driver->setTransform(video::ETS_WORLD, core::matrix4());
driver->setMaterial(material);
driver->draw3DTriangle(tri, video::SColor(0,255,0,0));
material.Texture1 = 0;
material.Lighting = false;
//** Add a light
smgr->addLightSceneNode(0, core::vector3df(-60,100,400),
video::SColorf(0.0f,1.0f,1.0f,1.0f),
900.0f);
scene::ISceneNode* selectedSceneNode = 0;
scene::ISceneNode* lastSelectedSceneNode = 0;
int lastFPS = -1;
while(device->run())
{
driver->beginScene(true, true, 0);
smgr->drawAll();
core::line3d<f32> line;
line.start = camera->getPosition();
line.end = line.start + (camera->getTarget() - line.start).normalize() * 1000.0f;
core::vector3df intersection;
core::triangle3df tri;
if (smgr->getSceneCollisionManager()->getCollisionPoint(
line, mapSelector, intersection, tri))
{
bill->setPosition(intersection);
driver->setTransform(video::ETS_WORLD, core::matrix4());
driver->setMaterial(material);
driver->draw3DTriangle(tri, video::SColor(0,255,0,0));
}
selectedSceneNode = smgr->getSceneCollisionManager()->getSceneNodeFromCameraBB(camera);
if (lastSelectedSceneNode)
lastSelectedSceneNode->setMaterialFlag(video::EMF_LIGHTING, true);
if (selectedSceneNode == LevelNode || selectedSceneNode == bill)
selectedSceneNode = 0;
if (selectedSceneNode)
selectedSceneNode->setMaterialFlag(video::EMF_LIGHTING, false);
lastSelectedSceneNode = selectedSceneNode;
/*
That's it, we just have to finish drawing.
*/
driver->endScene();
int fps = driver->getFPS();
if (lastFPS != fps)
{
wchar_t tmp[1024];
swprintf(tmp, 1024, L"A Game A02 - by fritz (fps:%d) Triangles:%d",
fps, driver->getPrimitiveCountDrawn());
device->setWindowCaption(tmp); //** Won't work on >WIN XP
lastFPS = fps;
}
}
device->drop();
}
bool CGame::OnEvent(SEvent event)
{
if (camera)
{
return camera->OnEvent(event);
}
if(event.EventType == irr::EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown)
{
switch(event.KeyInput.Key)
{
case KEY_ESCAPE:
{
ExitProcess(0);
return true;
}
return true;
}
}
/*
We send all other key and mouse input to the currently active camera
if there is one, so that the camera can move itself.
*/
return false;
}