⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graphics.c

📁 c语言开发方面的经典问题,包括源代码.c语言开发所要注意的问题,以及在嵌入式等各方面的应用
💻 C
📖 第 1 页 / 共 2 页
字号:
    InitCheck();    InstallFont();    XMSendCommand(FontMetricsCmd, "");    XMGetResponse(cmdBuffer);    (void) sscanf(cmdBuffer, "%*lg %*lg %lg", &height);    return (height);}/* Section 5 -- Mouse support */double GetMouseX(void){    double x, y;    int state;    InitCheck();    XMSendCommand(GetMouseCmd, "");    XMGetResponse(cmdBuffer);    (void) sscanf(cmdBuffer, "%d, %lg, %lg", &state, &x, &y);    return (x);}double GetMouseY(void){    string line;    double x, y;    int state;    InitCheck();    XMSendCommand(GetMouseCmd, "");    XMGetResponse(cmdBuffer);    (void) sscanf(cmdBuffer, "%d, %lg, %lg", &state, &x, &y);    return (y);}bool MouseButtonIsDown(void){    string line;    double x, y;    int state;    InitCheck();    XMSendCommand(GetMouseCmd, "");    XMGetResponse(cmdBuffer);    (void) sscanf(cmdBuffer, "%d, %lg, %lg", &state, &x, &y);    return (state != 0);}void WaitForMouseDown(void){    InitCheck();    XMSendCommand(WaitForMouseCmd, "D");    XMGetResponse(cmdBuffer);}void WaitForMouseUp(void){    InitCheck();    XMSendCommand(WaitForMouseCmd, "U");    XMGetResponse(cmdBuffer);}/* Section 6 -- Color support */bool HasColor(void){    InitCheck();    return (colorOK);}void SetPenColor(string color){    int cindex;    InitCheck();    cindex = FindColorName(color);    if (cindex == -1) Error("Undefined color: %s", color);    penColor = cindex;    if (penColor == lastColor) return;    lastColor = penColor;    if (HasColor()) {        sprintf(cmdBuffer, "%g %g %g\n",                colorTable[cindex].red,                colorTable[cindex].green,                colorTable[cindex].blue);        XMSendCommand(SetColorCmd, cmdBuffer);    } else {        SetEraseMode(eraseMode);    }}string GetPenColor(void){    InitCheck();    return (colorTable[penColor].name);}void DefineColor(string name,                 double red, double green, double blue){    int cindex;    InitCheck();    if (red < 0 || red > 1 || green < 0 || green > 1 || blue < 0 || blue > 1) {        Error("DefineColor: All color intensities must be between 0 and 1");    }    cindex = FindColorName(name);    if (cindex == -1) {        if (nColors == MaxColors) Error("DefineColor: Too many colors");        cindex = nColors++;    }    colorTable[cindex].name = CopyString(name);    colorTable[cindex].red = red;    colorTable[cindex].green = green;    colorTable[cindex].blue = blue;}/* Section 7 -- Miscellaneous functions */void SetEraseMode(bool mode){    InitCheck();    eraseMode = mode;    sprintf(cmdBuffer, "%d", (int) (mode || ShouldBeWhite()));    XMSendCommand(SetEraseCmd, cmdBuffer);}bool GetEraseMode(void){    InitCheck();    return (eraseMode);}void SetWindowTitle(string title){    windowTitle = CopyString(title);    if (initialized) {        sprintf(cmdBuffer, "%s", windowTitle);        XMSendCommand(SetTitleCmd, cmdBuffer);    }}string GetWindowTitle(void){    return (CopyString(windowTitle));}void UpdateDisplay(void){    int cnt;    InitCheck();    XMSendCommand(UpdateCmd, "");}void Pause(double seconds){    if (initialized) UpdateDisplay();    USleep((unsigned) (seconds * 1000000));}void ExitGraphics(void){    XMSendCommand(ExitGraphicsCmd, "");    exit(0);}void SaveGraphicsState(void){    graphicsStateT sb;    InitCheck();    sb = New(graphicsStateT);    sb->cx = cx;    sb->cy = cy;    sb->font = textFont;    sb->size = pointSize;    sb->style = textStyle;    sb->erase = eraseMode;    sb->color = penColor;    sb->link = stateStack;    stateStack = sb;}void RestoreGraphicsState(void){    graphicsStateT sb;    InitCheck();    if (stateStack == NULL) {        Error("RestoreGraphicsState called before SaveGraphicsState");    }    sb = stateStack;    cx = sb->cx;    cy = sb->cy;    textFont = sb->font;    pointSize = sb->size;    textStyle = sb->style;    eraseMode = sb->erase;    penColor = sb->color;    stateStack = sb->link;    FreeBlock(sb);    fontChanged = TRUE;    SetEraseMode(eraseMode);    SetPenColor(colorTable[penColor].name);}double GetFullScreenWidth(void){    double screenWidth, screenHeight;    XDGetScreenSize(&screenWidth, &screenHeight);    return (screenWidth);}double GetFullScreenHeight(void){    double screenWidth, screenHeight;    XDGetScreenSize(&screenWidth, &screenHeight);    return (screenHeight);}void SetWindowSize(double width, double height){    if (initialized) {        Error("The window size cannot be set after calling InitGraphics");    }    windowWidth = width;    windowHeight = height;}double GetXResolution(void){    double xdpi, ydpi;    XDGetResolution(&xdpi, &ydpi);    return (ydpi);}double GetYResolution(void){    double xdpi, ydpi;    XDGetResolution(&xdpi, &ydpi);    return (ydpi);}/* Private functions *//* * Function: InitCheck * Usage: InitCheck(); * ------------------- * This function merely ensures that the package has been * initialized before the client functions are called. */static void InitCheck(void){    if (!initialized) Error("InitGraphics has not been called");}/* * Function: InitGraphicsState * Usage: InitGraphicsState(); * --------------------------- * This function initializes the graphics state elements to * their default values. */static void InitGraphicsState(void){    cx = cy = 0;    eraseMode = FALSE;    textFont = "Default";    pointSize = DefaultSize;    textStyle = Normal;    stateStack = NULL;    regionState = NoRegion;    fontChanged = TRUE;    SetPenColor("Black");}static void InstallFont(void){    char fontbuf[MaxFontName];    string line;    if (!fontChanged) return;    sprintf(cmdBuffer, "%d %d %s", pointSize, textStyle, textFont);    XMSendCommand(SetFontCmd, cmdBuffer);    XMGetResponse(cmdBuffer);    (void) sscanf(cmdBuffer, "%d %d %s", &pointSize, &textStyle, fontbuf);    textFont = CopyString(fontbuf);    fontChanged = FALSE;}/* * Function: InitColors * Usage: InitColors(); * -------------------- * This function defines the built-in colors. */static void InitColors(void){    colorOK = (XDGetNColors() >= MinColors);    lastColor = -1;    nColors = 0;    DefineColor("Black", 0, 0, 0);    DefineColor("Dark Gray", .35, .35, .35);    DefineColor("Gray", .6, .6, .6);    DefineColor("Light Gray", .75, .75, .75);    DefineColor("White", 1, 1, 1);    DefineColor("Red", 1, 0, 0);    DefineColor("Yellow", 1, 1, 0);    DefineColor("Green", 0, 1, 0);    DefineColor("Cyan", 0, 1, 1);    DefineColor("Blue", 0, 0, 1);    DefineColor("Magenta", 1, 0, 1);}/* * Function: FindColorName * Usage: index = FindColorName(name); * ----------------------------------- * This function returns the index of the named color in the * color table, or -1 if the color does not exist. */static int FindColorName(string name){    int i;    for (i = 0; i < nColors; i++) {        if (StringMatch(name, colorTable[i].name)) return (i);    }    return (-1);}static bool ShouldBeWhite(void){    if (colorTable[penColor].red < .9) return (FALSE);    if (colorTable[penColor].blue < .9) return (FALSE);    if (colorTable[penColor].green < .9) return (FALSE);    return (TRUE);}/* * Function: StringMatch * Usage: if (StringMatch(s1, s2)) . . . * ------------------------------------- * This function returns TRUE if two strings are equal, ignoring * case distinctions. */static bool StringMatch(string s1, string s2){    register char *cp1, *cp2;    cp1 = s1;    cp2 = s2;    while (tolower(*cp1) == tolower(*cp2)) {        if (*cp1 == '\0') return (TRUE);        cp1++;        cp2++;    }    return (FALSE);}/* * Function: USleep * Usage: USleep(useconds); * ------------------------ * This function sleeps for the indicated number of microseconds. * Some versions of Unix implement a usleep call, but it does not * appear to be standard.  The easiest way to implement it is by * calling select with no descriptors, since the compatibility * library has already made sure that select is available. */static void USleep(unsigned useconds){    struct timeval tv;    tv.tv_sec = useconds / 1000000;    tv.tv_usec = useconds % 1000000;    (void) select(1, NULL, NULL, NULL, &tv);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -