OGRE + CEGUI + OIS + LUA 程序框架 Ogre-v1-6-5 CEGUI-SDK-0.6.2-vc9

时间:2022-07-28 03:39:47

http://blog.csdn.net/pizi0475

痞子龙3D编程

 

#include <Ogre.h>
#include <OIS/OIS.h>
#include <CEGUI.h>
#include <OgreCEGUIRenderer.h>
#include <CEGUIDefaultResourceProvider.h>
#include <OgreCEGUIResourceProvider.h>
#include <CEGUIScriptModule.h>
#include <CEGUIScriptModule.h>
#include <ScriptingModules/CEGUILua/LuaScriptModule/include/CEGUILua.h>

using namespace Ogre;

class ExitListener : public FrameListener
{
public:
 ExitListener(OIS::Keyboard *keyboard)
  : mKeyboard(keyboard)
 {
 }

 bool frameStarted(const FrameEvent& evt)
 {
  mKeyboard->capture();
  return !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
 }

private:
 OIS::Keyboard *mKeyboard;
};

class Application
{
public:
 void go()
 {
  createRoot();
  defineResources();
  setupRenderSystem();
  createRenderWindow();
  initializeResourceGroups();
  setupScene();
  setupInputSystem();
  setupCEGUI();
  createFrameListener();
  startRenderLoop();
 }

 ~Application()
 {
  mInputManager->destroyInputObject(mKeyboard);
  OIS::InputManager::destroyInputSystem(mInputManager);

  delete mRenderer;
  //delete mSystem;

  delete mListener;
  delete mRoot;
 }

private:
 Root *mRoot;
 OIS::Keyboard *mKeyboard;
 OIS::InputManager *mInputManager;
 CEGUI::OgreCEGUIRenderer *mRenderer;
 CEGUI::System *mSystem;
 ExitListener *mListener;

 void createRoot()
 {
  mRoot = new Root("Plugins.cfg");
 }

 void defineResources()
 {
  String secName, typeName, archName;
  ConfigFile cf;
  cf.load("resources.cfg");

  ConfigFile::SectionIterator seci = cf.getSectionIterator();
  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 setupRenderSystem()
 {
  if (!mRoot->restoreConfig() && !mRoot->showConfigDialog())
   throw Exception(52, "User canceled the config dialog!", "Application::setupRenderSystem()");

  //// Do not add this to the application
  //RenderSystem *rs = mRoot->getRenderSystemByName("Direct3D9 Rendering Subsystem");
  //                                      // or use "OpenGL Rendering Subsystem"
  //mRoot->setRenderSystem(rs);
  //rs->setConfigOption("Full Screen", "No");
  //rs->setConfigOption("Video Mode", "800 x 600 @ 32-bit colour");
 }

 void createRenderWindow()
 {
  mRoot->initialise(true, "Tutorial Render Window");

  //// Do not add this to the application
  //mRoot->initialise(false);
  //HWND hWnd = 0; // Get the hWnd of the application!
  //NameValuePairList misc;
  //misc["externalWindowHandle"] = StringConverter::toString((int)hWnd);
  //RenderWindow *win = mRoot->createRenderWindow("Main RenderWindow", 800, 600, false, &misc);
 }

 void initializeResourceGroups()
 {
  TextureManager::getSingleton().setDefaultNumMipmaps(5);
  ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
 }

 void setupScene()
 {
  SceneManager *mgr = mRoot->createSceneManager(ST_GENERIC, "Default SceneManager");
  Camera *cam = mgr->createCamera("Camera");
  Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam);
 }

 void setupInputSystem()
 {
  size_t windowHnd = 0;
  std::ostringstream windowHndStr;
  OIS::ParamList pl;
  RenderWindow *win = mRoot->getAutoCreatedWindow();

  win->getCustomAttribute("WINDOW", &windowHnd);
  windowHndStr << windowHnd;
  pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
  mInputManager = OIS::InputManager::createInputSystem(pl);

  try
  {
   mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, false));
   //mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, false));
   //mJoy = static_cast<OIS::JoyStick*>(mInputManager->createInputObject(OIS::OISJoyStick, false));
  }
  catch (const OIS::Exception &e)
  {
   throw new Exception(42, e.eText, "Application::setupInputSystem");
  }
 }

 void setupCEGUI()
 {
  SceneManager *mgr = mRoot->getSceneManager("Default SceneManager");
  RenderWindow *win = mRoot->getAutoCreatedWindow();

  // CEGUI setup
  mRenderer = new CEGUI::OgreCEGUIRenderer(win, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mgr);
  //通过已有的CEGUI渲染器构造渲染系统
  mSystem = new CEGUI::System(mRenderer);

  // Other CEGUI setup here.
  // 设置资源组
  CEGUI::Imageset::setDefaultResourceGroup("imagesets");
  CEGUI::Font::setDefaultResourceGroup("fonts");
  CEGUI::Scheme::setDefaultResourceGroup("schemes");
  CEGUI::WidgetLookManager::setDefaultResourceGroup("looknfeel");
  CEGUI::WindowManager::setDefaultResourceGroup("layouts");
  CEGUI::ScriptModule::setDefaultResourceGroup("lua_scripts");
  CEGUI::LuaScriptModule* script = new CEGUI::LuaScriptModule();
  CEGUI::System::getSingleton().setScriptingModule(script);
  CEGUI::System::getSingleton().executeScriptFile("GuiScript.lua");
 }

 void createFrameListener()
 {
  mListener = new ExitListener(mKeyboard);
  mRoot->addFrameListener(mListener);
 }

 void startRenderLoop()
 {
  mRoot->startRendering();

  //// Do not add this to the application
  //while (mRoot->renderOneFrame())
  //{
  //    // Do some things here, like sleep for x milliseconds or perform other actions.
  //}
 }
};

