⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mysnake.cpp

📁 ARM的嵌入式虹膜识别源代码
💻 CPP
字号:
// mySnake.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include "snake.h"
#include "LoadBmpFile.h"
#define MAX_LOADSTRING 100
#define POINT_NUM  20
// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];								// The title bar text
////////////////////////////global

snake Global_Snake;
load_bmp_file Global_Bmp;

// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
inline void  RButtonUp(int &DrawFlag,static point_list *List_Head,int &No_Of_Point);
////////////////////

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_MYSNAKE, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MYSNAKE);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_MYSNAKE);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(BLACK_BRUSH);
	wcex.lpszMenuName	= (LPCSTR)IDC_MYSNAKE;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	int i,j;
	PAINTSTRUCT ps;
	HDC hdc,hMemDC;
	TCHAR szHello[MAX_LOADSTRING];
	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
	static DrawFlag = FALSE;
	static point_list *List_Head = NULL;
	static point_list *Cur_Point;
	static int No_Of_Point =0;
	static int Window_Width,Window_Height;
	static point Eye_Center;
	point_list *Tmp_Point;	
	HPEN hPen;
	switch (message) 
	{
		case WM_TIMER:
			Global_Snake.SnakeAlgorithm();
			if(Global_Snake.No_Of_Mov<Global_Snake.Point_Num/10)
			{
				float xCollect=0,yCollect=0;
				KillTimer(hWnd,SNAKE_TIMER);
				for(i=0;i<Global_Snake.Point_Num;i++)
				{
					xCollect += Global_Snake.ReadPoint(i).x;
					yCollect += Global_Snake.ReadPoint(i).y;
				}
				Eye_Center.x = xCollect/i;
				Eye_Center.y = yCollect/i;
/*
				if(((Eye_Center.x-Global_Snake.Center.x)*(Eye_Center.x-Global_Snake.Center.x)
					+(Eye_Center.y-Global_Snake.Center.y)*(Eye_Center.y-Global_Snake.Center.y))
					>100)
				{
					Global_Snake.Center.x =Eye_Center.x-(Global_Snake.Window_Width/2-Global_Snake.Img_Width/2);
					Global_Snake.Center.y =Eye_Center.y-(Global_Snake.Window_Height/2-Global_Snake.Img_Height/2);
					
					if(Global_Snake.Snake_Point != NULL)
					{
						delete []Global_Snake.Snake_Point;
						Global_Snake.Point_Num = 0;
					}
					Global_Snake.SetPoints(POINT_NUM);
					Global_Snake.StartTimer(hWnd);
				}
				else
*/
					Global_Snake.Termination_Flag = TRUE;
			}
			InvalidateRect(hWnd,NULL,0);
			break;
		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{
				case IDM_STEP:
					if(DrawFlag&&List_Head)
					{
						RButtonUp(DrawFlag,List_Head,No_Of_Point);
						DrawFlag = 0;
						List_Head = NULL;
					}
					Global_Snake.SnakeAlgorithm();
					InvalidateRect(hWnd,NULL,0);
					break;
				case IDM_AUTO:
					if(List_Head)
					{
						RButtonUp(DrawFlag,List_Head,No_Of_Point);
						List_Head =NULL;
					}
					Global_Snake.StartTimer(hWnd);
					break;
				case IDM_OPEN:
					Global_Snake.Termination_Flag = FALSE;
					Global_Bmp.ofn.hwndOwner=hWnd;
					Global_Bmp.ofn.lpstrFile=Global_Snake.FileName;
				
					if(!GetOpenFileName(&Global_Bmp.ofn))
						return 0;
					
					if(!Global_Bmp.LoadBmpFile(hWnd,Global_Snake.FileName))
						MessageBox(hWnd,TEXT("LoadFile Failed!"),TEXT("Warning!"),MB_OK);
					Global_Snake.PointToImg(Global_Bmp);
					Global_Snake.Center = Global_Bmp.FindCenter();
					InvalidateRect(hWnd,NULL,1);
					break;
				case IDM_START:
					Global_Bmp.TemplateOperation(hWnd);
					Global_Snake.SnakeAlgorithm();
					Global_Snake.PointToImg(Global_Bmp);
					InvalidateRect(hWnd,NULL,1);
					break;
				case IDM_ABOUT:
				   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
				   break;
				case ID_MENUITEM32776://set points
					SendMessage(hWnd,WM_RBUTTONDOWN,wParam,lParam);
					break;
				case IDM_EXIT:
				   DestroyWindow(hWnd);
				   break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_PAINT:
			hdc =BeginPaint (hWnd, &ps) ;
			if(Global_Bmp.hBitmap)
			{
				hMemDC=CreateCompatibleDC(hdc);
				
				if(Global_Bmp.hPalette) //有调色板
				{

				//将调色板选入屏幕设备上下文

				SelectPalette (hdc,Global_Bmp.hPalette, FALSE); 

				//将调色板选入内存设备上下文

				SelectPalette (hMemDC,Global_Bmp.hPalette, FALSE);

				RealizePalette (hdc);
				}
				//将位图选入内存设备上下文
				SelectObject(hMemDC,Global_Bmp.hBitmap);
				//显示位图
				BitBlt(hdc,Window_Width/2-Global_Bmp.bi.biWidth/2,Window_Height/2-Global_Bmp.bi.biHeight/2,Global_Bmp.bi.biWidth,Global_Bmp.bi.biHeight,hMemDC,0,0,SRCCOPY);
				DeleteDC(hMemDC);
			}
			// TODO: Add any drawing code here...
			if(Global_Snake.Point_Num>0)
			{
				Global_Snake.DrawPoints(hdc);
			}
			/////////////
			if(Global_Snake.Termination_Flag)
			{
				hPen = CreatePen(PS_SOLID,3, RGB(0,255,0));  
				SelectObject(hdc,hPen);
				Ellipse(hdc,Eye_Center.x-3,Eye_Center.y-3,Eye_Center.x+3,Eye_Center.y+3);
				DeleteObject(hPen);
			}
			//SetPixel(hdc,Eye_Center.x,Eye_Center.y,RGB(255,0,0));
			EndPaint(hWnd, &ps);
			break;
		case WM_SIZE:
			Global_Snake.Window_Width=Window_Width = LOWORD(lParam);
			Global_Snake.Window_Height=Window_Height= HIWORD(lParam);
			break;
		case WM_LBUTTONDOWN:
			if(LOWORD(lParam)>(Window_Width/2-Global_Bmp.bi.biWidth/2) 
			   &&LOWORD(lParam)<(Window_Width/2+Global_Bmp.bi.biWidth/2)
			   &&HIWORD(lParam)>(Window_Height/2-Global_Bmp.bi.biHeight/2)
			   &&HIWORD(lParam)<(Window_Height/2+Global_Bmp.bi.biHeight/2)
			   )
			{
				point_list *Tmp_Point;
				Tmp_Point = new point_list;

				Tmp_Point->x = LOWORD(lParam)-(Window_Width/2-Global_Bmp.bi.biWidth/2);
				Tmp_Point->y = HIWORD(lParam)-(Window_Height/2-Global_Bmp.bi.biHeight/2);

				if(List_Head == NULL)
				{
					List_Head = Tmp_Point;
					Cur_Point = List_Head;
				}
				else
				{
					Cur_Point->next = Tmp_Point;
					Cur_Point = Cur_Point->next;
				}
				Cur_Point->next = NULL;
				No_Of_Point ++;
				InvalidateRect(hWnd,NULL,1);
/*
				////////////////////////
				if(List_Head)
				{
					Tmp_Point = List_Head;
					while(Tmp_Point)
					{
						List_Head = Tmp_Point;
						Tmp_Point = Tmp_Point->next;
						delete []List_Head;
					}
				}
				List_Head = NULL;
				///////////////////
				Tmp_Point = new point_list;
				Tmp_Point->x = LOWORD(lParam)-(Window_Width/2-Global_Bmp.bi.biWidth/2);
				Tmp_Point->y = HIWORD(lParam)-(Window_Height/2-Global_Bmp.bi.biHeight/2);
				List_Head = Tmp_Point;
				Cur_Point = List_Head;
				Cur_Point->next = NULL;
				No_Of_Point ++;
				////////////////
*/
				DrawFlag = TRUE;
			}
			break;
/*
		case WM_MOUSEMOVE:
			if(DrawFlag&&
	    	   LOWORD(lParam)>(Window_Width/2-Global_Bmp.bi.biWidth/2) 
			   &&LOWORD(lParam)<(Window_Width/2+Global_Bmp.bi.biWidth/2)
			   &&HIWORD(lParam)>(Window_Height/2-Global_Bmp.bi.biHeight/2)
			   &&HIWORD(lParam)<(Window_Height/2+Global_Bmp.bi.biHeight/2)
			   )
			{
				point_list *Tmp_Point;
				Tmp_Point = new point_list;

				Tmp_Point->x = LOWORD(lParam)-(Window_Width/2-Global_Bmp.bi.biWidth/2);
				Tmp_Point->y = HIWORD(lParam)-(Window_Height/2-Global_Bmp.bi.biHeight/2);

				if(List_Head == NULL)
				{
					List_Head = Tmp_Point;
					Cur_Point = List_Head;
				}
				else
				{
					Cur_Point->next = Tmp_Point;
					Cur_Point = Cur_Point->next;
				}
				Cur_Point->next = NULL;
				No_Of_Point ++;
			}
			break;
*/
		case WM_LBUTTONUP:
/*
			DrawFlag = FALSE;
			int Index;
			Index = 0;
			
			if(List_Head != NULL)
			{
				//store the list to the snake_array
				if(Global_Snake.Snake_Point != NULL)
				{
					delete []Global_Snake.Snake_Point;
					Global_Snake.Point_Num = 0;
				}
				
				Global_Snake.Point_Num = No_Of_Point;
				Global_Snake.Snake_Point = new point[No_Of_Point];

				Tmp_Point = List_Head;
				while(Tmp_Point!=NULL)
				{
					List_Head = Tmp_Point;
					Tmp_Point = Tmp_Point->next;
					Global_Snake.Snake_Point[Index].x = List_Head->x;
					Global_Snake.Snake_Point[Index].y = List_Head->y;
					Index ++;
					delete []List_Head;
				}

				List_Head = NULL;
				No_Of_Point = 0;
				
			}
*/
			InvalidateRect(hWnd,NULL,1);
			break;

		case WM_RBUTTONDOWN:
				if(Global_Snake.Snake_Point != NULL)
				{
					delete []Global_Snake.Snake_Point;
					Global_Snake.Point_Num = 0;
				}
				
				Global_Snake.SetPoints(POINT_NUM);

				/////////////delete List
				if(List_Head)
				{
					Tmp_Point = List_Head;
					while(Tmp_Point)
					{
						List_Head = Tmp_Point;
						Tmp_Point = Tmp_Point->next;
						delete []List_Head;
					}
				}
				List_Head = NULL;
				No_Of_Point = 0;
				Global_Snake.Termination_Flag=0;
				InvalidateRect(hWnd,NULL,1);
			break;
		case WM_CREATE:
			Global_Bmp.PopFileInitialize (hWnd);
			break;
		case WM_DESTROY:
			//delete list of points
			if(List_Head != NULL)
			{
				point_list *Tmp_Point;
				Tmp_Point = List_Head;
				while(Tmp_Point)
				{
					List_Head = Tmp_Point;
					Tmp_Point = Tmp_Point->next;
					delete []List_Head;
				}
				List_Head = NULL;
			}

			PostQuitMessage(0);

			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_INITDIALOG:
				return TRUE;

		case WM_COMMAND:
			if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
			{
				EndDialog(hDlg, LOWORD(wParam));
				return TRUE;
			}
			break;
	}
    return FALSE;
}

void RButtonUp(int &DrawFlag,static point_list * List_Head,int &No_Of_Point)
{
			DrawFlag = FALSE;
			int Index;
			Index = 0;
			point_list * Tmp_Point;
			
			if(List_Head != NULL)
			{
				//store the list to the snake_array
				if(Global_Snake.Snake_Point != NULL)
				{
					delete []Global_Snake.Snake_Point;
					Global_Snake.Point_Num = 0;
				}
				
				Global_Snake.Point_Num = No_Of_Point;
				Global_Snake.Snake_Point = new point[No_Of_Point];

				Tmp_Point = List_Head;
				while(Tmp_Point!=NULL)
				{
					List_Head = Tmp_Point;
					Tmp_Point = Tmp_Point->next;
					Global_Snake.Snake_Point[Index].x = List_Head->x;
					Global_Snake.Snake_Point[Index].y = List_Head->y;
					Index ++;
					delete []List_Head;
				}

				List_Head = NULL;
				No_Of_Point = 0;
				
			}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -