📄 graphics.c
字号:
savefontsize = currentfontsize; setfontsize (menu_font_size - 2); /* Smaller OK on paper */ ylow = ps_bot - 8.; fprintf(ps,"(%s) %.2f %.2f censhow\n",message,(ps_left+ps_right)/2., ylow); setcolor (savecolor); setfontsize (savefontsize); }}void update_message (char *msg) {/* Changes the message to be displayed on screen. */ strncpy (message, msg, BUFSIZE); draw_message ();}static void zoom_in (void (*drawscreen) (void)) {/* Zooms in by a factor of 1.666. */ float xdiff, ydiff; xdiff = xright - xleft; ydiff = ybot - ytop; xleft += xdiff/5.; xright -= xdiff/5.; ytop += ydiff/5.; ybot -= ydiff/5.; update_transform (); drawscreen();}static void zoom_out (void (*drawscreen) (void)) {/* Zooms out by a factor of 1.666. */ float xdiff, ydiff; xdiff = xright - xleft; ydiff = ybot - ytop; xleft -= xdiff/3.; xright += xdiff/3.; ytop -= ydiff/3.; ybot += ydiff/3.; update_transform (); drawscreen();}static void zoom_fit (void (*drawscreen) (void)) {/* Sets the view back to the initial view set by init_world (i.e. a full * * view) of all the graphics. */ xleft = saved_xleft; xright = saved_xright; ytop = saved_ytop; ybot = saved_ybot; update_transform (); drawscreen();}static void translate_up (void (*drawscreen) (void)) {/* Moves view 1/2 screen up. */ float ystep; ystep = (ybot - ytop)/2.; ytop -= ystep; ybot -= ystep; update_transform(); drawscreen();}static void translate_down (void (*drawscreen) (void)) {/* Moves view 1/2 screen down. */ float ystep; ystep = (ybot - ytop)/2.; ytop += ystep; ybot += ystep; update_transform(); drawscreen();}static void translate_left (void (*drawscreen) (void)) {/* Moves view 1/2 screen left. */ float xstep; xstep = (xright - xleft)/2.; xleft -= xstep; xright -= xstep; update_transform(); drawscreen();}static void translate_right (void (*drawscreen) (void)) {/* Moves view 1/2 screen right. */ float xstep; xstep = (xright - xleft)/2.; xleft += xstep; xright += xstep; update_transform(); drawscreen();}static void update_win (int x[2], int y[2], void (*drawscreen)(void)) { float x1, x2, y1, y2; x[0] = min(x[0],top_width-MWIDTH); /* Can't go under menu */ x[1] = min(x[1],top_width-MWIDTH); y[0] = min(y[0],top_height-T_AREA_HEIGHT); /* Can't go under text area */ y[1] = min(y[1],top_height-T_AREA_HEIGHT); if ((x[0] == x[1]) || (y[0] == y[1])) { printf("Illegal (zero area) window. Window unchanged.\n"); return; } x1 = XTOWORLD(min(x[0],x[1])); x2 = XTOWORLD(max(x[0],x[1])); y1 = YTOWORLD(min(y[0],y[1])); y2 = YTOWORLD(max(y[0],y[1])); xleft = x1; xright = x2; ytop = y1; ybot = y2; update_transform(); drawscreen();}static void adjustwin (void (*drawscreen) (void)) { /* The window button was pressed. Let the user click on the two * * diagonally opposed corners, and zoom in on this area. */ XEvent report; int corner, xold, yold, x[2], y[2]; corner = 0; xold = -1; yold = -1; /* Don't need to init yold, but stops compiler warning. */ while (corner<2) { XNextEvent (display, &report); switch (report.type) { case Expose:#ifdef VERBOSE printf("Got an expose event.\n"); printf("Count is: %d.\n",report.xexpose.count); printf("Window ID is: %d.\n",report.xexpose.window);#endif if (report.xexpose.count != 0) break; if (report.xexpose.window == menu) drawmenu(); else if (report.xexpose.window == toplevel) { drawscreen(); xold = -1; /* No rubber band on screen */ } else if (report.xexpose.window == textarea) draw_message(); break; case ConfigureNotify: top_width = report.xconfigure.width; top_height = report.xconfigure.height; update_transform();#ifdef VERBOSE printf("Got a ConfigureNotify.\n"); printf("New width: %d New height: %d.\n",top_width,top_height);#endif break; case ButtonPress:#ifdef VERBOSE printf("Got a buttonpress.\n"); printf("Window ID is: %d.\n",report.xbutton.window); printf("Location (%d, %d).\n", report.xbutton.x, report.xbutton.y);#endif if (report.xbutton.window != toplevel) break; x[corner] = report.xbutton.x; y[corner] = report.xbutton.y; if (corner == 0) { XSelectInput (display, toplevel, ExposureMask | StructureNotifyMask | ButtonPressMask | PointerMotionMask); } else { update_win(x,y,drawscreen); } corner++; break; case MotionNotify:#ifdef VERBOSE printf("Got a MotionNotify Event.\n"); printf("x: %d y: %d\n",report.xmotion.x,report.xmotion.y);#endif if (xold >= 0) { /* xold set -ve before we draw first box */ XDrawRectangle(display,toplevel,gcxor,min(x[0],xold), min(y[0],yold),abs(x[0]-xold),abs(y[0]-yold)); } /* Don't allow user to window under menu region */ xold = min(report.xmotion.x,top_width-1-MWIDTH); yold = report.xmotion.y; XDrawRectangle(display,toplevel,gcxor,min(x[0],xold), min(y[0],yold),abs(x[0]-xold),abs(y[0]-yold)); break; } } XSelectInput (display, toplevel, ExposureMask | StructureNotifyMask | ButtonPressMask); }static void postscript (void (*drawscreen) (void)) {/* Takes a snapshot of the screen and stores it in pic?.ps. The * * first picture goes in pic1.ps, the second in pic2.ps, etc. */ static int piccount = 1; int success; char fname[20]; sprintf(fname,"pic%d.ps",piccount); success = init_postscript (fname); if (success == 0) return; /* Couldn't open file, abort. */ drawscreen(); close_postscript (); piccount++;}static void proceed (void (*drawscreen) (void)) { /* Dummy routine. Just exit the event loop. */}static void quit (void (*drawscreen) (void)) { close_graphics(); exit(0);}void close_graphics (void) {/* Release all my drawing structures (through the X server) and * * close down the connection. */ int i; for (i=1;i<=MAX_FONT_SIZE;i++) if (font_is_loaded[i]) XFreeFont(display,font_info[i]); XFreeGC(display,gc); XFreeGC(display,gcxor); XFreeGC(display,gc_menus); if (private_cmap != None) XFreeColormap (display, private_cmap); XCloseDisplay(display); free (button);}int init_postscript (char *fname) {/* Opens a file for PostScript output. The header information, * * clipping path, etc. are all dumped out. If the file could * * not be opened, the routine returns 0; otherwise it returns 1. */ ps = fopen (fname,"w"); if (ps == NULL) { printf("Error: could not open %s for PostScript output.\n",fname); printf("Drawing to screen instead.\n"); return (0); } disp_type = POSTSCRIPT; /* Graphics go to postscript file now. *//* Header for minimal conformance with the Adobe structuring convention */ fprintf(ps,"%%!PS-Adobe-1.0\n"); fprintf(ps,"%%%%DocumentFonts: Helvetica\n"); fprintf(ps,"%%%%Pages: 1\n");/* Set up postscript transformation macros and page boundaries */ update_ps_transform();/* Bottom margin is at ps_bot - 15. to leave room for the on-screen message. */ fprintf(ps,"%%%%BoundingBox: %.2f %.2f %.2f %.2f\n", ps_left, ps_bot - 15., ps_right, ps_top); fprintf(ps,"%%%%EndComments\n"); fprintf(ps,"/censhow %%draw a centered string\n"); fprintf(ps," { moveto %% move to proper spot\n"); fprintf(ps," dup stringwidth pop %% get x length of string\n"); fprintf(ps," -2 div %% Proper left start\n"); fprintf(ps," yoff rmoveto %% Move left that much and down half font height\n"); fprintf(ps," show newpath } def %% show the string\n\n"); fprintf(ps,"/setfontsize %% set font to desired size and compute " "centering yoff\n"); fprintf(ps," { /Helvetica findfont\n"); fprintf(ps," exch scalefont\n"); fprintf(ps," setfont %% Font size set ...\n\n"); fprintf(ps," 0 0 moveto %% Get vertical centering offset\n"); fprintf(ps," (Xg) true charpath\n"); fprintf(ps," flattenpath pathbbox\n"); fprintf(ps," /ascent exch def pop -1 mul /descent exch def pop\n"); fprintf(ps," newpath\n"); fprintf(ps," descent ascent sub 2 div /yoff exch def } def\n\n"); fprintf(ps,"%% Next two lines for debugging only.\n"); fprintf(ps,"/str 20 string def\n"); fprintf(ps,"/pnum {str cvs print ( ) print} def\n"); fprintf(ps,"/drawline %% draw a line from (x2,y2) to (x1,y1)\n"); fprintf(ps," { moveto lineto stroke } def\n\n"); fprintf(ps,"/rect %% outline a rectangle \n"); fprintf(ps," { /y2 exch def /x2 exch def /y1 exch def /x1 exch def\n"); fprintf(ps," x1 y1 moveto\n"); fprintf(ps," x2 y1 lineto\n"); fprintf(ps," x2 y2 lineto\n"); fprintf(ps," x1 y2 lineto\n"); fprintf(ps," closepath } def\n\n"); fprintf(ps,"/drawrect %% draw outline of a rectanagle\n"); fprintf(ps," { rect stroke } def\n\n"); fprintf(ps,"/fillrect %% fill in a rectanagle\n"); fprintf(ps," { rect fill } def\n\n"); fprintf (ps,"/drawarc { arc stroke } def %% draw an arc\n"); fprintf (ps,"/drawarcn { arcn stroke } def " " %% draw an arc in the opposite direction\n\n"); fprintf (ps,"%%Fill a counterclockwise or clockwise arc sector, " "respectively.\n"); fprintf (ps,"/fillarc { moveto currentpoint 5 2 roll arc closepath fill } " "def\n"); fprintf (ps,"/fillarcn { moveto currentpoint 5 2 roll arcn closepath fill } " "def\n\n"); fprintf (ps,"/fillpoly { 3 1 roll moveto %% move to first point\n" " 2 exch 1 exch {pop lineto} for %% line to all other points\n" " closepath fill } def\n\n"); fprintf(ps,"%%Color Definitions:\n"); fprintf(ps,"/white { 1 setgray } def\n"); fprintf(ps,"/black { 0 setgray } def\n"); fprintf(ps,"/grey55 { .55 setgray } def\n"); fprintf(ps,"/grey75 { .75 setgray } def\n"); fprintf(ps,"/blue { 0 0 1 setrgbcolor } def\n"); fprintf(ps,"/green { 0 1 0 setrgbcolor } def\n"); fprintf(ps,"/yellow { 1 1 0 setrgbcolor } def\n"); fprintf(ps,"/cyan { 0 1 1 setrgbcolor } def\n"); fprintf(ps,"/red { 1 0 0 setrgbcolor } def\n"); fprintf(ps,"/darkgreen { 0 0.5 0 setrgbcolor } def\n"); fprintf(ps,"/magenta { 1 0 1 setrgbcolor } def\n"); fprintf(ps,"\n%%Solid and dashed line definitions:\n"); fprintf(ps,"/linesolid {[] 0 setdash} def\n"); fprintf(ps,"/linedashed {[3 3] 0 setdash} def\n"); fprintf(ps,"\n%%%%EndProlog\n"); fprintf(ps,"%%%%Page: 1 1\n\n");/* Set up PostScript graphics state to match current one. */ force_setcolor (currentcolor); force_setlinestyle (currentlinestyle); force_setlinewidth (currentlinewidth); force_setfontsize (currentfontsize); /* Draw this in the bottom margin -- must do before the clippath is set */ draw_message ();/* Set clipping on page. */ fprintf(ps,"%.2f %.2f %.2f %.2f rect ",ps_left, ps_bot,ps_right,ps_top); fprintf(ps,"clip newpath\n\n"); return (1);}void close_postscript (void) {/* Properly ends postscript output and redirects output to screen. */ fprintf(ps,"showpage\n"); fprintf(ps,"\n%%%%Trailer\n"); fclose (ps); disp_type = SCREEN; update_transform(); /* Ensure screen world reflects any changes * * made while printing. *//* Need to make sure that we really set up the graphics context -- * * don't want the change requested to match the current setting and * * do nothing -> force the changes. */ force_setcolor (currentcolor); force_setlinestyle (currentlinestyle); force_setlinewidth (currentlinewidth); force_setfontsize (currentfontsize); }#else /* NO_GRAPHICS build -- rip out graphics */void event_loop (void (*act_on_button) (float x, float y), void (*drawscreen) (void)) { }void init_graphics (char *window_name) { }void close_graphics (void) { }void update_message (char *msg) { }void draw_message (void) { }void init_world (float xl, float yt, float xr, float yb) { }void flushinput (void) { }void setcolor (int cindex) { }void setlinestyle (int linestyle) { }void setlinewidth (int linewidth) { }void setfontsize (int pointsize) { }void drawline (float x1, float y1, float x2, float y2) { }void drawrect (float x1, float y1, float x2, float y2) { }void fillrect (float x1, float y1, float x2, float y2) { }void fillpoly (t_point *points, int npoints) { }void drawarc (float xcen, float ycen, float rad, float startang, float angextent) { }void fillarc (float xcen, float ycen, float rad, float startang, float angextent) { }void drawtext (float xc, float yc, char *text, float boundx) { }void clearscreen (void) { }void create_button (char *prev_button_text , char *button_text, void (*button_func) (void (*drawscreen) (void))) { }void destroy_button (char *button_text) { }int init_postscript (char *fname) { return (1);}void close_postscript (void) { }#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -