///////////////////////////////////////////////////////////
//
//		     Free Speed Control API Demo
//			Southern.Liang
//			  1998.1.14
///////////////////////////////////////////////////////////

#include <windows.h>
#include <stdio.h>
#include <mmsystem.h>
#include "resource.h"
#include "STHSDK.H"

void	DisplaySpeedText(int Frames)
{
	HDC		hDC;
	float		Mult;
	COLORREF	TextColor;
	COLORREF	BackColor;
	char		Buffer[256];

	//Display Speed
	hDC=GetDC(GetVideoWnd());
	Mult=(float)Frames/30.0f;
	sprintf(Buffer,"%f±¶",Mult);
//	SetBkMode(hDC,TRANSPARENT);
	TextColor=SetTextColor(hDC,RGB(255,255,255));
	BackColor=SetBkColor(hDC,RGB(255,0,255));
	TextOut(hDC,0,0,Buffer,lstrlen(Buffer));
	SetBkColor(hDC,BackColor);
	SetTextColor(hDC,TextColor);
	ReleaseDC(GetVideoWnd(),hDC);
}
int	TimerID=0;
int	LeftTime=0;
int	OldFrames;
void	CALLBACK	TimeCallBack(UINT uTimerID,UINT uMsg,DWORD dwUser,DWORD dw1,DWORD dw2)
{
	static	int	Timer=0;
	int	Ret;
	int	Frames;

	_asm	{
		xor	eax,eax
		mov	edx,0x3F8
		in	al,dx
		mov	[Ret],eax
		}
	Ret=timeGetTime();
	Ret-=Timer;
	Timer=timeGetTime();
	LeftTime=Ret;
/*
	Frames=Ret;
	if(OldFrames!=Frames)
		{
		ControlFrameSpeed(Frames);
		OldFrames=Frames;
		}
*/
}

int	APIENTRY	SpeedProc(HWND hwnd,UINT message,UINT wParam,LONG lParam)
{
	static	int	Frames=32;
	static	HWND	ControlSpeed;
	int		Ret;
	PAINTSTRUCT 	ps;

	switch(message)
		{
		case WM_INITDIALOG:
//			sndPlaySound("C:\\DXSDK\\SDK\\MEDIA\\PLANE.WAV",SND_ASYNC|SND_LOOP);
			//Set Video Window Position
//			SetWindowLong(GetVideoWnd(),GWL_STYLE,WS_POPUP);
			SetWindowPos(GetVideoWnd(),NULL,0,0,0,0,SWP_NOZORDER|SWP_NOSIZE);
//			SetWindowPos(GetVideoWnd(),NULL,0,0,640,480,SWP_NOZORDER|SWP_NOMOVE);
			//Open The File
			Ret=MPEGOpenFile("DEMO.MPG");//avseq02.dat");
//			Ret=MPEGOpenFile("E:\\MPEGAV\\music01.DAT");
			if(Ret==0)
				{
				MessageBox(hwnd,"Open Fail .\\DEMO.MPG","Open Fail",MB_OK);
				EndDialog(hwnd,0);
				}
			Frames=32;
			ControlSpeed=GetDlgItem(hwnd,IDC_CONTROLSPEED);
			SetScrollRange(ControlSpeed,SB_CTL,0,200,TRUE);
			SetScrollPos(ControlSpeed,SB_CTL,200-Frames,TRUE);
			SetDlgItemInt(hwnd,IDC_CURSPEED,Frames,FALSE);
			//Set Speed
			ControlFrameSpeed(Frames);
			//Set the Dialogbox Position
			SetWindowPos(hwnd,NULL,400,10,0,0,SWP_NOZORDER|SWP_NOSIZE);

			SetTimer(hwnd,1111,30,NULL);

		TimerID=timeSetEvent(100,TIME_CALLBACK_FUNCTION,
			TimeCallBack,0,TIME_PERIODIC);
			return	TRUE;
		case WM_TIMER:
//			SetDlgItemInt(hwnd,IDC_PORT,LeftTime,FALSE);
			return TRUE;
		case WM_PAINT:
			BeginPaint(hwnd,&ps);
			EndPaint(hwnd,&ps);
			//Display Text
			DisplaySpeedText(Frames);
			return	TRUE;
		case WM_VSCROLL:
			if(LOWORD(wParam)==SB_ENDSCROLL)
				break;
			switch(LOWORD(wParam))
				{
				case SB_TOP:
					Frames=200;	break;
				case SB_BOTTOM:
					Frames=0;	break;
				case SB_PAGEDOWN:
					Frames-=4;	break;
				case SB_PAGEUP:
					Frames+=4;	break;
				case SB_LINEDOWN:
					Frames--;	break;
				case SB_LINEUP:
					Frames++;	break;
				case SB_THUMBPOSITION:
				case SB_THUMBTRACK:
					Frames=200-HIWORD(wParam);
					break;
				}
			if(Frames<0)   Frames=0;
//			if(Frames<10 && Frames!=0)  Frames=10;
			if(Frames>200) Frames=200;
			SetScrollPos(ControlSpeed,SB_CTL,200-Frames,TRUE);
			SetDlgItemInt(hwnd,IDC_CURSPEED,Frames,FALSE);
			//Control New Speed
//			sndPlaySound("C:\\DXSDK\\SDK\\MEDIA\\RUN.WAV",SND_ASYNC|SND_LOOP);
			ControlFrameSpeed(Frames);
			//Display Text
			DisplaySpeedText(Frames);
			return	TRUE;
		case WM_CLOSE:
			//Stop First
			StopPlay();
			//Close the File
			MPEGCloseFile();
			KillTimer(hwnd,1111);
			timeKillEvent(TimerID);
			EndDialog(hwnd,0);
			return	TRUE;
		case WM_COMMAND:
			switch(wParam)
				{
				case IDC_PLAY:
					//Start Play
					StartPlay();
					break;
				case IDC_STOP:
					//Stop Play
					StopPlay();
					break;
				case IDC_EXIT:
					//Stop First
					StopPlay();
					//Close the File
					MPEGCloseFile();
					KillTimer(hwnd,1111);
					timeKillEvent(TimerID);
					EndDialog(hwnd,TRUE);
					return TRUE;
				}
		}
	return	FALSE;
}


int  APIENTRY	WinMain(HANDLE 	hInstance,
			HANDLE 	hPrevInstance,
			LPSTR 	lpszCmdLine,
			int 	nCmdShow)
{
	
	DialogBox(hInstance,MAKEINTRESOURCE(IDD_SPEED),NULL,SpeedProc);
	return 0;
}

