📄 sceneloading.cpp
字号:
/*//////////////////////////////////////////////////////
场景读取控制
参照sceneLighting的做法
先通过exec把mis文件生成需要的对象数据
然后遍历datagroup调用preload(client)读取Client数据
其中,要使用simEvent,以便能更新进度提示
进度以datablock数量提示
场景读取开始,需要指定场景读取完毕后的脚本回调
李亦 liease@163.com qq:4040719
2006.6.1
/*//////////////////////////////////////////////////////
#include "sceneGraph/sceneLoading.h"
#include "game/gameConnection.h"
#include "sceneGraph/sceneGraph.h"
#include "core/fileStream.h"
#include "console/consoleTypes.h"
#include "gui/core/guiCanvas.h"
#include "core/zipSubStream.h"
bool SceneLoading::s_bTerminateLoading = false;
StringTableEntry SceneLoading::s_pScriptCallback = 0;
F32 SceneLoading::s_fLoadingProgress = 0.f;
SceneLoading* g_pSceneLoading = 0;
//------------------------------------------------------------------------------
class SceneLoadingProcessEvent : public SimEvent
{
private:
U32 m_uLoadingIndex;
public:
SceneLoadingProcessEvent(U32 loadingIndex)
{
m_uLoadingIndex = loadingIndex; // size(): end of loading
}
void process(SimObject * object)
{
AssertFatal(object, "SceneLoadingProcessEvent:: null event object!");
if(object)
static_cast<SceneLoading*>(object)->processEvent(m_uLoadingIndex);
};
};
//-------------------------------------------------------------
//-------------------------------------------------------------
SceneLoading::SceneLoading(void)
{
static bool sbInitialized = false;
if(!sbInitialized)
{
Con::addVariable("SceneLoading::terminate", TypeBool, &s_bTerminateLoading);
Con::addVariable("SceneLoading::Progress", TypeF32, &s_fLoadingProgress);
sbInitialized = true;
}
}
SceneLoading::~SceneLoading(void)
{
}
//-------------------------------------------------------------
bool SceneLoading::loadScenes(void)
{
m_nStartTime = Platform::getRealMilliseconds();
SimGroup* pGroup = Sim::getDataBlockGroup();
AssertFatal(pGroup, "SceneLoading:: 有没搞错,getDataBlockGroup居然为NULL");
m_uLoadingNum = pGroup->size();
m_uLoadingIndex= 0;
//for(SimGroupIterator itr=pGroup->begin();;)
//{
//}
// get things started
Sim::postEvent(this, new SceneLoadingProcessEvent(0), Sim::getTargetTime() + 1);
return(true);
}
//------------------------------------------------------------------------------
void SceneLoading::processEvent(U32 loadingIndex)
{
//处理中断了
if(s_bTerminateLoading)
{
completed(false);
deleteObject();
g_pSceneLoading = 0;
return;
}
s_fLoadingProgress = (float)(loadingIndex+1) / (float)m_uLoadingNum;
m_uLoadingIndex = loadingIndex;
//处理完毕
if(loadingIndex >= m_uLoadingNum)
{
completed(true);
deleteObject();
g_pSceneLoading = 0;
return;
}
SimGroup* pGroup = Sim::getDataBlockGroup();
SimDataBlock* pDataBlock = static_cast<SimDataBlock*>((*pGroup)[loadingIndex]);
if(pDataBlock)
{
static char errorBuffer[256];
if(!pDataBlock->preload(false,errorBuffer))
{
Con::errorf(ConsoleLogEntry::General, "%s: preload failed for %s.", errorBuffer);
}
}//if
Sim::postEvent(this, new SceneLoadingProcessEvent(loadingIndex+1), Sim::getTargetTime() + 1);
}
void SceneLoading::completed(bool success)
{
success;
if(s_pScriptCallback && s_pScriptCallback[0])
Con::executef(1, s_pScriptCallback);
}
//-------------------------------------------------------------
bool SceneLoading::isLoading()
{
return (bool(g_pSceneLoading));
}
bool SceneLoading::beginSceneLoading(StringTableEntry pScriptCallback)
{
if(g_pSceneLoading)
{
Con::errorf(ConsoleLogEntry::General, "强制重读场景数据...");
g_pSceneLoading->deleteObject();
g_pSceneLoading = 0;
}
SceneLoading * pLoading = new SceneLoading;
// register the object
if(!pLoading->registerObject())
{
AssertFatal(0, "SceneLoading:: 无法注册sceneLoading对象!");
Con::errorf(ConsoleLogEntry::General, "SceneLoading:: 无法注册sceneLoading对象!");
delete pLoading;
return(false);
}
// could have interior resources but no instances (hey, got this far didnt we...)
GameConnection * con = dynamic_cast<GameConnection*>(NetConnection::getConnectionToServer());
if(!con)
{
Con::errorf(ConsoleLogEntry::General, "SceneLoading:: no GameConnection");
return(false);
}
con->addObject(pLoading);
// 设置全局属性
g_pSceneLoading = pLoading;
s_bTerminateLoading = false;
s_fLoadingProgress = 0.f;
s_pScriptCallback = pScriptCallback;
if(!pLoading->loadScenes())
{
pLoading->completed(true);
pLoading->deleteObject();
g_pSceneLoading = 0;
return(false);
}
return(true);
}
ConsoleFunction(beginSceneLoading,bool,1,2,"beginSceneLoading(char* pScriptCallback=\"onSceneLoadingCompleted\");")
{
StringTableEntry pCallback;
if(argc < 2)
pCallback = StringTable->insert("sceneLoadingCompleted");
else
pCallback = StringTable->insert(argv[1]);
return(SceneLoading::beginSceneLoading(pCallback));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -