// CINEBENCH script, (C) MAXON Computer GmbH 2006

var AUTORUN_RENDERING_SINGLE = 5;
var AUTORUN_RENDERING_MULTI = 4;
var AUTORUN_OGLENH = 3;
var AUTORUN_OGLHW = 2;
var AUTORUN_ALL = 1;
// set this variable to the numerical value of AUTORUN_* if you want to run CINEBENCH in batch mode, otherwise 0
var AUTORUN = 0;

var version   = "V5.00";
var PLUGIN_ID = 900323;

var calc_maximum  = 0;
var c4d_shad_cb   = 0.0;	// sum for 3D Bench
//var ogl_soft_cb   = 0.0;	// sum for 3D Bench
var ogl_hard_cb   = 0.0;	// sum for 3D Bench
//var ogl_hard_ext  = 0.0;	// sum for 3D Bench (Enhanced OpenGL)
var render_1_cb   = 0.0;	// sum for RenderBench single
var render_x_cb   = 0.0;	// sum for RenderBench MP
var render_1_time = 0.0;
var render_x_time = 0.0;
var renderer = "",info_machine ="", info_os = "";

var dialog;
var autoruntime = 0;
var autostarted = 0;
var hasOGLExt = 0;

var SHADING_FAC = 50;

var R_PANELVIEW = 59000;

CalcNewCB(oldcb,newcb)
{
	println("CB ",tostring(newcb,"0.f")," (",tostring(oldcb,"0.f"),")");
	if (!calc_maximum) return newcb;
	if (oldcb<newcb) return newcb;
	return oldcb;
}

// ************************ Renderbereich **********************
PrepareRender(name)
{
	var d,start;

	LoadDocument(name);
	d = GetActiveDocument();

	if (!d)
	{
		var text;
    text = stradd("Error!\n",name->GetLastString(),"\nnot found");

		TextDialog(text);
		return FALSE;
	}

	var rd = d->GetFirstRenderData();
	var bc = rd->GetContainer();

	//println("...prerendering");

	var xm = bc->GetData(RDATA_XRES);
	var ym = bc->GetData(RDATA_YRES);
	var shadow = bc->GetData(RDATA_SHADOW);

	bc->SetData(RDATA_XRES,20);
	bc->SetData(RDATA_YRES,10);
	bc->SetData(RDATA_SHADOW,0);
	rd->SetContainer(bc);

	var nap = new(BaseBitmap,20,10);
	d->Render(nap,FALSE);

	bc->SetData(RDATA_XRES,xm);
	bc->SetData(RDATA_YRES,ym);
	bc->SetData(RDATA_SHADOW,shadow);
	rd->SetContainer(bc);

	return TRUE;
}

DoRender(mp)
{
	var start;
	var d  = GetActiveDocument();
	var rd = d->GetFirstRenderData();
	var bc = rd->GetContainer();

	bc->SetData(RDATA_SUPPRESSMP,mp);
	rd->SetContainer(bc);

	var map = new(BaseBitmap,bc->GetData(RDATA_XRES),bc->GetData(RDATA_YRES));

	//println("...rendering scene");

	start = time();
	d->Render(map,TRUE);

	return start;
}

// ***********************  3D Bereich **********************
ClearAll(doc)
{
	while (doc->GetFirstObject())
	{
		doc->GetFirstObject()->Remove();
	}

	while (doc->GetFirstMaterial())
	{
		doc->GetFirstMaterial()->Remove();
	}
}

ShadeIt_2(name,cycles,teststring)
{
	var doc,f1,f2,t1,t2,t,i,j;

	LoadDocument(name);
	doc = GetActiveDocument();
	var fn = new(Filename);
	fn->SetFullString(teststring + ": Pump Action");
	//doc->SetFilename(fn);

	// primitiv caches aufbauen!!!
	DrawViews(DA_NO_THREAD | DA_NO_REDUCTION | DA_FORCEFULLREDRAW | DA_NODUALPLANE | DA_ONLY_ACTIVE_VIEW);

	t = new(BaseTime);
	f1 = doc->GetMinTime()->GetFrame(doc->GetFps());
	f2 = doc->GetMaxTime()->GetFrame(doc->GetFps());

	t1 = time();
	for (j=0;j<cycles;j++)
	{
		for(i=f1; i<f2; i++)
		{
			t->SetFrame(i,doc->GetFps());
			doc->SetTime(t);
			DrawViews(DA_NO_THREAD | DA_NO_REDUCTION | DA_FORCEFULLREDRAW | DA_NODUALPLANE | DA_ONLY_ACTIVE_VIEW);
		}

		if (time()-t1>8000) { j++; break; }
		j++;

		for(i=f2; i>=f1; i--)
		{
			t->SetFrame(i,doc->GetFps());
			doc->SetTime(t);
			doc->AnimateDocument(0);

			DrawViews(DA_NO_THREAD | DA_NO_REDUCTION | DA_FORCEFULLREDRAW | DA_NODUALPLANE | DA_ONLY_ACTIVE_VIEW);
		}
		if (time()-t1>8000) { j++; break; }
	}
	t2 = time();
	var t = ((t2-t1)/j/1000.0);
	var fps = (f2-f1)/t;
	//println(tostring(t,"0.2f")," sec; ",tostring(fps,".1f")," fps");

	Sleep(400);

	KillDocument(doc);

	return (t2-t1)/j/1000.0;
}

DoShade(teststring,type)
{
	var t2;
	var e2=0.0;

gc();

	var doc=GetActiveDocument();

	KillDocument(doc);

	SetMousePointer(MOUSE_HIDE);

	var file = GeGetStartupPath(); file->AddLast("plugins"); file->AddLast("bench"); 
	
	if (type==2)
	{
		file->AddLast("ogl"); file->AddLast("ogl.c4d");
		t2 = ShadeIt_2(file,SHADING_FAC,teststring);
//println("ogl: ",t2);
		if (t2!=0.0) e2 = 41595.0/t2;
	}
	else if (type==3)
	{
		file->AddLast("eogl"); file->AddLast("eogl.c4d");
		t2 = ShadeIt_2(file,SHADING_FAC,teststring);
//println("eogl: ",t2);
		if (t2!=0.0) e2 = 96899.97/t2;
	}

	SetMousePointer(MOUSE_SHOW);

	return e2;
}

// ************************ make a dialog class  ********************************************

enum
{
	B_COMPLETE  = 4000,
	B_RENDER,
	B_RENDER_M,
	B_CINEMA,
	B_OPENGL_SW,
	B_OPENGL_HW,
	B_OPENGL_EXT,
	B_CLIP1,
	B_CLIP2,

	R_RENDER,
	R_RENDER_M,
	R_CINEMA,
	R_OPENGL_SOFT,
	R_OPENGL_HARD,
	R_OPENGL_EXT,

	R_GAIN_MP,
	R_GAIN_OGLHW_OGLSW,

	R_RM1,
	R_RM2,

	B_INFO_NAME,
	B_INFO_MACH,
	B_INFO_MHZ,
	B_INFO_CPUCOUNT,
	B_INFO_OS,
	B_INFO_GFX,

	B_ABOUT0,
	B_ABOUT1,
	B_ABOUT2,
	B_ABOUT3,
	B_ABOUT4,
	B_BEST_CB_SCORE
}

// definition of my user area
class MyUserArea : GeUserArea
{
	private:
		var bmp;

	public:
		MyUserArea(id,dialog);
		Init();
		GetUserWidth();
		GetUserHeight();
		Draw(x1,y1,x2,y2);
		InputEvent(msg);
}

MyUserArea::MyUserArea(id,dialog)
{
	super(id,dialog);
	bmp = new(BaseBitmap,1,1);
	if (bmp)
	{
		var f = GeGetStartupPath(); 
		f->AddLast("plugins"); 
		f->AddLast("bench"); 
		f->AddLast("cb2.jpg");

		bmp->Load(f);
	}
}

MyUserArea::Init()
{
}

MyUserArea::GetUserWidth()
{
	return bmp->GetWidth();
}

MyUserArea::GetUserHeight()
{
	return bmp->GetHeight();
}

MyUserArea::InputEvent(msg)
{
	if (msg->GetInt(BFM_INPUT_DEVICE)==BFM_INPUT_MOUSE)
	{
		if (msg->GetInt(BFM_INPUT_CHANNEL)==BFM_INPUT_MOUSELEFT)
		{
			CallCommand(16377); // MAXON Support...			
		}
	}
}

MyUserArea::Draw(x1,y1,x2,y2)
{
	var w = GetWidth()-1;
	var h = GetHeight()-1;

	DrawSetPen(COLOR_BG);
	DrawBitmap( bmp, 0, 0, bmp->GetWidth()-1, bmp->GetHeight()-1, 0, 0, GetWidth()-1, GetHeight()-1, BMP_NORMAL);
}

class MyDialog : GeDialog
{
	private:
		var start,rendermode,multi;
		var ua;

	public:
		MyDialog();

		CreateLayout();
		Command(id,msg);
		UpdateDialog();
		Init();
		CoreMessage(id,msg);
		Timer(msg);

		BringToFront(managerid);

		AllTestsDone();

		Test1();
		Test2();
		Test_C4D_Shading();
//		Test_OGL_SW();
		Test_OGL_HW();
//		Test_OGL_Ext();

		OptEnable();
		StartAllTests();
}

MyDialog::MyDialog()
{
	super(PLUGIN_ID);
	rendermode = -1;
	multi = FALSE;
}

MyDialog::BringToFront(managerid)
{
	var msg = new(BaseContainer,'show');
	msg->SetData('id',managerid);
	SendCoreMessage(0xC0FFEE,msg,0);
}

MyDialog::AllTestsDone()
{
	if (!AUTORUN) return;

	var t;
	var le;

	le = GeGetLineEnd();

	t =  "CINEBENCH R10"+le;
	t += "Rendering (Single   CPU)           : "; if (render_1_cb!=0.0) t += tostring(render_1_cb,".0f"); else t += "---"; t += " CB-CPU"; if (render_1_time!=0.0) t += ", "+tostring(render_1_time,".1f")+" sec"; t += le;
	t += "Rendering (Multiple CPU)           : "; if (render_x_cb!=0.0) t += tostring(render_x_cb,".0f"); else t += "---"; t += " CB-CPU"; if (render_x_time!=0.0) t += ", "+tostring(render_x_time,".1f")+" sec"; t += le;
	//t += "Shading (CINEMA 4D)                : "; if (c4d_shad_cb!=0.0) t += tostring(c4d_shad_cb,".0f"); else t += "---"; t += " CB-GFX"+le;
	//t += "Shading (OpenGL Software Lighting) : "; if (ogl_soft_cb!=0.0) t += tostring(ogl_soft_cb,".0f"); else t += "---"; t += " CB-GFX"+le;
	t += "Shading (OpenGL Standard)          : "; if (ogl_hard_cb!=0.0) t += tostring(ogl_hard_cb,".0f"); else t += "---"; t += " CB-GFX"+le;
//	t += "Shading (OpenGL Enhanced)          : "; if (hasOGLExt) t += ((ogl_hard_ext!=0.0) ? tostring(ogl_hard_ext,".0f") :  "---") + " CB-GFX"+le; else t += "n.a." + le;
	t += le;

	SetClipboardData(t);

	println("AUTORUN complete\n");

	Sleep(2);

	shutdown();
}

var RENDER_FAC = 884400.0;

MyDialog::CoreMessage(id,msg)
{
	if (id==-1003 && rendermode!=100)
	{
		var diff = msg->GetInt(BFM_CORE_PAR2);
		if (diff==-1)
		{
			println("Rendering aborted\n");
		}
		else
		{
			if (rendermode==0)
			{
				render_1_time = diff/1000.0;
				if (diff!=0.0) render_1_cb = CalcNewCB(render_1_cb,RENDER_FAC / render_1_time);
				if (!(GetCPUCount()>1 && multi))
				{
					BringToFront(PLUGIN_ID);
					AllTestsDone();
				}
//println(tostring(render_1_time,".1f")," sec");
			}
			else if (rendermode==1)
			{
				render_x_time = diff/1000.0;
				if (diff!=0.0) render_x_cb = CalcNewCB(render_x_cb,RENDER_FAC / render_x_time);
				multi=FALSE;
				BringToFront(PLUGIN_ID);
//println(tostring(render_x_time,".1f")," sec");
				AllTestsDone();
			}
		}
		var d = GetActiveDocument();
		KillDocument(d);

		rendermode = -1;
		Init();
		println(" ");
	}
}


