// rglapp.cpp : An example program that uses RGL
//

#include <d3dx8.h>
#include <mmsystem.h>
#include "rgl.h"


//-----------------------------------------------------------------------------
// Render()
//-----------------------------------------------------------------------------

VOID Render()
{
    RGLBeginRender();

	LPDIRECT3DDEVICE8 theDevice = RGLGetDevice();

	RGLLoadIdentity();
    RGLRotate(timeGetTime()/1000.0f, 0.0f, 1.0f, 0.0f);
	RGLLookAt(-10.0f, -10.0f, -10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
    RGLPerspective(10.0f, 10.0f, -5.0f, 10.0f);

	RGLVertex xaxis[2] = {
		{10.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255,255,255)},
		{-10.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255,255,255)}
	};

	RGLVertex yaxis[2] = {
		{0.0f, 10.0f, 0.0f, D3DCOLOR_XRGB(255,255,255)},
		{0.0f, -10.0f, 0.0f, D3DCOLOR_XRGB(255,255,255)}
	};

	RGLVertex zaxis[2] = {
		{0.0f, 0.0f, 10.0f, D3DCOLOR_XRGB(255,255,255)},
		{0.0f, 0.0f, -10.0f, D3DCOLOR_XRGB(255,255,255)}
	};
	
	RGLDrawLine(xaxis);
	RGLDrawLine(yaxis);
    RGLDrawLine(zaxis);

	RGLVertex mytriangle[3] = {
		{0.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255,0,0)},
		{10.0f, 0.0f, 10.0f, D3DCOLOR_XRGB(0,255,0)},
		{10.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(0,0,255)}
	};

	RGLDrawFacet3(mytriangle);

	RGLVertex mysquare[5] = {
		{0.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255,0,0)},
		{0.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(0,255,0)},
		{3.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(0,0,255)},
		{3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(0,0,255)},
		{6.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(0,0,255)}
	};

	RGLDrawFacetN(mysquare, 5);

    RGLEndRender();

}


//-----------------------------------------------------------------------------
// MsgProc
//-----------------------------------------------------------------------------

LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
        case WM_DESTROY:
            PostQuitMessage( 0 );
            return 0;

        case WM_PAINT:
            Render();
            ValidateRect( hWnd, NULL );
            return 0;
		case WM_KEYUP:
			PostQuitMessage(0);
			return 0;
    }

    return DefWindowProc( hWnd, msg, wParam, lParam );
}

//-----------------------------------------------------------------------------
// MsgProc
//-----------------------------------------------------------------------------

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    // Register the window class
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, 
                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                      "D3D Tutorial", NULL };
    RegisterClassEx( &wc );

    // Create the application's window
    HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial: RGL", 
                              WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
                              GetDesktopWindow(), NULL, wc.hInstance, NULL );

    // Initialize Direct3D
    if( SUCCEEDED( RGLInit(hWnd, 1024, 768) ) )
    {
        // Show the window
        ShowWindow( hWnd, SW_SHOWDEFAULT );
        UpdateWindow( hWnd );

        // Enter the message loop
        MSG msg;
        ZeroMemory( &msg, sizeof(msg) );
        while( msg.message!=WM_QUIT )
        {
            if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
            {
                TranslateMessage( &msg );
                DispatchMessage( &msg );
            }
            else
                Render();
        }
    
    }

    // Clean up everything and exit the app
    RGLShutdown();
    UnregisterClass( "D3D Tutorial", wc.hInstance );
    return 0;
}



