#include "Ogre\Ogre.h"
#include "OIS\OIS.h"
class MyFrameListener : public Ogre::FrameListener
{
private:
OIS::InputManager* _InputManager;
OIS::Keyboard* _Keyboard;
OIS::Mouse* _Mouse;
Ogre::Camera* _Cam;
Ogre::Viewport* _viewport;
float _movementspeed;
bool comp1,comp2,comp3;
bool down1,down2,down3;
float _WalkingSpeed;
Ogre::SceneNode* _node;
Ogre::AnimationState* _aniState;
Ogre::AnimationState* _aniStateTop;
public:
MyFrameListener(Ogre::RenderWindow* win,Ogre::Camera* cam,Ogre::Viewport* viewport,Ogre::SceneNode* node,Ogre::Entity* ent)
{
_Cam = cam;
_movementspeed = 50.0f;
_viewport = viewport;
comp1 = false;
comp2 = false;
comp3 = false;
down1 = false;
down2 = false;
down3 = false;
_WalkingSpeed = 50.0f;
_node = node;
OIS::ParamList parameters;
unsigned int windowHandle = 0;
std::ostringstream windowHandleString;
win->getCustomAttribute("WINDOW", &windowHandle);
windowHandleString << windowHandle;
parameters.insert(std::make_pair("WINDOW", windowHandleString.str()));
_InputManager = OIS::InputManager::createInputSystem(parameters);
_Keyboard = static_cast<OIS::Keyboard*>(_InputManager->createInputObject( OIS::OISKeyboard, false ));
_Mouse = static_cast<OIS::Mouse*>(_InputManager->createInputObject( OIS::OISMouse, false ));
_aniState = ent->getAnimationState("RunBase");
_aniState->setLoop(false);
_aniStateTop = ent->getAnimationState("RunTop");
_aniStateTop->setLoop(false);
}
~MyFrameListener()
{
_InputManager->destroyInputObject(_Keyboard);
_InputManager->destroyInputObject(_Mouse);
OIS::InputManager::destroyInputSystem(_InputManager);
}
bool frameStarted(const Ogre::FrameEvent& evt)
{
_Keyboard->capture();
if(_Keyboard->isKeyDown(OIS::KC_ESCAPE))
{
return false;
}
Ogre::Vector3 translate(0,0,0);
if(_Keyboard->isKeyDown(OIS::KC_W))
{
translate += Ogre::Vector3(0,0,-1);
}
if(_Keyboard->isKeyDown(OIS::KC_S))
{
translate += Ogre::Vector3(0,0,1);
}
if(_Keyboard->isKeyDown(OIS::KC_A))
{
translate += Ogre::Vector3(-1,0,0);
}
if(_Keyboard->isKeyDown(OIS::KC_D))
{
translate += Ogre::Vector3(1,0,0);
}
if(_Keyboard->isKeyDown(OIS::KC_1) && !down1)
{
down1 = true;
comp1 = !comp1;
Ogre::CompositorManager::getSingleton().setCompositorEnabled(_viewport, "Compositor2", comp1);
}
if(_Keyboard->isKeyDown(OIS::KC_2) && !down2)
{
down2 = true;
comp2 = !comp2;
Ogre::CompositorManager::getSingleton().setCompositorEnabled(_viewport, "Compositor3", comp2);
}
if(_Keyboard->isKeyDown(OIS::KC_3) && !down3)
{
down3 = true;
comp3 = !comp3;
Ogre::CompositorManager::getSingleton().setCompositorEnabled(_viewport, "Compositor7", comp3);
}
if(!_Keyboard->isKeyDown(OIS::KC_1))
{
down1 = false;
}
if(!_Keyboard->isKeyDown(OIS::KC_2))
{
down2 = false;
}
if(!_Keyboard->isKeyDown(OIS::KC_3))
{
down3 = false;
}
_Cam->moveRelative(translate*evt.timeSinceLastFrame * _movementspeed);
_Mouse->capture();
float rotX = _Mouse->getMouseState().X.rel * evt.timeSinceLastFrame* -1;
float rotY = _Mouse->getMouseState().Y.rel * evt.timeSinceLastFrame * -1;
_Cam->yaw(Ogre::Radian(rotX));
_Cam->pitch(Ogre::Radian(rotY));
Ogre::Vector3 SinbadTranslate(0,0,0);
float _rotation = 0.0f;
bool walked = false;
if(_Keyboard->isKeyDown(OIS::KC_UP))
{
SinbadTranslate += Ogre::Vector3(0,0,-1);
_rotation = 3.14f;
walked = true;
}
if(_Keyboard->isKeyDown(OIS::KC_DOWN))
{
SinbadTranslate += Ogre::Vector3(0,0,1);
_rotation = 0.0f;
walked = true;
}
if(_Keyboard->isKeyDown(OIS::KC_LEFT))
{
SinbadTranslate += Ogre::Vector3(-1,0,0);
_rotation = -1.57f;
walked = true;
}
if(_Keyboard->isKeyDown(OIS::KC_RIGHT))
{
SinbadTranslate += Ogre::Vector3(1,0,0);
_rotation = 1.57f;
walked = true;
}
_node->translate(SinbadTranslate * evt.timeSinceLastFrame * _WalkingSpeed);
_node->resetOrientation();
_node->yaw(Ogre::Radian(_rotation));
if(walked)
{
_aniState->setEnabled(true);
_aniStateTop->setEnabled(true);
if(_aniState->hasEnded())
{
_aniState->setTimePosition(0.0f);
}
if(_aniStateTop->hasEnded())
{
_aniStateTop->setTimePosition(0.0f);
}
}
else
{
_aniState->setTimePosition(0.0f);
_aniState->setEnabled(false);
_aniStateTop->setTimePosition(0.0f);
_aniStateTop->setEnabled(false);
}
_aniState->addTime(evt.timeSinceLastFrame);
_aniStateTop->addTime(evt.timeSinceLastFrame);
return true;
}
};
class MyResourceListener : public Ogre::ResourceGroupListener
{
private:
int num;
int endnum;
public:
MyResourceListener()
{
num = 0;
endnum = 0;
}
void resourceGroupScriptingStarted (const Ogre::String &groupName, size_t scriptCount)
{
endnum += scriptCount;
}
void scriptParseStarted (const Ogre::String &scriptName, bool &skiphisScript)
{
}
void scriptParseEnded (const Ogre::String &scriptName, bool skipped)
{
num++;
std::cout << num <<"/" << endnum << std::endl;
}
void resourceGroupScriptingEnded (const Ogre::String &groupName)
{
}
void resourceGroupLoadStarted (const Ogre::String &groupName, size_t resourceCount)
{
std::cout << "!!!!" << resourceCount << std::endl;
}
void resourceLoadStarted (const Ogre::ResourcePtr &resource)
{
}
void resourceLoadEnded (void)
{
}
void worldGeometryStageStarted (const Ogre::String &description)
{
}
void worldGeometryStageEnded (void)
{
}
void resourceGroupLoadEnded (const Ogre::String &groupName)
{
}
};
class MyApplication
{
private:
Ogre::SceneManager* _sceneManager;
Ogre::Root* _root;
Ogre::SceneNode* _SinbadNode;
Ogre::Entity* _SinbadEnt;
MyFrameListener* _listener;
bool _keepRunning;
public:
MyApplication()
{
_sceneManager = NULL;
_listener = NULL;
_keepRunning = true;
}
~MyApplication()
{
if(_root)
{
delete _root;
}
if(_listener)
{
delete _listener;
}
}
void loadResources()
{
Ogre::ConfigFile cf;
cf.load("resources_d.cfg");
Ogre::ConfigFile::SectionIterator sectionIter = cf.getSectionIterator();
Ogre::String sectionName, typeName, dataname;
while (sectionIter.hasMoreElements())
{
sectionName = sectionIter.peekNextKey();
Ogre::ConfigFile::SettingsMultiMap *settings = sectionIter.getNext();
Ogre::ConfigFile::SettingsMultiMap::iterator i;
for (i = settings->begin(); i != settings->end(); ++i)
{
typeName = i->first;
dataname = i->second;
Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
dataname, typeName, sectionName);
}
}
MyResourceListener* listener = new MyResourceListener();
Ogre::ResourceGroupManager::getSingleton().addResourceGroupListener(listener);
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
}
int startup()
{
_root = new Ogre::Root("plugins_d.cfg");
if(!_root->showConfigDialog())
{
return -1;
}
Ogre::RenderWindow* window = _root->initialise(true,"Ogre3D Beginners Guide");
_sceneManager = _root->createSceneManager(Ogre::ST_GENERIC);
Ogre::Camera* camera = _sceneManager->createCamera("Camera");
camera->setPosition(Ogre::Vector3(0,0,50));
camera->lookAt(Ogre::Vector3(0,0,0));
camera->setNearClipDistance(5);
Ogre::Viewport* viewport = window->addViewport(camera);
viewport->setBackgroundColour(Ogre::ColourValue(0.0,0.0,0.0));
camera->setAspectRatio(Ogre::Real(viewport->getActualWidth())/ Ogre::Real(viewport->getActualHeight()));
loadResources();
createScene();
_listener = new MyFr
评论6
最新资源