⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chrlimg.cpp

📁 Windows上的MUD客户端程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*----------------------------------------------------------------------------
                        _                              _ _       
        /\             | |                            | (_)      
       /  \   _ __   __| |_ __ ___  _ __ ___   ___  __| |_  __ _ 
      / /\ \ | '_ \ / _` | '__/ _ \| '_ ` _ \ / _ \/ _` | |/ _` |
     / ____ \| | | | (_| | | | (_) | | | | | |  __/ (_| | | (_| |
    /_/    \_\_| |_|\__,_|_|  \___/|_| |_| |_|\___|\__,_|_|\__,_|

    The contents of this file are subject to the Andromedia Public
	License Version 1.0 (the "License"); you may not use this file
	except in compliance with the License. You may obtain a copy of
	the License at http://www.andromedia.com/APL/

    Software distributed under the License is distributed on an
	"AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
	implied. See the License for the specific language governing
	rights and limitations under the License.

    The Original Code is Pueblo client code, released November 4, 1998.

    The Initial Developer of the Original Code is Andromedia Incorporated.
	Portions created by Andromedia are Copyright (C) 1998 Andromedia
	Incorporated.  All Rights Reserved.

	Andromedia Incorporated                         415.365.6700
	818 Mission Street - 2nd Floor                  415.365.6701 fax
	San Francisco, CA 94103

    Contributor(s):
	--------------------------------------------------------------------------
	   Chaco team:  Dan Greening, Glenn Crocker, Jim Doubek,
	                Coyote Lussier, Pritham Shetty.

					Wrote and designed original codebase.

------------------------------------------------------------------------------

	Implementation of the ChRLImage class for  Render Morphics

----------------------------------------------------------------------------*/

// $Header: 

#include "grheader.h"


#if (defined(CH_USE_RLAB) || defined(CH_USE_D3D))

#include "ChMazDep.h"
#include "ChRLImg.h"

//#define CH_ALWAYS_USE_24_BIT_TEXTURES	1

/*----------------------------------------------------------------------------
	ChRLImage class	-- a wrapper for RenderMorphics version of RLImage
----------------------------------------------------------------------------*/
ChRLImage::ChRLImage( bool boolScale, chuint uOption ) 
			: 	m_boolChroma(false),
				m_boolScale( boolScale ),
				m_uOption( uOption ),
			  	m_pDestroyCallback( 0 ),
			  	m_pDestroyData( 0 ),
				m_iUseCount( 1 ),
				m_pDibImage( 0 ),
				m_iChromaIndex( -1 )
{
	ChMemClearStruct( &m_rlImg );
}

ChRLImage::ChRLImage( ChDib* pDib, bool boolScale, 
			chuint uOption, chuint32 luChromaKey /*= 0 */ ) 
			: 	m_boolChroma(false),
				m_boolScale( boolScale ),
				m_uOption( uOption ),
			  	m_pDestroyCallback( 0 ),
			  	m_pDestroyData( 0 ),
				m_iUseCount( 1 ),
				m_pDibImage( 0 ),
				m_iChromaIndex( -1 )
{
	ConvertDib2RL( pDib, boolScale, 
					uOption, luChromaKey);
}
												// tile img onto new one
ChRLImage::ChRLImage( const ChRLImage &img, int width, int height ) 
					: m_boolChroma(false),
					m_boolScale( false ),
					m_uOption( 0 ),
				  	m_pDestroyCallback( 0 ),
				  	m_pDestroyData( 0 ),
					m_pDibImage( 0 ),
					m_iChromaIndex( -1 )

{
	// To start we have a use count of 1
	m_iUseCount = 1;
	
	m_rlImg =  img.m_rlImg;

	// Adjust the size
	m_rlImg.height = height;
	m_rlImg.width = width;
	int iDepth = img.m_rlImg.depth / 8;
	m_rlImg.bytes_per_line = m_rlImg.width * iDepth;

	// Copy the palette if any
	if ( img.m_rlImg.palette )
	{
	    m_rlImg.palette = new ChNrPaletteEntry[m_rlImg.palette_size];
		memcpy(m_rlImg.palette, img.m_rlImg.palette, sizeof(ChNrPaletteEntry) * m_rlImg.palette_size);
	}
	 
	// Tile the bits, starting at top left
	int srcRow = 0;
	m_rlImg.buffer1 = new BYTE[m_rlImg.bytes_per_line * height];

	LPBYTE pBuffer = (LPBYTE)m_rlImg.buffer1;
	int bytesPerCopy = img.m_rlImg.width * iDepth;		// NOT bytes per line, there may be trailing junk

	for(int dstRow = 0; dstRow < height; dstRow++)				  //m_rlImg.bytes_per_line
	{
		if(srcRow >= img.m_rlImg.height) srcRow = 0;
		LPBYTE pSrcBuffer = (LPBYTE)img.m_rlImg.buffer1 + img.m_rlImg.bytes_per_line * srcRow;
		int bytesCopied = 0;
		LPBYTE pDst = pBuffer + dstRow * m_rlImg.bytes_per_line;
		while(bytesCopied < m_rlImg.bytes_per_line)
		{
			int bytesToCopy = bytesPerCopy;
			if(bytesPerCopy + bytesCopied > m_rlImg.bytes_per_line) 
			{
				bytesToCopy = m_rlImg.bytes_per_line - bytesCopied;
			} 
			memcpy(pDst, pSrcBuffer, bytesToCopy);
			bytesCopied += bytesToCopy;
			pDst += bytesToCopy;
		}
		srcRow++;
	}
}

ChRLImage::~ChRLImage()
{
	ASSERT( m_iUseCount == 0 );
	// do the cleanup
	if ( m_rlImg.palette ) delete [] m_rlImg.palette;
	m_rlImg.palette = 0;
	if ( m_rlImg.buffer1 ) delete [] m_rlImg.buffer1;
	m_rlImg.buffer1 = 0;

	delete m_pDibImage;
}


void ChRLImage::Release()
{
	ASSERT( m_iUseCount > 0 );

	m_iUseCount--;

	if ( 0 == m_iUseCount )
	{
		if ( m_pDestroyCallback )
		{
			m_pDestroyCallback( m_pDestroyData, this );
		}
		delete this;
	}
}

bool ChRLImage::NewImage( pChImageInfo pImage )
{
	m_imgInfo = *pImage;
	return true;
}

bool ChRLImage::Create( int iFrame, BITMAPINFO* pBMI, BYTE* pBits) // Create from existing mem
{
	ASSERT( false );
	return false;
}


bool ChRLImage::Create( pChImageFrameInfo pFrameInfo, int iBitCount /*= 8 */)
{	 
	if ( pFrameInfo->iFrame != 0 )
	{ // we handle only one frame now
		return false;
	}

	int 	iWidth = pFrameInfo->iWidth, 
			iHeight = pFrameInfo->iHeight;

	ComputeSize( pFrameInfo->iWidth, pFrameInfo->iHeight, 
				m_boolScale, m_uOption, iWidth, iHeight );



	if ( //pFrameInfo->luAttrs & ChImageConsumer::imgTransparent || 
			iWidth != pFrameInfo->iWidth || iHeight != pFrameInfo->iHeight )
	{	// we need to scale, it is easier to convert image to dib and then back to RL 
		// in this case
		TRACE( "ChRLImage::Severe performance loss : Using non-power of 2 textures\n" );
		if ( 0 == m_pDibImage )
		{
			m_pDibImage = new ChDib;
			ASSERT( m_pDibImage );
		}
		m_pDibImage->NewImage( &m_imgInfo );
		m_rlImg.width = pFrameInfo->iWidth; 
		m_rlImg.height = pFrameInfo->iHeight;

	}

	
	if ( m_pDibImage )
	{
	 	if ( m_pDibImage->Create( pFrameInfo->iFrame, pFrameInfo->iWidth, 
	 							pFrameInfo->iHeight, iBitCount ) )
		{

			if ( pFrameInfo->luAttrs & ChImageConsumer::imgTransparent )
			{
				// Transparency makes us use 24bits + alpha
				m_chromaKey = pFrameInfo->colorTransparent & 0xffffff;
				m_boolChroma = true;
				m_uOption |= ChMazeTextureHTTPReq::textureChromaKey;
			}

		 	return true;
		}
	}
	else
	{
		int		iDepth = 3;		// in bytes
		#if !defined(CH_ALWAYS_USE_24_BIT_TEXTURES)
		if( iBitCount == 8) iDepth = 1;
		#endif
		bool	boolAlpha = false;

		if ( pFrameInfo->luAttrs & ChImageConsumer::imgTransparent )
		{
			// Transparency makes us use 24bits + alpha
			iDepth = 4;		// 24 bits and add an alpha
			boolAlpha = true;
			m_chromaKey = pFrameInfo->colorTransparent & 0xffffff;
			m_iChromaIndex = pFrameInfo->iTransparentIndex;
			m_boolChroma = true;
		}

		// Create the RLImage now
		m_rlImg.width = iWidth; 
		m_rlImg.height = iHeight;
		m_rlImg.aspectx = 2;
		m_rlImg.aspecty = 1;
		// bits/pixel
		m_rlImg.depth	= 8 * iDepth; // we keep it as either 8 or 24 bit + maybe an alpha
		// storage width
		m_rlImg.bytes_per_line = m_rlImg.width * iDepth;
		if(m_rlImg.bytes_per_line & 3)
		{
			m_rlImg.bytes_per_line += 4;
			m_rlImg.bytes_per_line &= ~3;
		}

		if(boolAlpha)
		{
			m_rlImg.alpha_mask 	= 0xFF000000;
		}
		else
		{
			m_rlImg.alpha_mask 	= 0;
		}

		// Make an 8 bit paletted RLImage
		if ( 8 == m_rlImg.depth )
		{
			m_rlImg.rgb 			= 0;

		    m_rlImg.red_mask		= 0xFF; 
		    m_rlImg.green_mask		= 0xFF;   
		    m_rlImg.blue_mask		= 0xFF;
			
			m_rlImg.alpha_mask 		= 0xFF;
		}
		else
		{
			m_rlImg.rgb = true;	

			m_rlImg.red_mask 	= 0xFF0000;
			m_rlImg.green_mask 	= 0xFF00;
			m_rlImg.blue_mask 	= 0xFF;
		}

		m_rlImg.palette_size 	= 0;
		m_rlImg.palette 		= 0;


		m_rlImg.buffer1 = new BYTE[ m_rlImg.bytes_per_line * m_rlImg.height]; 
		ASSERT( m_rlImg.buffer1 );
		m_rlImg.buffer2 = 0;
		
		return true;
	}

	return false;
}

bool ChRLImage::SetColorTable( int iFrame, RGBQUAD* pColorTbl, int iColors ) 
{
	ASSERT( iFrame == 0  );

	if ( m_pDibImage )
	{
		m_pDibImage->SetColorTable( iFrame, pColorTbl, iColors );
	}
	else
	{

	    m_rlImg.palette_size 	= 256;
	    m_rlImg.palette 		= new ChNrPaletteEntry[m_rlImg.palette_size];

		// Copy the palette
		for(int j = 0; j < iColors && j < m_rlImg.palette_size; j++)
		{
			m_rlImg.palette[j].blue 	= pColorTbl[ j ].rgbBlue;  		
			m_rlImg.palette[j].green 	= pColorTbl[ j ].rgbGreen; 	
			m_rlImg.palette[j].red 		= pColorTbl[ j ].rgbRed;   
			m_rlImg.palette[j].flags = ChNrPaletteReadOnly;
		}
			
		for( j = iColors; j < 256; j++)
		{
			m_rlImg.palette[j].flags = ChNrPaletteFree;   // unuse the  entry
		}
	}

	return true;
} 

bool ChRLImage::SetScanLine( int iFrame, int iScanLine, 
					BYTE* pPixels, int iBufferLength, int iFormat )
{
	ASSERT( iFrame == 0  );

	if ( m_pDibImage )
	{
		m_pDibImage->SetScanLine( iFrame, iScanLine, 
						pPixels, iBufferLength, iFormat );

		// if this is the last scan line then convert the dib to rl and free the 
		// dib
		if ( m_pDibImage && iScanLine == m_rlImg.height - 1 )
		{ // convert the image to RL now

			ConvertDib2RL( m_pDibImage, m_boolScale, m_uOption, m_chromaKey );
			delete m_pDibImage;
			m_pDibImage = 0; 
		}
	}
	else
	{
		LPBYTE pBuffer = (LPBYTE)m_rlImg.buffer1;
		// Move the pointer to the current scan line

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -