#include <windows.h>
#include <mmsystem.h>
#include <math.h>
#include "zn.h"



//--- DATA -------------------------------------------------------------------//

HINSTANCE hMainInstance;
HWND hMainWindow = NULL;

char szAppName[40] = "Lewpen's pattern generator";

ZN *zn;



//---- FUNCTION PROTOS -------------------------------------------------------//



//---- Startup ---------------------------------------------------------------//

void Startup()
{
  zn = new ZN(640, 480);
}



//---- Redraw ----------------------------------------------------------------//

void Redraw(HDC dc)
{
  static int start_t = 0;
  int t = timeGetTime();

  if(!start_t) start_t = t;

  t -= start_t;

  zn->recalc(t * 0.001);
  zn->display(dc);
}



//---- WinProc ---------------------------------------------------------------//

LRESULT CALLBACK WinProc(HWND hwnd, UINT uiMsg, UINT wParam, LONG lParam)
{
  HDC dc;
  PAINTSTRUCT ps;

  switch(uiMsg) {

  case WM_CREATE:
    Startup();
    SetTimer(hwnd, 1, 10, NULL);
    break;

  case WM_TIMER:
    dc = GetDC(hwnd);
    Redraw(dc);
    ReleaseDC(hwnd, dc);
    return 0L;

  case WM_PAINT:
    dc = BeginPaint(hwnd, &ps);
    Redraw(dc);
    EndPaint(hwnd, &ps);
    return 0L;

  case WM_KEYDOWN:
    if((int)wParam == VK_ESCAPE)
    {
      SendMessage(hwnd, WM_DESTROY, 0, 0);
    }
    return 0L;

  case WM_KEYUP:
    return 0L;

  case WM_NCHITTEST:
    return HTCAPTION;

  case WM_DESTROY:
    KillTimer(hwnd, 1);
    PostQuitMessage(0);
    return 0L;

  }

  return DefWindowProc(hwnd, uiMsg, wParam, lParam);
}




//---- WinMain ---------------------------------------------------------------//

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
  WNDCLASS wndclass;
  char cSwitch;
  MSG msg;
  RECT rc;

  hMainInstance = hInstance;

  if(hPrevInstance) return 1;

  //- Create window class

  wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  wndclass.lpfnWndProc   = WinProc;
  wndclass.cbClsExtra    = 0;
  wndclass.cbWndExtra    = 0;
  wndclass.hInstance     = hInstance;
  wndclass.hIcon         = NULL;
  wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  wndclass.lpszMenuName  = NULL;
  wndclass.lpszClassName = szAppName;

  if(!RegisterClass(&wndclass)) return 1;

  //- Create main window

  hMainWindow = CreateWindowEx(
    0,
    szAppName,
    szAppName,
    WS_POPUP,
    128,
    128,
    640,
    480,
    NULL,
    NULL,
    hInstance,
    NULL
  );

  if(!hMainWindow) return 1;

  ShowWindow(hMainWindow, SW_SHOW);

  //- Message loop

  HDC dc = GetDC(hMainWindow);

  while(TRUE) {
    while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
      if (msg.message == WM_QUIT) return TRUE;
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    }
    Redraw(dc);
  }

  ReleaseDC(hMainWindow, dc);

  return 0;
}



//---- END OF FILE -----------------------------------------------------------//