MyDialog::CreateLayout()
{
	SetTitle("CINEBENCH R10");
	SetTimer(1000);

	if (AUTORUN)
	{
		autoruntime = time();
		println("CINEBENCH AUTORUN\nPlease wait until all tests are done!\n");
	}

	AddGroupBeginV(1000,BFH_SCALEFIT,1,"",0);
		AddGroupBorderSpace(4,4,4,4);

		AddGroupSpace(8,8);

		AddGroupBeginV(1000,BFH_SCALEFIT,2,"Info",0);
			AddGroupBorder(BORDER_GROUP_IN|BORDER_WITH_TITLE);
			AddGroupBorderSpace(4,4,4,4);
			AddGroupSpace(2,1);

			AddStaticText(1000,BFH_LEFT,0,0,"Processor",0);
			AddEditText(B_INFO_MACH,BFH_SCALEFIT,120,11);

			AddStaticText(1000,BFH_LEFT,0,0,"MHz (real freq.)",0);
			AddGroupBeginH(0,BFH_SCALEFIT,1,"",0);
				AddEditText(B_INFO_CPUCOUNT,BFH_LEFT,20,11);
				AddStaticText(0,0,0,0,"x",0);
				AddEditText(B_INFO_MHZ,BFH_SCALEFIT,100,11);
			AddGroupEnd();			

			AddStaticText(1000,BFH_LEFT,0,0,"OS",0);
			AddEditText(B_INFO_OS,BFH_SCALEFIT,120,11);

			AddStaticText(1000,BFH_LEFT,0,0,"GFX-Board",0);
			AddEditText(B_INFO_GFX,BFH_SCALEFIT,120,11);

			AddStaticText(1000,BFH_LEFT,0,0,"Tester",0);
			AddEditText(B_INFO_NAME,BFH_SCALEFIT,120,11);

		AddGroupEnd();

		AddGroupBeginV(1000,BFH_FIT,2,"Main",0);
			AddGroupBorder(BORDER_GROUP_IN|BORDER_WITH_TITLE);
			AddGroupBorderSpace(4,4,4,4);

			AddButton(B_COMPLETE,BFH_FIT,0,0,"Start all tests");
			AddButton(B_CLIP1,BFH_FIT,0,0,"-> To Clipboard");
			AddCheckbox(B_BEST_CB_SCORE,BFH_LEFT,0,0,"Keep Best CB Score");
			AddButton(B_CLIP2,BFH_FIT,0,0,"-> To Database");
		AddGroupEnd();

		AddGroupBeginV(1000,BFH_FIT,2,"CPU Benchmark",0);
			AddGroupBorder(BORDER_GROUP_IN|BORDER_WITH_TITLE);
			AddGroupBorderSpace(4,4,4,4);

			AddButton(B_RENDER,BFH_FIT,0,0,"Rendering (1 CPU)");
			AddStaticText(R_RENDER,BFH_SCALEFIT,120,0,"---",0);

			AddButton(B_RENDER_M,BFH_FIT,0,0,"Rendering (x CPU)");
			AddStaticText(R_RENDER_M,BFH_SCALEFIT,120,0,"---",0);

			AddStaticText(R_RM2,BFH_LEFT,0,0,"Multiprocessor Speedup:",0);
			AddStaticText(R_GAIN_MP ,BFH_SCALEFIT,60,0,"---",0);
	//		AddButton(B_ABOUT4,BFH_LEFT,0,0,"Readme");
		AddGroupEnd();

		AddGroupBeginV(1000,BFH_SCALEFIT,2,"OpenGL Benchmark",0);
			AddGroupBorder(BORDER_GROUP_IN|BORDER_WITH_TITLE);
			AddGroupBorderSpace(4,4,4,4);

//			AddButton(B_CINEMA,BFH_FIT,0,0,"C4D Shading");
//			AddStaticText(R_CINEMA,BFH_SCALEFIT,100,0,"-o-",0);
	//		AddStaticText(1000,BFH_LEFT,20,0,"",0);
	//		AddStaticText(1000,BFH_LEFT,20,0,"",0);
	//		AddButton(B_ABOUT1,BFH_LEFT,0,0,"Readme");

	//		AddButton(B_OPENGL_SW,BFH_FIT,0,0,"OpenGL SW-L");
	//		AddStaticText(R_OPENGL_SOFT,BFH_SCALEFIT,100,0,"---",0);

			AddButton(B_OPENGL_HW,BFH_FIT,0,0,"OpenGL Standard");
			AddStaticText(R_OPENGL_HARD,BFH_SCALEFIT,100,0,"---",0);

//			AddButton(B_OPENGL_EXT,BFH_FIT,0,0,"OpenGL Enhanced");
//			AddStaticText(R_OPENGL_EXT,BFH_SCALEFIT,100,0,"---",0);

//			AddStaticText(1000,BFH_LEFT,0,0,"OpenGL Speedup:",0);
//			AddStaticText(R_GAIN_OGLHW_OGLSW,BFH_SCALEFIT,60,0,"---",0);
		AddGroupEnd();

		AddGroupBeginV(1000,BFH_CENTER,1,"",0);
			AddGroupBorderSpace(4,4,4,4);
			//AddStaticText(1000,BFH_CENTER,0,0,"CINEBENCH is based",0);
			//AddStaticText(1000,BFH_CENTER,0,0,"on the high performance",0);
			//AddStaticText(1000,BFH_CENTER,0,0,"3D software CINEMA 4D.",0);
			//AddStaticText(1000,BFH_CENTER,0,0,"More info at www.maxon.net",0);
			AddUserArea(5000,BFH_CENTER,0,0);
		AddGroupEnd();
	AddGroupEnd();

	ua = new(MyUserArea,5000,this);

	return TRUE;
}

MyDialog::Test1()
{
	var file = GeGetStartupPath(); file->AddLast("plugins"); file->AddLast("bench"); file->AddLast("bike"); file->AddLast("bike.c4d");

	println("Single CPU Render Test");

	rendermode = 100;
	Init();
	PrepareRender(file);
	rendermode = 0;
	OptEnable();
	start = DoRender(TRUE);
}

MyDialog::Test2()
{
	var file = GeGetStartupPath(); file->AddLast("plugins"); file->AddLast("bench"); file->AddLast("bike"); file->AddLast("bike.c4d");

	if (GetCPUCount()>1)
	{
		println("Multiple CPU Render Test");
		rendermode = 100;
		Init();
		PrepareRender(file);
		rendermode = 1;
		OptEnable();
		start = DoRender(FALSE);
	}
	else
		rendermode = -1;
}

MyDialog::Test_C4D_Shading()
{
	rendermode = 10;
	Init();

	println("CINEMA 4D Shading Test");

	SetOpenGL(0);
	c4d_shad_cb = CalcNewCB(c4d_shad_cb,DoShade("CINEMA 4D Shading Test", 0));

	rendermode = -1;
	Init();
	println(" ");
}

/*MyDialog::Test_OGL_SW()
{
	rendermode = 10;
	Init();

	println("OpenGL Software Lighting Test");
	SetOpenGL(1);
	ogl_soft_cb = CalcNewCB(ogl_soft_cb,DoShade("OpenGL Software Lighting Test", 1));

	rendermode = -1;
	Init();
	println(" ");
}*/

MyDialog::Test_OGL_HW()
{
	rendermode = 10;
	Init();

	println("OpenGL Standard Test");
	SetOpenGL(2);
	ogl_hard_cb = CalcNewCB(ogl_hard_cb,DoShade("OpenGL Standard Test", 2));

	rendermode = -1;
	Init();
	println(" ");
}
/*
MyDialog::Test_OGL_Ext()
{
	rendermode = 10;
	Init();

	println("OpenGL Enhanced Test");
	SetOpenGL(3);
//	ogl_hard_ext = CalcNewCB(ogl_hard_ext,DoShade("OpenGL Enhanced Test", 3));

	rendermode = -1;
	Init();
	println(" ");
}
*/
MyDialog::StartAllTests()
{
	BringToFront(R_PANELVIEW);
//	if (hasOGLExt)
//		Test_OGL_Ext();
	
	Test_OGL_HW();

	//Test_OGL_SW();
	//Test_C4D_Shading();
	
	multi=GetCPUCount()>1;
	Test1();
}

