Copyright © MikeFeng QQ: 76848502
Basic Tutorial 5在官方wiki上有完整的源代码,参见
本文的代码来自于, 这个代码使用Ogre自带的Example框架, 主要实现了以下功能:
效果图如下:
下面是代码:
/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/
Copyright (c) 2000-2006 Torus Knot Software Ltd
Also see acknowledgements in Readme.html
You may use this sample code for anything you like, it is not covered by the
LGPL like the rest of the engine.
-----------------------------------------------------------------------------
*/
/**
/file
demo.h
/brief
Specialisation of OGRE's framework application to show the
rendering plugin
*/
//mem probs without this next one
#include
<OgreNoMemoryMacros.h>
#include
<CEGUI/CEGUIImageset.h>
#include
<CEGUI/CEGUISystem.h>
#include
<CEGUI/CEGUILogger.h>
#include
<CEGUI/CEGUISchemeManager.h>
#include
<CEGUI/CEGUIWindowManager.h>
#include
<CEGUI/CEGUIWindow.h>
#include
<CEGUI/elements/CEGUIPushButton.h>
#include
"OgreCEGUIRenderer.h"
#include
"OgreCEGUIResourceProvider.h"
//regular mem handler
#include
<OgreMemoryMacros.h>
#include
"ExampleApplication.h"
class
GuiFrameListener : public ExampleFrameListener,
public OIS::MouseListener,
public OIS::KeyListener
{
private
:
::Renderer* mGUIRenderer;
bool mShutdownRequested;
public
:
// using buffered input
GuiFrameListener(RenderWindow* win, Camera* cam, ::Renderer* renderer)
: ExampleFrameListener(win, cam, true, true,true),
mGUIRenderer(renderer),
mShutdownRequested(false)
{
mMouse->setEventCallback(this);
mKeyboard->setEventCallback(this);
}
// Tell the frame listener to exit at the end of the next frame
void requestShutdown(void)
{
mShutdownRequested = true;
}
bool frameEnded(const FrameEvent &evt)
{
if (mShutdownRequested)
return false;
else
return ExampleFrameListener::frameEnded(evt);
}
bool mouseMoved( const OIS::MouseEvent &e)
{
using namespace OIS;
::System::getSingleton().injectMouseMove(
e.state.X.rel,e.state.Y.rel);
return true;
}
bool mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
{
using namespace OIS;
::System::getSingleton().injectMouseButtonDown(
convertOgreButtonToCegui(id));
return true;
}
bool mouseReleased( const OIS::MouseEvent &e, OIS::MouseButtonID id )
{
::System::getSingleton().injectMouseButtonUp(
convertOgreButtonToCegui(id));
return true;
}
bool keyPressed( const OIS::KeyEvent &e)
{
if(e.key == OIS::KC_ESCAPE)
{
mShutdownRequested = true;
return true;
}
::System::getSingleton().injectKeyDown(e.key);
::System::getSingleton().injectChar(e.text);
return true;
}
bool keyReleased( const OIS::KeyEvent &e )
{
::System::getSingleton().injectKeyUp(e.key);
return true;
}
::MouseButton convertOgreButtonToCegui(int buttonID)
{
switch (buttonID)
{
case OIS::MB_Left:
return ::LeftButton;
case OIS::MB_Right:
return ::RightButton;
case OIS::MB_Middle:
return ::MiddleButton;
//case MouseEvent::BUTTON3_MASK:
// return ::X1Button;
default:
return ::LeftButton;
}
}
};
class
App : public ExampleApplication
{
private
:
::OgreCEGUIRenderer* mGUIRenderer;
::System* mGUISystem;
::Window* mEditorGuiSheet;
public
:
App() : mGUIRenderer(0),
mGUISystem(0),
mEditorGuiSheet(0)
{
}
~App()
{
if (mEditorGuiSheet)
{
::WindowManager::getSingleton().destroyWindow(mEditorGuiSheet);
}
if(mGUISystem)
{
delete mGUISystem;
mGUISystem = 0;
}
if(mGUIRenderer)
{
delete mGUIRenderer;
mGUIRenderer = 0;
}
}
protected
:
void createScene(void)
{
// Set ambient light
mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
// Set up GUI system
mGUIRenderer = new ::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mSceneMgr);
mGUISystem = new ::System(mGUIRenderer);
::Logger::getSingleton().setLoggingLevel(CEGUI::Informative);
// using the 'Taharez Look'
::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLookSkin.scheme");
mGUISystem->setDefaultMouseCursor((CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseArrow");
::MouseCursor::getSingleton().setImage("TaharezLook", "MouseArrow");
mGUISystem->setDefaultFont((CEGUI::utf8*)"BlueHighway-12");
// Quit push button.
mEditorGuiSheet = ::WindowManager::getSingleton().loadWindowLayout((CEGUI::utf8*)"Tutorial Gui.xml");
mGUISystem->setGUISheet(mEditorGuiSheet);
::PushButton* quitButton = (CEGUI::PushButton*)CEGUI::WindowManager::getSingleton().getWindow((CEGUI::utf8*)"Quit");
//mEditorGuiSheet = ::WindowManager::getSingleton().createWindow((CEGUI::utf8*)"DefaultWindow", (CEGUI::utf8*)"Sheet");
//mGUISystem->setGUISheet(mEditorGuiSheet);
//CEGUI::PushButton* quitButton = (CEGUI::PushButton*)CEGUI::WindowManager::getSingleton().createWindow("TaharezLook/Button", (CEGUI::utf8*)"Quit");
//mEditorGuiSheet->addChildWindow(quitButton);
//quitButton->setPosition(CEGUI::UVector2(cegui_reldim(0.35f), cegui_reldim( 0.45f)) );
//quitButton->setSize(CEGUI::UVector2(cegui_reldim(0.35f), cegui_reldim( 0.1f)) );
//quitButton->setText("Quit");
setupEventHandlers();
}
void createFrameListener(void)
{
mFrameListener = new GuiFrameListener(mWindow, mCamera, mGUIRenderer);
mRoot->addFrameListener(mFrameListener);
}
void setupEventHandlers(void)
{
::WindowManager& wmgr = ::WindowManager::getSingleton();
wmgr.getWindow((CEGUI::utf8*)"Quit")->subscribeEvent(CEGUI::PushButton::EventClicked, ::Event::Subscriber(&App::handleQuit, this));
}
bool handleQuit(const ::EventArgs& e)
{
static_cast<GuiFrameListener*>(mFrameListener)->requestShutdown();
return true;
}
};