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

📄 ppu_texture.c

📁 凌阳32位单片机开发的小游戏
💻 C
字号:
/******************************************************************************
 *
 *     The information contained herein is the exclusive property of
 *   Sunplus Technology Co. And shall not be distributed, reproduced,
 *   or disclosed in whole in part without prior written permission.
 *
 *         (C) COPYRIGHT 2005   SUNPLUS TECHNOLOGY CO.
 *                        ALL RIGHTS RESERVED
 *
 * The entire notice above must be reproduced on all authorized copies.
 *
 *****************************************************************************/

/******************************************************************************
 *  Filename:   	PPU_Texture.c
 *  Author:     	xydeng  (eMail: xydeng@sunplus.com)
 *  Tel:        	00885-028-87848688-5884
 *  Date:       	2005-11-11
 *  Description:	Texture type define(platform relative)
 *  Reference:
 *  Version history:
 *-----------------------------------------------------------------------------
 *	Version   YYYY-MM-DD-INDEX   Modified By         Description
 *	1.0.0     2005-11-11           xydeng               Create
 *
 *****************************************************************************/
#include "Include/PPU_Texture.h"

extern U8	g_nText3Mode;//add by xjliu for sensor

S16			g_aTextPos_X[3];
S16			g_aTextPos_Y[3];
U8			g_aPNTPtrFlag[3];	//bit0: flag; bit1: ptr add(1) or sub(0)
bool		g_bTextScrollFlag;
ANITEXTHEAD	*pAniTextHead;
ANI_TEXT	*pAniText[3];

const S32 CellSize[]={
	8, 16, 32, 64,
}; 

/**
 * ClearTextRegister - clear text1 ~ text3 register value
 */
void ClearTextRegister(void)
{
	U8	i;
	for( i = 0 ; i < 7 ; i++ )
	{
		*(P_PPU_TX1_X + i ) = 0;
		*(P_PPU_TX2_X + i ) = 0;				
		*(P_PPU_TX3_X + i ) = 0;				
	}			
}

/**
 * PPU_InitStructure - Initialize File Structure.
 */
void PPU_InitStructure(void)
{
    pAniTextHead	= (ANITEXTHEAD*)TEXT_HEADER_ADDR;
    pAniText[0] 	= (ANI_TEXT*)(TEXT_HEADER_ADDR + sizeof(ANITEXTHEAD));
    pAniText[1] 	= (ANI_TEXT*)(TEXT_HEADER_ADDR + sizeof(ANITEXTHEAD) + sizeof(ANI_TEXT));
    pAniText[2] 	= (ANI_TEXT*)(TEXT_HEADER_ADDR + sizeof(ANITEXTHEAD) + sizeof(ANI_TEXT) + sizeof(ANI_TEXT));    
}

/**
 * PPU_InitTexture - Initialize Texture.
 */
bool PPU_InitTexture(void)
{
	//initial setting for driver
	PPU_InitStructure();
	
	//Initialize Text1
	switch( pAniText[TEXT1]->bType & 0xf )
	{
		case CHAR_MODE:
			PPU_InitCharacterText( pAniText[TEXT1], TEXT1);
			break;
		case BITMAP_MODE:
		case HICOLOR_32K:
		case HICOLOR_64K:
			//PPU_InitBitmapText( pAniText[TEXT1], TEXT1 );
			break;
	}

	//Initialize Text2
	if( pAniTextHead->textnum < 2 )
		return false;
	switch( pAniText[TEXT2]->bType & 0xf )
	{
		case CHAR_MODE:
			PPU_InitCharacterText( pAniText[TEXT2], TEXT2);
			break;
		case BITMAP_MODE:
		case HICOLOR_32K:
		case HICOLOR_64K:
			//PPU_InitBitmapText( pAniText[TEXT2], TEXT2 );
			break;
	}
	
	//Initialize Text3
	if( pAniTextHead->textnum < 3 || !g_nText3Mode)//add by xjliu for sensor
		return false;
	switch( pAniText[TEXT3]->bType & 0xf)
	{
		case CHAR_MODE:
			PPU_InitCharacterText( pAniText[TEXT3], TEXT3);
			break;
		case BITMAP_MODE:
		case HICOLOR_32K:
		case HICOLOR_64K:
			//PPU_InitBitmapText( pAniText[TEXT3], TEXT3 );
			break;
	}
	return true;	
}

/**
 * PPU_InitCharacterText - Initialize Texture with character mode.
 * @ptrAniText:
 * @sText:
 * Note: this function is only for VGA mode!!!
 */
void PPU_InitCharacterText(ANI_TEXT *ptrAniText,U32 nTEXT)
{
    U32	    i,j;
	U32 	TextPixel, TextIndex;
	U16 	*port1, *port2;
    S32	    cxSize,cySize;
    S32	    TotalXSize, TotalYSize;
    S32     x, y;
  	
    if( ptrAniText->cxSize != 0x40 )
    	cxSize	= (ptrAniText->cxSize/0x10)<<4;
    else	
    	cxSize 	= 0x3<<4;

    if( ptrAniText->cySize != 0x40 )
    	cySize 	= (ptrAniText->cySize/0x10)<<6;
    else	
    	cySize 	= 0x3<<6;

	TotalXSize	= CellSize[cxSize>>4];
	TotalYSize 	= CellSize[cySize>>6];    
	
	port1 		= TEXT1_PNT_BUFF_ADDR + 0x8000 * nTEXT;
	port2 		= TEXT1_PNT_ADDR + 0x8000 * nTEXT;
	TextPixel 	= TEXT1_PGT_ADDR + 0x100000 * nTEXT;
	TextIndex   = TEXT1_PNT_BUFF_ADDR + 0x8000 * nTEXT;
	
	for(i=0; i<ptrAniText->ySize; i++)
	{
		for(j=0; j<ptrAniText->xSize; j++)
			*(port1+j+i*1024/TotalXSize) = *(port2 + j + i*ptrAniText->xSize);					
		for(j=0; j<1024/TotalXSize-ptrAniText->xSize; j++)
			*(port1+j+i*1024/TotalXSize+ptrAniText->xSize) = 0;
	}
    x = TotalXSize*ptrAniText->xSize;		//Texture HSize (pixels)
    y = TotalYSize*ptrAniText->ySize;		//Texture VSize (pixels)		
	PPU_CharacterShow(
		nTEXT, 
		x, y, 
		TextPixel, 
		TextIndex, 
		ptrAniText->ColorBit, 
		cxSize, cySize, 
		ptrAniText->bank, 
		ptrAniText->layer);				
}

//====================================================================================================
// Show Texture for Character Mode
// void CharacterShow(S32 nTextLayer, S32 nXSize, S32 nYSize, U32 *pPatAddr, U32 *pIndexAddr, S32 nColorMode, S32 nCellXSize, S32 nCellYSize, S32 nPaletteBank, S32 nDepth)
// Input parameters:	
//			S32 nTextLayer: Select Text layer for Text1/Text2/Text3
//						define TEXT1 		0x00000000
//						define TEXT2 		0x00000001
//						define TEXT3 		0x00000002
//			S32 nXSize:	Texture Size for horizontal value
//			S32 nYSize:	Texture Size for vertical value
//			U32 *pPatAdr:	The Texture data address
//			U32 *pIndexAddr:The Texture index data address
//			S32 nColorMode:	Text Color Mode
//						define TX_Color4       0x00000002
//						define TX_Color16      0x00000004
//						define TX_Color64      0x00000006
//						define TX_Color256     0x00000008
//			S32 nCellXSize:	Character Size for Horizontal value
//						define TXHSize8		0x00000000
//						define TXHSize16	0x00000010
//						define TXHSize32	0x00000020
//						define TXHSize64	0x00000030
//			S32 nCellYSize:	Character Size for Vertical value
//						define TXVSize8		0x00000000
//						define TXVSize16	0x00000040
//						define TXVSize32	0x00000080
//						define TXVSize64	0x000000C0
//			S32 nPaletteBank:	Palette Bank for 0~31
//			S32 nDepth:	Text Depth Layer 
//						define TXDepth0		0x00000000	
//						define TXDepth1		0x00002000	
//						define TXDepth2		0x00004000	
//						define TXDepth3		0x00006000	
// Returns: none
//====================================================================================================
void PPU_CharacterShow
(
 U32 nTextLayer, 
 U32 nXSize, U32 nYSize, 
 U32 *pPatAddr, 
 U32 *pIndexAddr, 
 U32 nColorMode, 
 U32 nCellXSize, U32 nCellYSize, 
 U32 nPaletteBank, 
 U32 nDepth
)
{
	U32 *index, *addr, *attr, *x, *y, *control;
	
	x 		= P_PPU_TX1_X 			+ nTextLayer * 7;
	y 		= P_PPU_TX1_Y 			+ nTextLayer * 7;	
	attr 	= P_PPU_TX1_Attribute 	+ nTextLayer * 7;
	control = P_PPU_TX1_Control 	+ nTextLayer * 7;
	index 	= P_PPU_TX1_N_PTR 		+ nTextLayer * 7;
	addr 	= P_PPU_TX1_START_ADR1 	+ nTextLayer * 3;
	
	//set Texture's x & y position
	*x	= 0;
	*y	= 0;

	//set Texture's attribute
	if(nColorMode>=TX_Color64)
		nColorMode = (nColorMode>>2) + 1;	
	else
		nColorMode = nColorMode>>2;		
	*attr = nColorMode | nCellXSize | nCellYSize | (nPaletteBank<<8) | (nDepth<<13);	

	//set Char Number Pointer start address(PNT)
	*index = pIndexAddr;

	//Setup PPU Text Buffer(0 ~ 2) Start Address(PGT)
	*addr 		= pPatAddr;
	*(addr + 1) = pPatAddr;
	*(addr + 2) = pPatAddr;

	//set Texture's control register
	*control = TXREGMODE;//TXEN|
}

/**
 * PPU_InitBitmapText
 */
/*void PPU_InitBitmapText(ANI_TEXT *ptrAniText,S32 eTEXT )
{
	U32	TextPixel, TextIndex;

    if( (ptrAniText->bType&0xf) == 2)  	// high color 32768
		ptrAniText->ColorBit = TX_Color32768;
	if( (ptrAniText->bType&0xf) == 3)	// high color 65536		
	    ptrAniText->ColorBit = TX_Color65536;
	    
	TextPixel = TEXT1_PGT_ADDR + 0x100000 * eTEXT;
	TextIndex = TEXT1_PNT_ADDR + 0x8000 * eTEXT;		
	
  	PPU_BitmapShow(eTEXT, ptrAniText->xSize, ptrAniText->ySize, ptrAniText->ColorBit, ptrAniText->bank<<8, ptrAniText->layer<<13, TextPixel, TextIndex);
}*/