MyDialog::Command(id,msg)
{
	switch (id)
	{
		case B_INFO_NAME:
		case B_INFO_MACH:
		case B_INFO_MHZ:
		case B_INFO_OS:
		case B_INFO_GFX:
		{
			var world = GetWorldContainer();
			var cbdata = world->GetData(PLUGIN_ID);
			if (!cbdata) cbdata = new(BaseContainer);

			cbdata->SetData(B_INFO_NAME,GetString(B_INFO_NAME));
			cbdata->SetData(B_INFO_MACH,GetString(B_INFO_MACH));
			cbdata->SetData(B_INFO_MHZ ,GetString(B_INFO_MHZ) );
			cbdata->SetData(B_INFO_OS  ,GetString(B_INFO_OS)  );
			cbdata->SetData(B_INFO_GFX ,GetString(B_INFO_GFX) );
			world->SetData(PLUGIN_ID,cbdata);
			SetWorldContainer(world);
		}
		break;

		case B_ABOUT0:
		{
			var file = GeGetStartupPath(); file->AddLast("bench"); file->AddLast("about0.pdf");
			GeExecuteFile(file);
		}
		break;

		case B_ABOUT1:
		{
			var file = GeGetStartupPath(); file->AddLast("bench"); file->AddLast("about1.pdf");
			GeExecuteFile(file);
		}
		break;

		case B_ABOUT2:
		{
			var file = GeGetStartupPath(); file->AddLast("bench"); file->AddLast("about2.pdf");
			GeExecuteFile(file);
		}
		break;

		case B_ABOUT3:
		{
			var file = GeGetStartupPath(); file->AddLast("bench"); file->AddLast("about3.pdf");
			GeExecuteFile(file);
		}
		break;

		case B_ABOUT4:
		{
			var file = GeGetStartupPath(); file->AddLast("bench"); file->AddLast("about4.pdf");
			GeExecuteFile(file);
		}
		break;

		case B_BEST_CB_SCORE:
		{
			var world = GetWorldContainer();
			var cbdata = world->GetData(PLUGIN_ID);
			if (!cbdata) cbdata = new(BaseContainer);
			calc_maximum = msg->GetInt(BFM_ACTION_VALUE);
			cbdata->SetData(B_BEST_CB_SCORE,calc_maximum);
			world->SetData(PLUGIN_ID,cbdata);
			SetWorldContainer(world);
		}
		break;

		case B_RENDER:   			Test1(); break;
		case B_RENDER_M: 			Test2(); break;
		case B_CINEMA:   			BringToFront(R_PANELVIEW); Test_C4D_Shading(); BringToFront(PLUGIN_ID); break;
		//case B_OPENGL_SW:   BringToFront(R_PANELVIEW); Test_OGL_SW(); BringToFront(PLUGIN_ID); break;
		case B_OPENGL_HW:   	BringToFront(R_PANELVIEW); Test_OGL_HW(); BringToFront(PLUGIN_ID); break;
		//case B_OPENGL_EXT: 		BringToFront(R_PANELVIEW); Test_OGL_Ext(); BringToFront(PLUGIN_ID); break;

		case B_COMPLETE:
			StartAllTests();
			break;

		case B_CLIP1:
		case B_CLIP2:
		{
			var t;
			var le;

			le = GeGetLineEnd();

			t =  "CINEBENCH R10"+le;
			t += "****************************************************"+le+le;
			t += "Tester           : " + GetString(B_INFO_NAME)+le+le;
			t += "Processor        : " + GetString(B_INFO_MACH)+le;
			t += "MHz              : " + GetString(B_INFO_MHZ)+le;
			t += "Number of CPUs   : " + tostring(GetCPUCount()) +le;
			t += "Operating System : " + GetString(B_INFO_OS)+le;
			t += le;
			t += "Graphics Card    : " + GetString(B_INFO_GFX)+le;
			t += "Resolution       : <fill this out>"+le;
			t += "Color Depth      : <fill this out>"+le+le;
			t += "****************************************************"+le+le;
			t += "Rendering (Single   CPU): "; if (render_1_cb !=0.0) t += tostring( render_1_cb,".0f"); else t += "---"; t += " CB-CPU "+le;
			t += "Rendering (Multiple CPU): "; if (render_x_cb!=0.0) t += tostring(render_x_cb,".0f"); else t += "---"; t += " CB-CPU "+le;
			t += le;
			if (render_1_cb!=0.0 && render_x_cb!=0.0)
				t += "Multiprocessor Speedup: "+tostring(render_x_cb/render_1_cb,".2f")+le;
			t += le;

			//t += "Shading (CINEMA 4D)                : "; if (c4d_shad_cb!=0.0) t += tostring(c4d_shad_cb,".0f"); else t += "---"; t += " CB-GFX "+le;
			//t += "Shading (OpenGL Software Lighting) : "; if (ogl_soft_cb!=0.0) t += tostring(ogl_soft_cb,".0f"); else t += "---"; t += " CB-GFX "+le;
			t += "Shading (OpenGL Standard)          : "; if (ogl_hard_cb!=0.0) t += tostring(ogl_hard_cb,".0f"); else t += "---"; t += " CB-GFX "+le;
//			t += "Shading (OpenGL Enhanced)          : "; if (hasOGLExt) t += ((ogl_hard_ext!=0.0) ? tostring(ogl_hard_ext,".0f") :  "---") + " CB-GFX"+le; else t += "n.a." + le;

			t += le;

			var a,b,s;
			a = c4d_shad_cb;
			//b = ogl_soft_cb; 
			//if (ogl_hard_cb>b) 
				b=ogl_hard_cb;
//			if (ogl_hard_ext>b && hasOGLExt) 
//				b=ogl_hard_ext;
			if (a!=0.0 && b!=0.0)
			{
				t += "OpenGL Speedup: " + tostring(b/a,".2f")+le;
			}

			t += le + "****************************************************"+le;

			if (id == B_CLIP2)
			{
				// send data to CINEBENCH database plugin
				var b = new(BaseContainer,0);
				b->SetData(0,t);
				SendPluginMessage(b,200000020);
			}
			else
			{
				SetClipboardData(t);
			}
		}
		break;
	}
}




