Pages - Menu

Monday 8 October 2012

Basic graphic library for console applications in C

GRAPH_LIB.h is a C library providing functions developed based on the Windows.h header file that can be used for adding visual effects to the console window.

Below you have the functions' name and their description.


Name
Description
ConsoleDimensions
Sets the length and width of the console
ConsoleColours
Sets console's background and text colour
GoTo
Moves the colour on the screen to the specified coordinates
Colour
Sets background and text colours
DrawOutline
Draws the outline of a rectangle
DrawRectangle
Draws a filled rectangle
DrawGrid
Draws the outline of a grid
DrawInnerGrid
Draws a filled grid
StripesOutline
Draws the striped outline of a rectangle
StripesRectangle
Draws a rectangle shape filled with stripes
DoubleBorderRectangle
Draws a rectangle-shaped border
DoubleBorderGrid
Draws a grid
#include<Windows.h>

/*
 * Description:
 *  Moves the cursor on the screen
 * Parameters:
 *  horizontalCoordinate - the new  horizontal coordinate
 *  verticalCoordinate   - the new vertical coodinate
 */
void GoTo(int horizontalCoordinate, int verticalCoordinate)
{
    COORD coordinates;
    coordinates.X=horizontalCoordinate;
    coordinates.Y=verticalCoordinate;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coordinates);
}

/*
 * Description:
 *  Sets the length and width of the console
 * Parameters:
 *  length - number of characters allowed horizontally
 *  width  - number of characters allowed verically
 */
void ConsoleDimensions(int length, int width)
{
    char string[20]="mode";
    char columns[4];
    char rows[4];
    itoa(length,columns,10);
    itoa(width,rows,10);
    strcat(string," ");
    strcat(string,columns);
    strcat(string,",");
    strcat(string,rows);
    system(string);
}

/*
 * Description:
 *  Sets background and text colours
 * Parameters:
 *  backgroundColour - the new background colour
 *  textColour       - the new text colour
 */
void Colour(char backgroundColour[], char textColour[])
{
    char string[20]="0x";
    char *end;
    int background=0, text=0;
    char backgroundString[4], textString[4];
    int code=0;
    //codes for background colour
    if(strcmp(backgroundColour,"black") == 0)
        background=0;
    else if(strcmp(backgroundColour,"dark blue") == 0)
        background=1;
    else if(strcmp(backgroundColour,"green") == 0)
        background=2;
    else if(strcmp(backgroundColour,"cyan") == 0)
        background=3;
    else if(strcmp(backgroundColour,"red") == 0)
        background=4;
    else if(strcmp(backgroundColour,"dark purple") == 0)
        background=5;
    else if(strcmp(backgroundColour,"brown") == 0)
        background=6;
    else if(strcmp(backgroundColour,"gray") == 0)
        background=7;
    else if(strcmp(backgroundColour,"dark gray") == 0)
        background=8;
    else if(strcmp(backgroundColour,"blue") == 0)
        background=9;
    else if(strcmp(backgroundColour,"neon green") == 0)
        background=10;
    else if(strcmp(backgroundColour,"ligth blue") == 0)
        background=11;
    else if(strcmp(backgroundColour,"light red") == 0)
        background=12;
    else if(strcmp(backgroundColour,"magenta") == 0)
        background=13;
    else if(strcmp(backgroundColour,"yellow") == 0)
        background=14;
    else 
        background=15;
    //codes for text colour
    if(strcmp(textColour,"black") == 0)
        text=0;
    else if(strcmp(textColour,"dark blue") == 0)
        text=1;
    else if(strcmp(textColour,"green") == 0)
        text=2;
    else if(strcmp(textColour,"cyan") == 0)
        text=3;
    else if(strcmp(textColour,"red") == 0)
        text=4;
    else if(strcmp(textColour,"dark purple") == 0)
        text=5;
    else if(strcmp(textColour,"brown") == 0)
        text=6;
    else if(strcmp(textColour,"gray") == 0)
        text=7;
    else if(strcmp(textColour,"dark gray") == 0)
        text=8;
    else if(strcmp(textColour,"blue") == 0)
        text=9;
    else if(strcmp(textColour,"neon green") == 0)
        text=10;
    else if(strcmp(textColour,"ligth blue") == 0)
        text=11;
    else if(strcmp(textColour,"light red") == 0)
        text=12;
    else if(strcmp(textColour,"magenta") == 0)
        text=13;
    else if(strcmp(textColour,"yellow") == 0)
        text=14;
    else
        text=15;
    //constructing the code for background and text colour
    itoa(background,backgroundString,16);
    itoa(text,textString,16);
    strcat(string,backgroundString);
    strcat(string,textString);
    code=strtol(string,&end,16);
    //sets the desired background and text colour
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),code);
}

