[1092] | 1 | /*
|
---|
| 2 | -----------------------------------------------------------------------------
|
---|
| 3 | This source file is part of OGRE
|
---|
| 4 | (Object-oriented Graphics Rendering Engine)
|
---|
| 5 | For the latest info, see http://www.ogre3d.org/
|
---|
| 6 |
|
---|
| 7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
| 8 | Also see acknowledgements in Readme.html
|
---|
| 9 |
|
---|
| 10 | You may use this sample code for anything you like, it is not covered by the
|
---|
| 11 | LGPL like the rest of the engine.
|
---|
| 12 | -----------------------------------------------------------------------------
|
---|
| 13 | */
|
---|
| 14 | /*
|
---|
| 15 | -----------------------------------------------------------------------------
|
---|
| 16 | Filename: ExampleLoadingBar.h
|
---|
| 17 | Description: Defines an example loading progress bar which you can use during
|
---|
| 18 | startup, level changes etc to display loading progress.
|
---|
| 19 | IMPORTANT: Note that this progress bar relies on you having the OgreCore.zip
|
---|
| 20 | package already added to a resource group called 'Bootstrap' - this provides
|
---|
| 21 | the basic resources required for the progress bar and will be loaded automatically.
|
---|
| 22 | */
|
---|
| 23 | #include "OgreResourceGroupManager.h"
|
---|
| 24 | #include "OgreException.h"
|
---|
| 25 | #include "OgreOverlay.h"
|
---|
| 26 | #include "OgreOverlayManager.h"
|
---|
| 27 | #include "OgreRenderWindow.h"
|
---|
| 28 |
|
---|
| 29 | using namespace Ogre;
|
---|
| 30 |
|
---|
| 31 | /** Defines an example loading progress bar which you can use during
|
---|
| 32 | startup, level changes etc to display loading progress.
|
---|
| 33 | @remarks
|
---|
| 34 | Basically you just need to create an instance of this class, call start()
|
---|
| 35 | before loading and finish() afterwards. You may also need to stop areas of
|
---|
| 36 | your scene rendering in between since this method will call
|
---|
| 37 | RenderWindow::update() to update the display of the bar - we advise using
|
---|
| 38 | SceneManager's 'special case render queues' for this, see
|
---|
| 39 | SceneManager::addSpecialCaseRenderQueue for details.
|
---|
| 40 | @note
|
---|
| 41 | This progress bar relies on you having the OgreCore.zip package already
|
---|
| 42 | added to a resource group called 'Bootstrap' - this provides the basic
|
---|
| 43 | resources required for the progress bar and will be loaded automatically.
|
---|
| 44 | */
|
---|
| 45 | class ExampleLoadingBar : public ResourceGroupListener
|
---|
| 46 | {
|
---|
| 47 | protected:
|
---|
| 48 | RenderWindow* mWindow;
|
---|
| 49 | Overlay* mLoadOverlay;
|
---|
| 50 | Real mInitProportion;
|
---|
| 51 | unsigned short mNumGroupsInit;
|
---|
| 52 | unsigned short mNumGroupsLoad;
|
---|
| 53 | Real mProgressBarMaxSize;
|
---|
| 54 | Real mProgressBarScriptSize;
|
---|
| 55 | Real mProgressBarInc;
|
---|
| 56 | OverlayElement* mLoadingBarElement;
|
---|
| 57 | OverlayElement* mLoadingDescriptionElement;
|
---|
| 58 | OverlayElement* mLoadingCommentElement;
|
---|
| 59 |
|
---|
| 60 | public:
|
---|
| 61 | ExampleLoadingBar() {}
|
---|
| 62 | virtual ~ExampleLoadingBar(){}
|
---|
| 63 |
|
---|
| 64 | /** Show the loading bar and start listening.
|
---|
| 65 | @param window The window to update
|
---|
| 66 | @param numGroupsInit The number of groups you're going to be initialising
|
---|
| 67 | @param numGroupsLoad The number of groups you're going to be loading
|
---|
| 68 | @param initProportion The proportion of the progress which will be taken
|
---|
| 69 | up by initialisation (ie script parsing etc). Defaults to 0.7 since
|
---|
| 70 | script parsing can often take the majority of the time.
|
---|
| 71 | */
|
---|
| 72 | virtual void start(RenderWindow* window,
|
---|
| 73 | unsigned short numGroupsInit = 1,
|
---|
| 74 | unsigned short numGroupsLoad = 1,
|
---|
| 75 | Real initProportion = 0.70f)
|
---|
| 76 | {
|
---|
| 77 | mWindow = window;
|
---|
| 78 | mNumGroupsInit = numGroupsInit;
|
---|
| 79 | mNumGroupsLoad = numGroupsLoad;
|
---|
| 80 | mInitProportion = initProportion;
|
---|
| 81 | // We need to pre-initialise the 'Bootstrap' group so we can use
|
---|
| 82 | // the basic contents in the loading screen
|
---|
| 83 | ResourceGroupManager::getSingleton().initialiseResourceGroup("Bootstrap");
|
---|
| 84 |
|
---|
| 85 | OverlayManager& omgr = OverlayManager::getSingleton();
|
---|
| 86 | mLoadOverlay = (Overlay*)omgr.getByName("Core/LoadOverlay");
|
---|
| 87 | if (!mLoadOverlay)
|
---|
| 88 | {
|
---|
| 89 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND,
|
---|
| 90 | "Cannot find loading overlay", "ExampleLoadingBar::start");
|
---|
| 91 | }
|
---|
| 92 | mLoadOverlay->show();
|
---|
| 93 |
|
---|
| 94 | // Save links to the bar and to the loading text, for updates as we go
|
---|
| 95 | mLoadingBarElement = omgr.getOverlayElement("Core/LoadPanel/Bar/Progress");
|
---|
| 96 | mLoadingCommentElement = omgr.getOverlayElement("Core/LoadPanel/Comment");
|
---|
| 97 | mLoadingDescriptionElement = omgr.getOverlayElement("Core/LoadPanel/Description");
|
---|
| 98 |
|
---|
| 99 | OverlayElement* barContainer = omgr.getOverlayElement("Core/LoadPanel/Bar");
|
---|
| 100 | mProgressBarMaxSize = barContainer->getWidth();
|
---|
| 101 | mLoadingBarElement->setWidth(0);
|
---|
| 102 |
|
---|
| 103 | // self is listener
|
---|
| 104 | ResourceGroupManager::getSingleton().addResourceGroupListener(this);
|
---|
| 105 |
|
---|
| 106 |
|
---|
| 107 |
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /** Hide the loading bar and stop listening.
|
---|
| 111 | */
|
---|
| 112 | virtual void finish(void)
|
---|
| 113 | {
|
---|
| 114 | // hide loading screen
|
---|
| 115 | mLoadOverlay->hide();
|
---|
| 116 |
|
---|
| 117 | // Unregister listener
|
---|
| 118 | ResourceGroupManager::getSingleton().removeResourceGroupListener(this);
|
---|
| 119 |
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 |
|
---|
| 123 | // ResourceGroupListener callbacks
|
---|
| 124 | void resourceGroupScriptingStarted(const String& groupName, size_t scriptCount)
|
---|
| 125 | {
|
---|
| 126 | assert(mNumGroupsInit > 0 && "You stated you were not going to init "
|
---|
| 127 | "any groups, but you did! Divide by zero would follow...");
|
---|
| 128 | // Lets assume script loading is 70%
|
---|
| 129 | mProgressBarInc = mProgressBarMaxSize * mInitProportion / (Real)scriptCount;
|
---|
| 130 | mProgressBarInc /= mNumGroupsInit;
|
---|
| 131 | mLoadingDescriptionElement->setCaption("Parsing scripts...");
|
---|
| 132 | mWindow->update();
|
---|
| 133 | }
|
---|
| 134 | void scriptParseStarted(const String& scriptName)
|
---|
| 135 | {
|
---|
| 136 | mLoadingCommentElement->setCaption(scriptName);
|
---|
| 137 | mWindow->update();
|
---|
| 138 | }
|
---|
| 139 | void scriptParseEnded(void)
|
---|
| 140 | {
|
---|
| 141 | mLoadingBarElement->setWidth(
|
---|
| 142 | mLoadingBarElement->getWidth() + mProgressBarInc);
|
---|
| 143 | mWindow->update();
|
---|
| 144 | }
|
---|
| 145 | void resourceGroupScriptingEnded(const String& groupName)
|
---|
| 146 | {
|
---|
| 147 | }
|
---|
| 148 | void resourceGroupLoadStarted(const String& groupName, size_t resourceCount)
|
---|
| 149 | {
|
---|
| 150 | assert(mNumGroupsLoad > 0 && "You stated you were not going to load "
|
---|
| 151 | "any groups, but you did! Divide by zero would follow...");
|
---|
| 152 | mProgressBarInc = mProgressBarMaxSize * (1-mInitProportion) /
|
---|
| 153 | (Real)resourceCount;
|
---|
| 154 | mProgressBarInc /= mNumGroupsLoad;
|
---|
| 155 | mLoadingDescriptionElement->setCaption("Loading resources...");
|
---|
| 156 | mWindow->update();
|
---|
| 157 | }
|
---|
| 158 | void resourceLoadStarted(const ResourcePtr& resource)
|
---|
| 159 | {
|
---|
| 160 | mLoadingCommentElement->setCaption(resource->getName());
|
---|
| 161 | mWindow->update();
|
---|
| 162 | }
|
---|
| 163 | void resourceLoadEnded(void)
|
---|
| 164 | {
|
---|
| 165 | }
|
---|
| 166 | void worldGeometryStageStarted(const String& description)
|
---|
| 167 | {
|
---|
| 168 | mLoadingCommentElement->setCaption(description);
|
---|
| 169 | mWindow->update();
|
---|
| 170 | }
|
---|
| 171 | void worldGeometryStageEnded(void)
|
---|
| 172 | {
|
---|
| 173 | mLoadingBarElement->setWidth(
|
---|
| 174 | mLoadingBarElement->getWidth() + mProgressBarInc);
|
---|
| 175 | mWindow->update();
|
---|
| 176 | }
|
---|
| 177 | void resourceGroupLoadEnded(const String& groupName)
|
---|
| 178 | {
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | };
|
---|
| 182 |
|
---|