Example #1
0
void	RRotateDialog::RotateDisplay( int iPos )
{
	if ( iPos == m_iPrevPos )
		return;

	// We need to use the RBitmap::Rotate() function, so to do that, convert to
	// Renaissance types.
	CClientDC			clientDC( &m_staticDisplay );
	HDC					hClientDC = clientDC.GetSafeHdc();

	RDcDrawingSurface	drawSurface;
	drawSurface.Initialize( hClientDC, hClientDC );

	R2dTransform transform;

	// Prepare the image on an offscreen DC
	RAutoDrawingSurface	drawSurfaceOffscreen;
	RRealRect				rRealStaticRect(m_rectStaticDisplay);
	BOOLEAN					fPrepared = drawSurfaceOffscreen.Prepare( &drawSurface, transform, rRealStaticRect );
	RDrawingSurface*		pSurface	= ( (fPrepared)? &drawSurfaceOffscreen : &drawSurface );

	// Paint the offscreen DC white
	RColor rColor = RSolidColor( kWhite );
	pSurface->SetFillColor( rColor );
	pSurface->SetPenColor( rColor );
	pSurface->FillRectangle( m_rectStaticDisplay );

	YAngle	flRadiansRotation = ::DegreesToRadians( YFloatType( iPos ) );

	// The rotation leaves a black background, so blit with a mask.  The mask
	// has to be created the same size as the original (not rotated) bitmap
	// size, so create it and then rotate it.
	RBitmapImage		bmpRotated;
	RBitmapImage		bmpMaskRotated;
	RImageLibrary rLibrary;
	rLibrary.Rotate( m_rBitmapImage, bmpRotated, bmpMaskRotated, flRadiansRotation );

	RIntSize				sizeBmpRotated;
	sizeBmpRotated.m_dx = bmpRotated.GetWidthInPixels();
	sizeBmpRotated.m_dy = bmpRotated.GetHeightInPixels();

	RIntRect				rectSource;
	RIntRect				rectDest;
	DeriveSourceAndDestinationRects( sizeBmpRotated, rectSource, rectDest );

	bmpRotated.RenderWithMask( *pSurface, bmpMaskRotated, rectSource, rectDest );

	if ( fPrepared )
		drawSurfaceOffscreen.Release();
	drawSurface.DetachDCs();

	m_iPrevPos = iPos;
}