// ************************************************************************************************
//
//  Direct X TEST.
//
// ************************************** (c) Pedro Díez López ************************************

#include "DXVideo.h"  

#define ID_NOMBRE "Test"
#define ID_TITULO "Test"
#define ID_TEMP       1

CDXVideo *DXVideo;
BOOL      Par;
int       Temporizador;
BOOL      AppActiva;

//
// El TEST
//
void Test ()
{
  if(Par==TRUE)
  {
    memset(DXVideo->pBuffer.bpp,064,DXVideo->DimBuffer);
    Par = FALSE;
  }
  else
  {
    memset(DXVideo->pBuffer.bpp,128,DXVideo->DimBuffer);
    Par = TRUE;
  }
}

//
//
//  W i n d o w P r o c 
//
//
long FAR PASCAL WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch( message )
  {
    case WM_ACTIVATEAPP:
      AppActiva = (wParam == WA_ACTIVE) || (wParam == WA_CLICKACTIVE);
	  break;
	
    case WM_CREATE:
      break;
	
    case WM_SETCURSOR:
	  SetCursor(DXVideo->mDDCursorShape);
	  return TRUE;

    case WM_TIMER:
      if (AppActiva && ID_TEMP == wParam)
      {
        Test();
        DXVideo->Pinta();
      }
      break;

    case WM_KEYDOWN:
  	  switch( wParam )
      {
	    case VK_ESCAPE:
		  PostMessage(hWnd, WM_CLOSE, 0, 0);
		  break;
      }
	  break;

	case WM_PAINT:
      break;

	case WM_DESTROY:
	  DXVideo->Fin();
      delete DXVideo;
	  PostQuitMessage(0);
	  break;
  }
  return DefWindowProc(hWnd, message, wParam, lParam);
}

//
//
//  W i n M a i n  
//
//
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
  WNDCLASS	wc;
  HWND		hwnd;
  MSG       msg;

  wc.style         = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc   = WindowProc;
  wc.cbClsExtra    = 0;
  wc.cbWndExtra    = 0;
  wc.hInstance     = hInstance;
  wc.hIcon         = LoadIcon(hInstance, IDI_APPLICATION);
  wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground = NULL;
  wc.lpszMenuName  = ID_NOMBRE;
  wc.lpszClassName = ID_NOMBRE;
  RegisterClass(&wc);
   
  hwnd = CreateWindowEx(WS_EX_TOPMOST,ID_NOMBRE,ID_TITULO,WS_POPUP,0,0,
                        GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),
						NULL, NULL, hInstance, NULL);
  if (!hwnd) return FALSE;
  ShowWindow(hwnd, nCmdShow);
  UpdateWindow(hwnd);

  Temporizador = 100;
  if(!SetTimer(hwnd,ID_TEMP,Temporizador,NULL)) return FALSE;

  DXVideo = new CDXVideo;
  DXVideo->Inicio (hwnd,640,480,16);
  DXVideo->Cursor(NULL);
	
  while (GetMessage(&msg, NULL, 0, 0))
  {
	TranslateMessage(&msg);
    DispatchMessage(&msg);
  }

  return msg.wParam;
}