/*
 * Description:
 *  Draw a filled rectangle
 * Parameters:
 *  horizontalCoordinate - the upper-left horizontal coordinate of the rectangle
 *  verticalCoordinate   - the upper-left vertical coordinate of the rectangle
 *  length     - the length of the rectangle in number of characters
 *  width     - the width of the rectabgle in number of characters
 *  colour     - the colour of the rectangle
 */
void DrawRectangle(int horizontalCoordinate, int verticalCoordinate,
                   int length, int width, char colour[])
{
    int i=0, j=0;

    GoTo(horizontalCoordinate,verticalCoordinate);//starting position
    Colour(colour,colour);//sets colour to be drawn

    for(i; i<length; i++)
    {
        for(j=0; j<width; j++)
        {
            GoTo(horizontalCoordinate+i,verticalCoordinate+j);
            printf("*");
        }
    }
}

/*
 * Description:
 *  Sets console's background and text colours
 * Parameters:
 *  backgroundColour - the new background colour of the console
 *  textColour       - the new text colour
 */
void ConsoleColours(char backgroundColour[], char textColour[])
{
    char string[20]="Color ";
    int background=0, text=0;
    char sBack[4], sText[4];
    //codes for background colour
    if(strcmp(backgroundColour,"black") == 0)
        background=0;
    else if(strcmp(backgroundColour,"dark blue") == 0)
        background=1;
    else if(strcmp(backgroundColour,"green") == 0)
        background=2;
    else if(strcmp(backgroundColour,"cyan") == 0)
        background=3;
    else if(strcmp(backgroundColour,"red") == 0)
        background=4;
    else if(strcmp(backgroundColour,"dark purple") == 0)
        background=5;
    else if(strcmp(backgroundColour,"brown") == 0)
        background=6;
    else if(strcmp(backgroundColour,"gray") == 0)
        background=7;
    else if(strcmp(backgroundColour,"dark gray") == 0)
        background=8;
    else if(strcmp(backgroundColour,"blue") == 0)
        background=9;
    else if(strcmp(backgroundColour,"neon green") == 0)
        background=10;
    else if(strcmp(backgroundColour,"ligth blue") == 0)
        background=11;
    else if(strcmp(backgroundColour,"light red") == 0)
        background=12;
    else if(strcmp(backgroundColour,"magenta") == 0)
        background=13;
    else if(strcmp(backgroundColour,"yellow") == 0)
        text=14;
    else 
        background=15;
    //codes for text colour
    if(strcmp(textColour,"black") == 0)
        text=0;
    else if(strcmp(textColour,"dark blue") == 0)
        text=1;
    else if(strcmp(textColour,"green") == 0)
        text=2;
    else if(strcmp(textColour,"cyan") == 0)
        text=3;
    else if(strcmp(textColour,"red") == 0)
        text=4;
    else if(strcmp(textColour,"dark purple") == 0)
        text=5;
    else if(strcmp(textColour,"brown") == 0)
        text=6;
    else if(strcmp(textColour,"gray") == 0)
        text=7;
    else if(strcmp(textColour,"dark gray") == 0)
        text=8;
    else if(strcmp(textColour,"blue") == 0)
        text=9;
    else if(strcmp(textColour,"neon green") == 0)
        text=10;
    else if(strcmp(textColour,"ligth blue") == 0)
        text=11;
    else if(strcmp(textColour,"light red") == 0)
        text=12;
    else if(strcmp(textColour,"magenta") == 0)
        text=13;
    else if(strcmp(textColour,"yellow") == 0)
        text=14;
    else
        text=15;
    itoa(background,sBack,16);
    itoa(text,sText,16);
    strcat(string,sBack);
    strcat(string,sText);
    system(string);
}

/*
 * Description:
 *  Draws the outline of a rectangle
 * Parameters:
 *  horizontalCoordinate - the upper-left horizontal coordinate of the rectangle
 *  verticalCoordinate   - the upper-left vertical coodinate of the rectangle
 *  length     - the length of the rectangle in number of characters
 *  width     - the width of the rectangle in number of characters
 *  colour     - the colour of the outline
 */
void DrawOutline(int horizontalCoordinate, int verticalCoordinate, 
                 int length, int width, char colour[])
{
    int i=0, j=0;
    Colour(colour,colour);

    GoTo(horizontalCoordinate,verticalCoordinate);//up
    for(i=0; i<length+2; i++)
        printf("*");
    for(i=0; i<width; i++)//left
    {
        GoTo(horizontalCoordinate,verticalCoordinate+i+1);
        printf("*");
    }
    for(i=0; i<width; i++)//right
    {
        GoTo(horizontalCoordinate+length+1,verticalCoordinate+i+1);
        printf("*");
    }
    GoTo(horizontalCoordinate,verticalCoordinate+width+1);//down
    for(i=0; i<length+2; i++)
        printf("*");
}

/*
 * Description:
 *  Draws the outline of a grid
 * Parameters:
 *  horizontalCoordinate - the upper-left horizontal coordinate of the grid
 *  verticalCoordinate   - the upper-left vertical coodinate of the grid
 *  length     - the length of a rectangle in number of characters
 *  width     - the width of a rectangle in number of characters
 *  rows     - the number of rows of the grid
 *  columns     - the number of columns of the grid
 *  colour     - the colour of the outline
 */
void DrawGrid(int horizontalCoordinate, int verticalCoordinate, int length, int width, 
              int rows, int columns, char colour[])
{
    int i=0, j=0;
    for(i=0; i<rows; i++)
        for(j=0; j<columns; j++)
        {
                DrawOutline(horizontalCoordinate+i+length*i,verticalCoordinate+j+width*j,
                            length,width,colour);
        }

}

/*
 * Description:
 *  Draws a filled grid
 * Parameters:
 *  horizontalCoordinate - the upper-left horizontal coordinate of the grid
 *  verticalCoordinate   - the upper-left vertical coodinate of the grid
 *  length     - the length of a rectangle in number of characters
 *  width     - the width of a rectangle in number of characters
 *  rows     - the number of rows of the grid
 *  columns     - the number of columns of the grid
 *  colour     - the colour of each rectangle
 */
void DrawInnerGrid(int horizontalCoordinate, int verticalCoordinate, int length, int width, 
                   int rows, int columns, char colour[])
{
    int i=0, j=0;
    for(i=0; i<rows; i++)
        for(j=0; j<columns; j++)
        {
            DrawRectangle(horizontalCoordinate+i+length*i,verticalCoordinate+j+width*j,
                          length,width,colour);
        }
}

/*
 * Description:
 *  Draws the striped outline of a rectangle
 * Parameters:
 *  horizontalCoordinate - the upper-left horizontal coordinate of the rectangle
 *  verticalCoordinate   - the upper-left vertical coodinate of the rectangle
 *  length     - the length of the rectangle in number of characters
 *  width     - the width of the rectangle in number of characters
 *  backgroundColour  - the colour of the background
 *  textColour    - the colour of the text
 */
void StripesOutline(int horizontalCoordinate, int verticalCoordinate, int length, int width, 
                    char backgroundColour[], char textColour[])
{
    int i=0, j=0;
    Colour(backgroundColour,textColour);

    GoTo(horizontalCoordinate,verticalCoordinate);//up
    for(i=0; i<length+2; i++)
    {
        printf("%c",176);
    }
    for(i=0; i<width; i++)//left
    {
        GoTo(horizontalCoordinate,verticalCoordinate+i+1);
        printf("%c",176);
    }
    for(i=0; i<width; i++)//right
    {
        GoTo(horizontalCoordinate+length+1,verticalCoordinate+i+1);
        printf("%c",176);
    }
    GoTo(horizontalCoordinate,verticalCoordinate+width+1);//down
    for(i=0; i<length+2; i++)
    {
        printf("%c",176);
    }
}

/*
 * Description:
 *  Draws a rectangle shape filled with stripes
 * Parameters:
 *  horizontalCoordinate - the upper-left horizontal coordinate of the rectangle
 *  verticalCoordinate   - the upper-left vertical coodinate of the rectangle
 *  length     - the length of the rectangle in number of characters
 *  width     - the width of the rectangle in number of characters
 *  backgroundColour  - the colour of the background
 *  textColour    - the colour of the text
 */
