startrcewww-data endrce
BasicTutorial8Source         Basic Tutorial 8 - source

BasicTutorial8.h

BasicTutorial8.h
#ifndef __BasicTutorial8_h_
#define __BasicTutorial8_h_

#include "BaseApplication.h"

class BasicTutorial8 : public BaseApplication
{
public:
    BasicTutorial8(void);
    virtual ~BasicTutorial8(void);

protected:
    virtual void createScene(void);
    virtual void chooseSceneManager(void);
    virtual void createCamera(void);
    virtual void createViewports(void);
    virtual void createFrameListener(void);

    virtual bool frameRenderingQueued(const Ogre::FrameEvent& evt);


    // OIS::KeyListener
    virtual bool keyPressed( const OIS::KeyEvent &arg );
    virtual bool keyReleased( const OIS::KeyEvent &arg );
    // OIS::MouseListener
    virtual bool mouseMoved( const OIS::MouseEvent &arg );
    virtual bool mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id );
    virtual bool mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id );

private:
    Ogre::SceneManager* mPrimarySceneMgr;
    Ogre::SceneManager* mSecondarySceneMgr;
    bool mDual;

    virtual void setupViewport(Ogre::SceneManager *curr);
    virtual void dualViewport(Ogre::SceneManager *primarySceneMgr, Ogre::SceneManager *secondarySceneMgr);
};

#endif // #ifndef __BasicTutorial8_h_

BasicTutorial8.cpp

BasicTutorial8.cpp
#include "BasicTutorial8.h"
#define CAMERA_NAME "SceneCamera"

//-------------------------------------------------------------------------------------
BasicTutorial8::BasicTutorial8(void)
    :mPrimarySceneMgr(0),
    mSecondarySceneMgr(0),
    mDual(false)
{
}
//-------------------------------------------------------------------------------------
BasicTutorial8::~BasicTutorial8(void)
{
}


//-------------------------------------------------------------------------------------

//Local Functions
void BasicTutorial8::setupViewport(Ogre::SceneManager *curr)
{
    mWindow->removeAllViewports();
	 
    Ogre::Camera *cam = curr->getCamera(CAMERA_NAME);
    Ogre::Viewport *vp = mWindow->addViewport(cam);
	 
    vp->setBackgroundColour(Ogre::ColourValue(0,0,0));
    cam->setAspectRatio(Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));
}
 
void BasicTutorial8::dualViewport(Ogre::SceneManager *primarySceneMgr, Ogre::SceneManager *secondarySceneMgr)
{
    mWindow->removeAllViewports();
 
    Ogre::Viewport *vp = 0;
    Ogre::Camera *cam = primarySceneMgr->getCamera(CAMERA_NAME);
    vp = mWindow->addViewport(cam, 0, 0, 0, 0.5, 1);
    vp->setBackgroundColour(Ogre::ColourValue(0,0,0));
    cam->setAspectRatio(Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));
	 
    cam = secondarySceneMgr->getCamera(CAMERA_NAME);
    vp = mWindow->addViewport(cam, 1, 0.5, 0, 0.5, 1);
    vp->setBackgroundColour(Ogre::ColourValue(0,0,0));
    cam->setAspectRatio(Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));
}

static void swap(Ogre::SceneManager *&first, Ogre::SceneManager *&second)
{
    Ogre::SceneManager *tmp = first;
    first = second;
    second = tmp;
}



//-------------------------------------------------------------------------------------
void BasicTutorial8::createScene(void)
{
    // Setup the TerrainSceneManager
    mPrimarySceneMgr->setSkyBox(true, "Examples/SpaceSkyBox");
    // Setup the Generic SceneManager
    mSecondarySceneMgr->setSkyDome(true, "Examples/CloudySky", 5, 8);
}


void BasicTutorial8::chooseSceneManager(void)
{
    mPrimarySceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC, "primary");
    mSecondarySceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC, "secondary");
}

void BasicTutorial8::createCamera()
{
    mPrimarySceneMgr->createCamera(CAMERA_NAME);
    mSecondarySceneMgr->createCamera(CAMERA_NAME);
}

void BasicTutorial8::createViewports()
{
    setupViewport(mPrimarySceneMgr);
}


void BasicTutorial8::createFrameListener(void)
{
    Ogre::LogManager::getSingletonPtr()->logMessage("*** Initializing OIS ***");
    OIS::ParamList pl;
    size_t windowHnd = 0;
    std::ostringstream windowHndStr;
 
    mWindow->getCustomAttribute("WINDOW", &windowHnd);
    windowHndStr << windowHnd;
    pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
 
    mInputManager = OIS::InputManager::createInputSystem( pl );
 
    mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject( OIS::OISKeyboard, true ));
    mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject( OIS::OISMouse, true ));
 
    mMouse->setEventCallback(this);
    mKeyboard->setEventCallback(this);
 
    //Set initial mouse clipping size
    windowResized(mWindow);
 
    //Register as a Window listener
    Ogre::WindowEventUtilities::addWindowEventListener(mWindow, this);
 
    mRoot->addFrameListener(this);
}


bool BasicTutorial8::frameRenderingQueued(const Ogre::FrameEvent& evt)
{
    if(mWindow->isClosed())
        return false;

    if(mShutDown)
        return false;

    //Need to capture/update each device
    mKeyboard->capture();
    mMouse->capture();

    return true;
}


bool BasicTutorial8::keyPressed( const OIS::KeyEvent &arg )
{
    if (arg.key == OIS::KC_ESCAPE){
        mShutDown = true;
    }
    else if(arg.key == OIS::KC_V){
	mDual = !mDual;
	 
	if (mDual)
		dualViewport(mPrimarySceneMgr, mSecondarySceneMgr);
	else
		setupViewport(mPrimarySceneMgr);
	}
	else if(arg.key == OIS::KC_C){
	    swap(mPrimarySceneMgr, mSecondarySceneMgr);

	    if (mDual)
		dualViewport(mPrimarySceneMgr, mSecondarySceneMgr);
	    else
		setupViewport(mPrimarySceneMgr);
	}

    return true;
}

bool BasicTutorial8::keyReleased( const OIS::KeyEvent &arg )
{
    return true;
}

bool BasicTutorial8::mouseMoved( const OIS::MouseEvent &arg )
{
    return true;
}

bool BasicTutorial8::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
    return true;
}

bool BasicTutorial8::mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
    return true;
}



#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
    INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
    int main(int argc, char *argv[])
#endif
    {
        // Create application object
        BasicTutorial8 app;

        try {
            app.go();
        } catch( Ogre::Exception& e ) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
            MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
            std::cerr << "An exception has occured: " <<
                e.getFullDescription().c_str() << std::endl;
#endif
        }

        return 0;
    }

#ifdef __cplusplus
}
#endif