var splashbitmap = 0;

MyDialog::Timer(msg)
{
	if (!splashbitmap)
	{
		splashbitmap = TRUE;
	}
	if (multi && rendermode==-1)
	{
		multi=FALSE;
		Test2();
	}
	if (AUTORUN && !autostarted)
	{
		if (time()>autoruntime+2000)
		{
			autostarted = TRUE;
			//if (AUTORUN == AUTORUN_OGLENH)
			//{
			//	BringToFront(R_PANELVIEW);
			//	Test_OGL_Ext();
			//}
			//else
			if (AUTORUN == AUTORUN_OGLHW)
			{
				BringToFront(R_PANELVIEW);
				Test_OGL_HW();
			}
			else if (AUTORUN == AUTORUN_RENDERING_MULTI)
			{
				Test2();
			}
			else if (AUTORUN == AUTORUN_RENDERING_SINGLE)
			{
				Test1();
			}
			else
				StartAllTests();
		}
		return;
	}
}

MyDialog::OptEnable()
{
	Enable(B_RENDER_M,!AUTORUN && GetCPUCount()>1 && rendermode==-1);
	Enable(R_RENDER_M,!AUTORUN && GetCPUCount()>1);
	Enable(R_RM1     ,!AUTORUN && GetCPUCount()>1);
	Enable(R_RM2     ,!AUTORUN && GetCPUCount()>1);
	Enable(R_GAIN_MP ,!AUTORUN && GetCPUCount()>1);

	Enable(B_BEST_CB_SCORE,!AUTORUN && rendermode==-1);

	Enable(B_RENDER,!AUTORUN && rendermode==-1);
	Enable(B_CINEMA,!AUTORUN && rendermode==-1);
//	Enable(B_OPENGL_SW,!AUTORUN && rendermode==-1);
	Enable(B_OPENGL_HW,!AUTORUN && rendermode==-1);
//	Enable(B_OPENGL_EXT,!AUTORUN && rendermode==-1 && hasOGLExt);
	Enable(B_CLIP1,!AUTORUN && rendermode==-1);
	Enable(B_CLIP2,!AUTORUN && rendermode==-1);

	Enable(B_COMPLETE,!AUTORUN && rendermode==-1);
}

var showsplash;

