diff --git a/game/main.cpp b/game/main.cpp index 51c59598a..13fe736a5 100644 --- a/game/main.cpp +++ b/game/main.cpp @@ -7,6 +7,7 @@ #include "ogre/renderer.hpp" #include "tools/fileops.hpp" #include "input/oismanager.hpp" +#include "input/listener.hpp" using namespace std; @@ -43,17 +44,26 @@ void maintest() // Create the window ogre.createWindow("OpenMW"); - Render::MWScene scene; - scene.setup(&ogre); + cout << "\nSetting up cell rendering (not done)\n"; + + // Sets up camera, scene manager etc + Render::MWScene scene(ogre); // This doesn't do anything yet. Render::CellRender rend(cell); - Input::OISManager input; - input.setup(&ogre); + cout << "Setting up input system\n"; + + // Sets up the input system + Input::OISManager input(ogre); + + // Add the frame and input listener + Input::ExitListener frame(ogre, input); + + cout << "\nStart! Press Q/ESC or close window to exit.\n"; - // Add the frame listener - //root->addFrameListener(&mFrameListener); + // Start the main rendering loop + ogre.start(); cout << "\nThat's all for now!\n"; } diff --git a/game/render/mwscene.cpp b/game/render/mwscene.cpp index b916e940c..6f7c78a41 100644 --- a/game/render/mwscene.cpp +++ b/game/render/mwscene.cpp @@ -5,13 +5,11 @@ using namespace Render; using namespace Ogre; -void MWScene::setup(OgreRenderer *_rend) +MWScene::MWScene(OgreRenderer &_rend) + : rend(_rend) { - rend = _rend; - assert(rend); - - Root *root = rend->getRoot(); - RenderWindow *window = rend->getWindow(); + Root *root = rend.getRoot(); + RenderWindow *window = rend.getWindow(); // Get the SceneManager, in this case a generic one sceneMgr = root->createSceneManager(ST_GENERIC); diff --git a/game/render/mwscene.hpp b/game/render/mwscene.hpp index 9eaba76f7..b2b2c2b4f 100644 --- a/game/render/mwscene.hpp +++ b/game/render/mwscene.hpp @@ -6,10 +6,13 @@ namespace Render { /** Class responsible for Morrowind-specific interfaces to OGRE. + + This might be refactored partially into a non-mw specific + counterpart in ogre/ at some point. */ class MWScene { - OgreRenderer *rend; + OgreRenderer &rend; Ogre::SceneManager *sceneMgr; Ogre::Camera *camera; Ogre::Viewport *vp; @@ -20,7 +23,7 @@ namespace Render Ogre::SceneNode *mwRoot; public: - void setup(OgreRenderer *_rend); + MWScene(OgreRenderer &_rend); }; } diff --git a/input/listener.hpp b/input/listener.hpp new file mode 100644 index 000000000..f5412e701 --- /dev/null +++ b/input/listener.hpp @@ -0,0 +1,89 @@ +#ifndef _INPUT_LISTENER_H +#define _INPUT_LISTENER_H + +#include "oismanager.hpp" +#include "ogre/renderer.hpp" + +#include + +namespace Input +{ + struct ExitListener : Ogre::FrameListener, + OIS::KeyListener, + OIS::MouseListener + { + ExitListener(Render::OgreRenderer &rend, + Input::OISManager &input) + : doExit(false) + { + // Set up component pointers + mWindow = rend.getWindow(); + mMouse = input.mouse; + mKeyboard = input.keyboard; + + // Add ourself to the managers + rend.getRoot() -> addFrameListener(this); + mKeyboard -> setEventCallback(this); + mMouse -> setEventCallback(this); + } + + // Call this to exit the main loop + void exitNow() { doExit = true; } + + bool frameStarted(const Ogre::FrameEvent &evt) + { + if(mWindow->isClosed() || doExit) + return false; + + // Capture keyboard and mouse events + mKeyboard->capture(); + mMouse->capture(); + + return Ogre::FrameListener::frameStarted(evt); + } + + bool keyPressed( const OIS::KeyEvent &arg ) + { + /* + std::cout << "KeyPressed {" << arg.key + << ", " << ((OIS::Keyboard*)(arg.device))->getAsString(arg.key) + << "} || Character (" << (char)arg.text << ")\n"; + */ + using namespace OIS; + + if(arg.key == KC_Q || + arg.key == KC_ESCAPE) + exitNow(); + + return true; + } + + bool keyReleased( const OIS::KeyEvent &arg ) + { + return true; + } + + bool mouseMoved( const OIS::MouseEvent &arg ) + { + return true; + } + + bool mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id ) + { + return true; + } + + bool mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id ) + { + return true; + } + + private: + + Ogre::RenderWindow *mWindow; + OIS::Mouse *mMouse; + OIS::Keyboard *mKeyboard; + bool doExit; + }; +} +#endif diff --git a/input/oismanager.cpp b/input/oismanager.cpp index 37593c29d..b39b63eb5 100644 --- a/input/oismanager.cpp +++ b/input/oismanager.cpp @@ -10,11 +10,9 @@ using namespace OIS; #include using namespace std; -void OISManager::setup(Render::OgreRenderer *rend) +OISManager::OISManager(Render::OgreRenderer &rend) { - assert(rend); - - RenderWindow *window = rend->getWindow(); + RenderWindow *window = rend.getWindow(); assert(window); size_t windowHnd; @@ -62,15 +60,9 @@ void OISManager::setup(Render::OgreRenderer *rend) const MouseState &ms = mouse->getMouseState(); ms.width = window->getWidth(); ms.height = window->getHeight(); - - /* - // Register the input listener - keyboard -> setEventCallback( &mInput ); - mouse -> setEventCallback( &mInput ); - */ } -void OISManager::cleanup() +OISManager::~OISManager() { if(inputMgr == NULL) return; diff --git a/input/oismanager.hpp b/input/oismanager.hpp index 56cac6f94..8e687782e 100644 --- a/input/oismanager.hpp +++ b/input/oismanager.hpp @@ -6,15 +6,14 @@ namespace Input { - class OISManager + struct OISManager { OIS::InputManager *inputMgr; OIS::Mouse *mouse; OIS::Keyboard *keyboard; - public: - void setup(Render::OgreRenderer *rend); - void cleanup(); + OISManager(Render::OgreRenderer &rend); + ~OISManager(); }; } #endif diff --git a/ogre/renderer.hpp b/ogre/renderer.hpp index 11eae2576..7a67fb1d1 100644 --- a/ogre/renderer.hpp +++ b/ogre/renderer.hpp @@ -33,7 +33,13 @@ namespace Render /// Kill the renderer. void cleanup(); + /// Start the main rendering loop + void start() { mRoot->startRendering(); } + + /// Get the Root Ogre::Root *getRoot() { return mRoot; } + + /// Get the rendering window Ogre::RenderWindow *getWindow() { return mWindow; } }; } diff --git a/old_d_version/ogre/cpp_interface.cpp b/old_d_version/ogre/cpp_interface.cpp index aeb556750..88d53c824 100644 --- a/old_d_version/ogre/cpp_interface.cpp +++ b/old_d_version/ogre/cpp_interface.cpp @@ -148,11 +148,6 @@ extern "C" void ogre_setFog(float rf, float gf, float bf, // Fog color vp->setBackgroundColour(fogColor); } -extern "C" void ogre_startRendering() -{ - mRoot->startRendering(); -} - // Copy a scene node and all its children void cloneNode(SceneNode *from, SceneNode *to, char* name) {