draw.c 5.55 KB

/*---------------------------------------------------------------------*
        Copyright (C) 1997 Nintendo.
        
        $RCSfile: draw.c,v $
        $Revision: 1.1.1.1 $
        $Date: 2002/05/02 03:27:12 $
 *---------------------------------------------------------------------*/
#include"headers.h"
extern Gfx		*glistp;    
extern Dynamic	*dynamicp;
extern void	*cfb_ptrs[2];
extern int	draw_buffer;
extern ctrl Ctrl[4];
extern OSMesgQueue intMessageQ,dmaMessageQ;
extern OSMesg	dummyMessage,dmaMessageBuf;
extern OSIoMesg dmaIOMessageBuf;
extern unsigned char ascrom[];
#define	G_CC_INT4	0,0,0,PRIMITIVE,0,0,0,TEXEL0
static	Mtx	IMTX={
	0x00010000, 0x00000000, 0x00000001, 0x00000000,
	0x00000000, 0x00010000, 0x00000000, 0x00000001,
	0x00000000, 0x00000000, 0x00000000, 0x00000000,
	0x00000000, 0x00000000, 0x00000000, 0x00000000
};
extern int timer,resetflag,color,mode,kanadr;
static short palet[16]={0x0000,0x1085,0x2109,0x318D,0x4211,0x5295,0x6319,0x739D,0x8C63,0x9CE7,0xAD6B,0xBDEF,0xCE73,0xDEF7,0xEF7B,0xFFFF};
static int ccode[48]={
  0,  0,  0,     0,  0,136,      136,  0,  0,     255,119,204,
  0,136,  0,    51,255,170,      255,153, 51,     102,102,102,
255,204,187,     0,  0,255,      255,  0,  0,     255,  0,255,
  0,255,  0,     0,255,255,      255,255,  0,     255,255,255};

