📄 whamster.c
字号:
switch (dir) { /* evaluate direction */ case HMS_EAST : series = 0; break; case HMS_NORTH: series = 1; break; case HMS_WEST : series = 2; break; case HMS_SOUTH: series = 3; break; default : series = 0; break; } /* get sprite image series number */ spr_select(sprite, series, 0); spr_draw(sprite, x, y); /* select and draw sprite picture */ case HMS_SCORED: /* --- if score has changed */ sprintf(buf, "%s %10ld", PRGNAME, hms_score(hms)); SetWindowText(hmain, buf); break; /* set new window title */ case HMS_MOVED: /* --- if hamster was moved */ dir = hms_dir(hms); /* get direction (line of sight) */ switch (dir) { /* and evaluate it */ case HMS_EAST : x = 2; y = 0; cnt = FIELDWD/2; break; case HMS_NORTH: x = 0; y = -2; cnt = FIELDWD/2; break; case HMS_WEST : x = -2; y = 0; cnt = FIELDWD/2; break; case HMS_SOUTH: x = 0; y = 2; cnt = FIELDWD/2; break; default : x = 0; y = 0; cnt = 0; break; } /* get offsets to new field */ if (appdata.speed >= 16) { x *= 8; y *= 8; cnt /= 8; } else if (appdata.speed >= 8) { x *= 4; y *= 4; cnt /= 4; } else if (appdata.speed >= 4) { x *= 2; y *= 2; cnt /= 2; } spr_move(sprite, x, y, 0, cnt, 1.0F/cnt/appdata.speed); break; /* redraw and move sprite */ case HMS_TURNED: /* --- if hamster was turned */ dir = hms_dir(hms); /* get direction (line of sight) */ switch (dir) { /* and evaluate it */ case HMS_EAST : series = (arg == HMS_POS) ? 3 : 5; break; case HMS_NORTH: series = (arg == HMS_POS) ? 0 : 6; break; case HMS_WEST : series = (arg == HMS_POS) ? 1 : 7; break; case HMS_SOUTH: series = (arg == HMS_POS) ? 2 : 4; break; default : series = (arg == HMS_POS) ? 3 : 5; break; } /* get sprite image series number */ if (appdata.speed >= 50) {/* if not to show turns */ spr_select(sprite, series, 6); return; /* simply select the last image */ } /* and abort the function */ spr_select(sprite, series, 0); /* select image series */ if (appdata.speed >= 16) { x = 6; cnt = 1; } else if (appdata.speed >= 4) { x = 3; cnt = 2; } else { x = 1; cnt = 6; } spr_move(sprite, 0, 0, x, cnt, 1.0F/cnt/appdata.speed); break; /* redraw and turn sprite */ case HMS_FIELD: /* --- if field contents has changed */ mz_getpos(maze, &sx, &sy);/* get start position and */ hms_pos(hms, &x, &y); /* current position of hamster */ x += sx; y += sy; /* and compute absolute position */ spr_undraw(sprite); /* undraw sprite */ hdc = GetDC(hmain); /* get device context for window */ draw_field(hdc, x, y); /* and update field */ ReleaseDC(hmain, hdc); /* release device context for window */ spr_draw(sprite, INT_MIN, INT_MIN); break; /* redraw sprite */ case HMS_DELETED: /* --- if hamster is deleted */ spr_undraw(sprite); break;/* undraw sprite */ default: break; /* ignore everything else */ } } /* hms_disp() *//*--------------------------------------------------------------------*/static void resize (HWND hwnd){ /* --- adapt window size */ RECT rc; /* client area rectangle */ int x, y, dx, dy; /* coordinates and offsets */ GetClientRect(hwnd, &rc); /* get client rectangle of window */ xoff = 1; yoff = appdata.yext *FIELDWD; /* and compute offsets */ rc.right = rc.left +(dx = appdata.xext *FIELDWD +1); rc.bottom = rc.top +(dy = appdata.yext *FIELDWD +1); AdjustWindowRect(&rc, WINSTYLE, 1); x = rc.right -rc.left; /* get window client area, */ y = rc.bottom -rc.top; /* compute new window rectangle, */ GetWindowRect(hwnd, &rc); /* and resize window */ MoveWindow(hwnd, rc.left, rc.top, x, y, 1); GetClientRect(hwnd, &rc); /* get resulting rectangle */ if ((rc.right -rc.left != dx) /* if resulting client area */ || (rc.bottom -rc.top != dy)) { /* has a different size */ x += dx -(rc.right -rc.left); /* (e.g. because of a */ y += dy -(rc.bottom -rc.top); /* two-line menu) */ MoveWindow(hwnd, rc.left, rc.top, x, y, 1); } /* resize window a second time */ InvalidateRect(hwnd, NULL,0); /* trigger window redraw */} /* resize() *//*---------------------------------------------------------------------- Dialog Box Functions----------------------------------------------------------------------*/BOOL CALLBACK dp_about (HWND hwnd, UINT msg, WPARAM wp, LPARAM lp){ /* --- 'about' dialog box procedure */ switch (msg) { /* evaluate message */ case WM_COMMAND: /* --- if command */ if (wp != IDOK) break; /* process only 'Ok' button */ case WM_CLOSE: /* --- if dialog closed */ EndDialog(hwnd, 0); /* terminate dialog */ return 1; /* return 'ok' */ } return 0; /* everything else not handled */} /* dp_about() *//*--------------------------------------------------------------------*/BOOL CALLBACK dp_size (HWND hwnd, UINT msg, WPARAM wp, LPARAM lp){ /* --- 'set maze size' dialog box */ MAZE *tmp; /* temporary buffer */ int xext, yext; /* x- and y-extension */ switch (msg) { /* evaluate message */ case WM_INITDIALOG: /* --- if dialog initialization */ int_set(hwnd, DI_XEXT, appdata.xext); /* set edit */ int_set(hwnd, DI_YEXT, appdata.yext); /* controls */ return 1; /* add items to list box */ case WM_COMMAND: /* --- if command */ switch (wp) { /* evaluate command code */ case IDOK: /* if 'Ok' button pressed */ xext = int_get(hwnd, DI_XEXT); /* get input from */ yext = int_get(hwnd, DI_YEXT); /* edit controls */ if (xext < HMS_MINXEXT) xext = HMS_MINXEXT; if (xext > HMS_MAXXEXT) xext = HMS_MAXXEXT; if (yext < HMS_MINYEXT) yext = HMS_MINYEXT; if (yext > HMS_MAXYEXT) yext = HMS_MAXYEXT; tmp = mz_create(xext, yext); if (!tmp) { error(E_NOMEM); break; } mz_delete(maze); maze = tmp; appdata.xext = xext; /* create and set a new maze and */ appdata.yext = yext; /* note the new maze extensions */ EndDialog(hwnd, 0); /* terminate the dialog */ return 1; /* and return 'ok' */ case IDCANCEL: break; /* if 'Cancel' button pressed */ default : return 0; } /* everything else not handled */ case WM_CLOSE: /* --- if dialog closed */ EndDialog(hwnd, -1); /* terminate dialog */ return 1; /* return 'ok' */ } return 0; /* everything else not handled */} /* dp_size() *//*--------------------------------------------------------------------*/BOOL CALLBACK dp_random (HWND hwnd, UINT msg, WPARAM wp, LPARAM lp){ /* --- 'randomize maze' dialog box */ switch (msg) { /* evaluate message */ case WM_INITDIALOG: /* --- if dialog initialization */ int_set(hwnd, DI_WALLPROB, (int)(appdata.wallprob *100 +0.5F)); int_set(hwnd, DI_TOTAL, appdata.total); int_set(hwnd, DI_MAXHEAP, appdata.maxheap); return 1; /* set edit controls */ case WM_COMMAND: /* --- if command */ switch (wp) { /* evaluate command code */ case IDOK: /* if 'Ok' button pressed */ appdata.wallprob = int_get(hwnd, DI_WALLPROB) /100.0F; appdata.total = int_get(hwnd, DI_TOTAL); appdata.maxheap = int_get(hwnd, DI_MAXHEAP); if (appdata.wallprob < 0.0F) appdata.wallprob = 0.0F; if (appdata.wallprob > 1.0F) appdata.wallprob = 1.0F; if (appdata.maxheap < 1) appdata.maxheap = 1; if (appdata.maxheap > MAXHEAP) appdata.maxheap = MAXHEAP; mz_init(maze, drand, appdata.wallprob, appdata.total, appdata.maxheap); EndDialog(hwnd, 0); /* get inputs from edit controls */ return 1; /* and reinitialize maze */ case IDCANCEL: break; /* if 'Cancel' button pressed */ default : return 0; } /* everything else not handled */ case WM_CLOSE: /* --- if dialog closed */ EndDialog(hwnd, -1); /* terminate dialog */ return 1; /* return 'ok' */ } return 0; /* everything else not handled */} /* dp_random() *//*--------------------------------------------------------------------*/BOOL CALLBACK dp_speed (HWND hwnd, UINT msg, WPARAM wp, LPARAM lp){ /* --- 'set maze size' dialog box */ static char buf[16]; /* text buffer */ switch (msg) { /* evaluate message */ case WM_INITDIALOG: /* --- if dialog initialization */ sprintf(buf, "%g", appdata.speed); edt_set(hwnd, DI_SPEED, buf); return 1; /* add items to dialog box */ case WM_COMMAND: /* --- if command */ switch (wp) { /* evaluate command code */ case IDOK: /* if 'Ok' button pressed */ edt_get(hwnd, DI_SPEED, buf, 16); appdata.speed = (float)strtod(buf, NULL); if (appdata.speed < MINSPEED) appdata.speed = MINSPEED; if (appdata.speed > MAXSPEED) appdata.speed = MAXSPEED; EndDialog(hwnd, 0); /* get input from edit controls */ return 1; case IDCANCEL: break; /* if 'Cancel' button pressed */ default : return 0; } /* everything else not handled */ case WM_CLOSE: /* --- if dialog closed */ EndDialog(hwnd, -1); /* terminate dialog */ return 1; /* return 'ok' */ } return 0; /* everything else not handled */} /* dp_speed() *//*--------------------------------------------------------------------*/BOOL CALLBACK dp_result (HWND hwnd, UINT msg, WPARAM wp, LPARAM lp){ /* --- 'set maze size' dialog box */ static char buf[16]; /* text buffer */ switch (msg) { /* evaluate message */ case WM_INITDIALOG: /* --- if dialog initialization */ int_set(hwnd, DI_CORNCNT, appdata.corncnt); int_set(hwnd, DI_MOVECNT, appdata.movecnt); int_set(hwnd, DI_CRSHCNT, appdata.crshcnt); sprintf(buf, "%ld", appdata.score); edt_set(hwnd, DI_SCORE, buf); return 1; /* add items to dialog box */ case WM_COMMAND: /* --- if command */ if (wp != IDOK) break; /* process only 'Ok' button */ case WM_CLOSE: /* --- if dialog closed */ EndDialog(hwnd, 0); /* terminate dialog */ return 1; /* return 'ok' */ } return 0; /* everything else not handled */} /* dp_result() *//*--------------------------------------------------------------------*/static int mz_file (int mode){ /* --- execute maze file operations */ static char fname[_MAX_PATH+1]; /* file name */ static char path [_MAX_PATH+1]; /* and path */ static char filter[] = /* file selection filter */ "maze (*.maz)\0*.maz\0" /* mazes */ "all files (*.*)\0*.*\0"; /* all files */ static OPENFILENAME ofn = { /* file selector structure */ sizeof(OPENFILENAME), /* lStructSize */ NULL, NULL, /* hwndOwner, hInstance */ filter, NULL, /* lpstrFilter, lpstrCustomFilter */ 0, 1, /* nMaxCustFilter, nFilterIndex */ fname, _MAX_PATH, /* lpstrFile, nMaxFile */ NULL, _MAX_FNAME+_MAX_EXT, /* lpstrFileTitle, nMaxFileTitle */ path, /* lpstrInitialDir */ "Select Maze File", /* lpstrTitle */ 0, 0, 0, /* Flags, nFileOffset, nFileExtension */ "maz", 0L, /* lpstrDefExt, lCustData */ NULL, NULL }; /* lpfnHook, lpTemplateName */ FILE *file; /* file to load from/save to */ MAZE *tmp; /* temporary buffer */ if (!(mode & MZ_NEW) /* if to use old file name */ && (strcmp(appdata.fn_maze, NONAME) != 0)) strcpy(fname, appdata.fn_maze); else { /* if to get a new file name */ ofn.hwndOwner = hmain; /* set window handle and flags */ ofn.Flags = (mode & MZ_SAVE) ? OFN_PATHMUSTEXIST : OFN_FILEMUSTEXIST; if (GetOpenFileName(&ofn) == 0) return -1; /* open file selector box */ } /* and get maze file name */ if (mode & MZ_SAVE) { /* if to save a maze */ file = fopen(fname, "w"); /* open selected file */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -