Talk:Adding a Scope

From Valve Developer Community
Jump to navigation Jump to search

Different way to handle Screen Ratios

I got a different (and maybe better) way to handle the different screen ratios. This works by just adding black bars (assumed that your scope-image is black) on the Top and Bottom or Right and Left. The Code for this is pretty simple, just some mathematics are involved:


	int wide, tall;
	GetHudSize( wide, tall );

	float fAspectRatio = (float)wide / (float)tall;
	float fAspectRatioCRT = 4.0f / 3.0f;

	if( fAspectRatio == (4.0f / 3.0f) ) //Normal CRT (4:3)
	{
		vgui::surface()->DrawTexturedRect( 0, 0, wide, tall );
	}
	else if( fAspectRatio < 1.33f ) //LCD 5:4, draw at full width with bars on top & bottom
	{
		int iImageHeightHalf = (int)(((float)wide / fAspectRatioCRT) / 2 );
		vgui::surface()->DrawTexturedRect( 0, tall/2 - iImageHeightHalf, wide, tall/2 + iImageHeightHalf );
		vgui::surface()->DrawSetColor( 0, 0, 0, 255 );
		vgui::surface()->DrawFilledRect( 0, 0, wide, tall/2-iImageHeightHalf ); //Top Bar
		vgui::surface()->DrawFilledRect( 0, tall/2+iImageHeightHalf, wide, tall ); //Bottom Bar
	}
	else //LCD 16:9 or 16:10, draw at full heigth with bars left & right
	{
		int iImageWidthHalf = (int)(((float)tall * fAspectRatioCRT) / 2);
		vgui::surface()->DrawTexturedRect( wide/2 - iImageWidthHalf, 0, wide/2 + iImageWidthHalf, tall );
		vgui::surface()->DrawSetColor(0,0,0,255);
		vgui::surface()->DrawFilledRect( 0, 0, wide/2 - iImageWidthHalf, tall );//Left rectangle
		vgui::surface()->DrawFilledRect( wide/2 + iImageWidthHalf, 0, wide, tall );//Right rectangle
	}

This works good for me, and I think its less work to implement.

--RedPuma 14:27, 9 February 2009 (UTC)

Another way to handle diff. screen aspect ratios

I'm using one image per more screen aspect ratios. Only ideal if you using 1024x1024 scope texture and not using >1280x1024 screen resolution

	int screenWide = ScreenWidth();
	int screenTall = ScreenHeight();
	SetBounds(0, 0, screenWide, screenTall);

	int finalScopeSize = m_pScope->Height();
	int sizeDiff = (finalScopeSize - screenTall);

	finalScopeSize -= sizeDiff;

	float halfspacing = (screenWide - finalScopeSize) / 2;
	
	//Performing depth hack to prevent "clips" by world
	materials->DepthRange( 0.0f, 0.1f );

	m_pScope->DrawSelfCropped(halfspacing,0,0,0,m_pScope->Width(),m_pScope->Height(),finalScopeSize ,finalScopeSize,Color(1,1,1,1));

	//Fill with two black rects
	surface()->DrawSetColor(0,0,0,255);
	surface()->DrawFilledRect(0,0,halfspacing+1,screenTall);
	surface()->DrawFilledRect(finalScopeSize + halfspacing-1,0,screenWide,screenTall); 

	//Restore depth
	materials->DepthRange( 0.0f, 1.0f );