void StripesRectangle(int horizontalCoordinate, int verticalCoordinate, int length, int width,
                      char backgroundColour[], char textColour[])
{
    int i=0, j=0;

    GoTo(horizontalCoordinate,verticalCoordinate);//starting position
    Colour(backgroundColour,textColour);//sets colour to be drawn

    for(i; i<length; i++)
    {
        for(j=0; j<width; j++)
        {
            GoTo(horizontalCoordinate+i,verticalCoordinate+j);
            printf("%c",176);
        }
    }
}

/*
 * Description:
 *  Draws a rectangle-shaped border
 * Parameters:
 *  horizontalCoordinate - the upper-left horizontal coordinate of the rectangle
 *  verticalCoordinate   - the upper-left vertical coodinate of the rectangle
 *  length     - the length of the rectangle in number of characters
 *  width     - the width of the rectangle in number of characters
 *  backgroundColour  - the colour of the background
 *  textColour    - the colour of the text
 */
void DoubleBorderRectangle(int horizontalCoordinate, int verticalCoordinate, int length, int width, 
                           char backgroundColour[], char textColour[])
{
    int i=0, j=0;
    Colour(backgroundColour,textColour);

    GoTo(horizontalCoordinate,verticalCoordinate);//up
    for(i=0; i<length+2; i++)
    {
        printf("%c",205);
    }
    for(i=0; i<width; i++)//left
    {
        GoTo(horizontalCoordinate,verticalCoordinate+i+1);
        printf("%c",186);
    }
    for(i=0; i<width; i++)//right
    {
        GoTo(horizontalCoordinate+length+1,verticalCoordinate+i+1);
        printf("%c",186);
    }
    GoTo(horizontalCoordinate,verticalCoordinate+width+1);//down
    for(i=0; i<length+2; i++)
    {
        printf("%c",205);
    }
    //corners
    GoTo(horizontalCoordinate,verticalCoordinate);
    printf("%c",201);
    GoTo(horizontalCoordinate,verticalCoordinate+width+1);
    printf("%c",200);
    GoTo(horizontalCoordinate+length+1,verticalCoordinate);
    printf("%c",187);
    GoTo(horizontalCoordinate+length+1,verticalCoordinate+width+1);
    printf("%c",188);
}

/*
 * Description:
 *  Draws a grid
 * Parameters:
 *  horizontalCoordinate - the upper-left horizontal coordinate of the grid
 *  verticalCoordinate   - the upper-left vertical coodinate of the grid
 *  length     - the length of a rectangle in number of characters
 *  width     - the width of a rectangle in number of characters
 *  rows     - the number of rows of the grid
 *  columns     - the number of columns of the grid
 *  backgroundColour  - the background colour
 *  textColour    - the text colour
 */
void DoubleBorderGrid(int horizontalCoordinate, int verticalCoordinate, int length, int width, 
              int columns, int rows, char backgroundColour[], char textColour[])
{
    int i=0, j=0;
    for(i=0; i<rows; i++)
        for(j=0; j<columns; j++)
        {
                DoubleBorderRectangle(horizontalCoordinate+i*2+length*i+i,
                                      verticalCoordinate+j+width*j+j,length,width,
                                      backgroundColour,textColour);
        }

}

Example: I will show you how to use the functions above, as well as what they do on the console.
#include"GRAPH_LIB.h"

int main(void)
{
    ConsoleDimensions(30,20);
    ConsoleColours("dark gray","yellow");
    GoTo(1,1);
    printf("Vintage Coding");
    GoTo(2,2);
    Colour("light red","white");
    printf("Vintage Coding");
    DrawOutline(3,3,3,3,"black");
    DrawRectangle(4,4,3,3,"green");
    DrawGrid(1,8,3,3,2,2,"red");
    DrawInnerGrid(2,9,3,3,2,2,"blue");
    StripesOutline(14,3,3,3,"neon green","dark blue");
    StripesRectangle(15,4,3,3,"dark purple","magenta");
    DoubleBorderRectangle(25,3,3,3,"gray","cyan");
    DoubleBorderGrid(14,8,3,3,2,2,"dark gray","brown");
    getch();
    return 0;
}

Output:

No comments:

Post a Comment