📄 cslib.shar
字号:
X/* Exported entries */XX/* Section 1 -- Basic functions from graphics.h */XX/*X * Function: InitGraphicsX * ----------------------X * The implementation below hides considerable complexity underneathX * the InitXHandler call. If you are trying to modify or maintainX * this implementation, it is important to understand how thatX * function is implemented. For details, see the xhandler.cX * implementation.X */XXvoid InitGraphics(void)X{X if (initialized) {X XMSendCommand(ClearCmd, "");X } else {X initialized = TRUE;X ProtectVariable(stateStack);X ProtectVariable(windowTitle);X ProtectVariable(textFont);X XDSetWindowSize(windowWidth, windowHeight);X XMInitialize(windowTitle);X InitColors();X }X InitGraphicsState();X}XXvoid MovePen(double x, double y)X{X InitCheck();X if (regionState == RegionActive) regionState = PenHasMoved;X cx = x;X cy = y;X}XXvoid DrawLine(double dx, double dy)X{X InitCheck();X switch (regionState) {X case NoRegion: case RegionActive:X break;X case RegionStarting:X regionState = RegionActive;X break;X case PenHasMoved:X Error("Region segments must be contiguous");X }X sprintf(cmdBuffer, "%.12g %.12g %.12g %.12g", cx, cy, dx, dy);X XMSendCommand(LineCmd, cmdBuffer);X cx += dx;X cy += dy;X}XXvoid DrawArc(double r, double start, double sweep)X{X DrawEllipticalArc(r, r, start, sweep);X}XXdouble GetWindowWidth(void)X{X return (windowWidth);X}XXdouble GetWindowHeight(void)X{X return (windowHeight);X}XXdouble GetCurrentX(void)X{X InitCheck();X return (cx);X}XXdouble GetCurrentY(void)X{X InitCheck();X return (cy);X}XX/* Section 2 -- Elliptical arcs */XXvoid DrawEllipticalArc(double rx, double ry,X double start, double sweep)X{X double x, y;XX InitCheck();X switch (regionState) {X case NoRegion: case RegionActive:X break;X case RegionStarting:X regionState = RegionActive;X break;X case PenHasMoved:X Error("Region segments must be contiguous");X }X x = cx + rx * cos(GLRadians(start + 180));X y = cy + ry * sin(GLRadians(start + 180));X sprintf(cmdBuffer, "%.12g %.12g %.12g %.12g %.12g %.12g",X x, y, rx, ry, start, sweep);X XMSendCommand(ArcCmd, cmdBuffer);X cx = x + rx * cos(GLRadians(start + sweep));X cy = y + ry * sin(GLRadians(start + sweep));X}XX/* Section 3 -- Graphical structures */XXvoid StartFilledRegion(double density)X{X InitCheck();X if (regionState != NoRegion) {X Error("Region is already in progress");X }X if (density < 0 || density > 1) {X Error("Density for regions must be between 0 and 1");X }X regionState = RegionStarting;X sprintf(cmdBuffer, "%.12g", density);X XMSendCommand(StartRegionCmd, cmdBuffer);X}XXvoid EndFilledRegion(void)X{X InitCheck();X if (regionState == NoRegion) {X Error("EndFilledRegion without StartFilledRegion");X }X regionState = NoRegion;X XMSendCommand(EndRegionCmd, "");X}XX/* Section 4 -- String functions */XXvoid DrawTextString(string text)X{X InitCheck();X if (regionState != NoRegion) {X Error("Text strings are illegal inside a region");X }X if (strlen(text) > MaxTextString) {X Error("Text string too long");X }X InstallFont();X sprintf(cmdBuffer, "%.12g %.12g %s", cx, cy, text);X XMSendCommand(TextCmd, cmdBuffer);X cx += TextStringWidth(text);X}XXdouble TextStringWidth(string text)X{X double result;XX InitCheck();X if (strlen(text) > MaxTextString) {X Error("Text string too long");X }X InstallFont();X sprintf(cmdBuffer, "%s", text);X XMSendCommand(WidthCmd, cmdBuffer);X XMGetResponse(cmdBuffer);X (void) sscanf(cmdBuffer, "%lg", &result);X return (result);X}XXvoid SetFont(string font)X{X InitCheck();X if (strlen(font) > MaxFontName) Error("Font name too long");X textFont = CopyString(font);X fontChanged = TRUE;X}XXstring GetFont(void)X{X InitCheck();X InstallFont();X return (CopyString(textFont));X}XXvoid SetPointSize(int size)X{X InitCheck();X pointSize = size;X fontChanged = TRUE;X}XXint GetPointSize(void)X{X InitCheck();X InstallFont();X return (pointSize);X}XXvoid SetStyle(int style)X{X InitCheck();X textStyle = style;X fontChanged = TRUE;X}XXint GetStyle(void)X{X InitCheck();X InstallFont();X return (textStyle);X}XXdouble GetFontAscent(void)X{X double ascent;XX InitCheck();X InstallFont();X XMSendCommand(FontMetricsCmd, "");X XMGetResponse(cmdBuffer);X (void) sscanf(cmdBuffer, "%lg", &ascent);X return (ascent);X}XXdouble GetFontDescent(void)X{X double descent;XX InitCheck();X InstallFont();X XMSendCommand(FontMetricsCmd, "");X XMGetResponse(cmdBuffer);X (void) sscanf(cmdBuffer, "%*lg %lg", &descent);X return (descent);X}XXdouble GetFontHeight(void)X{X double height;XX InitCheck();X InstallFont();X XMSendCommand(FontMetricsCmd, "");X XMGetResponse(cmdBuffer);X (void) sscanf(cmdBuffer, "%*lg %*lg %lg", &height);X return (height);X}XX/* Section 5 -- Mouse support */XXdouble GetMouseX(void)X{X double x, y;X int state;XX InitCheck();X XMSendCommand(GetMouseCmd, "");X XMGetResponse(cmdBuffer);X (void) sscanf(cmdBuffer, "%d, %lg, %lg", &state, &x, &y);X return (x);X}XXdouble GetMouseY(void)X{X string line;X double x, y;X int state;XX InitCheck();X XMSendCommand(GetMouseCmd, "");X XMGetResponse(cmdBuffer);X (void) sscanf(cmdBuffer, "%d, %lg, %lg", &state, &x, &y);X return (y);X}XXbool MouseButtonIsDown(void)X{X string line;X double x, y;X int state;XX InitCheck();X XMSendCommand(GetMouseCmd, "");X XMGetResponse(cmdBuffer);X (void) sscanf(cmdBuffer, "%d, %lg, %lg", &state, &x, &y);X return (state != 0);X}XXvoid WaitForMouseDown(void)X{X InitCheck();X XMSendCommand(WaitForMouseCmd, "D");X XMGetResponse(cmdBuffer);X}XXvoid WaitForMouseUp(void)X{X InitCheck();X XMSendCommand(WaitForMouseCmd, "U");X XMGetResponse(cmdBuffer);X}XX/* Section 6 -- Color support */XXbool HasColor(void)X{X InitCheck();X return (colorOK);X}XXvoid SetPenColor(string color)X{X int cindex;XX InitCheck();X cindex = FindColorName(color);X if (cindex == -1) Error("Undefined color: %s", color);X penColor = cindex;X if (penColor == lastColor) return;X lastColor = penColor;X if (HasColor()) {X sprintf(cmdBuffer, "%g %g %g\n",X colorTable[cindex].red,X colorTable[cindex].green,X colorTable[cindex].blue);X XMSendCommand(SetColorCmd, cmdBuffer);X } else {X SetEraseMode(eraseMode);X }X}XXstring GetPenColor(void)X{X InitCheck();X return (colorTable[penColor].name);X}XXvoid DefineColor(string name,X double red, double green, double blue)X{X int cindex;XX InitCheck();X if (red < 0 || red > 1 || green < 0 || green > 1 || blue < 0 || blue > 1) {X Error("DefineColor: All color intensities must be between 0 and 1");X }X cindex = FindColorName(name);X if (cindex == -1) {X if (nColors == MaxColors) Error("DefineColor: Too many colors");X cindex = nColors++;X }X colorTable[cindex].name = CopyString(name);X colorTable[cindex].red = red;X colorTable[cindex].green = green;X colorTable[cindex].blue = blue;X}XX/* Section 7 -- Miscellaneous functions */XXvoid SetEraseMode(bool mode)X{X InitCheck();X eraseMode = mode;X sprintf(cmdBuffer, "%d", (int) (mode || ShouldBeWhite()));X XMSendCommand(SetEraseCmd, cmdBuffer);X}XXbool GetEraseMode(void)X{X InitCheck();X return (eraseMode);X}XXvoid SetWindowTitle(string title)X{X windowTitle = CopyString(title);X if (initialized) {X sprintf(cmdBuffer, "%s", windowTitle);X XMSendCommand(SetTitleCmd, cmdBuffer);X }X}XXstring GetWindowTitle(void)X{X return (CopyString(windowTitle));X}XXvoid UpdateDisplay(void)X{X int cnt;XX InitCheck();X XMSendCommand(UpdateCmd, "");X}XXvoid Pause(double seconds)X{X if (initialized) UpdateDisplay();X USleep((unsigned) (seconds * 1000000));X}XXvoid ExitGraphics(void)X{X XMSendCommand(ExitGraphicsCmd, "");X exit(0);X}XXvoid SaveGraphicsState(void)X{X graphicsStateT sb;XX InitCheck();X sb = New(graphicsStateT);X sb->cx = cx;X sb->cy = cy;X sb->font = textFont;X sb->size = pointSize;X sb->style = textStyle;X sb->erase = eraseMode;X sb->color = penColor;X sb->link = stateStack;X stateStack = sb;X}XXvoid RestoreGraphicsState(void)X{X graphicsStateT sb;XX InitCheck();X if (stateStack == NULL) {X Error("RestoreGraphicsState called before SaveGraphicsState");X }X sb = stateStack;X cx = sb->cx;X cy = sb->cy;X textFont = sb->font;X pointSize = sb->size;X textStyle = sb->style;X eraseMode = sb->erase;X penColor = sb->color;X stateStack = sb->link;X FreeBlock(sb);X fontChanged = TRUE;X SetEraseMode(eraseMode);X SetPenColor(colorTable[penColor].name);X}XXdouble GetFullScreenWidth(void)X{X double screenWidth, screenHeight;XX XDG
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -