Esempio n. 1
0
bool CTextureAtlas::Load()
{
    BEATS_ASSERT(!IsLoaded());

    TString fileext = CFilePathTool::GetInstance()->Extension(GetFilePath().c_str());
    if(fileext == _T(".xml"))
    {
        TiXmlDocument doc;
        CSerializer serializer;
        CFilePathTool::GetInstance()->LoadFile(&serializer, GetFilePath().c_str(), _T("rb"));
        if (serializer.GetWritePos() != serializer.GetReadPos())
        {
            doc.Parse((char*)serializer.GetReadPtr());
        }
        TiXmlElement *root = doc.RootElement();
        BEATS_ASSERT(root && strcmp(root->Value(), "Imageset") == 0, 
            _T("TextureAtlas file %s not found or incorrect!"), GetFilePath().c_str());

        const char *textureFile = root->Attribute("Imagefile");
        BEATS_ASSERT(textureFile);
        TCHAR szNameBuffer[MAX_PATH];
        CStringHelper::GetInstance()->ConvertToTCHAR(textureFile, szNameBuffer, MAX_PATH);
        m_texture = CResourceManager::GetInstance()->GetResource<CTexture>(szNameBuffer);
        kmVec2 size;
        kmVec2 point;
        TString strName;
        for(TiXmlElement *elemImage = root->FirstChildElement("Image");
            elemImage != nullptr; elemImage = elemImage->NextSiblingElement("Image"))
        {
            const char *name = elemImage->Attribute("Name");
            BEATS_ASSERT(name);
            kmVec2Fill(&point, 0.f, 0.f);
            kmVec2Fill(&size, 0.f, 0.f);
            elemImage->QueryFloatAttribute("XPos", &point.x);
            elemImage->QueryFloatAttribute("YPos", &point.y);
            elemImage->QueryFloatAttribute("Width", &size.x);
            elemImage->QueryFloatAttribute("Height", &size.y);

            CStringHelper::GetInstance()->ConvertToTCHAR(name, szNameBuffer, MAX_PATH);
            strName.assign(szNameBuffer);
            CreateTextureFrag(strName, point, size);
        }
    }
    else
    {
        m_texture = CResourceManager::GetInstance()->GetResource<CTexture>(GetFilePath());
    }

    m_name = CFilePathTool::GetInstance()->FileName(GetFilePath().c_str());
    super::Load();
    return true;
}
Esempio n. 2
0
bool CSkin::Load()
{
    bool bRet = false;
    BEATS_ASSERT(!IsLoaded(), _T("Can't Load a skin which is already loaded!"));

    // Load From File
    CSerializer serializer(_T("..\\Resource\\skin\\org.skin"));
    CSerializer tmpVerticesBufferPos, tmpVerticesBufferUV;
    CSerializer indexBuffer;
    size_t uVertexCount = 0;
    serializer >> uVertexCount;
    m_uVertexCount = uVertexCount;
    m_vertices = new CVertexPTB[uVertexCount];
    float x, y, z;
    float u,v;

    for (size_t i = 0; i < uVertexCount; ++i)
    {
        ESkeletonBoneType bone, bone1, bone2, bone3;
        float weight, weight1, weight2,weight3;
        serializer >> x >> y >> z >> u >> v;
        serializer >> bone;
        serializer >> weight;
        serializer >> bone1;
        serializer >> weight1;
        serializer >> bone2;
        serializer >> weight2;
        serializer >> bone3;
        serializer >> weight3;

        CVertexPTB &vertex = m_vertices[i];
        kmVec3Fill(&vertex.position,x,y,z);
        vertex.tex = CTex(u,v);
        vertex.bones = CIVector4(bone, bone1, bone2, bone3);
        kmVec4Fill(&vertex.weights,weight,weight1,weight2,weight3);

#ifdef _DEBUG
        float sum = weight + weight1 + weight2+weight3;
        BEATS_ASSERT(sum < 1.01F, _T("Weight can't be greater than 1.01F, cur Value : %f!"), sum);
        BEATS_WARNING(sum > 0.99F, _T("Weight can't be smaller than 0.99F, cur Value : %f!"), sum);
#endif
    }
    
    for (size_t i = 0; i < uVertexCount; ++i)
    {
        indexBuffer << (short)i;
    }

    CRenderer* pRenderer = CRenderer::GetInstance();
    pRenderer->GenVertexArrays(1, &m_uVAO);
    pRenderer->GenBuffers(2, m_uVBO);

#ifndef SW_SKEL_ANIM
    buildVBOVertex(m_vertices, m_uVertexCount*sizeof(CVertexPTB));
    BEATS_SAFE_DELETE_ARRAY(m_vertices);
#endif
    buildVBOIndex(indexBuffer.GetBuffer(), indexBuffer.GetWritePos());
    buildVAO();

    SetLoadedFlag(true);

    return bRet;
}
void CComponentProxyManager::Export(const TCHAR* pSavePath, std::function<bool(uint32_t, CComponentProxy*)> exportCallback)
{
    m_bExportingPhase = true;
    BEATS_ASSERT(pSavePath != NULL);
    static CSerializer serializer;
    serializer.Reset();
    serializer << COMPONENT_SYSTEM_VERSION;
    serializer << GetComponentTemplateMap()->size();
    CComponentProjectDirectory* pRootProject = m_pProject->GetRootDirectory();
    pRootProject->Serialize(serializer);
    serializer << m_pProject->GetStartFile();

    m_uOperateProgress = 0;
    uint32_t uFileCount = (uint32_t)(m_pProject->GetFileList()->size());
    serializer << uFileCount;

    for (uint32_t i = 0; i < uFileCount; ++i)
    {
        const TString strFileName = CFilePathTool::GetInstance()->FileName(m_pProject->GetComponentFileName(i).c_str());
        m_strCurrOperateFile = strFileName;
        serializer << strFileName;
        uint32_t uComponentCount = 0;
        uint32_t uWritePos = serializer.GetWritePos();
        serializer << uWritePos;// File Start pos.
        serializer << 12;// File size placeholder.
        serializer << uComponentCount; // component count place holder
        const std::map<uint32_t, std::map<uint32_t, std::set<uint32_t> > >* pFileToComponent = m_pProject->GetFileToComponentMap();
        auto iter = pFileToComponent->find(i);
        BEATS_ASSERT(iter != pFileToComponent->end(), _T("File: %s\ndoes not have a component!"), strFileName.c_str());
        if (iter != pFileToComponent->end())
        {
            std::vector<uint32_t>::iterator iterFile = std::find(m_loadedFiles.begin(), m_loadedFiles.end(), i);
            if (iterFile != m_loadedFiles.end())
            {
                for (auto subIter = iter->second.begin(); subIter != iter->second.end(); ++subIter)
                {
                    for (auto idIter = subIter->second.begin(); idIter != subIter->second.end(); ++idIter)
                    {
                        uint32_t uComponentId = *idIter;
                        CComponentProxy* pProxy = static_cast<CComponentProxy*>(CComponentProxyManager::GetInstance()->GetComponentInstance(uComponentId));
                        if (pProxy)
                        {
                            if (ExportComponentProxy(i, pProxy, serializer, exportCallback))
                            {
                                m_refreshFileList.insert(*iterFile);
                            }
                            ++uComponentCount;
                        }
                        else
                        {
                            BEATS_ASSERT(false, _T("Can't find proxy with GUID 0x%x id %d, have you removed that class in code?"), m_pProject->QueryComponentGuid(uComponentId), uComponentId);
                        }
                    }
                }
            }
            else
            {
                std::vector<CComponentBase*> vecComponents;
                // Don't create instance in LoadFile when exporting.
                BEATS_ASSERT(m_bCreateInstanceWithProxy);
                m_bCreateInstanceWithProxy = false;
                m_pIdManager->Lock();
                LoadFile(i, &vecComponents);
                iterFile = std::find(m_loadedFiles.begin(), m_loadedFiles.end(), i);
                BEATS_ASSERT(iterFile != m_loadedFiles.end(), _T("Load file index %d failed!"), i);
                for (uint32_t j = 0; j < vecComponents.size(); ++j)
                {
                    CComponentProxy* pProxy = static_cast<CComponentProxy*>(vecComponents[j]);
                    if (ExportComponentProxy(i, pProxy, serializer, exportCallback))
                    {
                        m_refreshFileList.insert(*iterFile);
                    }
                    ++uComponentCount;
                }
                ReSaveFreshFile();
                // Don't call CloseFile, because we have nothing to do with proxy's host component.
                for (uint32_t j = 0; j < vecComponents.size(); ++j)
                {
                    if (vecComponents[j]->GetId() != 0xFFFFFFFF)
                    {
                        CComponentProxyManager::GetInstance()->UnregisterInstance(vecComponents[j]);
                        CComponentProxyManager::GetInstance()->GetIdManager()->RecycleId(vecComponents[j]->GetId());
                    }
                    BEATS_SAFE_DELETE(vecComponents[j]);
                }
                m_loadedFiles.erase(iterFile);
                m_pIdManager->UnLock();
                m_bCreateInstanceWithProxy = true;// Restore.
            }
            uint32_t uCurWritePos = serializer.GetWritePos();
            uint32_t uFileDataSize = uCurWritePos - uWritePos;
            serializer.SetWritePos(uWritePos + sizeof(uint32_t));// Skip file start pos.
            serializer << uFileDataSize;
            serializer << uComponentCount;
            serializer.SetWritePos(uCurWritePos);
        }
        m_uOperateProgress = uint32_t((i + 1) * 100.f / uFileCount);
    }
    BEATS_ASSERT(m_uOperateProgress == 100);
    m_strCurrOperateFile.clear();
    serializer.Deserialize(pSavePath);
    m_bExportingPhase = false;
}
void ExportLanguage()
{
    std::map<uint32_t, std::map<ELanguageType, TString> > languageServerErrCodeMap;
    ReadServerErrCodeFile(languageServerErrCodeMap);
    std::map<TString, std::map<ELanguageType, TString> >& languageMap = CLanguageManager::GetInstance()->GetLanguageMap();
    // 1. Export Bin File.
    CSerializer fileData;
    for (int i = 0; i < eLT_Count; ++i)
    {
        fileData << languageMap.size();
        for (auto iter = languageMap.begin(); iter != languageMap.end(); ++iter)
        {
            auto subIter = iter->second.find((ELanguageType)i);
            if (subIter != iter->second.end())
            {
                fileData << subIter->second;
            }
            else
            {
                fileData << iter->first;
            }
        }
        bool bRet = false;
        for (auto iter : languageServerErrCodeMap)
        {
            auto subIter = iter.second.find((ELanguageType)i);
            if (subIter != iter.second.end())
            {
                if (!bRet)
                {
                    fileData << languageServerErrCodeMap.size();
                    bRet = true;
                }
                fileData << iter.first;
                fileData << iter.second[(ELanguageType)i];
            }
        }
        TString strFilePath = CResourceManager::GetInstance()->GetResourcePath(eRT_Language);
        strFilePath.append(_T("/")).append(pszLanguageTypeString[i]).append(_T(".bin"));
        fileData.Deserialize(strFilePath.c_str(), _T("wb+"));
        fileData.Reset();
    }
    //2. Export enum file.
    std::string content;
    content.append("#ifndef BEYONDENGINEEDITOR_LANGUAGEENUM_H__INCLUDE\n#define BEYONDENGINEEDITOR_LANGUAGEENUM_H__INCLUDE\n").append("\nenum ELanguageTextType\n{\n");
    for (auto iter = languageMap.begin(); iter != languageMap.end(); iter++)
    {
        content.append("    ").append(iter->first.c_str()).append(",\n");
    }
    content.append("\n    eL_Count,\n").append("    eL_Force32Bit = 0xFFFFFFFF\n};\n");
    content.append("#define LUA_LANGUAGE_MAP(LM)\\\n");
    int32_t nCounter = 0;
    for (auto iter = languageMap.begin(); iter != languageMap.end(); ++iter)
    {
        TCHAR szValueBuffer[32];
        _stprintf(szValueBuffer, "%d", nCounter++);
        content.append("    ").append("LM(").append(iter->first.c_str()).append(",").append(szValueBuffer).append(")\\\n");
    }
    content.append("\n#endif");
    fileData.Reset();
    fileData << content;
    fileData.SetWritePos(fileData.GetWritePos() - 1);// back scape the 0 in the string end.
    bool bFileTheSame = false;
    const TString strHeaderFilePath = CResourceManager::GetInstance()->GetResourcePath(eRT_SourceCode) + _T("/Language/Language.h");
    if (CFilePathTool::GetInstance()->Exists(strHeaderFilePath.c_str()))
    {
        CSerializer tmpData(strHeaderFilePath.c_str(), "rb");
        if (tmpData.GetWritePos() == fileData.GetWritePos())
        {
            CMD5 tmpMD5(tmpData.GetBuffer(), tmpData.GetWritePos());
            CMD5 currentMD5(fileData.GetBuffer(), fileData.GetWritePos());
            bFileTheSame = tmpMD5 == currentMD5;
        }
    }
    if (!bFileTheSame)
    {
        fileData.Deserialize(strHeaderFilePath.c_str(), _T("wb+"));
    }

    //3. Export txt file.
    for (int i = 0; i < eLT_Count; ++i)
    {
        bool bHasData = false;
        fileData.Reset();
        for (auto iter = languageMap.begin(); iter != languageMap.end(); ++iter)
        {
            auto subIter = iter->second.find((ELanguageType)i);
            if (subIter != iter->second.end())
            {
                TString strData = (TString)(wxString::FromUTF8(subIter->second.c_str()));
                if (!bHasData && !strData.empty())
                {
                    bHasData = true;
                }
                fileData << strData;
                fileData.SetWritePos(fileData.GetWritePos() - 1);
            }
            fileData << "\n";
            fileData.SetWritePos(fileData.GetWritePos() - 1);
        }
        if (bHasData)
        {
            TString strFilePath = CResourceManager::GetInstance()->GetResourcePath(eRT_Resource);
            strFilePath.append(_T("/")).append(pszLanguageTypeString[i]).append(_T(".txt"));
            fileData.Deserialize(strFilePath.c_str(), _T("wt+"));
        }
    }
    fileData.Reset();
    const std::map<TString, TString>& languageTagMap = CLanguageManager::GetInstance()->GetLanguageTagMap();
    for (auto iter = languageTagMap.begin(); iter != languageTagMap.end(); ++iter)
    {
        TString strData = (TString)(wxString::FromUTF8(iter->second.c_str()));
        fileData << strData;
        fileData.SetWritePos(fileData.GetWritePos() - 1);
        fileData << "\n";
        fileData.SetWritePos(fileData.GetWritePos() - 1);
    }
    TString strFilePath = CResourceManager::GetInstance()->GetResourcePath(eRT_Resource);
    strFilePath.append(_T("/")).append("Tag").append(_T(".txt"));
    fileData.Deserialize(strFilePath.c_str(), _T("wt+"));

    fileData.Reset();
    for (auto iter = languageMap.begin(); iter != languageMap.end(); ++iter)
    {
        TString strData = (TString)(wxString::FromUTF8(iter->first.c_str()));
        fileData << strData;
        fileData.SetWritePos(fileData.GetWritePos() - 1);
        fileData << "\n";
        fileData.SetWritePos(fileData.GetWritePos() - 1);
    }
    strFilePath = CResourceManager::GetInstance()->GetResourcePath(eRT_Resource);
    strFilePath.append(_T("/")).append(_T("Enum.txt"));
    fileData.Deserialize(strFilePath.c_str(), _T("wt+"));
}