This blog is under construction

Sunday 8 September 2013

Implement countdown clock using C graphics

C program to implement countdown clock


  #include <stdio.h>
  #include <conio.h>
  #include <graphics.h>
  #include <string.h>
  #include <dos.h>

  int main() {
        /* request auto detection */
        int gdriver = DETECT, gmode, err;
        int i, midx, midy;
        char str[64];

        /* initialize graphic mode */
        initgraph(&gdriver, &gmode, "C:/TURBOC3/BGI");
        err = graphresult();

        if (err != grOk) {
                /* error occurred */
                printf("Graphics Error: %s\n",
                                grapherrormsg(err));
                return 0;
        }

        /* mid positions in x and y-axis */
        midx = getmaxx() / 2;
        midy = getmaxy() / 2;

        /* 60 second counter */
        for (i = 60; i > 0; i--) {
                /* draws the gray board */
                setcolor(DARKGRAY);
                setfillstyle(SOLID_FILL, DARKGRAY);
                rectangle(midx - 100, midy - 100, midx + 100, midy + 100);
                floodfill(midx, midy, DARKGRAY);

                /* place the text "60 sec couter" on the gray board */
                setcolor(WHITE);
                sprintf(str, "%s", "60 Sec Counter");
                settextstyle(TRIPLEX_FONT, HORIZ_DIR, 2);
                settextjustify(CENTER_TEXT, CENTER_TEXT);
                moveto(midx, midy - 70);
                outtext(str);

                /* print the current counter value on the gray board */
                sprintf(str, "%d", i);
                moveto(midx, midy + 30);
                outtext(str);

                /* sleep for a second */
                sleep(1);

                /* clears graphic screen */
                cleardevice();
        }

        /* print "Time's Up" after 60 seconds */
        setcolor(LIGHTRED);
        settextstyle(TRIPLEX_FONT, HORIZ_DIR, 3);
        settextjustify(CENTER_TEXT, CENTER_TEXT);
        moveto(midx, midy);
        sprintf(str, "Time's Up");
        outtext(str);

        getch();

        /* deallocate memory allocated for graphic screen */
        closegraph();
        return 0;
  }





Output: (C program to implement 60 second counter)


No comments:

Post a Comment