/******************************************************************************/
#include "stdafx.h"
#include "headers.h"
/******************************************************************************/
Memc<Str> moduleNames;
bool UseGeneratedWorlds = true;
bool Use2DWorld = true;
void InitPre()
{
	App.name("TerrainGen Library Tutorial"); // set application name
	Paks.add("Bin/Engine.pak"); // load engine data
   App.flag=APP_MAXIMIZABLE|APP_MINIMIZABLE|APP_WORK_IN_BACKGROUND|APP_MEM_LEAKS;
   D.viewRange(400);
   Cam.dist=10;
   Cam.at = Vec(0, -100, 0);

   Sky.atmospheric();
}
/******************************************************************************/
bool Init()
{
	LogDel();
	
	D.sync(false);
	Physics.create("Bin/");
	loadModules("Data/Modules/Test.modules"); //Load .modules file
	moduleNames = GetModuleNames();//Get list of modules and choose your module to generate from;
	/*USING THE LIBRARY - BASIC*/
	{
		//Generating 3D Textures
		Image example3D;
		example3D.createSoft(64, 64, 64, IMAGE_R8G8B8A8); // create a 3D texture beforehand
		Generate3D(VecI(0, 0, 0), example3D, "Test1", false); //VecI area position||||image input|||name of module|||if the algorithm will include "caves"

		//Generating 2D Heightmaps
		Image example2DHeight; example2DHeight.createSoft(128, 128, 1, IMAGE_F32);//Create 2D image for heightmap beforehand
		UInt time = Time.curTimeMs();
		Generate2DHeight(VecI2(0, 0), example2DHeight, "Test1"); //VecI2 area position|||image input|||name of module
		//Generating 2D ColorMaps
		Image example2DColor; example2DColor.createSoft(128, 128, 1, IMAGE_R8G8B8A8);//Create 2D image for colormap/materialmap
		Generate2DColor(VecI2(0, 0), example2DColor, "Test1"); //VecI2 area position|||image input|||name of module
	}

	//USING THE LIBRARY - ADVANCED - Beware this might put your computer on fire!
	if (UseGeneratedWorlds){
		if (Use2DWorld){
			World2D.Create();
		}else{
			World3D.Create();
		}
	}

   return true;
}
/******************************************************************************/
void Shut()
{
	if (UseGeneratedWorlds){
		if (Use2DWorld){
			World2D.del();
		}else{
			World3D.del();
		}
	}
}
/******************************************************************************/
bool Update()
{
	if (UseGeneratedWorlds){
		if (Use2DWorld){
			World2D.update(Cam.at.xz());
		}else{
			World3D.update(Cam.at.xz());
		}
	}
   if(Kb.bp(KB_ESC))return false;
   UInt flag = CAMH_ZOOM;
   if (Ms.b(1))FlagEnable(flag, CAMH_ROT);
   Cam.transformByMouse(0.01f, 50, flag);
   if (Kb.b(KB_SPACE)) Cam.at.y = Lerp(Cam.at.y, Cam.at.y + 10, Time.d()); // move camera up
   if (Kb.b(KB_LCTRL)) Cam.at.y = Lerp(Cam.at.y, Cam.at.y - 10, Time.d()); // move camera down
   if (Kb.b(KB_A))Cam.at -= Cam.matrix.x.x0z() * (Kb.b(KB_LSHIFT) ? 100 : 50) * Time.d(); // move camera left
   if (Kb.b(KB_D))Cam.at += Cam.matrix.x.x0z() * (Kb.b(KB_LSHIFT) ? 100 : 50) * Time.d(); // move camera right
   if (Kb.b(KB_W))Cam.at += Cam.matrix.orn().z * (Kb.b(KB_LSHIFT) ? 100 : 50) * Time.d(); // move camera forward
   if (Kb.b(KB_S))Cam.at -= Cam.matrix.orn().z * (Kb.b(KB_LSHIFT) ? 100 : 50) * Time.d(); // move camera backwards

   return true;
}
/******************************************************************************/
void Render()
{
	if (UseGeneratedWorlds){
		if (Use2DWorld){
			World2D.Draw();
		}else{
			World3D.Draw();
		}
	}
   switch(Renderer())
   {
      case RM_PREPARE:
      {
         LightDir(Cam.matrix.z, 1-D.ambientColor()).add();
      }break;
   }
}
/******************************************************************************/
bool drawphys=false;
void Draw()
{
	if (UseGeneratedWorlds){
		Renderer(Render);

		drawphys = Kb.bp(KB_1) ? !drawphys : drawphys;
		if (drawphys)Physics.draw();
		Renderer.wire = Kb.bp(KB_2) ? !Renderer.wire : Renderer.wire;
		D.text(0, 0.9, S + "FPS: " + (int)Time.fps());
	}
   else{
	   D.clear(BLACK);
	   Flt tpos = 0.8;
	   FREPA(moduleNames){
		   D.text(0, tpos, S + "Module Name: " + moduleNames(i));
		   tpos -= 0.1;
	   }
   }
}
/******************************************************************************/
/******************************************************************************/
