/******************************************************************************
*
* Title:	WinCode
*
* Desc:	Simple GDI Frame buffer
*
* Note:	1) this just displays a gradual black to white color change in the display
*	2) Now uses the Virtual Video interface
*
*******************************************************************************/

#pragma option w32
#speed
#pragma option ap TRUE

#define _WINGDI_ TRUE
#define _WINBASE_ TRUE
#define _ADVAPI_ TRUE

#include "Windows.h--"

/*****************************************************************************/
// Defines
/*****************************************************************************/
HWND			g_hWnd;

#include "VVideo.h--"
#include "GDI.h--"
#include "Tunnel.h--"

/*****************************************************************************/
// Module Data
/*****************************************************************************/

// class description
char				m_AppName	= "Tunnel";
char				m_AppTitle	= "Tunnel Demo";

/*****************************************************************************/
// Functions
/*****************************************************************************/

// windowing functions
HWND Window_Create(void);

/******************************************************************************
*
*	Function:	WinMain();
*
*	Desc:		Execution entry point
*
*	Notes:		1) Think of this as main()
*
******************************************************************************/
long main()
struct MSG msg;
{
	// create a window
	g_hWnd=Window_Create();

	// did we successed
	IF(EAX!=NULL){
		// select GDI Device
		VV_SetDriver();

		// get Virtaul Page address
		$CALL VV_GetAddress
		m_VPage = EAX;

		// init our tunnel effect
		Tunnel_Init();

		// the main message loop
		do{
			// Windows shit
			IF(PeekMessage(#msg,NULL,0,0,PM_REMOVE)){
				TranslateMessage(#msg);
				DispatchMessage(#msg);
			}

			// Do our 1 frame of our effect
			Tunnel_Update();

			// Flip the video page
			$CALL VV_Flip

		}while(msg.message!=WM_QUIT);

		// release
		Tunnel_Kill();

		// close Virtual Video Page
		VV_Kill();

	}

	// return value of our program
	return 0;
}

/****************************************************************************
*
*	Function:	WndProc()
*
*	Desc:		Our windows message loop
*
*	Notes:		1) This is message loop. we'll only process keyboard input here
*
*****************************************************************************/
long WndProc(dword hWnd, msg,wParam,lParam)
{
	// work out which message this is
	SWITCH(msg){
	// this is called when DestroyWindow() is called or when the User Closes the window
		CASE WM_DESTROY:
			PostQuitMessage(0);
			BREAK;

		// called when a key is pressed
		CASE WM_KEYDOWN:

			// which key was it
			SWITCH( wParam )
			{
				// the Esc key
				CASE VK_ESCAPE:
					// we want to quit the application
					PostQuitMessage(0);
					BREAK;
			}
			BREAK;
	}
	// let whoever else is interested in our messages process them
	return DefWindowProc(hWnd, msg, wParam, lParam);
}

/***********************************************************************
*
*	Function:	Window_Create()
*
*	Desc:		Creates a standard window at the position and dimentions specified
*
*	Notes:		1) Will return NULL if window creating failed
*
*****************************************************************************/
HWND Window_Create()
struct WNDCLASS	wc;
HWND hWnd=NULL;
struct RECT rect;
{
	// create the Window Class for our window
	wc=0;
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = #WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hThisInst;
	wc.hIcon = NULL;
	wc.hCursor = NULL;
	wc.hbrBackground =COLOR_WINDOW+1;
	wc.lpszMenuName = NULL;
	wc.lpszClassName = #m_AppName;

	rect.top = 0;
	rect.bottom = 240;
	rect.left = 0;
	rect.right = 320;

	// attempt to create it
	IF(RegisterClass( #wc )){
		// get window dimentions for a 320x240 client area window
		AdjustWindowRect( #rect, WS_OVERLAPPEDWINDOW, FALSE );

		// class has been created now create a window with it
		hWnd = CreateWindowEx(0,#m_AppName,
								#m_AppTitle,
								WS_OVERLAPPEDWINDOW,
								CW_USEDEFAULT,CW_USEDEFAULT,
								rect.right - rect.left, rect.bottom - rect.top,
								NULL,
								NULL,
								hThisInst,
								NULL);

		// did we succed in creating the window
		IF (EAX!=0){
			// show it to the world
			ShowWindow(EAX, SW_SHOW);
			UpdateWindow(hWnd);
		}
	}

	return hWnd;
}

