// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "ex1.h"

#include "MainFrm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_KEYDOWN()
	ON_WM_TIMER()
	ON_WM_SETCURSOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
	delete screen;
}

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

	return CFrameWnd::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

BOOL CMainFrame::InitDirectDraw()
{
	HWND hWnd = AfxGetMainWnd()->m_hWnd;
	screen = new CDXScreen();
	screen->CreateFullScreen(hWnd,640,480,8);
	return TRUE;
}

BOOL CMainFrame::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle, CWnd* pParentWnd, CCreateContext* pContext) 
{
	CString sClassName = ::AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW);
	if (!Create(sClassName, NULL, dwDefaultStyle, rectDefault,
                pParentWnd, NULL, 0L, pContext))
	  {
		return FALSE;
	  }
	
	return TRUE;

}

void CMainFrame::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	switch (nChar)
	{
		case VK_ESCAPE:
			{
				PostMessage(WM_CLOSE, 0, 0);
				break;
			}
	}
	
	CFrameWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CMainFrame::OnTimer(UINT nIDEvent) 
{
	if(Toggle)
	{
		screen->Fill(4);	// Fill the back buffer red
		Toggle = FALSE;
	}
	else
	{
		screen->Fill(1);	// Fill the back buffer blue
		Toggle = TRUE;
	}

	screen->Flip();	 // Flip the back buffer to the front
	
	CFrameWnd::OnTimer(nIDEvent);
}

BOOL CMainFrame::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	SetCursor(NULL);

	return TRUE;
}
