// Dark GDK - The Game Creators - www.thegamecreators.com

// include Dark GDK header file
#include "DarkGDK.h"

// main entry point for program
void DarkGDK ( void )
{
	// switch to media directory
	dbSetDir ( "media\\" );

	// set sync rate
	dbSyncOn   ( );
	dbSyncRate ( 0 );

	// load images
	dbLoadImage ( "media\\face.jpg", 1 );
	dbLoadImage ( "media\\water.jpg", 2 );

	// colour the backdrop
	dbColorBackdrop ( dbRGB ( 0, 0, 0 ) );

	// create two matrices
	for ( int m = 1; m < 3; m++ )
	{
		int s = 0;

		if ( m == 1 )
			s = 200;

		if ( m == 2 )
			s = 100;

		// make matrix
		dbMakeMatrix ( m, ( float ) s, ( float ) s, 36, 36 );

		// position matrix
		if ( m == 1 )
			dbPositionMatrix ( m, -50, 20, -50 );

		if ( m == 2 )
			dbPositionMatrix ( m, 0, 120, 0 );

		// set texturing and ghosting
		dbPrepareMatrixTexture ( m, m, 36, 36 );
		dbGhostMatrixOn ( m );

		int tc = 1;

		// control matrix tile
		for ( int z = 35; z > -1; z-- )
		{
			int x = 0;

			while ( x < 36 )
			{
				dbSetMatrixTile ( m, x, z, tc );
				tc++;
				x++;
			}
		}
	}

	// set up lights
	dbSetPointLight ( 0, 50, 500, 50 );
	dbColorLight ( 0, 1000, 1000, 200 );

	// position camera
	dbPositionCamera ( 50, 170, 50 );
	dbPointCamera ( 50,0,50 );
	
	float a = 0.0f;

	// main loop
	while ( LoopGDK ( ) )
	{
		// quit on escape key
		if ( dbEscapeKey ( ) )
			return;

		// adjust height and normals of matrices to create movement
		for ( int m = 1; m < 3; m++ )
		{
			a = dbWrapValue ( a + 1 );

			for ( int z = 0; z < 37; z++ )
			{
				for ( int x = 0; x < 37; x++ )
				{
					float fX = dbCos ( a + ( x * 10 ) );
					float fY = dbSin ( a + ( z * 10 ) );

					float nx = 0.0f - ( dbCos ( a + ( x * 10 ) ) * 0.25f );
					float nz = 0.0f - ( dbSin ( a + ( z * 10 ) ) * 0.25f );
					float ny = 1.0f - nx - nz;

					dbSetMatrixHeight ( m, x, z, ( fX + fY ) * 15.0f );
					dbSetMatrixNormal ( m, x, z, nx, ny, nz );
				}
			}

			// update the matrix
			dbUpdateMatrix ( m );
		}

		// show information
		dbText ( 20, dbScreenHeight ( ) - 40, "Water Matrix" );

		// display frames per second
		char szFPS [ 256 ] = "";
		strcpy ( szFPS, "fps = " );
		strcat ( szFPS, dbStr ( dbScreenFPS ( ) ) );
		dbText ( dbScreenWidth ( ) - 20 - dbTextWidth ( szFPS ), dbScreenHeight ( ) - 40, szFPS );

		// update screen
		dbSync ( );
	}
}