/*!
*/

#include "AppFramework.h"

#include <stdio.h>

#pragma managed(push, off)

// ************************************************************************************************************************************************************

CAppFramework *CAppFramework::s_owner;

// ************************************************************************************************************************************************************

CAppFramework::CAppFramework()
:	m_isWindowed(TRUE)
{
}

CAppFramework::~CAppFramework()
{
}

BOOL CAppFramework::Initialize(HINSTANCE hInstance, INT32 screenWidth, INT32 screenHeight)
{
	// Register window class
	static char szClassName[] = "CAppFramework";
	WNDCLASSEX wndClass;

	wndClass.cbSize = sizeof(WNDCLASSEX);
	wndClass.style = 0;
	wndClass.lpfnWndProc = FrameWndProc;
	wndClass.cbClsExtra = 0;
	wndClass.cbWndExtra = 0;
	wndClass.hInstance = hInstance;
	wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndClass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
	wndClass.lpszMenuName = NULL;
	wndClass.lpszClassName = szClassName;
	wndClass.hIconSm = NULL;

	RegisterClassEx(&wndClass);

	m_windowStyle = WS_OVERLAPPEDWINDOW | WS_VISIBLE;
	s_owner = this;

	// Set the window's initial width
	RECT rect;
	SetRect(&rect, 0, 0, screenWidth, screenHeight);
	AdjustWindowRect(&rect, m_windowStyle, FALSE);

	// Create window
	m_hFrameWnd = CreateWindowEx(0, szClassName, "Rune Development Platform", m_windowStyle, 0, 0, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, hInstance, NULL);

	// Store
	m_hAppInstance = hInstance;

	// Update and show window
	UpdateWindow(m_hFrameWnd);
	ShowWindow(m_hFrameWnd, SW_SHOW);

	// Get window rects
	GetWindowRect(m_hFrameWnd, &m_windowRect);
	GetClientRect(m_hFrameWnd, &m_clientRect);

	// Finish initialization
	OnInitialized();

	return TRUE;
}

INT32 CAppFramework::Run()
{
	MSG msg;

	INT32 processedMsgs = 0;
	INT32 maxMsgPerLoop = 100;

	PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);
	while(msg.message != WM_QUIT)
	{
		BOOL boMessage = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE | PM_NOYIELD);
		if(boMessage)
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);

			++processedMsgs;
		} 
		else
		{
			processedMsgs = 0;

			Update();
		}

		if(processedMsgs > maxMsgPerLoop)
		{
			processedMsgs = 0;

			Update();
		}
	}
/*
	while(TRUE)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
		{
			if(!GetMessage(&msg, NULL, 0, 0))
				return static_cast<INT32>(msg.wParam);

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		Update();
	}
*/
	return static_cast<INT32>(msg.wParam);
}

LRESULT CAppFramework::FrameWndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	// Call subclassed window message processor
	LRESULT result;
	BOOL msgProcessed = s_owner->ProcessWindowMessage(hWnd, iMsg, wParam, lParam, result);

	if(msgProcessed)
	{
		return result;
	}

	// Message was not processed, do default processing
	switch(iMsg)
	{
		case WM_DESTROY:
			PostQuitMessage(0);
			return 0;

	}

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

// ************************************************************************************************************************************************************

#pragma managed(pop)
