Exemplo n.º 1
0
	//! reads text from a file
	char* ShaderImpl::ReadFileText(const char* path)
	{
		File* pFile = File::Create(path, File::M_ReadBinary);
		pFile->Open();
		pFile->SetOffset(0, File::OT_End);
		u32 fileSize = pFile->GetOffset();		
		pFile->SetOffset(0, shoot::File::OT_Start);
		char* pText = snew char[fileSize+1];
		pFile->Read(pText, fileSize);
		pText[fileSize] = '\0';
		pFile->Close();
		sdelete(pFile);
		return pText;
	}
Exemplo n.º 2
0
	//! returns an instance
	CacheFile* CacheFile::GetFile(const std::string& strPath)
	{
		std::map<std::string, CacheFile::Info>::iterator it = m_sCache.find(strPath);
		if(it != m_sCache.end())
		{
			return snew CacheFile(strPath.c_str(), it->second);
		}

		Info info;
		File* pNativeFile = File::CreateNative(strPath.c_str(), File::M_ReadBinary);
		pNativeFile->Open();
		pNativeFile->SetOffset(0, File::OT_End);	
		info.m_Size = pNativeFile->GetOffset();
		info.m_pData = snew u8[info.m_Size];
		pNativeFile->SetOffset(0, File::OT_Start);
		pNativeFile->Read(info.m_pData, info.m_Size);		
		pNativeFile->Close();
		delete pNativeFile;

		CacheFile* pFile = snew CacheFile(strPath.c_str(), info);
		m_sCache[strPath] = info;
		return pFile;
	}
	//! returns the ShootFS file size
	uint FileSystemGenerator::GetShootFSFileSize(const char* strPath) const
	{
		auto extension = Utils::GetFileExtension(strPath);
		if (Utils::Equals(extension.c_str(), "xml"))
		{
			auto file = File::CreateNative(strPath, File::M_ReadBinary);
			file->Open();
			auto fileSize = file->GetSize();
			file->Close();
			sdelete(file);
			return (uint)fileSize;
		}

		auto binPath = Utils::GetBinPath(strPath);
		auto dirty = true;
		if (File::Exists(binPath.c_str()))
			dirty = Utils::IsFileMoreRecent(strPath, binPath);

		File* binFile = NULL;
		uint fileSize = 0;
		if (dirty)
		{
			Log << "Converting " << strPath << "\n";
			binFile = Utils::CreateBinFile(binPath);
			if (Utils::Equals(extension.c_str(), "png"))
			{
				auto textureResourcePath = Utils::GetFilePathNoExt(strPath) + ".xml";
				bool _32Bits = false;
				if (File::Exists(textureResourcePath.c_str()))
				{
					auto textureResource = static_cast<Texture*>(ObjectManager::Instance()->Load(textureResourcePath));
					_32Bits = textureResource->Is32Bits();
					sdelete(textureResource);
				}				
				int width = 0, height = 0, channels = 0, bpp = 0;
				auto data = (uchar*)PNGLoader::LoadFromRawPng(strPath, width, height, channels, bpp, binFile, _32Bits);
				fileSize = (uint)binFile->GetOffset();
				sdelete_array(data);
				
			}
			else if (Utils::Equals(extension.c_str(), "fbx"))
			{
				std::string meshResourcePath = Utils::GetFilePathNoExt(strPath) + ".xml";
				auto meshResource = static_cast<MeshResource*>(ObjectManager::Instance()->Load(meshResourcePath));
				FBXMeshLoader(meshResource).Load(strPath, binFile, true);
				fileSize = (uint)binFile->GetOffset();
				sdelete(meshResource);
			}
#ifdef DX11
			else if (Utils::Equals(extension.c_str(), "hlsl"))
			{
				bool bVS = path.find("vs") != std::string::npos;
				bool bPS = path.find("ps") != std::string::npos;
				SHOOT_ASSERT(bVS || bPS, "Couldn't determine type of HLSL file: %s", strPath);
				const char* strEntryPoint = bVS ? "VSMain" : "PSMain";
				const char* strLevel = bVS ? "vs_4_0_level_9_3" : "ps_4_0_level_9_3";
				COMReference<ID3DBlob> blob = ShaderDX11::CompileHLSL(strPath, strEntryPoint, strLevel);
				fileSize += binFile->Write(static_cast<u8*>(blob->GetBufferPointer()), blob->GetBufferSize());
			}
#endif
			else
			{				
				auto file = File::CreateNative(strPath, File::M_ReadBinary);
				file->Open();
				fileSize = (uint)file->GetSize();
				auto data = new uchar[fileSize];
				file->Read(data, fileSize);				
				binFile->Write(data, fileSize);
				file->Close();
				sdelete(file);				
			}			
		}
		else
		{
			binFile = File::CreateNative(binPath.c_str(), File::M_ReadBinary);
			binFile->Open();
			fileSize = (uint)binFile->GetSize();
		}

		binFile->Close();
		sdelete(binFile);
		return fileSize;		
	}