📄 xbi.c
字号:
/*---------------------------------------------------------------------- File : xbi.c Contents: X11 bridg-it program Author : Christian Borgelt History : 03.06.2001 file created from file xskel.c----------------------------------------------------------------------*/#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <time.h>#include <assert.h>#include <X11/Intrinsic.h>#include <X11/StringDefs.h>#include <X11/Core.h>#include <X11/Shell.h>#include <X11/Xaw/Form.h>#include <X11/Xaw/Viewport.h>#include "menu.h"#include "dialog.h"#include "fselect.h"#include "bridgit.h"/*---------------------------------------------------------------------- Preprocessor Definitions----------------------------------------------------------------------*//* --- extensions --- */#define MINXEXT 115 /* minimal x-extension of window */#define MINYEXT 83 /* minimal y-extension of window */#define PTDIST 32 /* distance between support points */#define PTOFFSET 8 /* offset for support points */#define PTSIZE 6 /* size of support points */#define MINSIZE 2 /* minimal size of the board */#define MAXSIZE 32 /* maximal size of the board *//* --- error codes --- */#define OK 0 /* no error */#define E_NONE 0 /* no error */#define E_NOMEM (-1) /* not enough memory */#define E_FOPEN (-2) /* cannot open file */#define E_FREAD (-3) /* read error on file */#define E_FWRITE (-4) /* write error on file */#define E_INIT (-5) /* initialization failed */#define E_WIDGET (-6) /* widget creation failed */#define E_DIALOG (-7) /* cannot create dialog box */#define E_COLOR (-8) /* color allocation failed */#define E_FONT (-9) /* font loading failed */#define E_GRAPH (-10) /* cannot create graphics context */#define E_UNKNOWN (-11) /* unkown error */#define FMT_MAX 256 /* max. len. of error message format *//* --- texts --- */#define BLUEWIN "Blue wins!" /* messages for notifying */#define REDWIN "Red wins!" /* that the game has ended *//*---------------------------------------------------------------------- Type Definitions----------------------------------------------------------------------*/typedef struct { /* --- error message data --- */ const char *name, *type; /* name and type of the error message */ const char *class; /* resource class of error message */ const char *dflt; /* default message */ int argc; /* number of arguments */} ERRMSG; /* (error message) *//*---------------------------------------------------------------------- Function Prototypes----------------------------------------------------------------------*/static void mouse (Widget w, XEvent *e, String *s, Cardinal *c);static void load (Widget w, XEvent *e, String *s, Cardinal *c);static void save (Widget w, XEvent *e, String *s, Cardinal *c);static void quit (Widget w, XEvent *e, String *s, Cardinal *c);static void start (Widget w, XEvent *e, String *s, Cardinal *c);static void flip (Widget w, XEvent *e, String *s, Cardinal *c);static void undo (Widget w, XEvent *e, String *s, Cardinal *c);static void redo (Widget w, XEvent *e, String *s, Cardinal *c);static void hint (Widget w, XEvent *e, String *s, Cardinal *c);static void setup (Widget w, XEvent *e, String *s, Cardinal *c);static void redraw (Widget w, XEvent *e, String *s, Cardinal *c);static void about (Widget w, XEvent *e, String *s, Cardinal *c);/*---------------------------------------------------------------------- Constants----------------------------------------------------------------------*/#include "xbi.rsc" /* fallback resources */static XtActionsRec actions[] = { { "mouse", mouse }, /* mouse button pressed */ { "load", load }, /* File > Load File... */ { "save", save }, /* File > Save File... */ { "quit", quit }, /* File > Quit */ { "redraw", redraw }, /* Actions > Redraw */ { "start", start }, /* Actions > New Game */ { "flip", flip }, /* Actions > Flip Sides */ { "undo", undo }, /* Actions > Undo Move */ { "redo", redo }, /* Actions > Redo Move */ { "hint", hint }, /* Actions > Give a Hint */ { "setup", setup }, /* Actions > Setup... */ { "about", about }, /* Help > About Bridg-It... */ { "db_close", db_close } }; /* close dialog box *//* --- error messages --- */static const ERRMSG errmsgs[] = { /* error message data */ /* E_NONE 0 */ { "error", "none", "Xbi.Error", "no error", 0 }, /* E_NOMEM -1 */ { "error", "nomem", "Xbi.Error", "not enough memory", 0 }, /* E_FOPEN -2 */ { "warning", "fopen", "Xbi.Warning", "cannot open file:\n%s", 1 }, /* E_FREAD -3 */ { "warning", "fread", "Xbi.Warning", "read error on file:\n%s", 1 }, /* E_FWRITE -4 */ { "warning", "fwrite", "Xbi.Warning", "write error on file:\n%s", 1 }, /* E_INIT -5 */ { "error", "init", "Xbi.Error", "initialization failed", 0 }, /* E_WIDGET -6 */ { "error", "widget", "Xbi.Error", "widget creation failed", 0 }, /* E_DIALOG -7 */ { "warning", "dialog", "Xbi.Warning", "cannot create dialog box", 0 }, /* E_COLOR -8 */ { "error", "color", "Xbi.Error", "color allocation failed", 0 }, /* E_FONT -9 */ { "error", "font", "Xbi.Error", "font loading failed", 0 }, /* E_GRAPH -10 */ { "error", "gc", "Xbi.Error", "cannot create graphics context", 0 }, /* E_UNKNOWN -11 */ { "error", "unknown", "Xbi.Error", "unknown error", 0 } };/*---------------------------------------------------------------------- Global Variables----------------------------------------------------------------------*/static XtAppContext appctx; /* X11 application context */static Display *display; /* display connected to */static int screen; /* screen of the display */static Window window; /* main window */static Atom wm_delwin; /* delete window atom of window mgr. */static char fmt[FMT_MAX]; /* format and buffer for messages */static char buf[PATH_MAX +FMT_MAX];/* --- main widgets and menu --- */static Widget w_top; /* top level widget */static Widget w_main; /* main window widget */static Widget w_view; /* viewport widget */static Widget w_board; /* bridg-it board widget */static MENU *menu; /* pull down menu */static GC gc; /* graphics context for maze *//* --- property sheet --- */static PSHEET *psh = NULL; /* property sheet *//* --- colors --- */static unsigned long white; /* background color */static unsigned long black; /* foreground color */static unsigned long red; /* color horizontal player */static unsigned long blue; /* color vertical player */static unsigned long dred; /* color for panel for horz. player */static unsigned long dblue; /* color for panel for vert. player */static unsigned long green; /* color for highlighting *//* --- game variables --- */static BRIDGIT *bi = NULL; /* bridg-it game */static struct { /* bridg-it parameters */ int size; /* board size (number of rows/cols.) */ int first; /* player to move first */ int next; /* player to move next */ int hint; /* hint for next move */} bip = { 5, BI_HORZ, BI_HORZ, BI_NONE };/*---------------------------------------------------------------------- Random Number Functions----------------------------------------------------------------------*/#ifdef DRAND48 /* if library for drand48() available */extern void srand48 (long seed);extern double drand48 (void); /* use drand48 functions */#define dseed(s) srand48((long)(s))#define drand drand48#else /* if only standard rand() available */#define dseed(s) srand((unsigned)(s))static double drand (void){ return rand()/(RAND_MAX +1.0); }#endif/*---------------------------------------------------------------------- Auxiliary Functions----------------------------------------------------------------------*/static void dcb_nop (Widget widget, XtPointer client, XtPointer call){ /* --- close dialog box (callback) */ while (XtClass(widget) != transientShellWidgetClass) widget = XtParent(widget); /* find the shell widget */ XtPopdown(widget); /* close dialog box */ mn_enable(menu, MN_MENU, 1); /* and enable menu bar */} /* dcb_nop() *//*--------------------------------------------------------------------*/static void error (int code, ...){ /* --- print error message */ va_list va; /* list of variable arguments */ const ERRMSG *msg; /* error message */ String argv[8]; /* variable argument vector */ Cardinal argc; /* number of arguments */ if ((code > 0) || (code < E_UNKNOWN)) code = E_UNKNOWN; /* check error code */ msg = errmsgs -code; /* get error message data and */ va_start(va, code); /* list of variable arguments */ for (argc = 0; (int)argc < msg->argc; argc++) argv[argc] = va_arg(va, String); va_end(va); /* get message arguments */ if (strcmp(msg->name, "error") == 0) /* if name is "error" */ XtAppErrorMsg(appctx, msg->name, msg->type, msg->class, msg->dflt, argv, &argc); /* print error message */ else { /* if name is "warning" */ XtAppGetErrorDatabaseText(appctx, msg->name, msg->type, msg->class, msg->dflt, fmt, FMT_MAX, NULL); va_start(va, code); /* list of variable arguments */ vsprintf(buf, fmt, va); /* get and format message */ va_end(va); /* end variable argument evaluation */ mn_enable(menu, MN_MENU,0); /* disable menu bar and show dialog */ if (db_alert(w_top, dcb_nop, buf) != 0) { mn_enable(menu, MN_MENU, 1); /* enable menu bar */ XtAppWarningMsg(appctx, msg->name, msg->type, msg->class, msg->dflt, argv, &argc);/* if the alert dialog box */ } /* cannot be shown, */ } /* print a warning message */} /* error() *//*--------------------------------------------------------------------*/static void resize (Dimension size){ /* --- resize widgets */ Dimension w, h; /* width and height of top widget */ size = size *PTDIST +2 *PTOFFSET +1; w = mn_minwidth(menu)-2; /* compute board size in pixels and */ h = mn_height(menu) +size; /* height of interior of top wiget */ if (size > w) w = size; /* get minimal width of board widget */ XtVaSetValues(w_top, XtNwidth, w+2, XtNheight, h+2, XtNmaxWidth, w+2, XtNmaxHeight, h+2, NULL); XtVaSetValues(w_board, XtNwidth, w, XtNheight, size, NULL); XtVaSetValues(w_view, XtNwidth, w, XtNheight, size, NULL); mn_resize(menu, w); /* set new widget extensions */} /* resize() *//*--------------------------------------------------------------------*/static void b_draw (int bridge, int hint){ /* --- draw/undraw a bridge */ unsigned long color; /* drawing color */ int x1, y1, x2, y2; /* coordinates of support points */ int a; /* angle for arc */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -