
/*

PolyCSG 001
-----------

. Basic render framework up and running

PolyCSG 002
-----------

  . Added Image class
  . Converted buffer to Image class

> First draft of polygon class

  . Polygon edge rendering (scanline based)
  . Not yet tested

PolyCSG 003
-----------

  . Solid rendering

  . Added scanline class
  . Use scanline class for polygon rendering
  . Moved classes to seperate cpp & h files

  . Pass polygon into buffer to render

*/



#include <windows.h>
#include <mmsystem.h>
#include <math.h>

//--- CONSTANTS --------------------------------------------------------------//

//- Settings

#define FULL_WINDOW_DRAG
#define ROUND_IMAGE_SIZE



//---- CLASS DECLARATIONS ----------------------------------------------------//

class Poly;
class Scanline;
class Image;

#include "Poly.h"
#include "Scanline.h"
#include "Image.h"



//--- DATA -------------------------------------------------------------------//

//---- Windows data

HINSTANCE hMainInstance;
HWND hMainWindow = NULL;

char szAppName[40] = "PolyCSG 001";

int mouse_x = 0, mouse_y = 0;
int mouse_xstart = 0, mouse_ystart = 0;

bool mouse_drag = false;

float rx=0, ry=0, rrx=0, rry=0;



//---- Image buffer data

int width = 480;
int height = 360;

Image *im;



//---- Display ---------------------------------------------------------------//

void Display(HDC dc, int *buf, int w, int h)
{
  BITMAPINFO bmi;

  bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  bmi.bmiHeader.biWidth = w;
  bmi.bmiHeader.biHeight = -h;
  bmi.bmiHeader.biPlanes = 1;
  bmi.bmiHeader.biBitCount = 32;
  bmi.bmiHeader.biCompression = BI_RGB;
  bmi.bmiHeader.biSizeImage = 0;
  bmi.bmiHeader.biXPelsPerMeter = 1000;
  bmi.bmiHeader.biYPelsPerMeter = 1000;
  bmi.bmiHeader.biClrUsed = 0;
  bmi.bmiHeader.biClrImportant = 0;

  StretchDIBits(
    dc,
    0, 0, w, h,
    0, 0, w, h,
    (LPVOID)buf,
    (LPBITMAPINFO)&bmi,
    DIB_RGB_COLORS, SRCCOPY
  );

}



//---- Startup ---------------------------------------------------------------//

void Startup()
{
  //- Allocate image

  #ifdef ROUND_IMAGE_SIZE

  width = (width+7) & -8;
  height = (height+7) & -8;

  #endif

  im = new Image( width, height );
}




//---- Shutdown --------------------------------------------------------------//

void Shutdown()
{
}



//---- Redraw ----------------------------------------------------------------//

void Redraw(HDC dc)
{
  im->checks();

//  im->clear( 0x000000 );

  //- Display

  int i, j;

  static int frame = 0;
  frame ++;

  for(j=0; j<8; j++) for(i=0; i<8; i++)
  {
    im->buf[j*width+i] = ((-(((i+j+frame)>>2)&1)) ^ 0xFFFF00 );
  }

  Poly *p = new Poly();

  p->begin( );

  float f = frame * 1.0f;

  for(i=0; i<7; i++)
  {
    float x = im->w*0.5 + 90 * sin(i*3.15159*2.0f*0.4271 + f*0.0671);
    float y = im->h*0.5 + 90 * cos(i*3.15159*2.0f*0.1823 + f*0.0467);
    p->addPoint( x, y );
  }

  p->end( );

  im->drawPolygon( p, 0x336699 );

  delete p;

  Display(dc, im->buf, width, height);
}



//---- 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_LBUTTONDOWN:
    mouse_xstart = HIWORD(lParam);
    mouse_ystart = LOWORD(lParam);
    mouse_drag = true;
    SetCapture(hwnd);
    return 0L;

  case WM_MOUSEMOVE:
    mouse_x = HIWORD(lParam);
    mouse_y = LOWORD(lParam);

    if(mouse_drag)
    {
      ry -= (mouse_y-mouse_ystart) * 0.004;
      rx += (mouse_x-mouse_xstart) * 0.004;
      mouse_xstart = mouse_x;
      mouse_ystart = mouse_y;
    }
    return 0L;

  case WM_LBUTTONUP:
    mouse_drag = false;
    ReleaseCapture();
    return 0L;

  #ifdef FULL_WINDOW_DRAG

  case WM_NCHITTEST:
    return HTCAPTION;

  #endif

  case WM_KEYDOWN:
    if( wParam == VK_ESCAPE ) DestroyWindow( hwnd );
    return 0L;

  case WM_DESTROY:
	  Shutdown();
    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

  GetWindowRect(GetDesktopWindow(), &rc);

  hMainWindow = CreateWindowEx(
    0,
    szAppName,
    szAppName,
    WS_POPUP,
    (rc.right-width)/2,
    (rc.bottom-height)/2,
    width,
    height,
    NULL,
    NULL,
    hInstance,
    NULL
  );

  if(!hMainWindow) return 1;

  ShowWindow(hMainWindow, SW_SHOW);

  //- Message loop

  while(TRUE) {
    while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
      if (msg.message == WM_QUIT) return TRUE;
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    }
    HDC dc = GetDC(hMainWindow);
    Redraw(dc);
    ReleaseDC(hMainWindow, dc);
  }

  return 0;
}



//---- END OF FILE -----------------------------------------------------------//