#include "windows.h"

INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
{
 try
 {
  Application app;
  app.go();
 }
 catch(Exception& e)
 {
  MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
 }

 return 0;
}

 

 

resources.cfg

 

[Bootstrap]
FileSystem=./media/gui/
[schemes]
FileSystem=./media/gui/schemes/
[imagesets]
FileSystem=./media/gui/imagesets/
[fonts]
FileSystem=./media/gui/fonts/
[layouts]
FileSystem=./media/gui/layouts/
[looknfeel]
FileSystem=./media/gui/looknfeel/
[lua_scripts]
FileSystem=./media/gui/lua_scripts/

 

GuiScript.lua

-----------------------------------------
-- Start of handler functions
-----------------------------------------

function luabtnOK_clicked(e)  
local we = CEGUI.toWindowEventArgs(e)  
we.window:setText("OK clicked");  
end

function luabtnCancel_clicked(e)  
local we = CEGUI.toWindowEventArgs(e)  
we.window:setText("Cancel clicked");  
end

-----------------------------------------
-- Script Entry Point
-----------------------------------------
local guiSystem = CEGUI.System:getSingleton()
local schemeMgr = CEGUI.SchemeManager:getSingleton()
local winMgr = CEGUI.WindowManager:getSingleton()

-- load our demo8 scheme
schemeMgr:loadScheme("TaharezLook.scheme");
-- load our demo8 window layout
local root = winMgr:loadWindowLayout("Cegui.layout")
-- set the layout as the root
guiSystem:setGUISheet(root)
-- set default mouse cursor
guiSystem:setDefaultMouseCursor("TaharezLook", "MouseArrow")
-- set the Tooltip type
guiSystem:setDefaultTooltip("TaharezLook/Tooltip")

 

 

Cegui.layout

 

<?xml version="1.0" encoding="UTF-8"?>

<GUILayout >
    <Window Type="DefaultWindow" Name="Root" >
        <Property Name="InheritsAlpha" Value="False" />
        <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
        <Property Name="UnifiedAreaRect" Value="{{0,0},{0,0},{1,0},{1,0}}" />
        <Window Type="TaharezLook/FrameWindow" Name="Hello" >
            <Property Name="Text" Value="Hello World!" />
            <Property Name="Alpha" Value="0.75" />
            <Property Name="TitlebarFont" Value="Commonwealth-10" />
            <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
            <Property Name="TitlebarEnabled" Value="True" />
            <Property Name="UnifiedAreaRect" Value="{{0.279703,0},{0.275744,0},{0.656124,0},{0.608014,0}}" />
            <Window Type="TaharezLook/StaticText" Name="Hello/StaticText" >
                <Property Name="Font" Value="Commonwealth-10" />
                <Property Name="Text" Value="Welcome to OSG + CEGUI" />
                <Property Name="HorzFormatting" Value="WordWrapLeftAligned" />
                <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
                <Property Name="VertFormatting" Value="TopAligned" />
                <Property Name="UnifiedAreaRect" Value="{{0.043543,0},{0.217033,0},{0.967456,0},{0.679647,0}}" />
            </Window>
            <Window Type="TaharezLook/Button" Name="Hello/Ok" >
                <Property Name="Text" Value="OK" />
                <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
                <Property Name="UnifiedAreaRect" Value="{{0.19414,0},{0.750594,0},{0.432419,0},{0.897941,0}}" />
                <Event Name="Clicked" Function="luabtnOK_clicked" />
            </Window>
            <Window Type="TaharezLook/Button" Name="Hello/Cancel" >
                <Property Name="Font" Value="Commonwealth-10" />
                <Property Name="Text" Value="Cancel" />
                <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
                <Property Name="UnifiedAreaRect" Value="{{0.52738,0},{0.753934,0},{0.76566,0},{0.901281,0}}" />
                <Event Name="Clicked" Function="luabtnCancel_clicked" />
            </Window>
        </Window>
    </Window>
</GUILayout>