#include <nds.h>
#include <l3d/Liquid3D.h>
#include "efs_lib.h"
#include <stdio.h>

L3D::v2d plop;

//---------------------------------------------------------------------------------
int main(int argc, char **argv) {
//---------------------------------------------------------------------------------
	
	// Initialise the console, required for printf
	consoleDemoInit();
	L3D::VideoCore::Init(UL_INIT_ALL);
	L3D::VideoCore::InitGfx(GFX_MODE_3D);
	L3D::VideoCore::InitInterrupts(GFX_INTERRUPT_HBL | GFX_INTERRUPT_VBL);
	ulInitText();
	EFS_Init(EFS_AND_FAT, NULL);
	
	glClearColor(0,0,0,31); // BG must be opaque for AA to work
	glClearPolyID(63); // BG must have a unique polygon ID for AA to work
	glClearDepth(0x7FFF);
	
	//if(nitroFSInit()) 
	L3D::v3d plap(18, 16384, 32456);
	plop.x = 4096;
	plop.y = 16384;
	
	printf("x:%d - y:%d\n", plop.x, plop.y);
	printf("length:%d\n", plop.Length() );
	plop.Normalise();
	printf("x:%d - y:%d\n", plop.x, plop.y);
	printf("length:%d\n", plop.Length() );
	
	printf("x:%d - y:%d - z:%d\n", plap.x, plap.y, plap.z);
	printf("length:%d\n", plap.Length() );
	
	//Création du scenemanager
	L3D::CSceneManager smgr;
	//Création du cube
	L3D::v3d angle(0, 0, 0);
	L3D::CCubeNode cube(8192, RGB15(31, 31, 31) );
	L3D::CCubeNode cube1(4096, RGB15(31, 0, 0) );
	L3D::CCubeNode cube2(4096, RGB15(0, 31, 0) );
	L3D::CCubeNode cube3(4096, RGB15(0, 0, 31) );
	L3D::CCubeNode cube4(4096, RGB15(0, 31, 31) );
	L3D::CCubeNode cube5(4096, RGB15(31, 0, 31) );
	L3D::CCubeNode cube6(4096, RGB15(0, 0, 15) );
	
	//Création de la caméra
	L3D::CCameraNode cam(CAMTYPE_FREEFLY);
	cam.Set(L3D::v3d(10*4096, 10*4096, 10*4096), L3D::v3d(0, 0, 0), L3D::v3d(0, 0, 4096) );
	smgr.Push(&cam);
	
	//Ajout au CSceneManager
	smgr.Push(&cube);
	cube1.TranslateObj(L3D::v3d(0, 0, 16384));
	cube2.TranslateObj(L3D::v3d(0, 16384, 0));
	cube3.TranslateObj(L3D::v3d(16384, 0, 0));
	cube4.TranslateObj(L3D::v3d(0, 0, -16384));
	cube5.TranslateObj(L3D::v3d(0, -16384, 0));
	cube6.TranslateObj(L3D::v3d(-16384, 0, 0));
	cube.Bind(&cube1);
	cube.Bind(&cube2);
	cube.Bind(&cube3);
	cube.Bind(&cube4);
	cube.Bind(&cube5);
	cube.Bind(&cube6);
	
	cube.SetMaterial(GL_AMBIENT, RGB15(8, 8, 8) );


	//Creation du ressource Manager
	L3D::CRessourceManager resman;
	
	//Test chargement image 2D
	UL_IMAGE* tails = resman.LoadImage("efs:/tails.png", UL_IN_VRAM, UL_PF_PAL4);
	
	//Test modèle MD2
	L3D::CMD2MeshNode mod;
	//mod.AssignImage(resman.LoadImage("/teddy.png", UL_IN_VRAM, UL_PF_PAL4) );
	//mod.Tint(RGB15(10, 0, 28) );
	mod.AssignModel(resman.LoadModel("efs:/teddy.md2", 128, 256.0f) );
	mod.Translate(L3D::v3d(0, 0, 32768));
	mod.SetMaterial(GL_AMBIENT, RGB15(8, 8, 8) );
	smgr.Push(&mod);
	
	//Test skydome
	L3D::CSkydomeNode skyd;
	smgr.Push(&skyd);
	
	while(1) 
	{
		ulReadKeys(0);
		
		L3D::VideoCore::StartDrawing3D();
		smgr.SetupCamera();
		
		glLight(0, RGB15(31,31,31) , 0,				  floattov10(-1.0),		 0);
	
		angle.y += 128;
		angle.x += 128;
		cube.Rotate(angle);
		
		smgr.Update();
		smgr.Draw();
		
		L3D::VideoCore::StartDrawing2D();
		ulDrawImage(tails);
		ulDrawString(0, 0, "Hello world!");
		ulPrintf_xy(0, 170, "cpu:%d%%", L3D::VideoCore::cpuUsage);
		ulPrintf_xy(0, 180, "x:%d y:%d z:%d", cam.mLookAt.x, cam.mLookAt.y, cam.mLookAt.z);
		
		L3D::VideoCore::EndDrawing();
		L3D::VideoCore::SyncFrame(GFX_SYNC_60);
	}

	return 0;
}
