drawleaf.c 4.16 KB
/********************************************************************************
						   NINTENDO64 DIsk Drive IPL4

							   Leaf drawing module

								December 6, 1996
 ********************************************************************************/

#include <ultra64.h>
#include "ipl4.h"
#include "graphics.h"
#include "static.h"


#define	NUMLEAVES		16

CtrlLeaf leafTable[NUMLEAVES];					/* leaf control works			*/

/********************************************************************************/
/*																				*/
/*	Make display list.															*/
/*																				*/
/********************************************************************************/
static void
MakeDisplayList(CtrlLeaf *leaf, int drawmode)
{
	FVector	radian;
	FVector position = leaf->position;
	FVector scaling  = leaf->scaling;
	Mtx *	modeling = (Mtx *)AllocDynamic(sizeof(Mtx));


	if (drawmode == LEAF_DRAW_MIRROR) {
		position.y *= -1.0f;
		scaling.y  *= -1.0f;	
	}
	AngvecToRadvec(&radian, &leaf->angle);
	CreateModelMatrix(modeling, &position, &radian, &scaling);

	gSPMatrix(graphicp++, K0_TO_PHYS(modeling), G_MTX_MODELVIEW|G_MTX_LOAD|G_MTX_NOPUSH);
	gSPDisplayList(graphicp++, Gfx_Leaf);
}
/********************************************************************************/
/*																				*/
/*	Initilaize leaf works.														*/
/*																				*/
/********************************************************************************/
extern void
InitLeaf(void)
{
	int index;

	for (index = 0; index < NUMLEAVES; index++) {
		leafTable[index].flag = 0;
	}
}
/********************************************************************************/
/*																				*/
/*	Draw leaves.																*/
/*																				*/
/********************************************************************************/
extern void
DrawLeaf(void)
{
	int		 count;
	CtrlLeaf *leaf;


	for (leaf = leafTable, count = 0; count < NUMLEAVES; leaf++, count++) {
		if (leaf->flag != 0) {
			leaf->angle.x += leaf->angvelo.x;
			leaf->angle.z += leaf->angvelo.z;
			leaf->shake   += leaf->angvelo.y;

			if ((leaf->falling -= 6.0f) < -16.0f)  leaf->falling = -16.0f;
			if ((leaf->moving  -= 1.2f) <   0.0f)  leaf->moving  =   0.0f;

			leaf->position.x += sin(leaf->angle.y) * (16.0f * sin(leaf->shake) + leaf->moving);
			leaf->position.z += cos(leaf->angle.y) * (16.0f * sin(leaf->shake) + leaf->moving);
			leaf->position.y +=	leaf->falling;

			if (leaf->flag = (leaf->position.y > 20.0f)) {
				MakeDisplayList(leaf, LEAF_DRAW_NORMAL);
				MakeDisplayList(leaf, LEAF_DRAW_MIRROR);
			}
		}
	}
}
/********************************************************************************/
/*																				*/
/*	Make a leaf.																*/
/*																				*/
/********************************************************************************/
extern void
MakeLeaf(FVector *position)
{
	int		 count;
	CtrlLeaf *leaf;


	for (leaf = leafTable, count = 0; count < NUMLEAVES; leaf++, count++) {
		if (leaf->flag == 0)  break;
	}
	if (count < NUMLEAVES) {
		float scale = Randomf() * 0.5f + 0.5f;

		leaf->flag		  = 1;
		leaf->shake		  = 0;
		leaf->position	  = *position;
		leaf->position.y += 50.0f;
		leaf->moving	  =  Randomf() * 20.0f + 20.0f;
		leaf->falling	  =  Randomf() * 15.0f + 10.0f;
		leaf->angvelo.y	  =  Randomf() * 0x400 + 0x600;		/* shake angle velocity	*/
		leaf->angvelo.x	  = (Randomf() - 0.5f) * 0x1000;
		leaf->angvelo.z	  = (Randomf() - 0.5f) * 0x1000;

		SetFVector(&leaf->scaling, scale    , scale    , scale	  );
		SetSVector(&leaf->angle  , Randomi(), Randomi(), Randomi());
	}
}
/********************************************************************************/
/*																				*/
/*	Blow leaves.																*/
/*																				*/
/********************************************************************************/
extern void
BlowLeaf(FVector *center)
{
	int		count;
	FVector position;

	for (count = 0; count < NUMLEAVES; count++) {
		position.x = center->x + (Randomf() - 0.5f) * 100.0f;
		position.z = center->z + (Randomf() - 0.5f) * 100.0f;
		position.y = center->y;

		MakeLeaf(&position);
	}
	for (count = 0; count < NUMLEAVES; count++) {
		leafTable[count].falling += Randomf() * 30.0f + 60.0f;
	}
}