#include"msg.sjs"
/* CLEAR FRAMEBUFFER */
static void clear_framebuffer(void)
{
	gDPPipeSync(glistp++);
	gDPSetCycleType(glistp++, G_CYC_FILL);
	/* clear color, cvg = FULL or 1 */
	gDPSetColorImage(glistp++,G_IM_FMT_RGBA,G_IM_SIZ_16b,SCREEN_WD,cfb_ptrs[draw_buffer]);

	gDPSetFillColor(glistp++, GPACK_RGBA5551(0,0,85,1) << 16 | GPACK_RGBA5551(0,0,85,1));
	gDPPipeSync(glistp++);
	gDPFillRectangle(glistp++,0,0,SCREEN_WD-1,SCREEN_HT-2);
	gDPPipeSync(glistp++);
	gDPSetCycleType(glistp++,G_CYC_COPY);
}
/* CLEAR Z-BUFFER */
static void clear_zbuffer(void)
{
	gDPPipeSync(glistp++);
	gDPSetCycleType(glistp++, G_CYC_FILL);

	gDPSetMaskImage(glistp++, K0_TO_PHYS(zbuffer));
	gDPSetColorImage(glistp++, G_IM_FMT_RGBA, G_IM_SIZ_16b, SCREEN_WD, K0_TO_PHYS(zbuffer));
	gDPSetFillColor(glistp++, GPACK_ZDZ(G_MAXFBZ, 0) << 16 | GPACK_ZDZ(G_MAXFBZ, 0));
	gDPPipeSync(glistp++);
	gDPFillRectangle(glistp++, 0, 0, SCREEN_WD-1, SCREEN_HT-1);
	gDPPipeSync(glistp++);
	gDPSetColorImage(glistp++, G_IM_FMT_RGBA, G_IM_SIZ_16b, SCREEN_WD, cfb_ptrs[draw_buffer]);
	gDPSetDepthSource(glistp++, G_ZS_PIXEL);
}
/* 戳机1矢机山绩 */
void kanchr(int x,int y,int code)
{
	int i;
	i=LeoGetKAdr(code);

	dmaIOMessageBuf.hdr.pri 	= OS_MESG_PRI_NORMAL;
	dmaIOMessageBuf.hdr.retQueue 	= &dmaMessageQ;
	dmaIOMessageBuf.dramAddr 	= &kbuffer[kanadr];
	dmaIOMessageBuf.devAddr 	= DDROM_FONT_START + i;
	dmaIOMessageBuf.size		= 128;
	
	handler->transferInfo.cmdType	= OS_OTHERS;
	osEPiStartDma(handler, &dmaIOMessageBuf, OS_READ);

	/* gDPLoadTextureBlock_4b(glistp++,&kbuffer[kanadr],G_IM_FMT_CI,16,16,0,G_TX_CLAMP,G_TX_CLAMP,G_TX_NOMASK,G_TX_NOMASK,G_TX_NOLOD,G_TX_NOLOD); */
	gDPLoadTextureBlock_4b(glistp++,&kbuffer[kanadr],G_IM_FMT_I,16,16,0,G_TX_CLAMP,G_TX_CLAMP,G_TX_NOMASK,G_TX_NOMASK,G_TX_NOLOD,G_TX_NOLOD);
	gSPTextureRectangle(glistp++,x<<2,y<<2,(x+16)<<2,(y+16)<<2,0,0,0,0x400,0x400);
	kanadr+=128;
	osRecvMesg(&dmaMessageQ,&dummyMessage,OS_MESG_BLOCK);
}
/* 戳机矢机误山绩 */
void printk(int x,int y,unsigned char *s)
{
	int i;
	for(i=0;s[i];i+=2,x+=16)kanchr(x,y,s[i]*256+s[i+1]);
}
/* ASCII矢机误山绩 (zは0×2) */
void printa(int x,int y,int z,unsigned char *s)
{
	int ofs,i,sx,dx,dy,cy;
	for(i=0;s[i];i++){
		ofs=LeoGetAAdr((int)s[i]-32+z*192,&dx,&dy,&cy);

		dmaIOMessageBuf.hdr.pri 	= OS_MESG_PRI_NORMAL;
		dmaIOMessageBuf.hdr.retQueue 	= &dmaMessageQ;
		dmaIOMessageBuf.dramAddr 	= &kbuffer[kanadr];
		dmaIOMessageBuf.devAddr 	= DDROM_FONT_START + ofs;
		dmaIOMessageBuf.size		= 128;

		handler->transferInfo.cmdType	= OS_OTHERS;
		osEPiStartDma(handler, &dmaIOMessageBuf, OS_READ);

		sx=(dx+1)&254;
		/* gDPLoadTextureBlock_4b(glistp++,&kbuffer[kanadr],G_IM_FMT_I,dx,dy,0,G_TX_CLAMP,G_TX_CLAMP,G_TX_NOMASK,G_TX_NOMASK,G_TX_NOLOD,G_TX_NOLOD); */
		gDPLoadTextureTile_4b(glistp++,&kbuffer[kanadr],G_IM_FMT_I,sx,dy,0,0,(sx-1)<<G_TEXTURE_IMAGE_FRAC,(dy-1)<<G_TEXTURE_IMAGE_FRAC,0,G_TX_CLAMP,G_TX_CLAMP,G_TX_NOMASK,G_TX_NOMASK,G_TX_NOLOD,G_TX_NOLOD);
		gSPTextureRectangle(glistp++,x<<2,(y-cy)<<2,(x+dx)<<2,(y-cy+dy)<<2,0,0,0,0x400,0x400);
		kanadr+=128;x+=dx+1;
		osRecvMesg(&dmaMessageQ,&dummyMessage,OS_MESG_BLOCK);
	}
}
void dspall(void)
{
	printk(24,32,strings[0]);
	printk(24,52,strings[1]);
	printa(24,88,10,strings[2]);
	printa(24,108,11,strings[3]);
} 
/* DRAW */
void draw(void)
{
	u16	perspNorm;
	/* 介袋步 */
	gSPDisplayList(glistp++,rdpinit_dl);
	gSPDisplayList(glistp++,rspinit_dl);
	clear_zbuffer();
	clear_framebuffer();
	gSPSetGeometryMode(glistp++,G_CULL_BACK | G_SHADING_SMOOTH | G_SHADE);

	guPerspective(&dynamicp->projection,&perspNorm,33,320.0/240.0,16,2000,1.0);
	gSPPerspNormalize(glistp++,perspNorm);
	gSPMatrix(glistp++, K0_TO_PHYS((u32) &(dynamicp->projection)),G_MTX_PROJECTION|G_MTX_LOAD|G_MTX_NOPUSH);
	guLookAt(&dynamicp->viewing,0,0,1000,0,0,0,0,1,0);
	gSPMatrix(glistp++, K0_TO_PHYS((u32) &(dynamicp->viewing)),G_MTX_MODELVIEW|G_MTX_LOAD|G_MTX_NOPUSH);
	/* 称硷肋年 */
	gSPDisplayList(glistp++,sprite_dl);
	gDPPipeSync(glistp++);
	/* gDPSetCycleType(glistp++,G_CYC_COPY); */
	gDPSetCycleType(glistp++,G_CYC_1CYCLE);

	gDPSetRenderMode(glistp++,G_RM_XLU_SURF,G_RM_XLU_SURF2);
	gDPSetCombineMode(glistp++,G_CC_INT4,G_CC_INT4);
	gDPSetPrimColor(glistp++,0,0,ccode[color*3],ccode[color*3+1],ccode[color*3+2],255);
	kanadr=0;
	if(Ctrl[0].nowtrg&0x2000)color=(color+1)&15;
	dspall();
	osInvalDCache(kbuffer,kanadr);
	gDPFullSync(glistp++);
	gSPEndDisplayList(glistp++);
}