//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop

#include "idleform1.h"
#include "mmsystem.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"

// Globals and Defines
#define FRAME_DELAY 1 // set this to your desired ms per frame
                      // ex: 55 = 18.2 FPS
TIdleForm *IdleForm;
//---------------------------------------------------------------------------
__fastcall TIdleForm::TIdleForm(TComponent* Owner)
	: TForm(Owner)
{
    CanUpdate=true;
    Application->OnIdle = IdleFunction;
}
//---------------------------------------------------------------------------
void __fastcall TIdleForm::IdleFunction(System::TObject *Sender, bool &Done)
{
    static bool FirstTime = true;
    static long oldTime = 0;

    if (FirstTime)
    {
        // Do Initialization stuff
        FirstTime = false;
    }

    //------------------------------------
    // Secondary Application Message Loop -- This is how we take over the CPU!
   	Done = false;

    if (!Application->Terminated ) {
       	if (timeGetTime() - oldTime > FRAME_DELAY) {
           	oldTime = timeGetTime();
	       	if (CanUpdate) {
                DoUpdate();
           	}
        } else {
            Application->ProcessMessages();
        }
    }
}
//------------------------------------------------------------------------------
void __fastcall TIdleForm::DoUpdate()
{
    static long i=0;
    Label1->Caption = "Iterations: " + AnsiString((int)i);
    Update();
    i++;
}
//---------------------------------------------------------------------------
