housekeeper.c

来自「minigui PDA系统 可实现手机功能」· C语言 代码 · 共 1,389 行 · 第 1/2 页

C
1,389
字号
		nextafter = boxLeave[nextbefore];		assert(nextafter != B_FORBID);		nextafter = manGoInto[nextafter];		assert(nextafter != B_FORBID);		//for UNDO		pAStep = (ptagStep) calloc (1, sizeof(Step));		pAStep->iType  = STEP_TYPE_PUSH;		pAStep->pt1[0] = curx;		pAStep->pt1[1] = cury;		pAStep->pt1[2] = curbefore;		pAStep->pt2[0] = nextx;		pAStep->pt2[1] = nexty;		pAStep->pt2[2] = nextbefore;		pAStep->pt3[0] = farx;		pAStep->pt3[1] = fary;		pAStep->pt3[2] = farbefore;		PushStep(pAStep);		//act		cur_level->data[cury  * iCol + curx]  = curafter;		cur_level->data[nexty * iCol + nextx] = nextafter;		cur_level->data[fary * iCol + farx] = farafter;		DrawALittleBlock(hwnd, curx, cury, curafter);			DrawALittleBlock(hwnd, nextx, nexty, nextafter);			DrawALittleBlock(hwnd, farx, fary, farafter);					cur_level->manx = nextx;		cur_level->many = nexty;			return STEP_TYPE_PUSH;	}	return 0;}ptagStep PopStep(void) {	ptagStep pStep1;	int i = theMap->shead;	if (i == -1) 		return NULL;	pStep1 = theMap->pSteps[i];	theMap->pSteps[i] = NULL;	i --;	if ((i < 0) && (theMap->pSteps[BACK_STEP_SIZE - 1]))		i = BACK_STEP_SIZE - 1;	theMap->shead = i;	return pStep1;}void PushStep(ptagStep pStep){	theMap->shead++;	theMap->shead %= 50;		if(theMap->pSteps[theMap->shead])		free(theMap->pSteps[theMap->shead]);	theMap->pSteps[theMap->shead] = pStep;}void PlayRestart(void) {		ptagLevel pLevel1, pLevel2;	DestroyLevel(theMap->currentLevel);	DestroyStep();	//duplcate the current level	pLevel1 = theMap->current->current;	pLevel2 = (ptagLevel)calloc(1, sizeof(Level));	pLevel2->iNo = pLevel1->iNo;	pLevel2->row = pLevel1->row;	pLevel2->col = pLevel1->col;	pLevel2->manx = pLevel1->manx;	pLevel2->many = pLevel1->many;	pLevel2->next = pLevel1->next;	pLevel2->prev = pLevel1->prev;	pLevel2->data = (int *)calloc(pLevel2->row*pLevel2->col, sizeof(int));	memcpy(pLevel2->data, pLevel1->data, pLevel2->row * pLevel2->col * sizeof(int));	theMap->currentLevel = pLevel2;	//}BOOL CheckMissionComplete(void){	int i,j;	int iRow, iCol;		int a;			ptagLevel cur_level;	cur_level = theMap->currentLevel;	iRow = cur_level->row;	iCol = cur_level->col;	for (i = 0; i < iRow; i++)		for (j = 0; j < iCol; j++) {			a = cur_level->data[i*iCol+j]; 			if ( (a == B_OBJECT) || (a == B_GOAL))				return FALSE;		}	return TRUE;}int PlayUndo(HWND hwnd){	ptagStep pAStep;	ptagLevel cur_level;	int itype;	int x, y, value;	int iRow, iCol;		pAStep = PopStep();	if (!pAStep)		return -1;	itype = pAStep->iType;	assert((itype == STEP_TYPE_PUSH) || (itype == STEP_TYPE_MOVE));	cur_level = theMap->currentLevel;	iCol = cur_level->col;	iRow = cur_level->row;	x = pAStep->pt1[0];	y = pAStep->pt1[1];	value = pAStep->pt1[2];	cur_level->data[y * iCol + x] = value;	DrawALittleBlock(hwnd,x, y, value);		cur_level->manx = x;	cur_level->many = y;	x = pAStep->pt2[0];	y = pAStep->pt2[1];	value = pAStep->pt2[2];	cur_level->data[y * iCol + x] = value;	DrawALittleBlock(hwnd,x, y, value);		if(itype == STEP_TYPE_PUSH) {		x = pAStep->pt3[0];		y = pAStep->pt3[1];		value = pAStep->pt3[2];		cur_level->data[y * iCol + x] = value;		DrawALittleBlock(hwnd,x, y, value);		}	free(pAStep);	return 0;}//////////////////////////////////////////////////////////////DLGTEMPLATE DlgInputLevel = {    WS_BORDER | WS_CAPTION,    WS_EX_IMECOMPOSE,    70, 80, 400, 158,    "Choose level",    0, 0,    4, NULL,    0};CTRLDATA CtrlInputLevel [] = {    {        "static",        WS_VISIBLE | SS_RIGHT,        14, 30, 150, 18,        IDC_STATIC,         "Game Level:",        0    },    {        "edit",        WS_VISIBLE | WS_BORDER | WS_TABSTOP,         180, 30, 100, 22,         IDC_LEVELINPUT,         NULL,        0    },    {        "button",        WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,         80, 94, 100, 28,         IDOK,         "Ok",        0     },    {        "button",        WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,        256, 94, 100, 28,         IDCANCEL,         "Cancel",         0    }};  static int DialogInputLevelProc(HWND hDlg, int message, WPARAM wParam, LPARAM lParam){	char strText[4];	int i;	switch (message) {	case MSG_INITDIALOG:        	return 1;            	case MSG_COMMAND:		switch (wParam) {		case IDOK:			GetDlgItemText(hDlg,IDC_LEVELINPUT,strText,3);			i = atoi(strText);			EndDialog(hDlg,i);			break;		case IDCANCEL:		    EndDialog (hDlg, 0);		    break;		}		break;    }        return DefaultDialogProc (hDlg, message, wParam, lParam);}HMENU createpmenuabout(void){    HMENU hmnu;    MENUITEMINFO mii;    memset (&mii, 0, sizeof(MENUITEMINFO));    mii.type        = MFT_STRING;    mii.id          = 0;    mii.typedata    = (DWORD)"关于";    hmnu = CreatePopupMenu (&mii);        memset (&mii, 0, sizeof(MENUITEMINFO));    mii.type        = MFT_STRING ;    mii.state       = 0;    mii.id          = ID_ABOUT;    mii.typedata    = (DWORD)"关于";    InsertMenuItem(hmnu, 0, TRUE, &mii);	hmnu = StripPopupHead(hmnu);	    return hmnu;}HMENU createpmenugame(void){    HMENU hmnu;    MENUITEMINFO mii;    memset (&mii, 0, sizeof(MENUITEMINFO));    mii.type        = MFT_STRING;    mii.id          = 0;    mii.typedata    = (DWORD)"游戏";    hmnu = CreatePopupMenu (&mii);        memset (&mii, 0, sizeof(MENUITEMINFO));    mii.type        = MFT_STRING ;    mii.state       = 0;    mii.id          = ID_GOTO;    mii.typedata    = (DWORD)"跳关";    InsertMenuItem(hmnu, 4, TRUE, &mii);    memset (&mii, 0, sizeof(MENUITEMINFO));    mii.type        = MFT_STRING ;    mii.state       = 0;    mii.id          = ID_NEXT;    mii.typedata    = (DWORD)"下一级";    InsertMenuItem(hmnu, 1, TRUE, &mii);    memset (&mii, 0, sizeof(MENUITEMINFO));    mii.type        = MFT_STRING ;    mii.state       = 0;    mii.id          = ID_PREVIOUS;    mii.typedata    = (DWORD)"上一级";    InsertMenuItem(hmnu, 2, TRUE, &mii);        mii.type        = MFT_STRING ;    mii.state       = 0;    mii.id          = ID_RESTART;    mii.typedata    = (DWORD)"重新开始";    InsertMenuItem(hmnu, 0, TRUE, &mii);        mii.type        = MFT_STRING ;    mii.state       = 0;    mii.id          = ID_UNDO;    mii.typedata    = (DWORD)"撤销";    InsertMenuItem(hmnu, 3, TRUE, &mii);    mii.type        = MFT_STRING ;    mii.state       = 0;    mii.id          = ID_EXIT;    mii.typedata    = (DWORD)"退出";    InsertMenuItem(hmnu, 5, TRUE, &mii);	hmnu = StripPopupHead(hmnu);        return hmnu;}HMENU createmenu1(void){    HMENU hmnu;    MENUITEMINFO mii;    hmnu = CreateMenu();    memset (&mii, 0, sizeof(MENUITEMINFO));    mii.type        = MFT_STRING;    mii.id          = 100;    mii.typedata    = (DWORD)"游戏";    mii.hsubmenu    = createpmenugame();    InsertMenuItem(hmnu, 0, TRUE, &mii);        mii.type        = MFT_STRING;    mii.id          = 120;    mii.typedata    = (DWORD)"关于";    mii.hsubmenu    = createpmenuabout();    InsertMenuItem(hmnu, 1, TRUE, &mii);                       return hmnu;}void OnDrawPushark(HWND hWnd,HDC hDC) {	RECT rect;	POINT poStart1;	int iSideLength;	ptagLevel cur_level;	char strText[50];	int iRow;	int iCol;	int i, j;	int iBmp;	GetClientRect(hWnd,&rect);	SetBkColor(hDC,RGB2Pixel(HDC_SCREEN, 102, 102, 102));	//SetTextColor(hDC,COLOR_green);		SetPenColor(hDC,COLOR_black);	poStart1.x = rect.left;	poStart1.y = rect.top;		cur_level = theMap->currentLevel;	snprintf(strText, 50, "Collection %s, Level %d ", theMap->current->strName, cur_level->iNo);		TextOut(hDC, rect.left + 40, rect.bottom - 20, strText);	rect.bottom -= 20;		if (rect.bottom - rect.top < 100)		return;	iRow = cur_level->row;	iCol = cur_level->col;	i = RECTH(rect)/iRow;	j = RECTW(rect)/iCol;	if (i <= j) 		iSideLength = i;	else 			iSideLength = j;		for (i = 0; i < iRow; i++) {		for (j = 0; j < iCol; j++) {			iBmp = cur_level->data[i * iCol + j];			if (iBmp < B_NOTHING) {				FillBoxWithBitmap(hDC, poStart1.x + iSideLength * j \						, poStart1.y + iSideLength * i, iSideLength, iSideLength, &bitmapAll[iBmp]);            }		}    }}BOOL InitializeApp(void){	if(LoadBitmap(HDC_SCREEN, &bitmapAll[B_GOAL], "image/pushark/res/goal.gif"))		return FALSE;	if(LoadBitmap(HDC_SCREEN, &bitmapAll[B_MAN], "image/pushark/res/man.gif"))		return FALSE;	if(LoadBitmap(HDC_SCREEN, &bitmapAll[B_OBJECT], "image/pushark/res/object.gif"))		return FALSE;	if(LoadBitmap(HDC_SCREEN, &bitmapAll[B_SAVEMAN], "image/pushark/res/saveman.gif"))		return FALSE;	if(LoadBitmap(HDC_SCREEN, &bitmapAll[B_STONE], "image/pushark/res/stone.gif"))		return FALSE;	if(LoadBitmap(HDC_SCREEN, &bitmapAll[B_TREASURE], "image/pushark/res/treasure.gif"))		return FALSE;	InitMap();	return TRUE;}void OnClosePushark(HWND hWnd){	DestroyMap();	UnloadBitmap(&bitmapAll[B_GOAL]);	UnloadBitmap(&bitmapAll[B_MAN]);	UnloadBitmap(&bitmapAll[B_OBJECT]);	UnloadBitmap(&bitmapAll[B_SAVEMAN]);	UnloadBitmap(&bitmapAll[B_STONE]);	UnloadBitmap(&bitmapAll[B_TREASURE]);	DestroyMainWindow (hWnd);	PostQuitMessage (hWnd);}void CovertCoord(HWND hWnd,int *px, int *py){	RECT rect;	int iSideLength;	ptagLevel cur_level;	int iRow;	int iCol;	int i, j;	GetClientRect(hWnd,&rect);	rect.bottom -= 20;		if (rect.bottom - rect.top < 100)		return;	cur_level = theMap->currentLevel;	iRow = cur_level->row;	iCol = cur_level->col;	i = RECTH(rect)/iRow;	j = RECTW(rect)/iCol;	if (i <= j) 		iSideLength = i;	else 			iSideLength = j;		i = *px;	j = *py;	*px = rect.left + i * iSideLength;	*py = rect.top  + j * iSideLength;}BOOL ptChoosePlace(HWND hWnd, int *px, int *py) {	RECT rect;	int iSideLength;	ptagLevel cur_level;	int iRow;	int iCol;	int i, j;	GetClientRect(hWnd,&rect);	rect.bottom -= 20;		if (rect.bottom - rect.top < 100)		return FALSE;	cur_level = theMap->currentLevel;	iRow = cur_level->row;	iCol = cur_level->col;	i = RECTH(rect)/iRow;	j = RECTW(rect)/iCol;	if (i <= j) 		iSideLength = i;	else 			iSideLength = j;		i = *px;	j = *py;	if ( (i > rect.left) && (i < rect.left + iSideLength * iCol) &&	     (j > rect.top ) && (j < rect.top  + iSideLength * iRow) ) {		 *px = (i - rect.left) / iSideLength;		 *py = (j - rect.top ) / iSideLength;		 return TRUE;	}	return FALSE;}void DrawALittleBlock(HWND hWnd, int x, int y, int itype)	{	HDC hdc;	RECT rect;	int iSideLength;	ptagLevel cur_level;	int iRow;	int iCol;	int i, j;	GetClientRect(hWnd,&rect);	rect.bottom -= 20;		if (rect.bottom - rect.top < 100)		return;	cur_level = theMap->currentLevel;	iRow = cur_level->row;	iCol = cur_level->col;	i = RECTH(rect)/iRow;	j = RECTW(rect)/iCol;	if (i <= j) 		iSideLength = i;	else 			iSideLength = j;		i = x;	j = y;	x = rect.left + i * iSideLength;	y = rect.top  + j * iSideLength;	hdc = GetClientDC(hWnd);	if(itype <= B_NOTHING) {		if(itype == B_NOTHING) {			SetBrushColor(hdc,RGB2Pixel(HDC_SCREEN, 102, 102, 102));			FillBox(hdc, x, y, iSideLength, iSideLength);		}		else {			FillBoxWithBitmap(hdc, x, y, iSideLength, iSideLength, &bitmapAll[itype]);        }	}	ReleaseDC(hdc);}int TestPushArkMyWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam){	HDC hdc;	int iRet;	int x,y;	ptagLevelCollection pTempColl;	ptagLevel pTempLev;	int iDir = 0;	//char strTemp[50];	switch(message){	case MSG_CREATE:		if(!InitializeApp()){            return 1;		}    	DlgInputLevel.controls = CtrlInputLevel;		break;	case MSG_COMMAND:		switch(LOWORD(wParam)){			case ID_GOTO:    			iRet = DialogBoxIndirectParam(&DlgInputLevel, hWnd, DialogInputLevelProc, 0L);			if ((iRet > 0) && (iRet <= theMap->current->iNoOfLevels)) {				pTempLev = theMap->current->head;				if (pTempLev->iNo != iRet) {					pTempLev = pTempLev->next;					while(pTempLev != theMap->current->head) {						if(pTempLev->iNo == iRet) 							break;						pTempLev = pTempLev->next;					}				}				        if ((pTempLev->iNo == iRet) && (pTempLev != theMap->currentLevel)) {					theMap->current->current = pTempLev;					PlayRestart();					InvalidateRect(hWnd, NULL, TRUE);				}					}			break;		case ID_NEXT:			theMap->current->current = theMap->current->current->next;			PlayRestart();			InvalidateRect(hWnd, NULL, TRUE);			break;		case ID_PREVIOUS:			theMap->current->current = theMap->current->current->prev;			PlayRestart();			InvalidateRect(hWnd, NULL, TRUE);			break;		case ID_RESTART:			PlayRestart();			InvalidateRect(hWnd, NULL, TRUE);			break;		case ID_UNDO:			PlayUndo(hWnd); 			break;		case ID_EXIT:		 	OnClosePushark(hWnd);			break;		case ID_ABOUT:							MessageBox(HDC_SCREEN, "推箱子 V1.0 \n 2007/11", "About", MB_OK);			break;		}				return 0;	case MSG_LBUTTONDOWN:		x = LOWORD(lParam);		y = HIWORD(lParam);		if (!ptChoosePlace(hWnd, &x, &y))			return 0;		//snprintf(strTemp, 50, "%d:%d", x, y);		//MessageBox(HDC_SCREEN, strTemp, strTemp, MB_OK);		PlayMove(hWnd, x, y);		return 0;	case MSG_RBUTTONDOWN:		PlayUndo(hWnd); 		return 0;	case MSG_PAINT:		hdc = BeginPaint(hWnd);		OnDrawPushark(hWnd,hdc);		EndPaint(hWnd,hdc);		return 0;	case MSG_KEYDOWN:		switch (wParam) {			case SCANCODE_CURSORBLOCKUP:     				iDir = DIR_UP;				break;			case SCANCODE_CURSORBLOCKLEFT:        				iDir = DIR_LEFT;				break;			case SCANCODE_CURSORBLOCKRIGHT:      				iDir = DIR_RIGHT;				break;			case SCANCODE_CURSORBLOCKDOWN:        				iDir = DIR_DOWN;				break;			default:				iDir = -1;		}				if ((iDir != -1) && (PlayKeyboard(hWnd, iDir) == STEP_TYPE_PUSH)){			if (CheckMissionComplete()) {				MessageBox(HDC_SCREEN, "任务完成!", "祝贺!", MB_OK);				theMap->current->current = theMap->current->current->next;				PlayRestart();				InvalidateRect(hWnd, NULL, TRUE);			}			return 0;		}				if (wParam == SCANCODE_U) 			PlayUndo(hWnd); 		return 0;				case MSG_TIMER:		return 0;			    case MSG_CLOSE:		 OnClosePushark(hWnd);	    	 return 0;	}	return DefaultMainWinProc(hWnd, message, wParam, lParam);}static void InitMyPushArkWinCreateInfo(PMAINWINCREATE pCreateInfo){    pCreateInfo->dwStyle = WS_CAPTION | WS_BORDER | WS_SYSMENU |  WS_VISIBLE;    pCreateInfo->dwExStyle = WS_EX_NONE;    pCreateInfo->spCaption="推箱子";    pCreateInfo->hMenu = createmenu1();    pCreateInfo->hCursor = GetSystemCursor(0);    pCreateInfo->hIcon = 0;    pCreateInfo->MainWindowProc = TestPushArkMyWinProc;    pCreateInfo->lx = 0;    pCreateInfo->ty = 0;    pCreateInfo->rx = WIDTH_LARGEWIN;    pCreateInfo->by = HEIGHT_LARGEWIN;    pCreateInfo->iBkColor = RGB2Pixel(HDC_SCREEN, 102, 102, 102);     pCreateInfo->dwAddData = 0;    pCreateInfo->hHosting = HWND_DESKTOP;}int pushArk(HWND hWnd){    MSG Msg;    MAINWINCREATE CreateInfo;    HWND hMainWnd;    const char* layer = NULL;    RECT max_rect = {0, 0, 0, 0};    InitMyPushArkWinCreateInfo(&CreateInfo);    hMainWnd = CreateMainWindow(&CreateInfo);    if (hMainWnd == HWND_INVALID)        return -1;    ShowWindow(hMainWnd,SW_SHOWNORMAL);    while( GetMessage(&Msg, hMainWnd) ) {        TranslateMessage (&Msg);        DispatchMessage(&Msg);    }    MainWindowThreadCleanup(hMainWnd);    return 0;}

⌨️ 快捷键说明

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