Пример #1
0
bool CP3DRenderer::InitRenderer(HWND hWnd)
{
	I_RegisterModule("rendererDX9");
	// memory leaks detection
	_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
	//_CrtSetBreakAlloc(152);

	g_pConsole = (IP3DConsole*)I_GetClass(IP3DENGINE_CONSOLE);
	g_pConsole->RegisterLastConVar(g_pLastConVar); // zaregistrovat ConVary tohoto dll projektu - NUTNÉ!

	g_pFS = (IP3DFileSystem*)I_GetClass(IP3DENGINE_FILESYSTEM); //get filesystem
	g_pEngine = (IP3DEngine*)I_GetClass(IP3DENGINE_ENGINE);
	g_pTimer = (IP3DTimer*)I_GetClass(IP3DENGINE_TIMER);

	// profiler
	Prof_stackPTR = g_pEngine->GetProf_stack();
	Prof_dummyPTR = g_pEngine->GetProf_dummy();
	Prof_StackAppendPTR = g_pEngine->GetProf_StackAppendFn();

	g_pEngSet.Width = CVr_width.GetInt();
	g_pEngSet.Height = CVr_height.GetInt();
	g_pEngSet.Windowed = CVr_windowed.GetBool();

	g_pEngSet.hWnd = hWnd;

	// zkontroluj, zda se shoduji verze .h a DLL
	if (!D3DXCheckVersion(D3D_SDK_VERSION, D3DX_SDK_VERSION))
		CON(MSG_CON_ERR, "Warning: Wrong DirectX DLL versions, please install latest DirectX!");

	// Vytvoø D3D objekt
	if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
		CON(MSG_ERR_FATAL, "Can't create Direct3D object! Please install DirectX 9...");

	// test for depth buffer support - zatial sa nepouziva stencil
	// D3DFMT_D32, D3DFMT_D24X8, D3DFMT_D16
	D3DFORMAT	DepthBufFormat = D3DFMT_D16;
	if (SUCCEEDED (g_pD3D->GetDeviceCaps (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &m_caps)))
		if (SUCCEEDED (g_pD3D->CheckDeviceFormat(m_caps.AdapterOrdinal, D3DDEVTYPE_HAL, \
							D3DFMT_X8R8G8B8, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D24X8)))
			DepthBufFormat = D3DFMT_D24X8;
			
	// test na vs a ps 2.0
	if(m_caps.VertexShaderVersion<D3DVS_VERSION(1,1) || m_caps.PixelShaderVersion<D3DPS_VERSION(2,0))
	{
		CON(MSG_ERR_FATAL, "Pixel shaders 2.0 not supported!");
	}
	
	// test na format backbufferu
	if(FAILED(g_pD3D->CheckDeviceType(D3DADAPTER_DEFAULT, CVr_ref.GetBool() ? D3DDEVTYPE_REF : D3DDEVTYPE_HAL, 
										 D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8, g_pEngSet.Windowed)))
	{
		CON(MSG_ERR_FATAL, "Backbuffer format A8R8G8B8 not supported!");
	}

	// ziskej caps do member promennych
	m_caps_max_anisotr = (int)m_caps.MaxAnisotropy;

	// Set up the structure used to create the D3DDevice
	ZeroMemory( &m_pparams, sizeof(m_pparams) );
	m_pparams.Windowed = g_pEngSet.Windowed;
	m_pparams.hDeviceWindow = hWnd;
	m_pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
	m_pparams.BackBufferCount = 1;
	m_pparams.BackBufferFormat = D3DFMT_A8R8G8B8;
	m_pparams.EnableAutoDepthStencil = TRUE;
	m_pparams.AutoDepthStencilFormat = DepthBufFormat;
	m_pparams.BackBufferWidth = g_pEngSet.Width;
	m_pparams.BackBufferHeight = g_pEngSet.Height;
	m_pparams.MultiSampleType = CVr_multisample.GetInt() ? D3DMULTISAMPLE_NONMASKABLE : D3DMULTISAMPLE_NONE;
	m_pparams.MultiSampleQuality = CVr_multisample.GetInt();
	m_pparams.PresentationInterval = CVr_vsync.GetBool() ? D3DPRESENT_INTERVAL_ONE : D3DPRESENT_INTERVAL_IMMEDIATE;
	m_pparams.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER | D3DPRESENT_LINEAR_CONTENT;

	m_nWidth = g_pEngSet.Width;
	m_nHeight = g_pEngSet.Height;

#ifdef _DEBUG
	CON(MSG_CON_INFO, "= DirectX 9 (D3D%d, D3DX%d) Renderer (%s, %s, DEBUG) initialization =", D3D_SDK_VERSION, D3DX_SDK_VERSION,__DATE__, __TIME__);
#else
	CON(MSG_CON_INFO, "= DirectX 9 (D3D%d, D3DX%d) Renderer (%s, %s) initialization =", D3D_SDK_VERSION, D3DX_SDK_VERSION, __DATE__, __TIME__);
#endif

	// vypis nazev a info o grafarne do konzole
	D3DADAPTER_IDENTIFIER9 ai;
	if(SUCCEEDED(g_pD3D->GetAdapterIdentifier(CVr_adapter.GetInt(), 0, &ai)))
	{
		CON(MSG_CON_INFO, "Renderer: %s", ai.Description);
	}

	// vytvoreni zarizeni
#ifdef USE_PERFHUD
	// debug pre NVPerfHUD
	if (FAILED (g_pD3D->CreateDevice (g_pD3D->GetAdapterCount()-1, D3DDEVTYPE_REF, hWnd, \
	D3DCREATE_HARDWARE_VERTEXPROCESSING, &m_pparams, &g_pD3DDevice)))
#else
	if (FAILED (g_pD3D->CreateDevice( CVr_adapter.GetInt(), CVr_ref.GetBool() ? D3DDEVTYPE_REF : D3DDEVTYPE_HAL, hWnd, \
		D3DCREATE_HARDWARE_VERTEXPROCESSING, &m_pparams, &g_pD3DDevice)))
#endif
	{
		CON(MSG_ERR_FATAL, "Renderer: Can't create rendering device!\n\rTry restart or reinstall game...\n\rTry reinstall DirectX and graphic drivers...");
		return false;
	}

	g_pResMgr = new CP3DResourceManager;
	g_pResMgr->Initialize();

	g_pFrustum = (IP3DFrustum*)I_GetClass(IP3DRENDERER_FRUSTUM);
	if (g_pFrustum == NULL) 
	{
		CON(MSG_ERR_FATAL, "Renderer: Can't obtain Frustum class!");
		return false;
	}
	g_pMeshLoader = (IP3DMeshLoader*)I_GetClass(IP3DRENDERER_MESHLOADER);
	if (g_pMeshLoader == NULL) 
	{
		CON(MSG_ERR_FATAL, "Renderer: Can't obtain MeshLoader!");
		return false;
	}
	g_pAlphaManager = (IP3DAlphaManager*)I_GetClass(IP3DRENDERER_ALPHAMANAGER);
	if (g_pAlphaManager == NULL) 
	{
		CON(MSG_ERR_FATAL, "Renderer: Can't obtain AlphaManager!");
		return false;
	}

	g_pXML = (IP3DXML*)I_GetClass(IP3DENGINE_XML);
	if (g_pXML == NULL) 
	{
		CON(MSG_ERR_FATAL, "Renderer: Can't obtain XML class!");
		return false;
	}

	g_pMaterialManager = (IP3DMaterialManager*)I_GetClass(IP3DRENDERER_MATERIALMANAGER);
	if (g_pMaterialManager == NULL) 
	{
		CON(MSG_ERR_FATAL, "Renderer: Can't obtain MaterialManager!");
		return false;
	}
	float ViewportSize[2];
	ViewportSize[0] = 1.0f / float(g_pEngSet.Width);
	ViewportSize[1] = 1.0f / float(g_pEngSet.Height);
	g_pMaterialManager->OnViewportSizeChange (ViewportSize);

	g_pPostProcessMgr = (IP3DPostprocessManager*)I_GetClass(IP3DRENDERER_POSTPROCESSMANAGER);
	if (g_pPostProcessMgr == NULL) 
	{
		CON(MSG_ERR_FATAL, "Renderer: Can't obtain PostProcessManager!");
		return false;
	}

	g_pPhysEngine = (IP3DPhysEngine*)I_GetClass(IP3DPHYS_PHYSENGINE);
	if (g_pPhysEngine == NULL) 
	{
		CON(MSG_ERR_FATAL, "Renderer: Can't obtain Physics Engine Class!");
		return false;
	}

	g_pLightManager = (IP3DLightManager*)I_GetClass(IP3DRENDERER_LIGHTMANAGER);
	if (g_pLightManager == NULL) 
	{
		CON(MSG_ERR_FATAL, "Renderer: Can't obtain DynlightManager!");
		return false;
	}

	if ( !g_TextureLoader.Init() ) 
	{
		CON(MSG_ERR_FATAL, "Renderer: Can't initialize TextureLoader!");
		return false;
	}

	g_pSoundMan = (IP3DSoundManager*)I_GetClass(IP3DSOUND_SOUNDMANAGER);
	if (g_pSoundMan == NULL) 
	{
		CON(MSG_ERR_FATAL, "Renderer: Can't obtain SoundManager!");
		return false;
	}

	SetDefaultRenderStates ();

	// nastavenie sampler states pre potreby shader modelu 3.0
	g_pD3DDevice->SetSamplerState(4, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
	g_pD3DDevice->SetSamplerState(4, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
	g_pD3DDevice->SetSamplerState(4, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR );
	g_pD3DDevice->SetSamplerState(5, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
	g_pD3DDevice->SetSamplerState(5, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
	g_pD3DDevice->SetSamplerState(5, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR );
	g_pD3DDevice->SetSamplerState(6, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
	g_pD3DDevice->SetSamplerState(6, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
	g_pD3DDevice->SetSamplerState(6, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR );
	g_pD3DDevice->SetSamplerState(7, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
	g_pD3DDevice->SetSamplerState(7, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
	g_pD3DDevice->SetSamplerState(7, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR );

	// --- get original parameters
	g_pD3DDevice->GetGammaRamp(0, &m_origGama);

	// get default render target
	if(FAILED(g_pD3DDevice->GetRenderTarget(0, &m_pBackBufferSurf)))

	CON(MSG_CON_INFO, "Renderer: DX9 %d x %d %s initialized!", m_pparams.BackBufferWidth, m_pparams.BackBufferHeight, m_pparams.Windowed ? "windowed" : "fullscreen");
	// nastavit callbacky ConVar-ov
	CVr_wireframe.SetChangeCallback(CV_wireframe);
	CVr_gamma.SetChangeCallback(&CP3DRenderer::CV_SetGamma);

	#ifdef _DEBUG
	g_stats.Init(); // STATS
	#endif

	/////////////////
	tmpVertPosClr myvert[] =
	{
		{P3DXPoint3D(-50,0,0), P3DXPoint3D(1,0,0)},
		{P3DXPoint3D(-50,50,0), P3DXPoint3D(0,1,0)},
		{P3DXPoint3D(50,50,0), P3DXPoint3D(0,0,1)}
	};
	P3DVertexElement ve[3];
	ve[0] = P3DVertexElement(P3DVD_FLOAT3, P3DVU_POSITION);
	ve[1] = P3DVertexElement(P3DVD_FLOAT3, P3DVU_COLOR);
	ve[2] = P3DVE_END();

	vb.CreateVB(3, ve, sizeof(tmpVertPosClr));
	tmpVertPosClr *pv = 0;
	
	vb.Lock((void**)&pv);
		pv[0] = myvert[0];
		pv[1] = myvert[1];
		pv[2] = myvert[2];
	vb.UnLock();
	effect.Create("solid.fx");
	////////////////

	return true;
}