📄 xhamster.c
字号:
file = fopen(fname, "r"); /* open input file */ if (!file) { error(E_FOPEN, fname); return; } tmp = mz_load(file); /* load maze from file */ fclose(file); /* close input file */ if (!tmp) { error(E_FREAD, fname); return; } src = fname; dst = appdata.fn_maze; while (*src && (--i >= 0)) *dst++ = *src++; *dst = '\0'; /* copy file name to buffer */ mz_delete(maze); maze = tmp; /* set new maze and draw it */ mz_exts(maze, &appdata.xext, &appdata.yext); resize(appdata.xext, appdata.yext); redraw(NULL, NULL, NULL, NULL);} /* dcb_load() *//*--------------------------------------------------------------------*/static void dcb_save (Widget widget, XtPointer ok, XtPointer fname){ /* --- 'Save Maze...' callback */ int i = FN_MAX -1; /* loop variable */ char *src, *dst; /* to traverse file names */ FILE *file; /* file to write to */ int res; /* result of function mz_save */ mn_enable(menu, MN_MENU, 1); /* enable menu bar */ if (!ok) return; /* if 'cancel' button pressed, abort */ file = fopen(fname, "w"); /* open output file */ if (!file) { error(E_FOPEN, fname); return; } res = mz_save(maze, file); /* save current maze to file */ if ((fclose(file) != 0) || (res != 0)) error(E_FWRITE, fname); /* check for write error */ src = fname; dst = appdata.fn_maze; while (*src && (--i >= 0)) *dst++ = *src++; *dst = '\0'; /* copy file name to buffer */} /* dcb_save() *//*--------------------------------------------------------------------*/static void dcb_select (Widget widget, XtPointer ok, XtPointer fname){ /* --- 'Select Hamster...' callback */ int i = FN_MAX -1; /* loop variable */ char *src, *dst; /* to traverse file names */ mn_enable(menu, MN_MENU, 1); /* enable menu bar */ if (!ok) return; /* if 'cancel' button pressed, abort */ src = fname; dst = appdata.fn_hms; while (*src && (--i >= 0)) *dst++ = *src++; *dst = '\0'; /* copy file name to buffer */} /* dcb_select() *//*--------------------------------------------------------------------*/static void dcb_size (Widget widget, XtPointer ok, XtPointer call){ /* --- 'Set Maze Size...' callback */ MAZE *tmp; /* temporary buffer for maze */ int xext, yext; /* maze extensions */ mn_enable(menu, MN_MENU, 1); /* and enable menu bar */ if (!ok) return; /* if 'cancel' button pressed, abort */ xext = atoi(psh_getval(psh_size, "xExt")); /* get x- and */ yext = atoi(psh_getval(psh_size, "yExt")); /* y-extension */ 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); /* get and check inputs */ if (!tmp) return; /* and try to create new maze */ mz_delete(maze); maze = tmp; /* if successful, delete old maze, */ appdata.xext = xext; /* set new maze, and */ appdata.yext = yext; /* note new extensions */ xoff = 0; yoff = yext *FIELDWD -2; resize(xext, yext); /* set offsets, resize window, */ redraw(NULL,NULL,NULL,NULL); /* and redraw maze */} /* dcb_size() *//*--------------------------------------------------------------------*/static void dcb_random (Widget widget, XtPointer ok, XtPointer call){ /* --- 'Randomize Maze...' callback */ mn_enable(menu, MN_MENU, 1); /* and enable menu bar */ if (!ok) return; /* if 'cancel' button pressed, abort */ appdata.wallprob = atof(psh_getval(psh_random, "wallProb")) /100; appdata.total = atoi(psh_getval(psh_random, "total")); appdata.maxheap = atoi(psh_getval(psh_random, "maxHeap")); if (appdata.wallprob < 0.0) appdata.wallprob = 0.0F; if (appdata.wallprob > 1.0) appdata.wallprob = 1.0F; if (appdata.total < 0) appdata.total = 0; if (appdata.maxheap < 1) appdata.maxheap = 1; if (appdata.maxheap > MAXHEAP) appdata.maxheap = MAXHEAP; mz_init(maze, drand, appdata.wallprob, appdata.total, appdata.maxheap); redraw(NULL,NULL,NULL,NULL); /* reinitialize and redraw maze */} /* dcb_random() *//*--------------------------------------------------------------------*/static void dcb_speed (Widget widget, XtPointer ok, XtPointer call){ /* --- 'Set Hamster Speed' callback */ mn_enable(menu, MN_MENU, 1); /* and enable menu bar */ if (!ok) return; /* if 'cancel' button pressed, abort */ if (appdata.speed >= MAXSPEED) /* if run without display, */ redraw(NULL, NULL, NULL, NULL); /* redraw maze */ appdata.speed = atof(psh_getval(psh_speed, "speed")); if (appdata.speed > MAXSPEED) appdata.speed = MAXSPEED; if (appdata.speed < MINSPEED) appdata.speed = MINSPEED;} /* dcb_speed() *//*----------------------------------------------------------------------*/static void dcb_result (Widget widget, XtPointer client, XtPointer call){ /* --- 'Result' callback */ mn_enable(menu, MN_MENU, 1); /* and enable menu bar */} /* dcb_result() *//*---------------------------------------------------------------------- Result Dialog Box Function----------------------------------------------------------------------*/static void result (void){ /* --- 'Result' dialog box */ if (!running) return; /* if hamster is not running, abort */ if (!psh_result) { /* if dialog box is not created */ psh_result = psh_create("resultDlg", w_top, 0); if (!psh_result) return; /* create a property sheet */ if ((psh_addline(psh_result, "corncntLabel") != 0) || (psh_additem(psh_result, "corncnt", PSH_TEXT, 0) != 0) || (psh_addline(psh_result, "movecntLabel") != 0) || (psh_additem(psh_result, "movecnt", PSH_TEXT, 0) != 0) || (psh_addline(psh_result, "crshcntLabel") != 0) || (psh_additem(psh_result, "crshcnt", PSH_TEXT, 0) != 0) || (psh_addline(psh_result, "scoreLabel") != 0) || (psh_additem(psh_result, "score", PSH_TEXT, 0) != 0)) { psh_delete(psh_result); error(E_WIDGET); } } /* set browse button callback */ mn_enable(menu, MN_MENU, 0); /* disable menu bar */ sprintf(buf, "%d", appdata.corncnt); psh_setval(psh_result, "corncnt", buf); sprintf(buf, "%d", appdata.movecnt); psh_setval(psh_result, "movecnt", buf); sprintf(buf, "%d", appdata.crshcnt); psh_setval(psh_result, "crshcnt", buf); sprintf(buf, "%d", appdata.score); psh_setval(psh_result, "score", buf); psh_handle(psh_result, dcb_result); /* set values and handle dialog */ running = 0; /* clear running flag */} /* result() *//*---------------------------------------------------------------------- Menu Callback Functions----------------------------------------------------------------------*/static void mcb_load (Widget widget, XtPointer item, XtPointer call){ load(NULL, NULL, (String*)&item, &one); }static void mcb_save (Widget widget, XtPointer item, XtPointer call){ save(NULL, NULL, (String*)&item, &one); }static void mcb_select (Widget widget, XtPointer item, XtPointer call){ select(NULL, NULL, NULL, NULL); }static void mcb_quit (Widget widget, XtPointer item, XtPointer call){ quit(NULL, NULL, NULL, NULL); }static void mcb_redraw (Widget widget, XtPointer item, XtPointer call){ redraw(NULL, NULL, NULL, NULL); }static void mcb_size (Widget widget, XtPointer item, XtPointer call){ size(NULL, NULL, NULL, NULL); }static void mcb_random (Widget widget, XtPointer item, XtPointer call){ random(NULL, NULL, NULL, NULL); }static void mcb_clear (Widget widget, XtPointer item, XtPointer call){ clear(NULL, NULL, NULL, NULL); }static void mcb_speed (Widget widget, XtPointer item, XtPointer call){ speed(NULL, NULL, NULL, NULL); }static void mcb_start (Widget widget, XtPointer item, XtPointer call){ exec(NULL, NULL, NULL, NULL); }static void mcb_about (Widget widget, XtPointer item, XtPointer call){ about(NULL, NULL, NULL, NULL); }static void mcb_menu (Widget widget, XtPointer item, XtPointer call){ mn_info(menu, ""); } /* clear info field in menu bar *//*---------------------------------------------------------------------- Child Process and Pipe Communication Functions----------------------------------------------------------------------*/static void stop (int errcode){ /* --- stop hamster */ HAMSTER *hamster; /* the hamster and */ SPRITE *sprite; /* its associated sprite */ if (!running) return; /* if hamster is not running, abort */ kill(pid, SIGKILL); /* kill child process and */ wait(NULL); /* wait for termination */ XtRemoveInput(ip_pipe); /* remove pipe input procedure */ fclose(p_in); fclose(p_out); /* close both pipes and */ hmscb[0].reply[0] = '\0'; /* clear buffered reply */ hamster = hmscb[0].hamster; /* get hamster */ if (!hamster) /* if no hamster exists, */ running = 0; /* stop running at once */ else { /* if a hamster exists */ appdata.corncnt = hms_corncnt(hamster); appdata.movecnt = hms_movecnt(hamster); appdata.crshcnt = hms_crshcnt(hamster); appdata.score = hms_score(hamster); if (errcode != E_NONE) appdata.score -= BDPENALTY; hms_delete(hamster); hmscb[0].hamster = NULL; } /* note score and delete hamster */ sprite = hmscb[0].sprite; /* get hamster sprite */ if (sprite) { /* if a sprite exists, */ spr_stop(sprite); /* stop moving sprite, */ spr_undraw(sprite); /* then undraw sprite */ spr_delete(sprite); hmscb[0].sprite = NULL; } /* delete sprite */ if (appdata.speed >= MAXSPEED) /* if run without display, */ redraw(NULL, NULL, NULL, NULL); /* redraw maze */ mn_visible(menu, MN_ITEMS, 3);/* show/hide menu items */ mn_enable (menu, MN_ITEMS, 3);/* enable/disable menu items */ mn_info(menu, ""); /* clear score */ if (errcode != E_NONE) /* show alert dialog box */ error(errcode, appdata.fn_hms); else if (running) /* if still running, */ result(); /* show result */} /* stop() *//*--------------------------------------------------------------------*/static void reply (Widget w, XtPointer client, XtPointer call){ /* --- send reply to client program */ if (!running) return; /* if hamster is not running, abort */ fprintf(p_out, hmscb[0].reply); /* send reply to client */ hmscb[0].reply[0] = '\0'; /* and mark reply as sent */ if (fflush(p_out)) stop(E_PWRITE); /* flush output pipe buffer */} /* reply() *//*--------------------------------------------------------------------*/static void icb_pipe (XtPointer client, int *src, XtInputId *iid){ /* --- pipe input callback function */ HAMSTER *hamster; /* hamster to control */ char cmd; /* command read */ int id = 0; /* hamster identifier */ int arg; /* command argument */ int x, y; /* hamster position */ if (!running) return; /* if hamster is not running, abort */ /* --- read command from pipe --- */ if ( (fscanf(p_in, " %c", &cmd) != 1) || ((cmd != 'c') && (fscanf(p_in, " %d", &id) != 1)) || (((cmd == 't') || (cmd == 'l')) && (fscanf(p_in, " %d", &arg) != 1)) || (getc(p_in) != '\n')) { /* read next command from pipe */ stop(feof(p_in) ? E_PBROKEN : E_PREAD); return; } if (((cmd != '@') && (id != 0)) /* if reply has not been sent yet, */ || (hmscb[id].reply[0] != '\0')) { /* stop execution */ stop(feof(p_in) ? E_PBROKEN : E_PREAD); return; } hamster = hmscb[id].hamster; /* get hamster to control */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -