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

📄 function.c

📁 GBA游戏程序
💻 C
字号:
//////////////////////////////////////////////////////////////
// File: function.c                                         //
// Author: Benjamin D. Hale AKA WhatZdat D. Pimp            //
// Purpose: Generic functions for commonly used tasks.      //
//////////////////////////////////////////////////////////////
 
// Includes/Defines
 
#include "malloc.h"
#include "stdlib.h"
#include "string.h"
//#include <gba.h>
//#include <lcd.h>
//#include <keys.h>
//#include <timer.h>
#include "common.h"
#include "gfx\gbazk.h" 
// Variables
  extern u16 pal[256];
int Seconds, MSeconds, NumBlanks, i;
unsigned short  *pDst, *pSrc, *iDst, iSrc;

// Functions

void Mode4DisplayImage(unsigned short *pSrc, unsigned short *iSrc)
{
	int i;
	pDst = (unsigned short*)SCREEN_PALETTE;
	iDst = (unsigned short*)VRAM_BASE;
		for(i = 0; i < 256; i++)
			pDst[i] = pSrc[i];

        DMA_Copy(3,iSrc,iDst,192,DMA_16NOW);
	//SetMode(MODE_4 | BG2_ENABLE);
}
 
void WaitBlanks(int NumBlanks)
{
	int i = 0;

	while(i < NumBlanks)
	{
		WaitVSync();
		i++;
	}
}

void WaitTime(int Seconds, int MSeconds)
{
	REG_TM2CNT = FREQUENCY_256 | TIMER_ENABLE;
	REG_TM3CNT = TIMER_CASCADE | TIMER_ENABLE;

	REG_TM2D = 0;
	REG_TM3D = 0;

	while(REG_TM3D < Seconds)
	{
	}

	REG_TM2D = 0;

	while(REG_TM2D / (65536/1000) < MSeconds)
	{
	}

	REG_TM2CNT = 0;
	REG_TM3CNT = 0;
	
    REG_TM2D = 0;
	REG_TM3D = 0;
}

void WaitVBlank(void)
{
	while(*((volatile unsigned short*)0x04000004) & (1<<0));
}

void WaitVSync(void)
{
	while(*((volatile unsigned short*)0x04000004) & (1<<0));
	while(!((*((volatile unsigned short*)0x04000004) & (1<<0))));
}
void DoScreenFadeOut(){
#ifdef __debug__ 
    return;
#endif
	//u16* _pal=malloc(256*sizeof(u16));
	u16* _pal=SCREEN_PALETTE;
	int l,loop; 
	u16 r,g,b;
	for(loop=0;loop<32;loop++){
	//	WaitVSync();
	//	WaitVSync();
	//	WaitVSync();
	//	WaitVSync();
		WaitVSync();
		for(l=0;l<256;l++){
			   getRGB(r,g,b,_pal[l]);
			   if(r>0) r--;
			   if(g>0) g--;
			   if(b>0) b--;
			   _pal[l]=(u16)RGB(r,g,b);
		}
	}
}
void DoScreenFadeIn(u16* dPal){
	u16* _pal=(u16*)SCREEN_PALETTE;
	int l,loop;
	u16 r,g,b,r1,g1,b1;
	for(loop=0;loop<32;loop++){
		//WaitVSync();
	//	WaitVSync();
	//	WaitVSync();
	//	WaitVSync();
		WaitVSync();
		for(l=0;l<256;l++){
			getRGB(r,g,b,_pal[l]);
			getRGB(r1,g1,b1,dPal[l]);
			if(r<r1) r++;
			if(g<g1) g++;
			if(b<b1) b++;
			_pal[l]=(u16)RGB(r,g,b);
		}
	}
}
void putpixel(int x,int y,u8 col) 
{ 
	u16 *tc; 
	tc=VRAM_BASE+(y)*120+(x>>1); 
	if(x&1) *tc=((*tc&255)+(col<<8)); 
	else 
		*tc=(*tc&65280)+col; 
} 
inline int convert(u16 str,int n) 
{ 
	return((str>>n)&0x1); 
}
void DrawText(char *str,int x,int y,u8 col)
{
	int i,j,k,n;
	// 字符所在区,位,实际位置
	int qu, wei, location;
	
	while(*str)
	{
		// 得到单字的区位码
		qu = *(str++)-0xa0; 
		wei = *(str++)-0xa0; 
		location = qu*94+wei; 
		
		// 在字库里查找
		for(n=0 ;n<NUM_STR;n++)
		{
			if(ZKQW[n]==location)
			{
				// 根据字符数据画出字符
				for(i=0;i<12;i++)
					for(j=0;j<2;j++)
						for(k=0;k<8;k++) 
							if(convert(ZKDATA[n*24+i*2+j],7-k))
								putpixel(x+j*8+k,y+i,col); 
							x+=12; 
			}
		}
	}
}
void DrawTextB(char *str,int x,int y,u8 col)
{
	int i,j,k,n;
	// 字符所在区,位,实际位置
	int qu, wei, location;
	
	while(*str)
	{
		// 得到单字的区位码
		qu = *(str++)-0xa0; 
		wei = *(str++)-0xa0; 
		location = qu*94+wei; 
		
		// 在字库里查找
		for(n=0 ;n<NUM_STR;n++)
		{
			if(ZKQW[n]==location)
			{
				// 根据字符数据画出字符
				for(i=0;i<12;i++)
					for(j=0;j<2;j++)
						for(k=0;k<8;k++) 
							if(convert(ZKDATA[n*24+i*2+j],7-k))
								putpixel(x+j*8+k,y+i,col); 
							x+=12; 
			}
		}
		WaitTime(0,50);
	}
} 
void clearPalette(){
	u16* _pal=(u16*) SCREEN_PALETTE;
	//WaitVSync();
	int loop;
	for(loop=0;loop<256;loop++) *_pal++=0;
}
void BltTo(u16* src,u16* dst,u16 x,u16 y,u16 width,u16 height,bool bTrans)  
{
	if(!src) return;
	u16 w,h;
	u16* _vram=dst;
	width=width>>1;
	x=x>>1;
	if(width+x<120) w=width ;else w=120-x;
	if(height+y<160) h=height ;else h=160-y;
	u16 keycol=src[0]>>8;
	//u16 keycol2=keycol1<<8;
	u16 x1,y1,i,j;
	if(bTrans){
	for(y1=0;y1<h;y1++){
		for(x1=0;x1<w;x1++){
			i=y1*width+x1;
			j=(y1+y)*120+x1+x;
			if(src[i]>>8!=keycol) _vram[j]=(_vram[j]&0xff)|(src[i]&0xff00);
			if((src[i]&0xff)!=keycol) _vram[j]=(_vram[j]&0xff00)|(src[i]&0xff);
		}
	}
	}else{
		for(y1=0;y1<h;y1++){
			for(x1=0;x1<w;x1++)
				_vram[(y1+y)*120+x1+x]=src[y1*width+x1];
	//	DMA_Copy(3,src+y1*width+x1,_vram+(y1+y)*120+x1+x,2,DMA_16NOW);
		}
	}
}
void DMA_Copy(u8 channel, void* source, void* dest, u32 WordCount, u32 mode)
{	
	switch (channel)	
	{	
	case 0: 
		*(u32*)0x40000B0= (u32)source;	
		*(u32*)0x40000B4= (u32)dest;                                            	
		*(u32*)0x40000B8= WordCount | mode;	
		break;		
	case 1:		
		*(u32*)0x40000BC = (u32)source;		
		*(u32*)0x40000C0 = (u32)dest;		
		*(u32*)0x40000C4 = WordCount | mode;		
		break;		
	case 2:		
		*(u32*)0x40000C8 = (u32)source;	
		*(u32*)0x40000CC = (u32)dest;		
		*(u32*)0x40000D0 = WordCount | mode;		
		break;				
	case 3:	
		*(u32*)0x40000D4 = (u32)source;		
		*(u32*)0x40000D8 = (u32)dest;		
		*(u32*)0x40000DC = WordCount | mode;		
		break;						
	}	
}
void InitializeSprites(){	
	u16 loop;	
	for(loop = 0; loop < 128; loop++){		
		sprites[loop].attribute0 = 160;  //y to > 159		
		sprites[loop].attribute1 = 240;  //x to > 239		
	}	
}
void CopyOAM(){	
	u16 loop;	
	u16* temp;	
	u16* _OAM = (u16*)0x7000000;	
	temp = (u16*)sprites;	
	for(loop = 0; loop < 128*4; loop++)		
	{		
		_OAM[loop] = temp[loop];		
	}	
}
void setpallete(u8 bGrey){
	u16 loop;
	u16 r,g,b,rgb;
	if(!bGrey){
	for(loop=0;loop<256;loop++)	SCREEN_PALETTE[loop]=pal[loop];
	}else{
		for(loop=0;loop<256;loop++) {
			getRGB(r,g,b,pal[loop]);
			rgb=(r+g+b)/3;
			SCREEN_PALETTE[loop]=RGB(rgb,rgb,rgb);
		}
	}
}

⌨️ 快捷键说明

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