#include <windows.h>
#include <tchar.h>

#include "Raster.h"

#include "CELLTimestamp.hpp"



LRESULT CALLBACK    windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
    case WM_SIZE:
        break;
    case WM_CLOSE:
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        break;
    }

    return  DefWindowProc( hWnd, msg, wParam, lParam );
}


void  getResourcePath(HINSTANCE hInstance,char pPath[1024])
{
    char    szPathName[1024];
    char    szDriver[64];
    char    szPath[1024];
    GetModuleFileNameA(hInstance,szPathName,sizeof(szPathName));
    _splitpath( szPathName, szDriver, szPath, 0, 0 );
    sprintf(pPath,"%s%s",szDriver,szPath);
}

int     WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
    //  1   ×¢²á´°¿ÚÀà
    ::WNDCLASSEXA winClass;
    winClass.lpszClassName  =   "Raster";
    winClass.cbSize         =   sizeof(::WNDCLASSEX);
    winClass.style          =   CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
    winClass.lpfnWndProc    =   windowProc;
    winClass.hInstance      =   hInstance;
    winClass.hIcon	        =   0;
    winClass.hIconSm	    =   0;
    winClass.hCursor        =   LoadCursor(NULL, IDC_ARROW);
    winClass.hbrBackground  =   (HBRUSH)(BLACK_BRUSH);
    winClass.lpszMenuName   =   NULL;
    winClass.cbClsExtra     =   0;
    winClass.cbWndExtra     =   0;
    RegisterClassExA(&winClass);

    //  2 ´´½¨´°¿Ú
    HWND    hWnd   =   CreateWindowExA(
        NULL,
        "Raster",
        "Raster",
        WS_OVERLAPPEDWINDOW,
        0,
        0,
        800,
        600, 
        0, 
        0,
        hInstance, 
        0
        );

    UpdateWindow( hWnd );
    ShowWindow(hWnd,SW_SHOW);

    RECT    rt      =   {0};
    GetClientRect(hWnd,&rt);

    int     width   =   rt.right - rt.left;
    int     height  =   rt.bottom - rt.top;
    void*   buffer  =   0;

    HDC     hDC     =   GetDC(hWnd);
    HDC     hMem    =   ::CreateCompatibleDC(hDC);

    BITMAPINFO	bmpInfor;
    bmpInfor.bmiHeader.biSize			=	sizeof(BITMAPINFOHEADER);
    bmpInfor.bmiHeader.biWidth			=	width;
    bmpInfor.bmiHeader.biHeight			=	-height;
    bmpInfor.bmiHeader.biPlanes			=	1;
    bmpInfor.bmiHeader.biBitCount		=	32;
    bmpInfor.bmiHeader.biCompression	=	BI_RGB;
    bmpInfor.bmiHeader.biSizeImage		=	0;
    bmpInfor.bmiHeader.biXPelsPerMeter	=	0;
    bmpInfor.bmiHeader.biYPelsPerMeter	=	0;
    bmpInfor.bmiHeader.biClrUsed		=	0;
    bmpInfor.bmiHeader.biClrImportant	=	0;

    HBITMAP	hBmp    =	CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
    SelectObject(hMem,hBmp);

    char    szPath[1024];
    getResourcePath(0,szPath);

    char    szImage[1024];
    sprintf(szImage,"%s/image/bg.png",szPath);
    CELL::Image*    image   =   CELL::Image::loadFromFile(szImage);
  
    sprintf(szImage,"%s/image/test.bmp",szPath);
    CELL::Image*    image1  =   CELL::Image::loadFromFile(szImage);

    
    CELL::Raster    raster(width,height,buffer);


    MSG     msg =   {0};
    while(true)
    {
        if (msg.message == WM_DESTROY  
            ||msg.message == WM_CLOSE
            ||msg.message == WM_QUIT)
        {
            break;
        }
        if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
        { 
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
        raster.clear();

        CELL::int2  pt[3]   =   
        {
            CELL::int2(100,10),
            CELL::int2(10,100),
            CELL::int2(200,100),
        };

        CELL::Rgba  color1(255,0,0);
        CELL::Rgba  color2(0,255,0);
        CELL::Rgba  color3(0,0,255);

        CELL::CELLTimestamp tms;

        tms.update();


        double  mis =   tms.getElapsedTimeInMicroSec();

        char    szBuf[128];
        sprintf(szBuf,"%f ",mis);
        int i   =   00;
        memcpy(buffer,raster._buffer,raster._width * raster._height * sizeof(CELL::Rgba));

        raster.drawImage(0,0,image);


        CELL::Raster::Vertex    vertex  =   
        {
            CELL::int2(100,10),   CELL::float2(0.5f,1.0f), CELL::Rgba(),
            CELL::int2(200,100),  CELL::float2(1.0f,0.0f), CELL::Rgba(),
            CELL::int2(10,100),   CELL::float2(0.0f,0.0f), CELL::Rgba(),
        };


        raster.drawTriangle(vertex);

        TextOut(hMem,10,10,szBuf,strlen(szBuf));
        BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);

    }


    delete  image;
    delete  image1;

    return  0;
}