#ifndef _Ogre_H__ #include <ogre.h> #endif //_Ogre_H__ #ifndef OIS_OISALL_H #include <OIS/OIS.h> #endif //OIS_OISALL_H #ifndef _CEGUI_h_ #include <CEGUI.h> #endif //_CEGUI_h_ #ifndef _OgreCEGUIRenderer_h_ #include "OgreCEGUIRenderer.h" #endif //_OgreCEGUIRenderer_h_ using namespace Ogre; using namespace OIS; using namespace CEGUI; CEGUI::MouseButton convertButton(OIS::MouseButtonID buttonID) { switch (buttonID) { case OIS::MB_Left: return CEGUI::LeftButton; case OIS::MB_Right: return CEGUI::RightButton; case OIS::MB_Middle: return CEGUI::MiddleButton; default: return CEGUI::LeftButton; } } class CSGameApp : public FrameListener, public OIS::KeyListener, public OIS::MouseListener, public WindowEventListener { public: CSGameApp(); virtual void Go(); virtual ~CSGameApp(); protected: virtual bool Initialise(); virtual bool InitOgreCore(); virtual void CreateSceneManager(); virtual void CreateCamera(); virtual void CreateViewports(); virtual void CreateResourceListener(); virtual void CreateFrameListener(); virtual void AddResourceLocations(); virtual void InitResource(); //I am pure virtual, override me! virtual void CreateScene() = 0; virtual void DestoryScene(); //FrameListener overrides virtual bool frameStarted(const FrameEvent& evt); virtual bool frameEnded(const FrameEvent& evt); virtual bool keyPressed( const KeyEvent &arg ); virtual bool keyReleased( const KeyEvent &arg ); virtual bool mouseMoved( const MouseEvent &arg ); virtual bool mousePressed( const MouseEvent &arg, MouseButtonID id ); virtual bool mouseReleased( const MouseEvent &arg, MouseButtonID id ); virtual bool processUnbufferedMouseInput(const FrameEvent& evt); virtual bool processUnbufferedKeyInput(const FrameEvent& evt); virtual void moveCamera(); public: void setupEventHandlers(void); bool quit(const CEGUI::EventArgs& e); bool selectserver(const CEGUI::EventArgs& e); //class of Ogre system Root* m_pRoot; Camera* m_pCamera; SceneManager* m_pSecneMgr; RenderWindow* m_pWindow; //OIS Input devices OIS::Mouse* m_pMouse; OIS::Keyboard* m_pKeyboard; OIS::InputManager* m_pInputManager; //translate camera Ogre::Vector3 m_TranslateVector; //circumrotate camera Radian m_RotX, m_RotY; //move speed Real m_MoveSpeed; Degree m_RotateSpeed; //move scale is m_MoveSpeed * (t2-t1) float m_MoveScale; Degree m_RotScale; private: //continue to rander bool m_bContinue; //show system mouse in the borderline of windows bool m_bSysMouseShowFlag; }; CSGameApp::CSGameApp() :m_pRoot(0), m_pMouse(0), m_pKeyboard(0), m_pInputManager(0), m_TranslateVector(Ogre::Vector3::ZERO), m_RotX(0), m_RotY(0) { m_bContinue = true; m_bSysMouseShowFlag = false; } void CSGameApp::Go() { if(!Initialise()) { return ; } m_pRoot->startRendering(); //clean up DestoryScene(); } CSGameApp::~CSGameApp() { //Remove ourself as a Window listener WindowEventUtilities::removeWindowEventListener(m_pWindow, this); if( m_pInputManager ) { m_pInputManager->destroyInputObject( m_pMouse ); m_pInputManager->destroyInputObject( m_pKeyboard ); OIS::InputManager::destroyInputSystem(m_pInputManager); m_pInputManager = 0; } delete m_pRoot; } bool CSGameApp::Initialise() { m_pRoot = new Root(); if(!m_pRoot) { return false; } //add rescource location AddResourceLocations(); // if we cannot initialise Ogre, just abandon the whole deal //--------------------------------------------------------- //do not show choose dialog,like this: //m_pRoot->restoreConfig(); //--------------------------------------------------------- if(!InitOgreCore()) { return false; } CreateSceneManager(); CreateCamera(); CreateViewports(); // Set default mipmap level (NB some APIs ignore this) TextureManager::getSingleton().setDefaultNumMipmaps(5); // Create any resource listeners (for loading screens) CreateResourceListener(); // Initialise resources InitResource(); // Create the scene CreateScene(); CreateFrameListener(); return true; } bool CSGameApp::InitOgreCore() { // Show the configuration dialog and initialise the system // You can skip this and use root.restoreConfig() to load configuration // settings if you were sure there are valid ones saved in ogre.cfg //Register as a Window listener if(m_pRoot->restoreConfig() || m_pRoot->showConfigDialog()) { // If returned true, user clicked OK so initialise // Here we choose to let the system create a default rendering window by passing 'true' m_pWindow = m_pRoot->initialise(true); WindowEventUtilities::addWindowEventListener(m_pWindow, this); OIS::ParamList pl; size_t windowHnd = 0; std::ostringstream windowHndStr; m_pWindow->getCustomAttribute("WINDOW", &windowHnd); windowHndStr << windowHnd; pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); //show the mouse pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE"))); pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND"))); m_pInputManager = OIS::InputManager::createInputSystem( pl ); //Create all devices (We only catch joystick exceptions here, as, most people have Key/Mouse) m_pKeyboard = static_cast<OIS::Keyboard*>(m_pInputManager->createInputObject( OIS::OISKeyboard, true )); m_pMouse = static_cast<OIS::Mouse*>(m_pInputManager->createInputObject( OIS::OISMouse, true )); m_pMouse->setEventCallback(this); m_pKeyboard->setEventCallback(this); ShowCursor(0); //Set initial mouse clipping size windowResized(m_pWindow); return true; } else { return false; } return true; } void CSGameApp::CreateSceneManager() { //Create the SceneManager, in this case a generic one m_pSecneMgr = m_pRoot->createSceneManager(ST_GENERIC); } void CSGameApp::CreateCamera() { //Create the camera m_pCamera = m_pSecneMgr->createCamera("PlayerCam"); //Position it at 500 in Z direction m_pCamera->setPosition(Ogre::Vector3(0,0,500)); //Look back along -Z m_pCamera->lookAt(Ogre::Vector3(0,0,-300)); m_pCamera->setNearClipDistance(5); } void CSGameApp::CreateViewports() { //Create one viewport, entire window Viewport* vp = m_pWindow->addViewport(m_pCamera); vp->setBackgroundColour(ColourValue(0,0,0)); //Alter the camera aspect ratio to match the viewport m_pCamera->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight())); } void CSGameApp::CreateResourceListener() { //... } void CSGameApp::CreateFrameListener() { m_RotateSpeed = 36; m_MoveSpeed = 100; m_MoveScale = 0.0f; m_RotScale = 0.0f; m_pRoot->addFrameListener(this); } void CSGameApp::AddResourceLocations() { //Load resource paths from config file ConfigFile cf; cf.load("resources.cfg"); //Go through all sections & settings in the file ConfigFile::SectionIterator seci = cf.getSectionIterator(); Ogre::String secName, typeName, archName; while (seci.hasMoreElements()) { secName = seci.peekNextKey(); ConfigFile::SettingsMultiMap *settings = seci.getNext(); ConfigFile::SettingsMultiMap::iterator i; for (i = settings->begin(); i != settings->end(); ++i) { typeName = i->first; archName = i->second; ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName); } } } void CSGameApp::InitResource() { //Initialise, parse scripts etc ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); } void CSGameApp::DestoryScene() { } bool CSGameApp::frameStarted(const FrameEvent& evt) { m_pKeyboard->capture(); m_pMouse->capture(); if(m_pWindow->isClosed()) { return false; } if(processUnbufferedKeyInput(evt) == false) { return false; } if(processUnbufferedMouseInput(evt) == false) { return false; } // If this is the first frame, pick a speed if (evt.timeSinceLastFrame == 0) { m_MoveScale = 1; m_RotScale = 0.1; } // Otherwise scale movement units by time passed since last frame else { // Move about 100 units per second, m_MoveScale = m_MoveSpeed * evt.timeSinceLastFrame; // Take about 10 seconds for full rotation m_RotScale = m_RotateSpeed * evt.timeSinceLastFrame; } moveCamera(); return m_bContinue && !m_pKeyboard->isKeyDown(OIS::KC_ESCAPE); } bool CSGameApp::frameEnded(const FrameEvent& evt) { return true; } bool CSGameApp::keyPressed( const KeyEvent &arg ) { CEGUI::System *sys = CEGUI::System::getSingletonPtr(); sys->injectKeyDown(arg.key); sys->injectChar(arg.text); return true; } bool CSGameApp::keyReleased( const KeyEvent &arg ) { CEGUI::System::getSingleton().injectKeyUp(arg.key); return true; } bool CSGameApp::mouseMoved( const MouseEvent &arg ) { m_pMouse->getMouseState().width = m_pWindow->getWidth(); m_pMouse->getMouseState().height = m_pWindow->getHeight(); if(arg.state.X.abs < m_pWindow->getViewport(0)->getActualLeft()+1 ||(arg.state.Y.abs < m_pWindow->getViewport(0)->getActualTop()+1) ||(arg.state.X.abs > m_pWindow->getViewport(0)->getActualLeft()+m_pWindow->getViewport(0)->getActualWidth()) ||(arg.state.Y.abs > m_pWindow->getViewport(0)->getActualTop() +m_pWindow->getViewport(0)->getActualHeight())) { if(!m_bSysMouseShowFlag) { ShowCursor(1); CEGUI::MouseCursor::getSingleton().hide(); m_bSysMouseShowFlag = true; } } else { if(m_bSysMouseShowFlag) { ShowCursor(0); CEGUI::MouseCursor::getSingleton().show(); m_bSysMouseShowFlag = false; } } CEGUI::System::getSingleton().injectMousePosition( arg.state.X.abs, arg.state.Y.abs ); return true; } bool CSGameApp::mousePressed( const MouseEvent &arg, MouseButtonID id ) { CEGUI::System::getSingleton().injectMouseButtonDown(convertButton(id)); return true; } bool CSGameApp::mouseReleased( const MouseEvent &arg, MouseButtonID id ) { CEGUI::System::getSingleton().injectMouseButtonUp(convertButton(id)); return true; } bool CSGameApp::processUnbufferedMouseInput(const FrameEvent& evt) { // Rotation factors, may not be used if the second mouse button is pressed // 2nd mouse button - slide, otherwise rotate const OIS::MouseState &ms = m_pMouse->getMouseState(); if( ms.buttonDown( OIS::MB_Right ) ) { m_TranslateVector.x += ms.X.rel * 0.13; m_TranslateVector.y -= ms.Y.rel * 0.13; } else { m_RotX = Degree(-ms.X.rel * 0.13); m_RotY = Degree(-ms.Y.rel * 0.13); } return true; } bool CSGameApp::processUnbufferedKeyInput(const FrameEvent& evt) { if(m_pKeyboard->isKeyDown(OIS::KC_A)) { m_TranslateVector.x = -m_MoveScale; // Move camera left } if(m_pKeyboard->isKeyDown(OIS::KC_D)) { m_TranslateVector.x = m_MoveScale; // Move camera RIGHT } if(m_pKeyboard->isKeyDown(OIS::KC_UP) || m_pKeyboard->isKeyDown(OIS::KC_W) ) { m_TranslateVector.z = -m_MoveScale; // Move camera forward } if(m_pKeyboard->isKeyDown(OIS::KC_DOWN) || m_pKeyboard->isKeyDown(OIS::KC_S) ) { m_TranslateVector.z = m_MoveScale; // Move camera backward } if(m_pKeyboard->isKeyDown(OIS::KC_PGUP)) { m_TranslateVector.y = m_MoveScale; // Move camera up } if(m_pKeyboard->isKeyDown(OIS::KC_PGDOWN)) { m_TranslateVector.y = -m_MoveScale; // Move camera down } if(m_pKeyboard->isKeyDown(OIS::KC_RIGHT)) { m_pCamera->yaw(-m_RotScale); } if(m_pKeyboard->isKeyDown(OIS::KC_LEFT)) { m_pCamera->yaw(m_RotScale); } if( m_pKeyboard->isKeyDown(OIS::KC_ESCAPE) || m_pKeyboard->isKeyDown(OIS::KC_Q) ) { return false; } return true; } void CSGameApp::moveCamera() { // Make all the changes to the camera // Note that YAW direction is around a fixed axis (freelook style) rather than a natural YAW (e.g. airplane) m_pCamera->yaw(m_RotX); m_pCamera->pitch(m_RotY); m_pCamera->moveRelative(m_TranslateVector); } bool CSGameApp::quit(const CEGUI::EventArgs& e) { CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton(); CEGUI::String strAccount = wmgr.getWindow((CEGUI::utf8*)"Game/Edit/Account")->getText(); m_bContinue = false; return true; } bool CSGameApp::selectserver(const CEGUI::EventArgs& e) { CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton(); //hide all exist windows wmgr.getWindow((CEGUI::utf8*)"Game/Edit/Account")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/Edit/Password")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/Login/Ok")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/Login/Cancel")->hide(); DefaultWindow* root = (DefaultWindow*)wmgr.getWindow("Root"); Listbox* wndListboxSelectServer = (Listbox*)wmgr.createWindow("TaharezLook/Listbox", "Game/Button/Login/SelectServer"); root->addChildWindow(wndListboxSelectServer); wndListboxSelectServer->setPosition(UVector2(cegui_reldim(0.0f), cegui_reldim( 0.0f))); wndListboxSelectServer->setSize(UVector2(cegui_reldim(0.30f), cegui_reldim( 0.5f))); //TEST wndListboxSelectServer->hide(); FrameWindow* wndTest = (FrameWindow*)wmgr.createWindow("TaharezLook/StaticImage", "Game/Test"); root->addChildWindow(wndTest); wndTest->setPosition(UVector2(cegui_reldim(0.0f), cegui_reldim( 0.0f))); wndTest->setSize(UVector2(cegui_reldim(1.00f), cegui_reldim( 1.0f))); //add new windows return true; } void CSGameApp::setupEventHandlers(void) { CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/Login/Ok") ->subscribeEvent( CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&CSGameApp::quit, this)); wmgr.getWindow((CEGUI::utf8*)"Game/Button/Login/Cancel") ->subscribeEvent( CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&CSGameApp::selectserver, this)); } class CSGame : public CSGameApp { public: CSGame(); void CreateScene(); bool frameStarted(const FrameEvent& evt); ~CSGame(); private: Entity* m_pOgreHead; SceneNode* m_pHeadNode; private: CEGUI::OgreCEGUIRenderer* m_pGUIRenderer; CEGUI::System* m_pGUISystem; }; CSGame::CSGame() :m_pOgreHead(0), m_pHeadNode(0) { } void CSGame::CreateScene() { m_pOgreHead = m_pSecneMgr->createEntity("Head", "ogrehead.mesh"); m_pHeadNode = m_pSecneMgr->getRootSceneNode()->createChildSceneNode(); m_pHeadNode->attachObject(m_pOgreHead); // Set ambient light m_pSecneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5)); // Create a skydome m_pSecneMgr->setSkyDome(true, "Examples/CloudySky", 5, 8); // Create a light Light* l = m_pSecneMgr->createLight("MainLight"); l->setPosition(20,80,50); //setup cegui m_pGUIRenderer = new CEGUI::OgreCEGUIRenderer(m_pWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, m_pSecneMgr); m_pGUISystem = new CEGUI::System(m_pGUIRenderer); // Mouse CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLookSkin.scheme"); CEGUI::System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow"); //another way to add windows ,by hand WindowManager& winMgr = WindowManager::getSingleton(); //--------------------------------------------------------------------------------------------- // Create a DefaultWindow called 'Root'. DefaultWindow* root = (DefaultWindow*)winMgr.createWindow("DefaultWindow", "Root"); // set the GUI root window (also known as the GUI "sheet"), so the gui we set up // will be visible. System::getSingleton().setGUISheet(root); // Create a FrameWindow in the TaharezLook style, and name it 'Button Ok' FrameWindow* wndButtonOK = (FrameWindow*)winMgr.createWindow("TaharezLook/Button", "Game/Button/Login/Ok"); // Here we attach the newly created FrameWindow to the previously created // DefaultWindow which we will be using as the root of the displayed gui. root->addChildWindow(wndButtonOK); wndButtonOK->setPosition(UVector2(cegui_reldim(0.35f), cegui_reldim( 0.6f))); wndButtonOK->setSize(UVector2(cegui_reldim(0.10f), cegui_reldim( 0.03f))); wndButtonOK->setText("OK"); //--------------------------------------------------------------------------------------------- // Create a FrameWindow in the TaharezLook style, and name it 'Button Cancel' FrameWindow* wndButtonCancel = (FrameWindow*)winMgr.createWindow("TaharezLook/Button", "Game/Button/Login/Cancel"); // Here we attach the newly created FrameWindow to the previously created // DefaultWindow which we will be using as the root of the displayed gui. root->addChildWindow(wndButtonCancel); wndButtonCancel->setPosition(UVector2(cegui_reldim(0.55f), cegui_reldim( 0.6f))); wndButtonCancel->setSize(UVector2(cegui_reldim(0.10f), cegui_reldim( 0.03f))); wndButtonCancel->setText("Cancel"); //--------------------------------------------------------------------------------------------- //add a editbox FrameWindow* wndEditAccount = (FrameWindow*)winMgr.createWindow("TaharezLook/Editbox", "Game/Edit/Account"); root->addChildWindow(wndEditAccount); wndEditAccount->setPosition(UVector2(cegui_reldim(0.35f), cegui_reldim( 0.5f))); wndEditAccount->setSize(UVector2(cegui_reldim(0.30f), cegui_reldim( 0.04f))); //--------------------------------------------------------------------------------------------- //add a editbox FrameWindow* wndEditPwd = (FrameWindow*)winMgr.createWindow("TaharezLook/Editbox", "Game/Edit/Password"); root->addChildWindow(wndEditPwd); wndEditPwd->setPosition(UVector2(cegui_reldim(0.35f), cegui_reldim( 0.55f))); wndEditPwd->setSize(UVector2(cegui_reldim(0.30f), cegui_reldim( 0.04f))); setupEventHandlers(); } bool CSGame::frameStarted(const FrameEvent& evt) { // Just a silly example to demonstrate how much easier this is than passing objects to an external framelistener m_pHeadNode->translate(0.0f, 0.005f, 0.0f); return CSGameApp::frameStarted(evt); } CSGame::~CSGame() { } INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT ) { // Create application object CSGame app; try { app.Go(); } catch( Ogre::Exception& e ) { MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL); } return 0; }
#ifndef _Ogre_H__ #include <ogre.h> #endif //_Ogre_H__ #ifndef OIS_OISALL_H #include <OIS/OIS.h> #endif //OIS_OISALL_H #ifndef _CEGUI_h_ #include <CEGUI.h> #endif //_CEGUI_h_ #ifndef _OgreCEGUIRenderer_h_ #include "OgreCEGUIRenderer.h" #endif //_OgreCEGUIRenderer_h_ using namespace Ogre; using namespace OIS; using namespace CEGUI; CEGUI::MouseButton convertButton(OIS::MouseButtonID buttonID) { switch (buttonID) { case OIS::MB_Left: return CEGUI::LeftButton; case OIS::MB_Right: return CEGUI::RightButton; case OIS::MB_Middle: return CEGUI::MiddleButton; default: return CEGUI::LeftButton; } } class CSGameApp : public FrameListener, public OIS::KeyListener, public OIS::MouseListener, public WindowEventListener { public: CSGameApp(); virtual void Go(); virtual ~CSGameApp(); protected: virtual bool Initialise(); virtual bool InitOgreCore(); virtual void CreateSceneManager(); virtual void CreateCamera(); virtual void CreateViewports(); virtual void CreateResourceListener(); virtual void CreateFrameListener(); virtual void AddResourceLocations(); virtual void InitResource(); //I am pure virtual, override me! virtual void CreateScene() = 0; virtual void DestoryScene(); //FrameListener overrides virtual bool frameStarted(const FrameEvent& evt); virtual bool frameEnded(const FrameEvent& evt); virtual bool keyPressed( const KeyEvent &arg ); virtual bool keyReleased( const KeyEvent &arg ); virtual bool mouseMoved( const MouseEvent &arg ); virtual bool mousePressed( const MouseEvent &arg, MouseButtonID id ); virtual bool mouseReleased( const MouseEvent &arg, MouseButtonID id ); virtual bool processUnbufferedMouseInput(const FrameEvent& evt); virtual bool processUnbufferedKeyInput(const FrameEvent& evt); virtual void moveCamera(); public: void setupEventHandlers(void); bool quit(const CEGUI::EventArgs& e); bool selectserver(const CEGUI::EventArgs& e); //class of Ogre system Root* m_pRoot; Camera* m_pCamera; SceneManager* m_pSecneMgr; RenderWindow* m_pWindow; //OIS Input devices OIS::Mouse* m_pMouse; OIS::Keyboard* m_pKeyboard; OIS::InputManager* m_pInputManager; //translate camera Ogre::Vector3 m_TranslateVector; //circumrotate camera Radian m_RotX, m_RotY; //move speed Real m_MoveSpeed; Degree m_RotateSpeed; //move scale is m_MoveSpeed * (t2-t1) float m_MoveScale; Degree m_RotScale; private: //continue to rander bool m_bContinue; //show system mouse in the borderline of windows bool m_bSysMouseShowFlag; }; CSGameApp::CSGameApp() :m_pRoot(0), m_pMouse(0), m_pKeyboard(0), m_pInputManager(0), m_TranslateVector(Ogre::Vector3::ZERO), m_RotX(0), m_RotY(0) { m_bContinue = true; m_bSysMouseShowFlag = false; } void CSGameApp::Go() { if(!Initialise()) { return ; } m_pRoot->startRendering(); //clean up DestoryScene(); } CSGameApp::~CSGameApp() { //Remove ourself as a Window listener WindowEventUtilities::removeWindowEventListener(m_pWindow, this); if( m_pInputManager ) { m_pInputManager->destroyInputObject( m_pMouse ); m_pInputManager->destroyInputObject( m_pKeyboard ); OIS::InputManager::destroyInputSystem(m_pInputManager); m_pInputManager = 0; } delete m_pRoot; } bool CSGameApp::Initialise() { m_pRoot = new Root(); if(!m_pRoot) { return false; } //add rescource location AddResourceLocations(); // if we cannot initialise Ogre, just abandon the whole deal //--------------------------------------------------------- //do not show choose dialog,like this: //m_pRoot->restoreConfig(); //--------------------------------------------------------- if(!InitOgreCore()) { return false; } CreateSceneManager(); CreateCamera(); CreateViewports(); // Set default mipmap level (NB some APIs ignore this) TextureManager::getSingleton().setDefaultNumMipmaps(5); // Create any resource listeners (for loading screens) CreateResourceListener(); // Initialise resources InitResource(); // Create the scene CreateScene(); CreateFrameListener(); return true; } bool CSGameApp::InitOgreCore() { // Show the configuration dialog and initialise the system // You can skip this and use root.restoreConfig() to load configuration // settings if you were sure there are valid ones saved in ogre.cfg //Register as a Window listener if(m_pRoot->restoreConfig() || m_pRoot->showConfigDialog()) { // If returned true, user clicked OK so initialise // Here we choose to let the system create a default rendering window by passing 'true' m_pWindow = m_pRoot->initialise(true); WindowEventUtilities::addWindowEventListener(m_pWindow, this); OIS::ParamList pl; size_t windowHnd = 0; std::ostringstream windowHndStr; m_pWindow->getCustomAttribute("WINDOW", &windowHnd); windowHndStr << windowHnd; pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); //show the mouse pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE"))); pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND"))); m_pInputManager = OIS::InputManager::createInputSystem( pl ); //Create all devices (We only catch joystick exceptions here, as, most people have Key/Mouse) m_pKeyboard = static_cast<OIS::Keyboard*>(m_pInputManager->createInputObject( OIS::OISKeyboard, true )); m_pMouse = static_cast<OIS::Mouse*>(m_pInputManager->createInputObject( OIS::OISMouse, true )); m_pMouse->setEventCallback(this); m_pKeyboard->setEventCallback(this); ShowCursor(0); //Set initial mouse clipping size windowResized(m_pWindow); return true; } else { return false; } return true; } void CSGameApp::CreateSceneManager() { //Create the SceneManager, in this case a generic one m_pSecneMgr = m_pRoot->createSceneManager(ST_GENERIC); } void CSGameApp::CreateCamera() { //Create the camera m_pCamera = m_pSecneMgr->createCamera("PlayerCam"); //Position it at 500 in Z direction m_pCamera->setPosition(Ogre::Vector3(0,0,500)); //Look back along -Z m_pCamera->lookAt(Ogre::Vector3(0,0,-300)); m_pCamera->setNearClipDistance(5); } void CSGameApp::CreateViewports() { //Create one viewport, entire window Viewport* vp = m_pWindow->addViewport(m_pCamera); vp->setBackgroundColour(ColourValue(0,0,0)); //Alter the camera aspect ratio to match the viewport m_pCamera->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight())); } void CSGameApp::CreateResourceListener() { //... } void CSGameApp::CreateFrameListener() { m_RotateSpeed = 36; m_MoveSpeed = 100; m_MoveScale = 0.0f; m_RotScale = 0.0f; m_pRoot->addFrameListener(this); } void CSGameApp::AddResourceLocations() { //Load resource paths from config file ConfigFile cf; cf.load("resources.cfg"); //Go through all sections & settings in the file ConfigFile::SectionIterator seci = cf.getSectionIterator(); Ogre::String secName, typeName, archName; while (seci.hasMoreElements()) { secName = seci.peekNextKey(); ConfigFile::SettingsMultiMap *settings = seci.getNext(); ConfigFile::SettingsMultiMap::iterator i; for (i = settings->begin(); i != settings->end(); ++i) { typeName = i->first; archName = i->second; ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName); } } } void CSGameApp::InitResource() { //Initialise, parse scripts etc ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); } void CSGameApp::DestoryScene() { } bool CSGameApp::frameStarted(const FrameEvent& evt) { m_pKeyboard->capture(); m_pMouse->capture(); if(m_pWindow->isClosed()) { return false; } if(processUnbufferedKeyInput(evt) == false) { return false; } if(processUnbufferedMouseInput(evt) == false) { return false; } // If this is the first frame, pick a speed if (evt.timeSinceLastFrame == 0) { m_MoveScale = 1; m_RotScale = 0.1; } // Otherwise scale movement units by time passed since last frame else { // Move about 100 units per second, m_MoveScale = m_MoveSpeed * evt.timeSinceLastFrame; // Take about 10 seconds for full rotation m_RotScale = m_RotateSpeed * evt.timeSinceLastFrame; } moveCamera(); return m_bContinue && !m_pKeyboard->isKeyDown(OIS::KC_ESCAPE); } bool CSGameApp::frameEnded(const FrameEvent& evt) { return true; } bool CSGameApp::keyPressed( const KeyEvent &arg ) { CEGUI::System *sys = CEGUI::System::getSingletonPtr(); sys->injectKeyDown(arg.key); sys->injectChar(arg.text); return true; } bool CSGameApp::keyReleased( const KeyEvent &arg ) { CEGUI::System::getSingleton().injectKeyUp(arg.key); return true; } bool CSGameApp::mouseMoved( const MouseEvent &arg ) { m_pMouse->getMouseState().width = m_pWindow->getWidth(); m_pMouse->getMouseState().height = m_pWindow->getHeight(); if(arg.state.X.abs < m_pWindow->getViewport(0)->getActualLeft()+1 ||(arg.state.Y.abs < m_pWindow->getViewport(0)->getActualTop()+1) ||(arg.state.X.abs > m_pWindow->getViewport(0)->getActualLeft()+m_pWindow->getViewport(0)->getActualWidth()) ||(arg.state.Y.abs > m_pWindow->getViewport(0)->getActualTop() +m_pWindow->getViewport(0)->getActualHeight())) { if(!m_bSysMouseShowFlag) { ShowCursor(1); CEGUI::MouseCursor::getSingleton().hide(); m_bSysMouseShowFlag = true; } } else { if(m_bSysMouseShowFlag) { ShowCursor(0); CEGUI::MouseCursor::getSingleton().show(); m_bSysMouseShowFlag = false; } } CEGUI::System::getSingleton().injectMousePosition( arg.state.X.abs, arg.state.Y.abs ); return true; } bool CSGameApp::mousePressed( const MouseEvent &arg, MouseButtonID id ) { CEGUI::System::getSingleton().injectMouseButtonDown(convertButton(id)); return true; } bool CSGameApp::mouseReleased( const MouseEvent &arg, MouseButtonID id ) { CEGUI::System::getSingleton().injectMouseButtonUp(convertButton(id)); return true; } bool CSGameApp::processUnbufferedMouseInput(const FrameEvent& evt) { // Rotation factors, may not be used if the second mouse button is pressed // 2nd mouse button - slide, otherwise rotate const OIS::MouseState &ms = m_pMouse->getMouseState(); if( ms.buttonDown( OIS::MB_Right ) ) { m_TranslateVector.x += ms.X.rel * 0.13; m_TranslateVector.y -= ms.Y.rel * 0.13; } else { m_RotX = Degree(-ms.X.rel * 0.13); m_RotY = Degree(-ms.Y.rel * 0.13); } return true; } bool CSGameApp::processUnbufferedKeyInput(const FrameEvent& evt) { if(m_pKeyboard->isKeyDown(OIS::KC_A)) { m_TranslateVector.x = -m_MoveScale; // Move camera left } if(m_pKeyboard->isKeyDown(OIS::KC_D)) { m_TranslateVector.x = m_MoveScale; // Move camera RIGHT } if(m_pKeyboard->isKeyDown(OIS::KC_UP) || m_pKeyboard->isKeyDown(OIS::KC_W) ) { m_TranslateVector.z = -m_MoveScale; // Move camera forward } if(m_pKeyboard->isKeyDown(OIS::KC_DOWN) || m_pKeyboard->isKeyDown(OIS::KC_S) ) { m_TranslateVector.z = m_MoveScale; // Move camera backward } if(m_pKeyboard->isKeyDown(OIS::KC_PGUP)) { m_TranslateVector.y = m_MoveScale; // Move camera up } if(m_pKeyboard->isKeyDown(OIS::KC_PGDOWN)) { m_TranslateVector.y = -m_MoveScale; // Move camera down } if(m_pKeyboard->isKeyDown(OIS::KC_RIGHT)) { m_pCamera->yaw(-m_RotScale); } if(m_pKeyboard->isKeyDown(OIS::KC_LEFT)) { m_pCamera->yaw(m_RotScale); } if( m_pKeyboard->isKeyDown(OIS::KC_ESCAPE) || m_pKeyboard->isKeyDown(OIS::KC_Q) ) { return false; } return true; } void CSGameApp::moveCamera() { // Make all the changes to the camera // Note that YAW direction is around a fixed axis (freelook style) rather than a natural YAW (e.g. airplane) m_pCamera->yaw(m_RotX); m_pCamera->pitch(m_RotY); m_pCamera->moveRelative(m_TranslateVector); } bool CSGameApp::quit(const CEGUI::EventArgs& e) { CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton(); CEGUI::String strAccount = wmgr.getWindow((CEGUI::utf8*)"Game/Edit/Account")->getText(); m_bContinue = false; return true; } bool CSGameApp::selectserver(const CEGUI::EventArgs& e) { CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton(); //hide all exist windows wmgr.getWindow((CEGUI::utf8*)"Game/Edit/Account")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/Edit/Password")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/Login/Ok")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/Login/Cancel")->hide(); DefaultWindow* root = (DefaultWindow*)wmgr.getWindow("Root"); Listbox* wndListboxSelectServer = (Listbox*)wmgr.createWindow("TaharezLook/Listbox", "Game/Button/Login/SelectServer"); root->addChildWindow(wndListboxSelectServer); wndListboxSelectServer->setPosition(UVector2(cegui_reldim(0.0f), cegui_reldim( 0.0f))); wndListboxSelectServer->setSize(UVector2(cegui_reldim(0.30f), cegui_reldim( 0.5f))); //TEST wndListboxSelectServer->hide(); FrameWindow* wndTest = (FrameWindow*)wmgr.createWindow("TaharezLook/StaticImage", "Game/Test"); root->addChildWindow(wndTest); wndTest->setPosition(UVector2(cegui_reldim(0.0f), cegui_reldim( 0.0f))); wndTest->setSize(UVector2(cegui_reldim(1.00f), cegui_reldim( 1.0f))); //add new windows return true; } void CSGameApp::setupEventHandlers(void) { CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/Login/Ok") ->subscribeEvent( CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&CSGameApp::quit, this)); wmgr.getWindow((CEGUI::utf8*)"Game/Button/Login/Cancel") ->subscribeEvent( CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&CSGameApp::selectserver, this)); } class CSGame : public CSGameApp { public: CSGame(); void CreateScene(); bool frameStarted(const FrameEvent& evt); ~CSGame(); private: Entity* m_pOgreHead; SceneNode* m_pHeadNode; private: CEGUI::OgreCEGUIRenderer* m_pGUIRenderer; CEGUI::System* m_pGUISystem; }; CSGame::CSGame() :m_pOgreHead(0), m_pHeadNode(0) { } void CSGame::CreateScene() { m_pOgreHead = m_pSecneMgr->createEntity("Head", "ogrehead.mesh"); m_pHeadNode = m_pSecneMgr->getRootSceneNode()->createChildSceneNode(); m_pHeadNode->attachObject(m_pOgreHead); // Set ambient light m_pSecneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5)); // Create a skydome m_pSecneMgr->setSkyDome(true, "Examples/CloudySky", 5, 8); // Create a light Light* l = m_pSecneMgr->createLight("MainLight"); l->setPosition(20,80,50); //setup cegui m_pGUIRenderer = new CEGUI::OgreCEGUIRenderer(m_pWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, m_pSecneMgr); m_pGUISystem = new CEGUI::System(m_pGUIRenderer); WindowManager& winMgr = WindowManager::getSingleton(); // load scheme and set up defaults CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLookSkin.scheme"); CEGUI::System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow"); // load an image to use as a background ImagesetManager::getSingleton().createImagesetFromImageFile("BackgroundImage", "ogrelogo.png"); // here we will use a StaticImage as the root, then we can use it to place a background image Window* background = winMgr.createWindow ("TaharezLook/StaticImage"); // set area rectangle background->setArea (URect (cegui_reldim (0), cegui_reldim (0), cegui_reldim (1), cegui_reldim (1))); // disable frame and standard background background->setProperty ("FrameEnabled", "false"); background->setProperty ("BackgroundEnabled", "false"); // set the background image background->setProperty ("Image", "set:BackgroundImage image:full_image"); // install this as the root GUI sheet System::getSingleton ().setGUISheet (background); /* //--------------------------------------------------------------------------------------------- // Create a DefaultWindow called 'Root'. DefaultWindow* root = (DefaultWindow*)winMgr.createWindow("DefaultWindow", "Root"); // set the GUI root window (also known as the GUI "sheet"), so the gui we set up // will be visible. System::getSingleton().setGUISheet(root); // Create a FrameWindow in the TaharezLook style, and name it 'Button Ok' FrameWindow* wndButtonOK = (FrameWindow*)winMgr.createWindow("TaharezLook/Button", "Game/Button/Login/Ok"); // Here we attach the newly created FrameWindow to the previously created // DefaultWindow which we will be using as the root of the displayed gui. root->addChildWindow(wndButtonOK); wndButtonOK->setPosition(UVector2(cegui_reldim(0.35f), cegui_reldim( 0.6f))); wndButtonOK->setSize(UVector2(cegui_reldim(0.10f), cegui_reldim( 0.03f))); wndButtonOK->setText("OK"); //--------------------------------------------------------------------------------------------- // Create a FrameWindow in the TaharezLook style, and name it 'Button Cancel' FrameWindow* wndButtonCancel = (FrameWindow*)winMgr.createWindow("TaharezLook/Button", "Game/Button/Login/Cancel"); // Here we attach the newly created FrameWindow to the previously created // DefaultWindow which we will be using as the root of the displayed gui. root->addChildWindow(wndButtonCancel); wndButtonCancel->setPosition(UVector2(cegui_reldim(0.55f), cegui_reldim( 0.6f))); wndButtonCancel->setSize(UVector2(cegui_reldim(0.10f), cegui_reldim( 0.03f))); wndButtonCancel->setText("Cancel"); //--------------------------------------------------------------------------------------------- //add a editbox FrameWindow* wndEditAccount = (FrameWindow*)winMgr.createWindow("TaharezLook/Editbox", "Game/Edit/Account"); root->addChildWindow(wndEditAccount); wndEditAccount->setPosition(UVector2(cegui_reldim(0.35f), cegui_reldim( 0.5f))); wndEditAccount->setSize(UVector2(cegui_reldim(0.30f), cegui_reldim( 0.04f))); //--------------------------------------------------------------------------------------------- //add a editbox FrameWindow* wndEditPwd = (FrameWindow*)winMgr.createWindow("TaharezLook/Editbox", "Game/Edit/Password"); root->addChildWindow(wndEditPwd); wndEditPwd->setPosition(UVector2(cegui_reldim(0.35f), cegui_reldim( 0.55f))); wndEditPwd->setSize(UVector2(cegui_reldim(0.30f), cegui_reldim( 0.04f))); setupEventHandlers(); */ } bool CSGame::frameStarted(const FrameEvent& evt) { // Just a silly example to demonstrate how much easier this is than passing objects to an external framelistener m_pHeadNode->translate(0.0f, 0.005f, 0.0f); return CSGameApp::frameStarted(evt); } CSGame::~CSGame() { } INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT ) { // Create application object CSGame app; try { app.Go(); } catch( Ogre::Exception& e ) { MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL); } return 0; }
#ifndef _Ogre_H__ #include <ogre.h> #endif //_Ogre_H__ #ifndef OIS_OISALL_H #include <OIS/OIS.h> #endif //OIS_OISALL_H #ifndef _CEGUI_h_ #include <CEGUI.h> #endif //_CEGUI_h_ #ifndef _OgreCEGUIRenderer_h_ #include "OgreCEGUIRenderer.h" #endif //_OgreCEGUIRenderer_h_ using namespace Ogre; using namespace OIS; using namespace CEGUI; CEGUI::MouseButton convertButton(OIS::MouseButtonID buttonID) { switch (buttonID) { case OIS::MB_Left: return CEGUI::LeftButton; case OIS::MB_Right: return CEGUI::RightButton; case OIS::MB_Middle: return CEGUI::MiddleButton; default: return CEGUI::LeftButton; } } class CSGameApp : public FrameListener, public OIS::KeyListener, public OIS::MouseListener, public WindowEventListener { public: CSGameApp(); virtual void Go(); virtual ~CSGameApp(); protected: virtual bool Initialise(); virtual bool InitOgreCore(); virtual void CreateSceneManager(); virtual void CreateCamera(); virtual void CreateViewports(); virtual void CreateResourceListener(); virtual void CreateFrameListener(); virtual void AddResourceLocations(); virtual void InitResource(); //I am pure virtual, override me! virtual void CreateScene() = 0; virtual void DestoryScene(); //FrameListener overrides virtual bool frameStarted(const FrameEvent& evt); virtual bool frameEnded(const FrameEvent& evt); virtual bool keyPressed( const KeyEvent &arg ); virtual bool keyReleased( const KeyEvent &arg ); virtual bool mouseMoved( const MouseEvent &arg ); virtual bool mousePressed( const MouseEvent &arg, MouseButtonID id ); virtual bool mouseReleased( const MouseEvent &arg, MouseButtonID id ); virtual bool processUnbufferedMouseInput(const FrameEvent& evt); virtual bool processUnbufferedKeyInput(const FrameEvent& evt); virtual void moveCamera(); public: void setupEventHandlers(void); bool quit(const CEGUI::EventArgs& e); bool selectserver(const CEGUI::EventArgs& e); //class of Ogre system Root* m_pRoot; Camera* m_pCamera; SceneManager* m_pSecneMgr; RenderWindow* m_pWindow; //OIS Input devices OIS::Mouse* m_pMouse; OIS::Keyboard* m_pKeyboard; OIS::InputManager* m_pInputManager; //translate camera Ogre::Vector3 m_TranslateVector; //circumrotate camera Radian m_RotX, m_RotY; //move speed Real m_MoveSpeed; Degree m_RotateSpeed; //move scale is m_MoveSpeed * (t2-t1) float m_MoveScale; Degree m_RotScale; private: //continue to rander bool m_bContinue; //show system mouse in the borderline of windows bool m_bSysMouseShowFlag; }; CSGameApp::CSGameApp() :m_pRoot(0), m_pMouse(0), m_pKeyboard(0), m_pInputManager(0), m_TranslateVector(Ogre::Vector3::ZERO), m_RotX(0), m_RotY(0) { m_bContinue = true; m_bSysMouseShowFlag = false; } void CSGameApp::Go() { if(!Initialise()) { return ; } m_pRoot->startRendering(); //clean up DestoryScene(); } CSGameApp::~CSGameApp() { //Remove ourself as a Window listener WindowEventUtilities::removeWindowEventListener(m_pWindow, this); if( m_pInputManager ) { m_pInputManager->destroyInputObject( m_pMouse ); m_pInputManager->destroyInputObject( m_pKeyboard ); OIS::InputManager::destroyInputSystem(m_pInputManager); m_pInputManager = 0; } delete m_pRoot; } bool CSGameApp::Initialise() { m_pRoot = new Root(); if(!m_pRoot) { return false; } //add rescource location AddResourceLocations(); // if we cannot initialise Ogre, just abandon the whole deal //--------------------------------------------------------- //do not show choose dialog,like this: //m_pRoot->restoreConfig(); //--------------------------------------------------------- if(!InitOgreCore()) { return false; } CreateSceneManager(); CreateCamera(); CreateViewports(); // Set default mipmap level (NB some APIs ignore this) TextureManager::getSingleton().setDefaultNumMipmaps(5); // Create any resource listeners (for loading screens) CreateResourceListener(); // Initialise resources InitResource(); // Create the scene CreateScene(); CreateFrameListener(); return true; } bool CSGameApp::InitOgreCore() { // Show the configuration dialog and initialise the system // You can skip this and use root.restoreConfig() to load configuration // settings if you were sure there are valid ones saved in ogre.cfg //Register as a Window listener if(m_pRoot->restoreConfig() || m_pRoot->showConfigDialog()) { // If returned true, user clicked OK so initialise // Here we choose to let the system create a default rendering window by passing 'true' m_pWindow = m_pRoot->initialise(true); WindowEventUtilities::addWindowEventListener(m_pWindow, this); OIS::ParamList pl; size_t windowHnd = 0; std::ostringstream windowHndStr; m_pWindow->getCustomAttribute("WINDOW", &windowHnd); windowHndStr << windowHnd; pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); //show the mouse pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE"))); pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND"))); m_pInputManager = OIS::InputManager::createInputSystem( pl ); //Create all devices (We only catch joystick exceptions here, as, most people have Key/Mouse) m_pKeyboard = static_cast<OIS::Keyboard*>(m_pInputManager->createInputObject( OIS::OISKeyboard, true )); m_pMouse = static_cast<OIS::Mouse*>(m_pInputManager->createInputObject( OIS::OISMouse, true )); m_pMouse->setEventCallback(this); m_pKeyboard->setEventCallback(this); ShowCursor(0); //Set initial mouse clipping size windowResized(m_pWindow); return true; } else { return false; } return true; } void CSGameApp::CreateSceneManager() { //Create the SceneManager, in this case a generic one m_pSecneMgr = m_pRoot->createSceneManager(ST_GENERIC); } void CSGameApp::CreateCamera() { //Create the camera m_pCamera = m_pSecneMgr->createCamera("PlayerCam"); //Position it at 500 in Z direction m_pCamera->setPosition(Ogre::Vector3(0,0,500)); //Look back along -Z m_pCamera->lookAt(Ogre::Vector3(0,0,-300)); m_pCamera->setNearClipDistance(5); } void CSGameApp::CreateViewports() { //Create one viewport, entire window Viewport* vp = m_pWindow->addViewport(m_pCamera); vp->setBackgroundColour(ColourValue(0,0,0)); //Alter the camera aspect ratio to match the viewport m_pCamera->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight())); } void CSGameApp::CreateResourceListener() { //... } void CSGameApp::CreateFrameListener() { m_RotateSpeed = 36; m_MoveSpeed = 100; m_MoveScale = 0.0f; m_RotScale = 0.0f; m_pRoot->addFrameListener(this); } void CSGameApp::AddResourceLocations() { //Load resource paths from config file ConfigFile cf; cf.load("resources.cfg"); //Go through all sections & settings in the file ConfigFile::SectionIterator seci = cf.getSectionIterator(); Ogre::String secName, typeName, archName; while (seci.hasMoreElements()) { secName = seci.peekNextKey(); ConfigFile::SettingsMultiMap *settings = seci.getNext(); ConfigFile::SettingsMultiMap::iterator i; for (i = settings->begin(); i != settings->end(); ++i) { typeName = i->first; archName = i->second; ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName); } } } void CSGameApp::InitResource() { //Initialise, parse scripts etc ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); } void CSGameApp::DestoryScene() { } bool CSGameApp::frameStarted(const FrameEvent& evt) { m_pKeyboard->capture(); m_pMouse->capture(); if(m_pWindow->isClosed()) { return false; } if(processUnbufferedKeyInput(evt) == false) { return false; } if(processUnbufferedMouseInput(evt) == false) { return false; } // If this is the first frame, pick a speed if (evt.timeSinceLastFrame == 0) { m_MoveScale = 1; m_RotScale = 0.1; } // Otherwise scale movement units by time passed since last frame else { // Move about 100 units per second, m_MoveScale = m_MoveSpeed * evt.timeSinceLastFrame; // Take about 10 seconds for full rotation m_RotScale = m_RotateSpeed * evt.timeSinceLastFrame; } moveCamera(); return m_bContinue && !m_pKeyboard->isKeyDown(OIS::KC_ESCAPE); } bool CSGameApp::frameEnded(const FrameEvent& evt) { return true; } bool CSGameApp::keyPressed( const KeyEvent &arg ) { CEGUI::System *sys = CEGUI::System::getSingletonPtr(); sys->injectKeyDown(arg.key); sys->injectChar(arg.text); return true; } bool CSGameApp::keyReleased( const KeyEvent &arg ) { CEGUI::System::getSingleton().injectKeyUp(arg.key); return true; } bool CSGameApp::mouseMoved( const MouseEvent &arg ) { m_pMouse->getMouseState().width = m_pWindow->getWidth(); m_pMouse->getMouseState().height = m_pWindow->getHeight(); if(arg.state.X.abs < m_pWindow->getViewport(0)->getActualLeft()+1 ||(arg.state.Y.abs < m_pWindow->getViewport(0)->getActualTop()+1) ||(arg.state.X.abs > m_pWindow->getViewport(0)->getActualLeft()+m_pWindow->getViewport(0)->getActualWidth()) ||(arg.state.Y.abs > m_pWindow->getViewport(0)->getActualTop() +m_pWindow->getViewport(0)->getActualHeight())) { if(!m_bSysMouseShowFlag) { ShowCursor(1); CEGUI::MouseCursor::getSingleton().hide(); m_bSysMouseShowFlag = true; } } else { if(m_bSysMouseShowFlag) { ShowCursor(0); CEGUI::MouseCursor::getSingleton().show(); m_bSysMouseShowFlag = false; } } CEGUI::System::getSingleton().injectMousePosition( arg.state.X.abs, arg.state.Y.abs ); return true; } bool CSGameApp::mousePressed( const MouseEvent &arg, MouseButtonID id ) { CEGUI::System::getSingleton().injectMouseButtonDown(convertButton(id)); return true; } bool CSGameApp::mouseReleased( const MouseEvent &arg, MouseButtonID id ) { CEGUI::System::getSingleton().injectMouseButtonUp(convertButton(id)); return true; } bool CSGameApp::processUnbufferedMouseInput(const FrameEvent& evt) { // Rotation factors, may not be used if the second mouse button is pressed // 2nd mouse button - slide, otherwise rotate const OIS::MouseState &ms = m_pMouse->getMouseState(); if( ms.buttonDown( OIS::MB_Right ) ) { m_TranslateVector.x += ms.X.rel * 0.13; m_TranslateVector.y -= ms.Y.rel * 0.13; } else { m_RotX = Degree(-ms.X.rel * 0.13); m_RotY = Degree(-ms.Y.rel * 0.13); } return true; } bool CSGameApp::processUnbufferedKeyInput(const FrameEvent& evt) { if(m_pKeyboard->isKeyDown(OIS::KC_A)) { m_TranslateVector.x = -m_MoveScale; // Move camera left } if(m_pKeyboard->isKeyDown(OIS::KC_D)) { m_TranslateVector.x = m_MoveScale; // Move camera RIGHT } if(m_pKeyboard->isKeyDown(OIS::KC_UP) || m_pKeyboard->isKeyDown(OIS::KC_W) ) { m_TranslateVector.z = -m_MoveScale; // Move camera forward } if(m_pKeyboard->isKeyDown(OIS::KC_DOWN) || m_pKeyboard->isKeyDown(OIS::KC_S) ) { m_TranslateVector.z = m_MoveScale; // Move camera backward } if(m_pKeyboard->isKeyDown(OIS::KC_PGUP)) { m_TranslateVector.y = m_MoveScale; // Move camera up } if(m_pKeyboard->isKeyDown(OIS::KC_PGDOWN)) { m_TranslateVector.y = -m_MoveScale; // Move camera down } if(m_pKeyboard->isKeyDown(OIS::KC_RIGHT)) { m_pCamera->yaw(-m_RotScale); } if(m_pKeyboard->isKeyDown(OIS::KC_LEFT)) { m_pCamera->yaw(m_RotScale); } if( m_pKeyboard->isKeyDown(OIS::KC_ESCAPE) || m_pKeyboard->isKeyDown(OIS::KC_Q) ) { return false; } return true; } void CSGameApp::moveCamera() { // Make all the changes to the camera // Note that YAW direction is around a fixed axis (freelook style) rather than a natural YAW (e.g. airplane) m_pCamera->yaw(m_RotX); m_pCamera->pitch(m_RotY); m_pCamera->moveRelative(m_TranslateVector); } bool CSGameApp::quit(const CEGUI::EventArgs& e) { CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton(); CEGUI::String strAccount = wmgr.getWindow((CEGUI::utf8*)"Game/Edit/Account")->getText(); m_bContinue = false; return true; } bool CSGameApp::selectserver(const CEGUI::EventArgs& e) { CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton(); //hide all exist windows wmgr.getWindow((CEGUI::utf8*)"Game/Edit/Account")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/Edit/Password")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/Login/Ok")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/Login/Cancel")->hide(); Window* background = (DefaultWindow*)wmgr.getWindow("Background"); Listbox* wndListboxSelectServer = (Listbox*)wmgr.createWindow("TaharezLook/Listbox", "Game/Button/Login/SelectServer"); background->addChildWindow(wndListboxSelectServer); wndListboxSelectServer->setPosition(UVector2(cegui_reldim(0.0f), cegui_reldim( 0.0f))); wndListboxSelectServer->setSize(UVector2(cegui_reldim(0.30f), cegui_reldim( 0.5f))); return true; } void CSGameApp::setupEventHandlers(void) { CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/Login/Ok") ->subscribeEvent( CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&CSGameApp::selectserver, this)); wmgr.getWindow((CEGUI::utf8*)"Game/Button/Login/Cancel") ->subscribeEvent( CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&CSGameApp::quit, this)); } class CSGame : public CSGameApp { public: CSGame(); void CreateScene(); bool frameStarted(const FrameEvent& evt); ~CSGame(); private: Entity* m_pOgreHead; SceneNode* m_pHeadNode; private: CEGUI::OgreCEGUIRenderer* m_pGUIRenderer; CEGUI::System* m_pGUISystem; }; CSGame::CSGame() :m_pOgreHead(0), m_pHeadNode(0) { } void CSGame::CreateScene() { m_pOgreHead = m_pSecneMgr->createEntity("Head", "ogrehead.mesh"); m_pHeadNode = m_pSecneMgr->getRootSceneNode()->createChildSceneNode(); m_pHeadNode->attachObject(m_pOgreHead); // Set ambient light m_pSecneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5)); // Create a skydome m_pSecneMgr->setSkyDome(true, "Examples/CloudySky", 5, 8); // Create a light Light* l = m_pSecneMgr->createLight("MainLight"); l->setPosition(20,80,50); //setup cegui m_pGUIRenderer = new CEGUI::OgreCEGUIRenderer(m_pWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, m_pSecneMgr); m_pGUISystem = new CEGUI::System(m_pGUIRenderer); WindowManager& winMgr = WindowManager::getSingleton(); // load scheme and set up defaults CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLookSkin.scheme"); CEGUI::System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow"); // load an image to use as a background ImagesetManager::getSingleton().createImagesetFromImageFile("BackgroundImage", "loading 1.jpg"); // here we will use a StaticImage as the root, then we can use it to place a background image Window* background = winMgr.createWindow ("TaharezLook/StaticImage","Background"); // set area rectangle background->setArea (URect (cegui_reldim (0), cegui_reldim (0), cegui_reldim (1), cegui_reldim (1))); // disable frame and standard background background->setProperty ("FrameEnabled", "false"); background->setProperty ("BackgroundEnabled", "false"); // set the background image background->setProperty ("Image", "set:BackgroundImage image:full_image"); // install this as the root GUI sheet System::getSingleton ().setGUISheet (background); // Create a FrameWindow in the TaharezLook style, and name it 'Button Ok' FrameWindow* wndButtonOK = (FrameWindow*)winMgr.createWindow("TaharezLook/Button", "Game/Button/Login/Ok"); // Here we attach the newly created FrameWindow to the previously created // DefaultWindow which we will be using as the root of the displayed gui. background->addChildWindow(wndButtonOK); wndButtonOK->setPosition(UVector2(cegui_reldim(0.35f), cegui_reldim( 0.6f))); wndButtonOK->setSize(UVector2(cegui_reldim(0.10f), cegui_reldim( 0.03f))); wndButtonOK->setText("OK"); // Create a FrameWindow in the TaharezLook style, and name it 'Button Cancel' FrameWindow* wndButtonCancel = (FrameWindow*)winMgr.createWindow("TaharezLook/Button", "Game/Button/Login/Cancel"); // Here we attach the newly created FrameWindow to the previously created // DefaultWindow which we will be using as the root of the displayed gui. background->addChildWindow(wndButtonCancel); wndButtonCancel->setPosition(UVector2(cegui_reldim(0.55f), cegui_reldim( 0.6f))); wndButtonCancel->setSize(UVector2(cegui_reldim(0.10f), cegui_reldim( 0.03f))); wndButtonCancel->setText("Cancel"); //add a editbox FrameWindow* wndEditAccount = (FrameWindow*)winMgr.createWindow("TaharezLook/Editbox", "Game/Edit/Account"); background->addChildWindow(wndEditAccount); wndEditAccount->setPosition(UVector2(cegui_reldim(0.35f), cegui_reldim( 0.5f))); wndEditAccount->setSize(UVector2(cegui_reldim(0.30f), cegui_reldim( 0.04f))); //add a editbox FrameWindow* wndEditPwd = (FrameWindow*)winMgr.createWindow("TaharezLook/Editbox", "Game/Edit/Password"); background->addChildWindow(wndEditPwd); wndEditPwd->setPosition(UVector2(cegui_reldim(0.35f), cegui_reldim( 0.55f))); wndEditPwd->setSize(UVector2(cegui_reldim(0.30f), cegui_reldim( 0.04f))); setupEventHandlers(); } bool CSGame::frameStarted(const FrameEvent& evt) { // Just a silly example to demonstrate how much easier this is than passing objects to an external framelistener m_pHeadNode->translate(0.0f, 0.005f, 0.0f); return CSGameApp::frameStarted(evt); } CSGame::~CSGame() { } INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT ) { // Create application object CSGame app; try { app.Go(); } catch( Ogre::Exception& e ) { MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL); } return 0; }
2010-08-01 09:32:19
#include <windows.h> #ifndef _Ogre_H__ #include <ogre.h> #endif //_Ogre_H__ #ifndef OIS_OISALL_H #include <OIS/OIS.h> #endif //OIS_OISALL_H #ifndef _CEGUI_h_ #include <CEGUI.h> #endif //_CEGUI_h_ #ifndef _OgreCEGUIRenderer_h_ #include "OgreCEGUIRenderer.h" #endif //_OgreCEGUIRenderer_h_ using namespace Ogre; using namespace OIS; using namespace CEGUI; CEGUI::MouseButton convertButton(OIS::MouseButtonID buttonID) { switch (buttonID) { case OIS::MB_Left: return CEGUI::LeftButton; case OIS::MB_Right: return CEGUI::RightButton; case OIS::MB_Middle: return CEGUI::MiddleButton; default: return CEGUI::LeftButton; } } class CSGameApp : public FrameListener, public OIS::KeyListener, public OIS::MouseListener, public WindowEventListener { public: CSGameApp(); virtual void Go(); virtual ~CSGameApp(); protected: virtual bool Initialise(); virtual bool InitOgreCore(); virtual void CreateSceneManager(); virtual void CreateCamera(); virtual void CreateViewports(); virtual void CreateResourceListener(); virtual void CreateFrameListener(); virtual void AddResourceLocations(); virtual void InitResource(); //I am pure virtual, override me! virtual void CreateScene() = 0; virtual void DestoryScene(); //FrameListener overrides virtual bool frameStarted(const FrameEvent& evt); virtual bool frameEnded(const FrameEvent& evt); virtual bool keyPressed( const KeyEvent &arg ); virtual bool keyReleased( const KeyEvent &arg ); virtual bool mouseMoved( const MouseEvent &arg ); virtual bool mousePressed( const MouseEvent &arg, MouseButtonID id ); virtual bool mouseReleased( const MouseEvent &arg, MouseButtonID id ); virtual bool processUnbufferedMouseInput(const FrameEvent& evt); virtual bool processUnbufferedKeyInput(const FrameEvent& evt); virtual void moveCamera(); public: void setupEventHandlers(void); bool quit(const CEGUI::EventArgs& e); bool selectserver(const CEGUI::EventArgs& e); bool loading(const CEGUI::EventArgs& e); bool createchar(const CEGUI::EventArgs& e); bool returnselectserver(const CEGUI::EventArgs& e); bool entergame(const CEGUI::EventArgs& e); //class of Ogre system Root* m_pRoot; Camera* m_pCamera; SceneManager* m_pSecneMgr; RenderWindow* m_pWindow; //OIS Input devices OIS::Mouse* m_pMouse; OIS::Keyboard* m_pKeyboard; OIS::InputManager* m_pInputManager; //translate camera Ogre::Vector3 m_TranslateVector; //circumrotate camera Radian m_RotX, m_RotY; //move speed Real m_MoveSpeed; Degree m_RotateSpeed; //move scale is m_MoveSpeed * (t2-t1) float m_MoveScale; Degree m_RotScale; private: //continue to rander bool m_bContinue; //show system mouse in the borderline of windows bool m_bSysMouseShowFlag; private: static unsigned int __stdcall LoadingFunction(void *pV); HANDLE m_hThread; DWORD m_dwThreadID; CRITICAL_SECTION m_ThreadLock; }; CSGameApp::CSGameApp() :m_pRoot(0), m_pMouse(0), m_pKeyboard(0), m_pInputManager(0), m_TranslateVector(Ogre::Vector3::ZERO), m_RotX(0), m_RotY(0) { ::InitializeCriticalSection(&m_ThreadLock); m_bContinue = true; m_bSysMouseShowFlag = false; } void CSGameApp::Go() { if(!Initialise()) { return ; } m_pRoot->startRendering(); //clean up DestoryScene(); } CSGameApp::~CSGameApp() { //Remove ourself as a Window listener WindowEventUtilities::removeWindowEventListener(m_pWindow, this); if( m_pInputManager ) { m_pInputManager->destroyInputObject( m_pMouse ); m_pInputManager->destroyInputObject( m_pKeyboard ); OIS::InputManager::destroyInputSystem(m_pInputManager); m_pInputManager = 0; } delete m_pRoot; CloseHandle(m_hThread); ::DeleteCriticalSection(&m_ThreadLock); } bool CSGameApp::Initialise() { m_hThread = ::CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)LoadingFunction,this,CREATE_SUSPENDED,&m_dwThreadID); if(NULL == m_hThread) { MessageBox(NULL,"_beginthreadex exception!","Error",MB_OK); return false; } m_pRoot = new Root(); if(!m_pRoot) { return false; } //add rescource location AddResourceLocations(); // if we cannot initialise Ogre, just abandon the whole deal //--------------------------------------------------------- //do not show choose dialog,like this: //m_pRoot->restoreConfig(); //--------------------------------------------------------- if(!InitOgreCore()) { return false; } CreateSceneManager(); CreateCamera(); CreateViewports(); // Set default mipmap level (NB some APIs ignore this) TextureManager::getSingleton().setDefaultNumMipmaps(5); // Create any resource listeners (for loading screens) CreateResourceListener(); // Initialise resources InitResource(); // Create the scene CreateScene(); CreateFrameListener(); return true; } bool CSGameApp::InitOgreCore() { // Show the configuration dialog and initialise the system // You can skip this and use root.restoreConfig() to load configuration // settings if you were sure there are valid ones saved in ogre.cfg //Register as a Window listener if(m_pRoot->restoreConfig() || m_pRoot->showConfigDialog()) { // If returned true, user clicked OK so initialise // Here we choose to let the system create a default rendering window by passing 'true' m_pWindow = m_pRoot->initialise(true); WindowEventUtilities::addWindowEventListener(m_pWindow, this); OIS::ParamList pl; size_t windowHnd = 0; std::ostringstream windowHndStr; m_pWindow->getCustomAttribute("WINDOW", &windowHnd); windowHndStr << windowHnd; pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str())); //show the mouse pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE"))); pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND"))); m_pInputManager = OIS::InputManager::createInputSystem( pl ); //Create all devices (We only catch joystick exceptions here, as, most people have Key/Mouse) m_pKeyboard = static_cast<OIS::Keyboard*>(m_pInputManager->createInputObject( OIS::OISKeyboard, true )); m_pMouse = static_cast<OIS::Mouse*>(m_pInputManager->createInputObject( OIS::OISMouse, true )); m_pMouse->setEventCallback(this); m_pKeyboard->setEventCallback(this); ShowCursor(0); //Set initial mouse clipping size windowResized(m_pWindow); return true; } else { return false; } return true; } void CSGameApp::CreateSceneManager() { //Create the SceneManager, in this case a generic one m_pSecneMgr = m_pRoot->createSceneManager(ST_GENERIC); } void CSGameApp::CreateCamera() { //Create the camera m_pCamera = m_pSecneMgr->createCamera("PlayerCam"); //Position it at 500 in Z direction m_pCamera->setPosition(Ogre::Vector3(0,0,500)); //Look back along -Z m_pCamera->lookAt(Ogre::Vector3(0,0,-300)); m_pCamera->setNearClipDistance(5); } void CSGameApp::CreateViewports() { //Create one viewport, entire window Viewport* vp = m_pWindow->addViewport(m_pCamera); vp->setBackgroundColour(ColourValue(0,0,0)); //Alter the camera aspect ratio to match the viewport m_pCamera->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight())); } void CSGameApp::CreateResourceListener() { //... } void CSGameApp::CreateFrameListener() { m_RotateSpeed = 36; m_MoveSpeed = 100; m_MoveScale = 0.0f; m_RotScale = 0.0f; m_pRoot->addFrameListener(this); } void CSGameApp::AddResourceLocations() { //Load resource paths from config file ConfigFile cf; cf.load("resources.cfg"); //Go through all sections & settings in the file ConfigFile::SectionIterator seci = cf.getSectionIterator(); Ogre::String secName, typeName, archName; while (seci.hasMoreElements()) { secName = seci.peekNextKey(); ConfigFile::SettingsMultiMap *settings = seci.getNext(); ConfigFile::SettingsMultiMap::iterator i; for (i = settings->begin(); i != settings->end(); ++i) { typeName = i->first; archName = i->second; ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName); } } } void CSGameApp::InitResource() { //Initialise, parse scripts etc ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); } void CSGameApp::DestoryScene() { } bool CSGameApp::frameStarted(const FrameEvent& evt) { m_pKeyboard->capture(); m_pMouse->capture(); if(m_pWindow->isClosed()) { return false; } if(processUnbufferedKeyInput(evt) == false) { return false; } if(processUnbufferedMouseInput(evt) == false) { return false; } // If this is the first frame, pick a speed if (evt.timeSinceLastFrame == 0) { m_MoveScale = 1; m_RotScale = 0.1; } // Otherwise scale movement units by time passed since last frame else { // Move about 100 units per second, m_MoveScale = m_MoveSpeed * evt.timeSinceLastFrame; // Take about 10 seconds for full rotation m_RotScale = m_RotateSpeed * evt.timeSinceLastFrame; } moveCamera(); return m_bContinue && !m_pKeyboard->isKeyDown(OIS::KC_ESCAPE); } bool CSGameApp::frameEnded(const FrameEvent& evt) { return true; } bool CSGameApp::keyPressed( const KeyEvent &arg ) { CEGUI::System *sys = CEGUI::System::getSingletonPtr(); sys->injectKeyDown(arg.key); sys->injectChar(arg.text); return true; } bool CSGameApp::keyReleased( const KeyEvent &arg ) { CEGUI::System::getSingleton().injectKeyUp(arg.key); return true; } bool CSGameApp::mouseMoved( const MouseEvent &arg ) { m_pMouse->getMouseState().width = m_pWindow->getWidth(); m_pMouse->getMouseState().height = m_pWindow->getHeight(); if(arg.state.X.abs < m_pWindow->getViewport(0)->getActualLeft()+1 ||(arg.state.Y.abs < m_pWindow->getViewport(0)->getActualTop()+1) ||(arg.state.X.abs > m_pWindow->getViewport(0)->getActualLeft()+m_pWindow->getViewport(0)->getActualWidth()) ||(arg.state.Y.abs > m_pWindow->getViewport(0)->getActualTop() +m_pWindow->getViewport(0)->getActualHeight())) { if(!m_bSysMouseShowFlag) { ShowCursor(1); CEGUI::MouseCursor::getSingleton().hide(); m_bSysMouseShowFlag = true; } } else { if(m_bSysMouseShowFlag) { ShowCursor(0); CEGUI::MouseCursor::getSingleton().show(); m_bSysMouseShowFlag = false; } } CEGUI::System::getSingleton().injectMousePosition( arg.state.X.abs, arg.state.Y.abs ); return true; } bool CSGameApp::mousePressed( const MouseEvent &arg, MouseButtonID id ) { CEGUI::System::getSingleton().injectMouseButtonDown(convertButton(id)); return true; } bool CSGameApp::mouseReleased( const MouseEvent &arg, MouseButtonID id ) { CEGUI::System::getSingleton().injectMouseButtonUp(convertButton(id)); return true; } bool CSGameApp::processUnbufferedMouseInput(const FrameEvent& evt) { // Rotation factors, may not be used if the second mouse button is pressed // 2nd mouse button - slide, otherwise rotate const OIS::MouseState &ms = m_pMouse->getMouseState(); if( ms.buttonDown( OIS::MB_Right ) ) { m_TranslateVector.x += ms.X.rel * 0.13; m_TranslateVector.y -= ms.Y.rel * 0.13; } else { m_RotX = Degree(-ms.X.rel * 0.13); m_RotY = Degree(-ms.Y.rel * 0.13); } return true; } bool CSGameApp::processUnbufferedKeyInput(const FrameEvent& evt) { if(m_pKeyboard->isKeyDown(OIS::KC_A)) { m_TranslateVector.x = -m_MoveScale; // Move camera left } if(m_pKeyboard->isKeyDown(OIS::KC_D)) { m_TranslateVector.x = m_MoveScale; // Move camera RIGHT } if(m_pKeyboard->isKeyDown(OIS::KC_UP) || m_pKeyboard->isKeyDown(OIS::KC_W) ) { m_TranslateVector.z = -m_MoveScale; // Move camera forward } if(m_pKeyboard->isKeyDown(OIS::KC_DOWN) || m_pKeyboard->isKeyDown(OIS::KC_S) ) { m_TranslateVector.z = m_MoveScale; // Move camera backward } if(m_pKeyboard->isKeyDown(OIS::KC_PGUP)) { m_TranslateVector.y = m_MoveScale; // Move camera up } if(m_pKeyboard->isKeyDown(OIS::KC_PGDOWN)) { m_TranslateVector.y = -m_MoveScale; // Move camera down } if(m_pKeyboard->isKeyDown(OIS::KC_RIGHT)) { m_pCamera->yaw(-m_RotScale); } if(m_pKeyboard->isKeyDown(OIS::KC_LEFT)) { m_pCamera->yaw(m_RotScale); } if( m_pKeyboard->isKeyDown(OIS::KC_ESCAPE) || m_pKeyboard->isKeyDown(OIS::KC_Q) ) { return false; } return true; } void CSGameApp::moveCamera() { // Make all the changes to the camera // Note that YAW direction is around a fixed axis (freelook style) rather than a natural YAW (e.g. airplane) m_pCamera->yaw(m_RotX); m_pCamera->pitch(m_RotY); m_pCamera->moveRelative(m_TranslateVector); } bool CSGameApp::quit(const CEGUI::EventArgs& e) { CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton(); CEGUI::String strAccount = wmgr.getWindow((CEGUI::utf8*)"Game/Edit/Account")->getText(); m_bContinue = false; return true; } bool CSGameApp::selectserver(const CEGUI::EventArgs& e) { CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton(); wmgr.getWindow((CEGUI::utf8*)"Game/Edit/Account")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/Edit/Password")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/Login/Ok")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/Login/Cancel")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/SelectServer/CHA")->show(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/SelectServer/CNC")->show(); // load an image to use as a background Imageset* pBackgroudImage = ImagesetManager::getSingleton().getImageset("BackgroundImage"); ImagesetManager::getSingleton().destroyImageset(pBackgroudImage); ImagesetManager::getSingleton().createImagesetFromImageFile("BackgroundImage", "loading 2.jpg"); return true; } bool CSGameApp::loading(const CEGUI::EventArgs& e) { CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton(); //hide all exist windows wmgr.getWindow((CEGUI::utf8*)"Game/Button/EnterGame")->hide(); // load an image to use as a background Imageset* pBackgroudImage = ImagesetManager::getSingleton().getImageset("BackgroundImage"); ImagesetManager::getSingleton().destroyImageset(pBackgroudImage); ImagesetManager::getSingleton().createImagesetFromImageFile("BackgroundImage", "loading 4.jpg"); ResumeThread(m_hThread); return true; } bool CSGameApp::createchar(const CEGUI::EventArgs& e) { CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/SelectServer/CHA")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/SelectServer/CNC")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/StaticText/InputNameTip")->show(); wmgr.getWindow((CEGUI::utf8*)"Game/Edit/CharName")->show(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/CreateChar")->show(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/ReturnSelectServer")->show(); return true; } bool CSGameApp::returnselectserver(const CEGUI::EventArgs& e) { CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton(); //hide all exist windows wmgr.getWindow((CEGUI::utf8*)"Game/StaticText/InputNameTip")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/Edit/CharName")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/CreateChar")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/ReturnSelectServer")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/SelectServer/CHA")->show(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/SelectServer/CNC")->show(); // load an image to use as a background Imageset* pBackgroudImage = ImagesetManager::getSingleton().getImageset("BackgroundImage"); ImagesetManager::getSingleton().destroyImageset(pBackgroudImage); ImagesetManager::getSingleton().createImagesetFromImageFile("BackgroundImage", "loading 2.jpg"); return true; } bool CSGameApp::entergame(const CEGUI::EventArgs& e) { CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton(); //hide all exist windows wmgr.getWindow((CEGUI::utf8*)"Game/StaticText/InputNameTip")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/Edit/CharName")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/CreateChar")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/ReturnSelectServer")->hide(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/EnterGame")->show(); return true; } void CSGameApp::setupEventHandlers(void) { CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton(); wmgr.getWindow((CEGUI::utf8*)"Game/Button/Login/Ok") ->subscribeEvent( CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&CSGameApp::selectserver, this)); wmgr.getWindow((CEGUI::utf8*)"Game/Button/Login/Cancel") ->subscribeEvent( CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&CSGameApp::quit, this)); wmgr.getWindow((CEGUI::utf8*)"Game/Button/SelectServer/CHA") ->subscribeEvent( CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&CSGameApp::createchar, this)); wmgr.getWindow((CEGUI::utf8*)"Game/Button/SelectServer/CNC") ->subscribeEvent( CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&CSGameApp::createchar, this)); wmgr.getWindow((CEGUI::utf8*)"Game/Button/CreateChar") ->subscribeEvent( CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&CSGameApp::entergame,this)); wmgr.getWindow((CEGUI::utf8*)"Game/Button/ReturnSelectServer") ->subscribeEvent( CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&CSGameApp::returnselectserver,this)); wmgr.getWindow((CEGUI::utf8*)"Game/Button/EnterGame") ->subscribeEvent( CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&CSGameApp::loading, this)); } unsigned int CSGameApp::LoadingFunction(void *pV) { CSGameApp* pThis = (CSGameApp*)pV; CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton(); Window* background = wmgr.getWindow ("Background"); //progress bar ProgressBar* pLoadding = (ProgressBar*)wmgr.getWindow((CEGUI::utf8*)"Game/ProgressBar/Loading"); pLoadding->show(); ::EnterCriticalSection(&pThis->m_ThreadLock); float fStep = 0.00f; for(int i = 0; i < 100; ++i) { fStep += 0.01; pLoadding->setProgress(fStep); ::Sleep(50); if(20 == i) { // load an image to use as a background Imageset* pBackgroudImage = ImagesetManager::getSingleton().getImageset("BackgroundImage"); ImagesetManager::getSingleton().destroyImageset(pBackgroudImage); ImagesetManager::getSingleton().createImagesetFromImageFile("BackgroundImage", "loading 5.jpg"); } else if(40 == i) { // load an image to use as a background Imageset* pBackgroudImage = ImagesetManager::getSingleton().getImageset("BackgroundImage"); ImagesetManager::getSingleton().destroyImageset(pBackgroudImage); ImagesetManager::getSingleton().createImagesetFromImageFile("BackgroundImage", "loading 6.jpg"); } else if(60 == i) { // load an image to use as a background Imageset* pBackgroudImage = ImagesetManager::getSingleton().getImageset("BackgroundImage"); ImagesetManager::getSingleton().destroyImageset(pBackgroudImage); ImagesetManager::getSingleton().createImagesetFromImageFile("BackgroundImage", "loading 7.jpg"); } else if(80 == i) { // load an image to use as a background Imageset* pBackgroudImage = ImagesetManager::getSingleton().getImageset("BackgroundImage"); ImagesetManager::getSingleton().destroyImageset(pBackgroudImage); ImagesetManager::getSingleton().createImagesetFromImageFile("BackgroundImage", "loading 8.jpg"); } } ::LeaveCriticalSection(&pThis->m_ThreadLock); pLoadding->hide(); Imageset* pBackgroudImage = ImagesetManager::getSingleton().getImageset("BackgroundImage"); ImagesetManager::getSingleton().destroyImageset(pBackgroudImage); return 0; } class CSGame : public CSGameApp { public: CSGame(); void CreateScene(); bool frameStarted(const FrameEvent& evt); void InitControls(); ~CSGame(); private: Entity* m_pOgreHead; SceneNode* m_pHeadNode; private: CEGUI::OgreCEGUIRenderer* m_pGUIRenderer; CEGUI::System* m_pGUISystem; }; CSGame::CSGame() :m_pOgreHead(0), m_pHeadNode(0) { } void CSGame::CreateScene() { m_pOgreHead = m_pSecneMgr->createEntity("Head", "ogrehead.mesh"); m_pHeadNode = m_pSecneMgr->getRootSceneNode()->createChildSceneNode(); m_pHeadNode->attachObject(m_pOgreHead); // Set ambient light m_pSecneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5)); // Create a skydome m_pSecneMgr->setSkyDome(true, "Examples/CloudySky", 5, 8); // Create a light Light* l = m_pSecneMgr->createLight("MainLight"); l->setPosition(20,80,50); InitControls(); setupEventHandlers(); } void CSGame::InitControls() { //setup cegui m_pGUIRenderer = new CEGUI::OgreCEGUIRenderer(m_pWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, m_pSecneMgr); m_pGUISystem = new CEGUI::System(m_pGUIRenderer); WindowManager& winMgr = WindowManager::getSingleton(); // load scheme and set up defaults CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLookSkin.scheme"); CEGUI::System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow"); // load an image to use as a background ImagesetManager::getSingleton().createImagesetFromImageFile("BackgroundImage", "loading 1.jpg"); // here we will use a StaticImage as the root, then we can use it to place a background image Window* background = winMgr.createWindow ("TaharezLook/StaticImage","Background"); // set area rectangle background->setArea (URect (cegui_reldim (0), cegui_reldim (0),cegui_reldim (1), cegui_reldim (1))); // disable frame and standard background background->setProperty ("FrameEnabled", "false"); background->setProperty ("BackgroundEnabled", "false"); // set the background image background->setProperty ("Image", "set:BackgroundImage image:full_image"); // install this as the root GUI sheet System::getSingleton ().setGUISheet (background); // Create a FrameWindow in the TaharezLook style, and name it 'Button Ok' FrameWindow* wndButtonOK = (FrameWindow*)winMgr.createWindow("TaharezLook/Button", "Game/Button/Login/Ok"); // Here we attach the newly created FrameWindow to the previously created // DefaultWindow which we will be using as the root of the displayed gui. background->addChildWindow(wndButtonOK); wndButtonOK->setPosition(UVector2(cegui_reldim(0.35f), cegui_reldim( 0.6f))); wndButtonOK->setSize(UVector2(cegui_reldim(0.10f), cegui_reldim( 0.03f))); wndButtonOK->setText("OK"); // Create a FrameWindow in the TaharezLook style, and name it 'Button Cancel' FrameWindow* wndButtonCancel = (FrameWindow*)winMgr.createWindow("TaharezLook/Button", "Game/Button/Login/Cancel"); // Here we attach the newly created FrameWindow to the previously created // DefaultWindow which we will be using as the root of the displayed gui. background->addChildWindow(wndButtonCancel); wndButtonCancel->setPosition(UVector2(cegui_reldim(0.55f), cegui_reldim( 0.6f))); wndButtonCancel->setSize(UVector2(cegui_reldim(0.10f), cegui_reldim( 0.03f))); wndButtonCancel->setText("Cancel"); FrameWindow* wndEditAccount = (FrameWindow*)winMgr.createWindow("TaharezLook/Editbox", "Game/Edit/Account"); background->addChildWindow(wndEditAccount); wndEditAccount->setPosition(UVector2(cegui_reldim(0.35f), cegui_reldim( 0.5f))); wndEditAccount->setSize(UVector2(cegui_reldim(0.30f), cegui_reldim( 0.04f))); FrameWindow* wndEditPwd = (FrameWindow*)winMgr.createWindow("TaharezLook/Editbox", "Game/Edit/Password"); background->addChildWindow(wndEditPwd); wndEditPwd->setPosition(UVector2(cegui_reldim(0.35f), cegui_reldim( 0.55f))); wndEditPwd->setSize(UVector2(cegui_reldim(0.30f), cegui_reldim( 0.04f))); ButtonBase* pButCHA = (ButtonBase*)winMgr.createWindow("TaharezLook/Button", "Game/Button/SelectServer/CHA"); background->addChildWindow(pButCHA); pButCHA->setPosition(UVector2(cegui_reldim(0.40f), cegui_reldim( 0.40f))); pButCHA->setSize(UVector2(cegui_reldim(0.20f), cegui_reldim( 0.1f))); pButCHA->setText("CHA"); pButCHA->hide(); ButtonBase* pButCNC = (ButtonBase*)winMgr.createWindow("TaharezLook/Button", "Game/Button/SelectServer/CNC"); background->addChildWindow(pButCNC); pButCNC->setPosition(UVector2(cegui_reldim(0.40f), cegui_reldim( 0.50f))); pButCNC->setSize(UVector2(cegui_reldim(0.20f), cegui_reldim( 0.1f))); pButCNC->setText("CNC"); pButCNC->hide(); ButtonBase* pStaticInputNameTip = (ButtonBase*)winMgr.createWindow("TaharezLook/StaticText", "Game/StaticText/InputNameTip"); background->addChildWindow(pStaticInputNameTip); pStaticInputNameTip->setPosition(UVector2(cegui_reldim(0.70f), cegui_reldim( 0.60f))); pStaticInputNameTip->setSize(UVector2(cegui_reldim(0.20f), cegui_reldim( 0.05f))); pStaticInputNameTip->setText("Please input your name:"); pStaticInputNameTip->setInheritsAlpha(true); pStaticInputNameTip->hide(); FrameWindow* wndEditCharName = (FrameWindow*)winMgr.createWindow("TaharezLook/Editbox", "Game/Edit/CharName"); background->addChildWindow(wndEditCharName); wndEditCharName->setPosition(UVector2(cegui_reldim(0.7f), cegui_reldim( 0.65f))); wndEditCharName->setSize(UVector2(cegui_reldim(0.20f), cegui_reldim( 0.05f))); wndEditCharName->setText("Clean Sky"); wndEditCharName->hide(); FrameWindow* wndButtonCreateChar = (FrameWindow*)winMgr.createWindow("TaharezLook/Button", "Game/Button/CreateChar"); background->addChildWindow(wndButtonCreateChar); wndButtonCreateChar->setPosition(UVector2(cegui_reldim(0.7f), cegui_reldim( 0.70f))); wndButtonCreateChar->setSize(UVector2(cegui_reldim(0.10f), cegui_reldim( 0.04f))); wndButtonCreateChar->setText("Create"); wndButtonCreateChar->hide(); FrameWindow* wndButtonReturnSelectServer = (FrameWindow*)winMgr.createWindow("TaharezLook/Button", "Game/Button/ReturnSelectServer"); background->addChildWindow(wndButtonReturnSelectServer); wndButtonReturnSelectServer->setPosition(UVector2(cegui_reldim(0.8f), cegui_reldim( 0.70f))); wndButtonReturnSelectServer->setSize(UVector2(cegui_reldim(0.10f), cegui_reldim( 0.04f))); wndButtonReturnSelectServer->setText("Return"); wndButtonReturnSelectServer->hide(); FrameWindow* wndButtonEnterGame = (FrameWindow*)winMgr.createWindow("TaharezLook/Button", "Game/Button/EnterGame"); background->addChildWindow(wndButtonEnterGame); wndButtonEnterGame->setPosition(UVector2(cegui_reldim(0.4f), cegui_reldim( 0.40f))); wndButtonEnterGame->setSize(UVector2(cegui_reldim(0.20f), cegui_reldim( 0.1f))); wndButtonEnterGame->setText("Enter Game"); wndButtonEnterGame->hide(); ProgressBar* pLoadding = (ProgressBar*)winMgr.createWindow("TaharezLook/ProgressBar", "Game/ProgressBar/Loading"); background->addChildWindow(pLoadding); pLoadding->setPosition(UVector2(cegui_reldim(0.0f), cegui_reldim( 0.80f))); pLoadding->setSize(UVector2(cegui_reldim(1.0f), cegui_reldim( 0.1f))); winMgr.getWindow((CEGUI::utf8*)"Game/ProgressBar/Loading")->hide(); } bool CSGame::frameStarted(const FrameEvent& evt) { // Just a silly example to demonstrate how much easier this is than passing objects to an external framelistener m_pHeadNode->translate(0.0f, 0.005f, 0.0f); return CSGameApp::frameStarted(evt); } CSGame::~CSGame() { } INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT ) { // Create application object CSGame app; try { app.Go(); } catch( Ogre::Exception& e ) { MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL); } return 0; }