MyDialog::Init()
{
	var world = GetWorldContainer();
	var cbdata = world->GetData(PLUGIN_ID);
	if (cbdata)
	{
		calc_maximum = cbdata->GetInt(B_BEST_CB_SCORE);
		SetInt(B_BEST_CB_SCORE,calc_maximum,0,1,1);
		SetString(B_INFO_NAME,cbdata->GetString(B_INFO_NAME));
		SetString(B_INFO_MACH,cbdata->GetString(B_INFO_MACH));
		SetString(B_INFO_MHZ, cbdata->GetString(B_INFO_MHZ ));
		SetString(B_INFO_OS,  cbdata->GetString(B_INFO_OS  ));
		SetString(B_INFO_GFX, cbdata->GetString(B_INFO_GFX ));
	}

	if (!showsplash)
	{
		var file = GeGetStartupPath(); file->AddLast("plugins"); file->AddLast("bench"); file->AddLast("cb.jpg");
		ShowBitmap(file);
		showsplash = TRUE;
	}

	OptEnable();

	if (render_1_cb!=0.0)
	{
		var txt = stradd(tostring(render_1_cb,".0f")," CB-CPU");
		SetString(R_RENDER,txt);
	}
	else
		SetString(R_RENDER,"---");

	if (render_x_cb!=0.0)
	{
		var txt = stradd(tostring(render_x_cb,".0f")," CB-CPU");
		SetString(R_RENDER_M,txt);
	}
	else
		SetString(R_RENDER_M,"---");

	if (c4d_shad_cb!=0.0)
	{
		var txt = stradd(tostring(c4d_shad_cb,".0f")," CB-GFX");
		SetString(R_CINEMA,txt);
	}
	else
		SetString(R_CINEMA,"---");

	//if (ogl_soft_cb!=0.0)
	//{
	//	var txt = stradd(tostring(ogl_soft_cb,".0f")," CB-GFX");
	//	SetString(R_OPENGL_SOFT,txt);
	//}
	//else
	//	SetString(R_OPENGL_SOFT,"---");

	if (ogl_hard_cb!=0.0)
	{
		var txt = stradd(tostring(ogl_hard_cb,".0f")," CB-GFX");
		SetString(R_OPENGL_HARD,txt);
	}
	else
		SetString(R_OPENGL_HARD,"---");


//	if (ogl_hard_ext!=0.0)
//	{
//		var txt = stradd(tostring(ogl_hard_ext,".0f")," CB-GFX");
//		SetString(R_OPENGL_EXT,txt);
//	}
//	else
//		SetString(R_OPENGL_EXT,"---");


	if (render_x_cb!=0.0 && render_1_cb!=0.0)
	{
		var txt = stradd(tostring(render_x_cb/render_1_cb,".2f")," x");
		SetString(R_GAIN_MP,txt);
	}
	else
		SetString(R_GAIN_MP,"---");

	var mmax=ogl_hard_cb;
	//if (ogl_soft_cb>ogl_hard_cb) mmax=ogl_soft_cb;

	SetString(R_GAIN_OGLHW_OGLSW,(c4d_shad_cb!=0.0 && mmax!=0.0) ? stradd(tostring(mmax/c4d_shad_cb,".2f")," x") : "---");
	SetString(B_INFO_MACH, info_machine);
	SetString(B_INFO_OS, info_os);
	SetString(B_INFO_GFX, renderer);
	SetString(B_INFO_CPUCOUNT,tostring(GetCPUCount()));
}

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

class MyMenuPlugin : MenuPlugin
{
	public:
		MyMenuPlugin();

		GetID();
		GetName();
		GetHelp();
		Execute(doc);
		RestoreLayout(secret);
}

MyMenuPlugin::MyMenuPlugin() { super(); }
MyMenuPlugin::GetID()        { return PLUGIN_ID; }
MyMenuPlugin::GetName()      { return "CINEBENCH"; }
MyMenuPlugin::GetHelp()      { return "CINEBENCH"; }

MyMenuPlugin::Execute(doc)
{
	if (!dialog) dialog = new(MyDialog);
	dialog->Open(TRUE,-1,-1);
}

MyMenuPlugin::RestoreLayout(secret)
{
	if (!dialog) dialog = new(MyDialog);
	dialog->RestoreLayout(secret);
}

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

main()
{
	c4d_shad_cb = 0.0;
	//ogl_soft_cb = 0.0;
	ogl_hard_cb = 0.0;
//	ogl_hard_ext= 0.0;
	render_1_cb  = 0.0;
	render_x_cb = 0.0;

	var msg = new (BaseContainer, COREMSG_CINEMA_GETMACHINEFEATURES);
	var bc = SendCoreMessage(COREMSG_CINEMA,msg,0);
	if (bc)
	{
		renderer     = bc->GetString(OPENGL_RENDERER_NAME);
		info_machine = bc->GetString(MACHINEINFO_PROCESSORNAME);
		info_os      = bc->GetString(MACHINEINFO_OSTYPE) + " " + bc->GetString(MACHINEINFO_OSVERSION);
		hasOGLExt    = bc->GetInt(OPENGL_SUPPORT_ENHANCED);
	}

	Register(MyMenuPlugin);
}
