#include <windows.h>
#include "Navigation3D.h"
#include <iostream>

using namespace std;

//声明函数
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

//主函数WinMain（）
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
	_In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
	//窗口类名
	TCHAR cls_name[] = TEXT("3DMouseTest");

	//设计窗口类
	WNDCLASSEX wce = { 0 };

	wce.cbSize = sizeof(WNDCLASSEX);				//结构体大小		
	wce.style = CS_HREDRAW | CS_VREDRAW;			//大小改变,水平和垂直重绘
	wce.lpfnWndProc = WindowProc;					//窗口回调函数地址
	wce.cbClsExtra = 0;								//类的附加数据
	wce.cbWndExtra = 0;								//窗口的附加数据
	wce.hInstance = hInstance;						//应用程序实例句柄
	wce.hIcon = LoadIcon(NULL, IDI_APPLICATION);	//大图标
	wce.hIconSm = wce.hIcon;						//小图标
	wce.hCursor = LoadCursor(NULL, IDC_ARROW);		//光标
	wce.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);	//背景色
	wce.lpszMenuName = NULL;						//菜单
	wce.lpszClassName = cls_name;					//窗口类的名称

	//注册窗口类
	//ATOM nres = RegisterClassEx(&wce);
	if (FALSE == RegisterClassEx(&wce))
		return 1;

	//创建窗口
	HWND hWnd = CreateWindowEx(
		WS_EX_APPWINDOW,		//窗口的扩展样式
		cls_name,				//窗口类名
		TEXT("3DMouseTest"),	//窗口标题
		WS_OVERLAPPEDWINDOW,	//窗口风格样式
		200, 120, 600, 400,		//窗口x,y,宽度,高度
		NULL, 					//父窗口句柄
		NULL, 					//菜单句柄
		hInstance,				//应用程序实例句柄
		NULL);					//附加数据

	//回调函数要写上默认的处理DefWindowProc，不然创建失败
	if (!hWnd)
		return 1;

	//显示，更新窗口
	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd);

	//添加加速键表
	HACCEL hAccel = NULL;
	/*::LoadAccelerators(hInstance,MAKEINTRESOURCE(IDR_ACCELERATOR1);*/

	// =======================================================================
	// 初始化3D鼠标

	three_mouse_global_init();

	three_mouse_open_device(hWnd, 1);

	// =======================================================================

	//消息循环
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(hWnd, hAccel, &msg))//有加速键表时用这,没有就不用
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

	}

	// =======================================================================
	// 销毁3D鼠标

	three_mouse_global_dispose();

	// =======================================================================

	return 0;
}

//窗口消息处理函数
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	ThreeDMouseData* mouse_data = three_mouse_on_message(1, wParam, lParam);

	if (mouse_data != NULL && mouse_data->result == TRUE)
	{

	}

	switch (uMsg)
	{
	case WM_CLOSE:
		DestroyWindow(hWnd);
		break;

	case WM_DESTROY:
		PostQuitMessage(0);
		break;

	default:	break;
	}

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