📄 graphics.c
字号:
/* * graphics.c - rudimentary X-windows graphics code for visualization * * This file is used in conjunction with the SimpleScalar tool suite * originally written by Todd M. Austin for the Multiscalar Research Project * at the University of Wisconsin-Madison. * * The file was created by Naraig Manjikian at Queen's University, * Kingston, Ontario, Canada. * * Copyright (C) 2000 by Naraig Manjikian * * This source file is distributed "as is" in the hope that it will be * useful. The tool set comes with no warranty, and no author or * distributor accepts any responsibility for the consequences of its * use. * * Everyone is granted permission to copy, modify and redistribute * this tool set under the following conditions: * * This source code is distributed for non-commercial use only. * Please contact the maintainer for restrictions applying to * commercial use. * * Permission is granted to anyone to make or distribute copies * of this source code, either as received or modified, in any * medium, provided that all copyright notices, permission and * nonwarranty notices are preserved, and that the distributor * grants the recipient permission for further redistribution as * permitted by this document. * * Permission is granted to distribute this file in compiled * or executable form under the same conditions that apply for * source code, provided that either: * * A. it is accompanied by the corresponding machine-readable * source code, * B. it is accompanied by a written offer, with no time limit, * to give anyone a machine-readable copy of the corresponding * source code in return for reimbursement of the cost of * distribution. This written offer must permit verbatim * duplication by anyone, or * C. it is distributed by someone who received only the * executable form, and is accompanied by a copy of the * written offer of source code that they received concurrently. * * In other words, you are welcome to use, share and improve this * source file. You are forbidden to forbid anyone else to use, share * and improve what you give them. */#include <stdio.h>#include <X11/Xlib.h>#include <X11/Xutil.h>#include "graphics.h"/* variables for X windows */static Display *display;static Window window;static GC gc;static XFontStruct *xfont;static char *xfontname = "fixed";static char *color_names[NUM_COLORS] ={ "white", "black", "blue", "green", "yellow", "cyan", "red"};static unsigned long color_pixels[NUM_COLORS];/*------------------------------------------------------------------*/void CreateWindow (int req_width, int req_height){ unsigned long fg, bg, bd; /* Pixel values */ unsigned long bw; /* Border width */ XColor temp_color; /* Temporary color */ Colormap colormap; /* Color map to use */ XGCValues gcvalues; /* Struct for creating GC */ XEvent event; /* Event received */ int i; display = XOpenDisplay ((char *) NULL); /* open default display */ if (display == NULL) { fprintf (stderr, "can't get display '%s'\n", XDisplayName(NULL)); exit (1); } if ((xfont = XLoadQueryFont (display, xfontname)) == NULL) { fprintf (stderr, "display '%s' doesn't know font '%s'\n", DisplayString(display), xfontname); exit(1); } colormap = DefaultColormap (display, DefaultScreen(display)); bd = BlackPixel (display, DefaultScreen(display)); bg = WhitePixel (display, DefaultScreen(display)); fg = BlackPixel (display, DefaultScreen(display)); bw = 1; window = XCreateSimpleWindow (display, DefaultRootWindow(display), /* x */ 0, /* y */ 0, req_width, req_height, bw, bd, bg); XSetStandardProperties(display, window, "sim-mpcache", "sim-mpcache", None, NULL, 0, NULL); gcvalues.font = xfont->fid; gcvalues.foreground = fg; gcvalues.background = bg; gcvalues.line_width = 0; gcvalues.line_style = LineSolid; gc = XCreateGC (display, window, (GCFont | GCForeground | GCBackground | GCLineWidth | GCLineStyle), &gcvalues); /* the only events we care about for this window are exposures and structure notifies (and for the latter, all we really want are unmap events) */ XSelectInput (display, window, (long) ExposureMask | StructureNotifyMask); XMapWindow (display, window); /* consume X events until we get the 'last' expose event from mapping the window to the screen */ while (1) { XNextEvent(display, &event); if (event.type == Expose && event.xexpose.count == 0) { while (XCheckTypedEvent(display, Expose, &event)) ; XClearWindow(display, window); gcvalues.graphics_exposures = False; XChangeGC(display, gc, (GCGraphicsExposures), &gcvalues); XSync(display, 1); break; } } /* get pixel values for colors to be used in graphics display */ for (i = 0;i < NUM_COLORS; i++) { if (XParseColor (display, colormap, color_names[i], &temp_color) == 0) { fprintf (stderr, "color name %s not in database", color_names[i]); exit (1); } else { if (XAllocColor (display, colormap, &temp_color) == 0) { fprintf (stderr, "couldn't allocate color %s\n", color_names[i]); } else { color_pixels[i] = temp_color.pixel; } } }}void SetPoint (x, y) int x, y;{ XDrawPoint (display, window, gc, x, y);}void SetColor (enum colors color_id){ XGCValues gcvalues; gcvalues.foreground = color_pixels[color_id]; XChangeGC(display, gc, GCForeground, &gcvalues);}void DrawLine (x1, y1, x2, y2) int x1, y1, x2, y2;{ XDrawLine (display, window, gc, x1, y1, x2, y2);}void DrawRectangle (x, y, width, height) int x, y, width, height;{ XDrawRectangle (display, window, gc, x, y, width, height);}void DrawFilledRectangle (x, y, width, height) int x, y, width, height;{ int i; for (i = y; i < y + height; i++) XDrawLine (display, window, gc, x, i, x+width-1, i);}void FlushWindow (void){ XFlush (display);}void ClearWindow (void){ XClearWindow (display, window); FlushWindow ();}void ClearWindowArea (int x, int y, int width, int height){ XClearArea (display, window, x, y, width, height, (Bool) 0);}void DrawString (char *str, int x, int y){ int len = strlen (str); x -= XTextWidth (xfont, str, len) / 2; /* center text */ XDrawString (display, window, gc, x, y, str, len);}void DrawLeftString (char *str, int x, int y){ int len = strlen (str); XDrawString (display, window, gc, x, y, str, len);}void DrawRightString (char *str, int x, int y){ int len = strlen (str); x -= XTextWidth (xfont, str, len); XDrawString (display, window, gc, x, y, str, len);}int ExposureCheck (void){ XEvent event; return XCheckTypedEvent(display, Expose, &event);}void ConsumeExposures (void){ XEvent event; while (XCheckTypedEvent(display, Expose, &event)) ;}void ConsumeMapEvents (void){ XEvent event; while (XCheckTypedEvent(display, MapNotify, &event)) ;}int UnmapCheck (void){ XEvent event; return XCheckTypedEvent(display, UnmapNotify, &event);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -