// SdiView.cpp : implementation of the CSdiView class
//

#include "stdafx.h"
#include "Sdi.h"

#include "SdiDoc.h"
#include "SdiView.h"
#include <gl/gl.h>
#include <gl/glu.h>


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSdiView

IMPLEMENT_DYNCREATE(CSdiView, CView)

BEGIN_MESSAGE_MAP(CSdiView, CView)
	//{{AFX_MSG_MAP(CSdiView)
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSdiView construction/destruction

CSdiView::CSdiView()
{
	// TODO: add construction code here

}

CSdiView::~CSdiView()
{
}

BOOL CSdiView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	 cs.style = cs.style | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CSdiView drawing

void CSdiView::OnDraw(CDC* pDC)
{
	CSdiDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here

	CClientDC cdc(this);
	wglMakeCurrent(cdc.m_hDC,hRC);   //Make the RC current

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

			glMatrixMode(GL_MODELVIEW);
		
			glLoadIdentity();             		//Initialize matrix
			glTranslatef(0.0, 0.0,-4.0);  	  //translate both cubes and the light into 

	SwapBuffers(cdc.m_hDC);			//Swap the buffers
	wglMakeCurrent(NULL,NULL);			//Make the RC not-current


}

/////////////////////////////////////////////////////////////////////////////
// CSdiView printing

BOOL CSdiView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CSdiView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CSdiView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CSdiView diagnostics

#ifdef _DEBUG
void CSdiView::AssertValid() const
{
	CView::AssertValid();
}

void CSdiView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CSdiDoc* CSdiView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSdiDoc)));
	return (CSdiDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSdiView message handlers

int CSdiView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here



PIXELFORMATDESCRIPTOR pfd=
	{
		sizeof(PIXELFORMATDESCRIPTOR),
		1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
		PFD_TYPE_RGBA,
		24,					//color depth
		0,0,0,0,0,0,
		0,0,0,0,0,0,0,
		32,					//number of Depth buffer bits per pixel
		0,0,
		PFD_MAIN_PLANE,
		0,
		0,0,0
	};



	CClientDC clientDC(this);

	int pixelformat=ChoosePixelFormat(clientDC.m_hDC,&pfd);
	SetPixelFormat(clientDC.m_hDC,pixelformat,&pfd);

	hRC= wglCreateContext(clientDC.m_hDC);

	//Set the state variables	

	wglMakeCurrent(clientDC.m_hDC,hRC);     //Make RC current
	glEnable(GL_DEPTH_TEST);		//Enable Depth Test
	glEnable(GL_LIGHTING);			//Enable Lighting	
	glShadeModel(GL_SMOOTH);		//Gauraud shading
	glClearColor(1.0f,0.0f,0.0f,0.0f);		//The color used to 
								//clear the screen is black
	glFrontFace(GL_CCW);	//Vertices of the primitives are wound 
								//in counter clock direction

	wglMakeCurrent(clientDC.m_hDC,NULL);     //Make the RC not-current

	return 0;
}
