std::shared_ptr<ShaderProgram> ContentManager::LoadNew(const std::string &name, bool async)
{
	FilePtr fp = FileManager::Instance()->OpenFile(name);

	if (!fp)
	{
		LOG("Failed loading '%s': file not found", name.c_str());
		return nullptr;
	}

	int size = fp->GetSize();
	char *data = new char[size + 1];
	fp->Read(data, size);
	fp->Close();

	data[size] = 0;

	ShaderProgramPtr program = std::make_shared<ShaderProgram>();

	ShaderPtr vs = std::make_shared<Shader>(GL_VERTEX_SHADER);
	if (!vs->Load(data))
	{
		LOG("Error loading '%s' vertex shader: %s", name.c_str(), vs->ErrorMessage().c_str());
		return nullptr;
	}
	program->AddShader(vs);
	
	ShaderPtr ps = std::make_shared<Shader>(GL_FRAGMENT_SHADER);
	if (!ps->Load(data))
	{
		LOG("Error loading '%s' fragment shader: %s", name.c_str(), vs->ErrorMessage().c_str());
		return nullptr;
	}
	program->AddShader(ps);

	if (!program->Link())
	{
		LOG("Error loading '%s': link failed", name.c_str());
		return nullptr;
	}

	ContentManager::Instance()->CacheObject(name, program);
	
	LOG("Loaded shader '%s'", name.c_str());

	return program;
}
Example #2
0
	bool DisplaySurface::Create(const boost::any& _rConfig)
	{
		CreateInfo* pInfo = boost::any_cast<CreateInfo*>(_rConfig);
		FilePtr pFile = NULL;

		bool bResult = (NULL != pInfo);
		{
			pFile = FS::GetRoot()->OpenFile(pInfo->m_strPath, FS::EOpenMode_READBINARY);
			bResult = (NULL != pFile);
		}

		if (false != bResult)
		{
			int sSize = pFile->Size();
			unsigned char* pBuffer = new unsigned char[sSize];
			sSize = pFile->Read(pBuffer, sSize);

			bResult = SUCCEEDED(D3DXGetImageInfoFromFileInMemory(pBuffer, sSize, &m_oInfo));

			if (false != bResult)
			{
				#pragma message(__FUNCTION__" : for better support some image formats must be converted into supported surface format. For example luminance image should be translated into paletted surface.")
				bResult = SUCCEEDED(m_rDisplay.GetDevicePtr()->CreateOffscreenPlainSurface(m_oInfo.Width, m_oInfo.Height, m_oInfo.Format, D3DPOOL_DEFAULT, &m_pSurface, NULL));
			}

			if (false != bResult)
			{
				bResult = SUCCEEDED(D3DXLoadSurfaceFromFileInMemory(m_pSurface, NULL, NULL, pBuffer, sSize, NULL, D3DX_FILTER_NONE, 0xff000000, NULL));
			}

			if (false != bResult)
			{
				m_uBPP = m_rDisplay.GetFormatBitsPerPixel(m_oInfo.Format);
				bResult = (0 != m_uBPP);
			}

			delete[] pBuffer;
			FS::GetRoot()->CloseFile(pFile);
		}

		return bResult;
	}
		bool Lua::Loadfile(const string& _strFileName, LuaStatePtr _pState)
		{
			FilePtr pFile = FS::GetRoot()->OpenFile(_strFileName, FS::EOpenMode_READTEXT);
			bool bResult = (NULL != pFile);
			if (false != bResult)
			{
				int sSize = pFile->Size();
				char* pSourceCode = new char[sSize + 1];
				sSize = pFile->Read(pSourceCode, sSize);
				FS::GetRoot()->CloseFile(pFile);
				pSourceCode[sSize] = '\0';
				_pState = (NULL == _pState) ? s_pState : _pState;
				const int sResult = _pState->DoString(pSourceCode);
				bResult = (0 == sResult);
				delete[] pSourceCode;
				if (false == bResult)
				{
					OutputError(sResult, _pState);
				}
			}
			return bResult;
		}