/* Main-Header File inserted by GenCodeC */
/* Libraries */
#include <libraries/mui.h>
#include <exec/types.h>
#include <workbench/startup.h>

/* Prototypes */
#include <clib/muimaster_protos.h>
#include <clib/exec_protos.h>
#include <clib/alib_protos.h>
#include <clib/dos_protos.h>

/*  Ansi  */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* Increase stack size */
LONG __stack=8192;
/* GenCodeC header end */

/* Include generated by GenCodeC */
#include "MeshWriterGUI.h"

#ifndef WITHMWLLIB
#include "/meshlib.h"
#else
#include <meshwriter/meshwriter.h>
#include <pragma/meshwriter_lib.h>
struct MeshWriterBase *MeshWriterBase = NULL;
#endif

#include "modules/modules.h"

/* Declarations for libraries (inserted by GenCodeC) */
struct Library * IntuitionBase;
struct Library * MUIMasterBase;

/* Init() function */
void init( void )
{
	if (!(IntuitionBase = OpenLibrary("intuition.library",37)))
	{
		exit(10);
	}
	if (!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME,MUIMASTER_VMIN)))
	{
		CloseLibrary(IntuitionBase);
		exit(20);
	}
#ifdef WITHMWLLIB
	if (!(MeshWriterBase = (APTR) OpenLibrary("meshwriter.library", 0)))
	{
 		CloseLibrary(IntuitionBase);
 		CloseLibrary(MUIMasterBase);
 		exit(30);
 	}
#endif
}
/* GenCodeC init() end */

/* End() function */
void end( void )
{
#ifdef WITHMWLLIB
	CloseLibrary((APTR) MeshWriterBase);
#endif
	CloseLibrary(MUIMasterBase);
	CloseLibrary(IntuitionBase);
}
/* GenCodeC end() end */

/*********************************************************************/

