/* ***********************************************
*	Introduction to C/C++ Programming
*		Mid-Project sample code [ OpenGL ]
*
*	send your comment to ahmadyan@gmail.com
*	CopyLeft 2006 NematAllah Ahmadyan,
*	Published under GNU-GPL license.
*
*
*   You can refer to online tutorials for more information
* ************************************************/


#include <windows.h>	// This file is part of Windows DDK, it's boundled with most of IDEs
						// including Microsoft Visual Studio and Dev-Cpp

#include <gl/gl.h>		// This is a header for OpenGL library. it's boundled with most of open-sourse IDEs like Dev-Cpp or K-Develop.
						// however you can obtain a copy of it from
						// http://www.xmission.com/~nate/glut.html or
						// http://192.48.159.181/resources/libraries/glut.html
#include <cmath>
#include <iostream>

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);

void Simulate(){
	// Write your motion code here ...

	// Code for Output 1

				// By defining a variable as a static variable it does not initialize every time
				// simulate runs, so you can use it as a history for your simulations.
				static float theta = 0.0f ;
	            glClearColor (0.0f, 0.0f, 0.0f, 0.0f);

	            // the command glClear will clear the background and flush the VGA memory
	            //glClear (GL_COLOR_BUFFER_BIT);
	            glPushMatrix ();

	            // as the title says this command will rotate the picture,
	            glRotatef (theta, 0.0f, 0.0f, 1.0f);

	            // Importatn:
	            // look how to create the glCommand: for openGL commands begin with glBegin(Shapename)
	            // and ends with glEnd(); inside this block you should specify the vertex of the shape and it's color.
	            // shapes can vary on their number of vertexes. for example for creating a quardiple you should specify exactly 4 vertex whereas
	            // to draw a line you should specify at lease 2.

	            // other possible shapres are:
	            // GL_TRIANGLES
	            // GL_QUADRIPLE
	            // GL_LINES
	            // GL_POINTS

	            // for example for defining a triangle:
	            //	glBegin(GL_TRIANGLES);
				//		glVertex3f(-0.5,-0.5,0.0);
				//		glVertex3f(0.5,0.0,0.0);
				//		glVertex3f(0.0,0.5,0.0);
				//	glEnd();

	            glBegin (GL_LINE_STRIP);
	            glColor3f (1.0f, 0.0f, 0.0f);
	            glVertex2f (0.0f, 1.0f);
	            glColor3f (0.0f, 1.0f, 0.0f);
	            glVertex2f (0.87f, -0.5f);
	            glColor3f (0.0f, 0.0f, 1.0f);
	            glVertex2f (-0.87f, -0.5f);
	            glEnd ();
	            glPopMatrix ();
	            theta += 1.0f;
/*
		void Simulate(){
	            static float theta = 0.0f ;
	            glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
	            //glClear (GL_COLOR_BUFFER_BIT);

	            glPushMatrix ();

	            glRotatef (theta, 0.0f, 0.0f, 1.0f);
	            int circle_points = 300 ;
	            double angle;
	            glBegin(GL_POINTS);
	                double R = 1 ;
	                for (int i = 0; i < circle_points; i++) {
	                angle = 2*3.1415*i/circle_points;
	                glVertex2f(R*cos(angle), R*sin(angle));
	                R *= 0.99 ;
	            }
	            glEnd();

	            glPopMatrix ();
	            theta += 1.0f;
}
*/
}

// the WinMain function on windows is like the standard main, however it is part of Windows API and create a window
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
    WNDCLASS wc;
    HWND hWnd;
    HDC hDC;
    HGLRC hRC;
    MSG msg;
    BOOL bQuit = FALSE;

    wc.style = CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "GLSample";
    RegisterClass (&wc);

	// hWnd is a handle to the drawing window.
    hWnd = CreateWindow ("GLSample", "Motion Simulation",  WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
      0, 0, 600, 400, // These values specify the size of the window.
      NULL, NULL, hInstance, NULL);

    EnableOpenGL (hWnd, &hDC, &hRC);
    glClear (GL_COLOR_BUFFER_BIT);
    while (!bQuit)
    {
     if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) // handling events, such as mouse motion or click.
        {
            /* handle or dispatch messages */
            if (msg.message == WM_QUIT)
            {
                bQuit = TRUE;
            }
            else
            {
                TranslateMessage (&msg);
                DispatchMessage (&msg);
            }
        }
        else
        {
            Simulate();
            SwapBuffers (hDC);
            Sleep (1);
        }
    }
    DisableOpenGL (hWnd, hDC, hRC);
    DestroyWindow (hWnd);
    return msg.wParam;
}


LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_CREATE:
        return 0;
    case WM_CLOSE:
        PostQuitMessage (0);
        return 0;

    case WM_DESTROY:
        return 0;

    case WM_KEYDOWN:
        switch (wParam)
        {
        case VK_ESCAPE:
            PostQuitMessage(0);
            return 0;
        }
        return 0;

    default:
        return DefWindowProc (hWnd, message, wParam, lParam);
    }
}


// This will enable OpenGL
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
    PIXELFORMATDESCRIPTOR pfd;
    int iFormat;
    *hDC = GetDC (hWnd);
    ZeroMemory (&pfd, sizeof (pfd));
    pfd.nSize = sizeof (pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;	// specify the color depth, be aware to not waste bits on higher depth that they are not supported by graphic adapter.
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
    iFormat = ChoosePixelFormat (*hDC, &pfd);
    SetPixelFormat (*hDC, iFormat, &pfd);
    *hRC = wglCreateContext( *hDC );
    wglMakeCurrent( *hDC, *hRC );

}

// This will disable OpenGL
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
    wglMakeCurrent (NULL, NULL);
    wglDeleteContext (hRC);
    ReleaseDC (hWnd, hDC);
}