//====================================================================================================
// Show Texture for Bitmap Mode
// BitmapShow(S32 nTextLayer, S32 nXSize, S32 nYSize, S32 nColorMode, S32 nBank, S32 nDepthLayer, U32 *pPatAddr, U32 *pIndexAddr);
// Input parameters:	
//			S32 nTextLayer: Select Text layer for Text1/Text2/Text3
//						define TEXT1 		0x00000000
//						define TEXT2 		0x00000001
//						define TEXT3 		0x00000002
//			S32 nXSize:	Texture Size for horizontal value
//			S32 nYSize:	Texture Size for vertical value
//			S32 nColorMode:	Text Color Mode
//						define TX_Color4       0x00000002
//						define TX_Color16      0x00000004
//						define TX_Color64      0x00000006
//						define TX_Color256     0x00000008
//						define TX_Color32768   0x00000010
//						define TX_Color65536	0x00000020
//			S32 nBank:	Palette Bank for 0~31
//			S32 nDepthLayer:	Text Depth Layer 
//						define TXDepth0		0x00000000	
//						define TXDepth1		0x00002000	
//						define TXDepth2		0x00004000	
//						define TXDepth3		0x00006000	
//			U32 *pPatAdr:	The Texture data address
//			U32 *pIndexAddr:The Texture index data address
// Returns: none
//====================================================================================================
/*void PPU_BitmapShow(S32 nTextLayer, S32 nXSize, S32 nYSize, S32 nColorMode, S32 nBank, S32 nDepthLayer, U32 *pPatAddr, U32 *pIndexAddr)
{
	U32 i, *port;	
	U32 *index, *addr, *x, *y, *attr, *control;
	
	addr 		= P_PPU_TX1_START_ADR1 	+ nTextLayer * 3;
	index 		= P_PPU_TX1_N_PTR 		+ nTextLayer * 7;
	attr 		= P_PPU_TX1_Attribute 	+ nTextLayer * 7;
	x 			= P_PPU_TX1_X 			+ nTextLayer * 7;
	y 			= P_PPU_TX1_Y 			+ nTextLayer * 7;
	control 	= P_PPU_TX1_Control 	+ nTextLayer * 7;
	
	port 		= pIndexAddr;
	
	for(i=0; i< nYSize; i++)
	{		
		if(nColorMode == TX_Color65536)
			*(port + i) = i* (nXSize * nColorMode / 32);
		else		
			*(port + i) = i* (nXSize * nColorMode / 16);	 
	}		
		
	*index 		= pIndexAddr;				
	*addr 		= pPatAddr;	
	*(addr + 1) = pPatAddr;	
	*(addr + 2) = pPatAddr;	
	*x 			= 0;
	*y 			= 0;
	*control 	= TXEN|TXLINEAR|TXREGMODE;	
	
	switch(nColorMode)
	{
		case TX_Color4:
		case TX_Color16:
			*attr 	|= ((nColorMode>>2) | nDepthLayer | nBank);
			break;
		case TX_Color64:
		case TX_Color256:
			*attr 	|= (((nColorMode>>2) + 1) | nDepthLayer | nBank);
			break;
		case TX_Color32768:
			*control|= TXRGB555 | nDepthLayer;					
			break;
		case TX_Color65536:
			*control|= TXRGB565 | nDepthLayer;			
			break;
	}
}
*/
/**
 * PPU_ScrollScreen
 */
void PPU_ScrollScreen(U8 *pPtrFlag)
{
	U8 i;
	U32 Index[3],IndexBuff[3];
	U32 *x[3];
	U32 *y[3];
	U32 *ptr[3];

	for (i = 0; i < 3; i ++)
	{
		Index[i]		= TEXT1_PNT_ADDR		+ 0x8000 * i;
		IndexBuff[i]	= TEXT1_PNT_BUFF_ADDR	+ 0x8000 * i;
		x[i]			= P_PPU_TX1_X			+ i * 7;
		y[i]			= P_PPU_TX1_Y			+ i * 7;
		ptr[i]			= P_PPU_TX1_N_PTR		+ i * 7;
	}

	//H scroll, only scroll in buffer size
	for (i = 0; i < 3; i ++)
	{
		*x[i] = g_aTextPos_X[i];
	}

	//V scroll
	for (i = 0; i < 3; i ++)
	{
		if ( TestBit(0, pPtrFlag[i]) )
		{
			if (*ptr[i] >= IndexBuff[i])
			{
				if ( TestBit(1, pPtrFlag[i]) )
				{
					*ptr[i] = Index[i] + (*ptr[i] - IndexBuff[i]) + (pAniText[i]->xSize << 1);
				}
				else
				{
					*ptr[i] = Index[i] + (*ptr[i] - IndexBuff[i]) - (pAniText[i]->xSize << 1);
				}
			}
			else
			{
				if ( TestBit(1, pPtrFlag[i]) )
				{
					*ptr[i] = IndexBuff[i] + (*ptr[i] - Index[i]) + (pAniText[i]->xSize << 1);
				}
				else
				{
					*ptr[i] = IndexBuff[i] + (*ptr[i] - Index[i]) - (pAniText[i]->xSize << 1);
				}
			}
			pPtrFlag[i] = 0;
		}
		*y[i] = g_aTextPos_Y[i];
	}
}


⌨️ 快捷键说明

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