/* Main Function inserted by GenCodeC */
int main(int argc,char **argv)
{
	struct ObjApp * App = NULL;	/* Object */
	BOOL	running = TRUE;
	ULONG	signal;
	char   bigstring[512];
	char   *stringarg=NULL,*stringarg2=NULL;
	LONG   longarg=0;
	FILE   *fp=NULL;
	ULONG  mesh=0;	/* Our mesh INITIALIZE IT */
	ULONG  format3d=0,format2d=0,drawmode=0,viewtype=0;	/* Our file formats and drawing modes and viewtype */
	STRPTR *fileformats3d,*fileformats2d,*drawmodes; /* Our arrays of file formats and drawmodes */
	ULONG  retval=0;
	STRPTR sfilename=NULL;
	

	/* Program initialisation : generated by GenCodeC */
	init();
	
	/* Create Object : generated by GenCodeC */
	if (!(App = CreateApp()))
	{
		MUI_Request(App->App,App->WI_MAIN,0,"Error","OK","Can't create app");
		end();
	}

//as long as no 2d part is correct
set(App->BT_SAVE2D,MUIA_ShowMe,FALSE);

	/*Initialize the extensions */
	fileformats3d = (STRPTR *)MWL3DFileFormatNamesGet();
	get(App->CY_FORMAT3D,MUIA_Cycle_Active,&longarg);
	format3d=MWL3DFileFormatIDGet(fileformats3d[longarg]);
	set(App->STR_EXTENSION3D,MUIA_String_Contents,MWL3DFileFormatExtensionGet(format3d));

	fileformats2d = (STRPTR *)MWL2DFileFormatNamesGet();
	get(App->CY_FORMAT2D,MUIA_Cycle_Active,&longarg);
	format2d=MWL2DFileFormatIDGet(fileformats2d[longarg]);
	set(App->STR_EXTENSION2D,MUIA_String_Contents,MWL2DFileFormatExtensionGet(format2d));

	drawmodes = (STRPTR *)MWLDrawModeNamesGet();

	while (running)
	{
		switch (DoMethod(App->App,MUIM_Application_Input,&signal))
		{
			case MUIV_Application_ReturnID_Quit:
				running = FALSE;
				break;

			case ID_CALCULATE:
				if((mesh = MWLMeshNew())==NULL) {
					MUI_Request(App->App,App->WI_MAIN,0,"Error","OK","Can't create the mesh");
					break;
				}
				set(App->WI_MAIN,MUIA_Window_Sleep,TRUE);

				get(App->CY_MESH,MUIA_Cycle_Active,&longarg);
				switch(longarg) {
					case 0 :
						ground(mesh);
						break;
					case 1 :
						circle(mesh);
						break;
					case 2 :
						checkboard(mesh);
						break;
					case 3 :
						pyramid(mesh);
						break;
					case 4 :
						cube(mesh);
						break;
					case 5 :
						cubetower(mesh);
						break;
					case 6 :
						wave(mesh);
						break;
					case 7 :
						ripples(mesh);
						break;
					case 8 :
						galaxy(mesh);
						break;
					case 9 :
						landscape(mesh);
						break;
					case 10 :
						pawn(mesh);
						break;
				}
				
				MWLMeshCameraLightDefaultSet(mesh);

				MWLMeshNameGet(mesh,&sfilename);
				set(App->STR_PA_FILE3D,MUIA_String_Contents,sfilename);
				set(App->STR_PA_FILE2D,MUIA_String_Contents,sfilename);
				
				set(App->WI_MAIN,MUIA_Window_Sleep,FALSE);
				break;
			/* end of case ID_CALCULATE */
			case ID_NEW:
				/*
				** Delete the mesh to make a new one
				*/
				set(App->WI_MAIN,MUIA_Window_Sleep,TRUE);
				MWLMeshDelete(mesh);
				mesh=0; /* INITIALIZE IT */
				set(App->WI_MAIN,MUIA_Window_Sleep,FALSE);
				break;
			/* end of case ID_NEW */				
			case ID_ABOUT:                               
				get(App->App,MUIA_Application_Version,&stringarg);
				sprintf(bigstring,"%s\n\nThis program is FREEWARE\n\n",stringarg+6);
				sprintf(bigstring,"%sAnd by the way, only for tests supposed :)\n\n",bigstring);
				sprintf(bigstring,"%sAuthor:\nStephan Bielmann\nIm Dorf 2\n",bigstring);                                  
				sprintf(bigstring,"%sCH-1718 Rechthalten\nURL:http://www.rega-sense.ch/~sbielmann/\nE-Mail:stephan.bielmann@rega-sense.ch\n\n",bigstring);
                
				MUI_Request(App->App,App->WI_MAIN,0,"About","OK",bigstring);
				break;
			/* end of case ID_ABOUT */
			case ID_INFO:
				sprintf(bigstring,"The mesh has :\n\n");
				sprintf(bigstring,"%s%ld vertices\n",bigstring,MWLMeshNumberOfVerticesGet(mesh));
				sprintf(bigstring,"%s%ld polygons\n",bigstring,MWLMeshNumberOfPolygonsGet(mesh));
				sprintf(bigstring,"%s%ld materials\n",bigstring,MWLMeshNumberOfMaterialsGet(mesh));
                
				MUI_Request(App->App,App->WI_MAIN,0,"Information","OK",bigstring);
				break;
			/* end of case ID_INFO */
			case ID_SAVE3D:
				/*
				** Save the mesh as 3D file
				*/
				get(App->PA_FILE3D,MUIA_String_Contents,&stringarg);
				if(strlen(stringarg)==0) {
					MUI_Request(App->App,App->WI_MAIN,0,"Warning","OK","You forgot to enter a filename.");
					break;
				}

				get(App->STR_COPYRIGHT,MUIA_String_Contents,&stringarg2);
				MWLMeshCopyrightSet(mesh,stringarg2);
				
				get(App->CY_FORMAT3D,MUIA_Cycle_Active,&longarg);
				format3d=MWL3DFileFormatIDGet(fileformats3d[longarg]);
				get(App->STR_EXTENSION3D,MUIA_String_Contents,&stringarg2);
				
				if (strlen(stringarg2)) sprintf(bigstring,"%s.%s",stringarg,stringarg2);
				else strcpy(bigstring,stringarg);
				
				if((fp=fopen(bigstring,"r"))!=NULL) {
					fclose(fp);
					if(!MUI_Request(App->App,App->WI_MAIN,0,"Warning","OK|Cancel","Do you want to overwrite this file ?")) break;
				}
				
				set(App->WI_MAIN,MUIA_Window_Sleep,TRUE);
				if((retval=MWLMeshSave3D(mesh,format3d,bigstring,NULL))!=RCNOERROR) {
					sprintf(bigstring,"Could not save the file! RC : %ld",retval);
					MUI_Request(App->App,App->WI_MAIN,0,"Error","OK",bigstring);
				}
				else MUI_Request(App->App,App->WI_MAIN,0,"Information","OK","The file is saved");
				set(App->WI_MAIN,MUIA_Window_Sleep,FALSE);
				break;
			/* end of case ID_SAVE2D */				
			case ID_SAVE2D:
				/*
				** Save the mesh as 2D file
				*/
				get(App->PA_FILE2D,MUIA_String_Contents,&stringarg);
				if(strlen(stringarg)==0) {
					MUI_Request(App->App,App->WI_MAIN,0,"Warning","OK","You forgot to enter a filename.");
					break;
				}

				get(App->STR_COPYRIGHT,MUIA_String_Contents,&stringarg2);
				MWLMeshCopyrightSet(mesh,stringarg2);
				
				get(App->CY_FORMAT2D,MUIA_Cycle_Active,&longarg);
				format2d=MWL2DFileFormatIDGet(fileformats2d[longarg]);
				get(App->STR_EXTENSION2D,MUIA_String_Contents,&stringarg2);

				get(App->CY_VIEWTYPE2D,MUIA_Cycle_Active,&longarg);
				switch (longarg) {
					case 0 :
						viewtype=TVWTOP;
						break;
					case 1 :
						viewtype=TVWBOTTOM;
						break;
					case 2 :
						viewtype=TVWLEFT;
						break;
					case 3 :
						viewtype=TVWRIGHT;
						break;
					case 4 :
						viewtype=TVWFRONT;
						break;
					case 5 :
						viewtype=TVWBACK;
						break;
					case 6 :
						viewtype=TVWPERSP;
						break;
					case 7 :
						viewtype=TVW4SIDES;
						break;
				}

				get(App->CY_DRAWMODE2D,MUIA_Cycle_Active,&longarg);
				drawmode=MWLDrawModeIDGet(drawmodes[longarg]);
				
				if (strlen(stringarg2)) sprintf(bigstring,"%s.%s",stringarg,stringarg2);
				else strcpy(bigstring,stringarg);
				
				if((fp=fopen(bigstring,"r"))!=NULL) {
					fclose(fp);
					if(!MUI_Request(App->App,App->WI_MAIN,0,"Warning","OK|Cancel","Do you want to overwrite this file ?")) break;
				}
				
				set(App->WI_MAIN,MUIA_Window_Sleep,TRUE);
				if((retval=MWLMeshSave2D(mesh,format2d,bigstring,viewtype,drawmode,NULL))!=RCNOERROR) {
					sprintf(bigstring,"Could not save the file! RC : %ld",retval);
					MUI_Request(App->App,App->WI_MAIN,0,"Error","OK",bigstring);
				}
				else MUI_Request(App->App,App->WI_MAIN,0,"Information","OK","The file is saved");
				set(App->WI_MAIN,MUIA_Window_Sleep,FALSE);
				break;



/*

!! viewtype
				get(App->PA_FILE,MUIA_String_Contents,&stringarg);
				if(strlen(stringarg)==0) {
					MUI_Request(App->App,App->WI_MAIN,0,"Warning","OK","You forgot to enter a filename.");
					break;
				}

				get(App->STR_COPYRIGHT,MUIA_String_Contents,&stringarg2);
				MWLMeshCopyrightSet(mesh,stringarg2);
				
				get(App->CY_FORMAT,MUIA_Cycle_Active,&longarg);
				format=MWL3DFileFormatIDGet(fileformats[longarg]);
				get(App->STR_EXTENSION,MUIA_String_Contents,&stringarg2);
				
				if (strlen(stringarg2)) sprintf(bigstring,"%s.%s",stringarg,stringarg2);
				else strcpy(bigstring,stringarg);
				
				if((fp=fopen(bigstring,"r"))!=NULL) {
					fclose(fp);
					if(!MUI_Request(App->App,App->WI_MAIN,0,"Warning","OK|Cancel","Do you want to overwrite t    F     è RŒ%D-his file ?")) break;
				}
				
				set(App->WI_MAIN,MUIA_Window_Sleep,TRUE);
				if(MWLMeshSave3D(mesh,format,bigstring)) MUI_Request(App->App,App->WI_MAIN,0,"Error","OK","Could not save the file!");
				else MUI_Request(App->App,App->WI_MAIN,0,"Information","OK","The file is saved");
				set(App->WI_MAIN,MUIA_Window_Sleep,FALSE);
				break;
*/
			/* end of case ID_SAVE2D */
			case ID_FORMAT3D :
				get(App->CY_FORMAT3D,MUIA_Cycle_Active,&longarg);
				format3d=MWL3DFileFormatIDGet(fileformats3d[longarg]);
				set(App->STR_EXTENSION3D,MUIA_String_Contents,MWL3DFileFormatExtensionGet(format3d));
				break;
			/* end of case ID_FORMAT3D */				
			case ID_FORMAT2D :
				get(App->CY_FORMAT2D,MUIA_Cycle_Active,&longarg);
				format2d=MWL2DFileFormatIDGet(fileformats2d[longarg]);
				set(App->STR_EXTENSION2D,MUIA_String_Contents,MWL2DFileFormatExtensionGet(format2d));
				break;
			/* end of case ID_FORMAT2D */
			case ID_OKCAMLIG: {
				TOCLVertex position;
				TOCLColor color;
				LONG longa=0;
				char *string=NULL;
				
				get(App->STR_CX,MUIA_String_Contents,&string);
				position.x=atof(string);
				get(App->STR_CY,MUIA_String_Contents,&string);
				position.y=atof(string);
				get(App->STR_CZ,MUIA_String_Contents,&string);
				position.z=atof(string);
				MWLMeshCameraPositionSet(mesh,&position);

				get(App->STR_CLX,MUIA_String_Contents,&string);
				position.x=atof(string);
				get(App->STR_CLY,MUIA_String_Contents,&string);
				position.y=atof(string);
				get(App->STR_CLZ,MUIA_String_Contents,&string);
				position.z=atof(string);
				MWLMeshCameraLookAtSet(mesh,&position);

				get(App->STR_LX,MUIA_String_Contents,&string);
				position.x=atof(string);
				get(App->STR_LY,MUIA_String_Contents,&string);
				position.y=atof(string);
				get(App->STR_LZ,MUIA_String_Contents,&string);
				position.z=atof(string);
				MWLMeshLightPositionSet(mesh,&position);

				get(App->SL_RED,MUIA_Slider_Level,&longa);
				color.r=longa;
				get(App->SL_GREEN,MUIA_Slider_Level,&longa);
				color.g=longa;
				get(App->SL_BLUE,MUIA_Slider_Level,&longa);
				color.b=longa;
				MWLMeshLightColorSet(mesh,&color);

				set(App->WI_MAIN,MUIA_Window_Sleep,FALSE);

				break;
			}
			/* end of case ID_OKCAMLIG */
			case ID_CANCELCAMLIG:
				set(App->WI_MAIN,MUIA_Window_Sleep,FALSE);
				
				break;
			/* end of case ID_CANCELCAMLIG */
			case ID_OPENCAMLIG: {
				TOCLVertex position;
				TOCLColor color;
				char string[80];
				LONG longa=0;
				
				MWLMeshCameraPositionGet(mesh,&position);
				sprintf(string,"%g",position.x);
				set(App->STR_CX,MUIA_String_Contents,&string);
				sprintf(string,"%g",position.y);
				set(App->STR_CY,MUIA_String_Contents,&string);
				sprintf(string,"%g",position.z);
				set(App->STR_CZ,MUIA_String_Contents,&string);

				MWLMeshCameraLookAtGet(mesh,&position);
				sprintf(string,"%g",position.x);
				set(App->STR_CLX,MUIA_String_Contents,&string);
				sprintf(string,"%g",position.y);
				set(App->STR_CLY,MUIA_String_Contents,&string);
				sprintf(string,"%g",position.z);
				set(App->STR_CLZ,MUIA_String_Contents,&string);

				MWLMeshLightPositionGet(mesh,&position);
				sprintf(string,"%g",position.x);
				set(App->STR_LX,MUIA_String_Contents,&string);
				sprintf(string,"%g",position.y);
				set(App->STR_LY,MUIA_String_Contents,&string);
				sprintf(string,"%g",position.z);
				set(App->STR_LZ,MUIA_String_Contents,&string);

				MWLMeshLightColorGet(mesh,&color);
				longa=color.r;
				set(App->SL_RED,MUIA_Slider_Level,&longa);
				longa=color.g;
				set(App->SL_BLUE,MUIA_Slider_Level,&longa);
				longa=color.b;
				set(App->SL_GREEN,MUIA_Slider_Level,&longa);
			
				break;
			}

			case ID_CAMLIGACT:
			/* end of case ID_CAMLIGACT */

			/* End computing of IDCMP */

			default:
				break;
		}
		if (running && signal) Wait(signal);
	}
	DisposeApp(App);
	end();
}

VOID wbmain (struct WBStartup *wbargs) {
	main(0,NULL);
}
