#include <windows.h>
#include <mmsystem.h>
#include <math.h>
#include "zgo.h"



#define WINDOW_W 1024
#define WINDOW_H 480



//--- DATA -------------------------------------------------------------------//

HINSTANCE hMainInstance;
HWND hMainWindow = NULL;

char szAppName[40] = "Lewpen's pattern generator";

ZGo *zgo;



//---- FUNCTION PROTOS -------------------------------------------------------//



//---- Startup ---------------------------------------------------------------//

void Startup()
{
  zgo = new ZGo(WINDOW_W, WINDOW_H);
}



//---- Redraw ----------------------------------------------------------------//

void Redraw(HDC dc)
{
  static int start_t = 0;
  int t = timeGetTime();

  static int last_t[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

  if(!start_t) start_t = t;

  t -= start_t;

  int i;

  for(i=0; i<9; i++) last_t[i] = last_t[i+1];
  last_t[9] = t;

  for(t=0, i=0; i<10; i++) t += 0.1 * last_t[i];

  zgo->render(t*0.001);
  zgo->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:
    zgo->keyDown((int)wParam);
    if((int)wParam == VK_ESCAPE) SendMessage(hwnd, WM_DESTROY, 0, 0);
    return 0L;

  case WM_KEYUP:
    zgo->keyUp((int)wParam);
    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,
    0,
    0,
    WINDOW_W,
    WINDOW_H,
    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 -----------------------------------------------------------//




