13.3 – introduction to graphics – Turbo C

by subbu on January 9, 2014

The main purpose of introducing graphics at this stage is to demonstrate how important it is to learn functions.

All the example programs given here, work only in Turbo C platform because functions like gotoxy(), clrscr(), textcolor(), cprintf(), window(), rand(), kbhit() and delay() are not available in other platforms.

Graphics in C language

Display can be normally used in either text mode or graphic mode. In text mode the screen is divided into 80 columns and 25 rows. In case of graphic mode the total screen is divided into 640 pixels width and 480 pixels height. Latest displays are even rich in resolution.

Generally text displays are faster then graphic displays because in graphic mode we have to program pixel by pixel. Here in this session, we are going to learn graphics in text mode. It may be help full in developing simple applications and games.

Turbo C is rich in graphics

Functions, colors used to generate graphics are not in the original specification of C language. Graphic libraries are available for different platforms in the market. But the turbo C is rich in graphic tools, provides number of functions to work with graphics both in text mode and in graphic mode. Most of the text mode graphic functions are available in conio.h

Screen resolution:

screen resolution in text mode

Here the screen resolution is in text mode. X-direction is horizontally across the screen and y-direction is vertically down the screen. The top left corner is (0, 0) and the bottom right corner is (80, 25).
While executing the program, Turbo C opens the console in text mode only.

Controlling the cursor

gotoxy()  is a function used to send the cursor to the specified coordinates in the active window. Here the first coordinate is the column (x) and the second coordinate is the row (y). Here it accepts relative coordinates to the current active window

gotoxy(40,12);

It sends the cursor to the 40th column and 12th row.

Example:

#include<conio.h>
#include<stdio.h>
int main()
{
 clrscr();
 gotoxy(5,5);            /* sending cursor to 5th column and 5th row */
 printf("Hello world");
 gotoxy(7,7);            /* sending cursor to 7th column and 7th row */
 printf("Hello world");
 gotoxy(9,9);            /* sending cursor to 9th column and 9th row */
 printf("Hello world");
 return 0;
}

Output:

gotoxy() in Turbo C

Working with colors in Turbo C (Text mode):

Turbo C supports the following colors in text mode. All colors can be used to set foreground color but, only some of them can be used as background color.

Either numeric value or symbolic name can be used to set the color. All these colors are defined as constants using enum type “colors” in conio.h

Numeric Value Symbolic Name As background Color As foreground color
0 BLACK YES YES
1 BLUE YES YES
2 GREEN YES YES
3 CYAN YES YES
4 RED YES YES
5 MAGENTA YES YES
6 BROWN YES YES
7 LIGHTGRAY YES YES
8 DARKGRAY NO YES
9 LIGHTBLUE NO YES
10 LIGHTGREEN NO YES
11 LIGHTCYAN NO YES
12 LIGHTRED NO YES
13 LIGHTMAGENTA NO YES
14 YELLOW NO YES
15 WHITE NO YES
128 BLINK NO YES

Setting the foreground (text) color

textcolor() is the function used to set the foreground (text) color in the active window. We can use either numeric value or symbolic name to set the text color.

textcolor(RED);   /* set the foreground (text) color to red */
textcolor(14);    /* change the foreground (text) color to yellow */

Printing colored text

Generally the printf() statement prints the text at the active cursor position only with the default foreground color white. Where as cprintf() helps to print the text with the active window color.

textcolor(14);
cprintf("Hello world"); /* prints the "Hello World" in yellow color */

Example:

#include<conio.h>
#include<stdio.h>
int main()
{
 int i,x,y;
 clrscr();
 x=5;                     /* setting column */
 y=3;                     /* setting row    */
 for(i=1;i<=15;i++)       /* changing colors */
 {
    textcolor(i);         /* setting text color */
    gotoxy(x,y);
    cprintf("Codingfox");
    y++;                  /* changing row */
 }
return 0;
}

Output:

textcolor() in Turbo C

Setting background color

textbackground() is the function used to set the background color to the active window. It accepts either numeric color value or symbolic name as argument.

Example:

#include<conio.h>
#include<stdio.h>
int main()
{
 clrscr();
 gotoxy(5,10);                   /* sends the cursor to (5,10) */
 textbackground(WHITE);          /* set the background color   */
 textcolor(BLACK);               /* set the foreground color    */
 cprintf("CodingFox");
 return 0;
}

Output:

textbackground() in Turbo C

Controlling video intensity:

Video intensity can be changed to high, low and normal using functions lowvideo(), highvideo() and normvideo().

lowvideo() sets the video intensity to low, highvideo() sets the video intensity to high and normvideo() sets the video to default color and intensity.

#include<stdio.h>
#include<conio.h>
int main()
{
 textcolor(YELLOW);
 gotoxy(2,2);
 cprintf("CodingFox");
 lowvideo();
 gotoxy(2,3);
 cprintf("CodingFox");
 highvideo();
 gotoxy(2,4);
 cprintf("CodingFox");
 getch();
}

Output

Video intensity

Accessories for animation in Turbo C

delay() in Turbo C:

It is the function defined in dos.h, used to delay the execution for specified number of milliseconds.

cprintf("Hello World");
delay(1000);               /* delay for 1 second */
cprintf("CodingFox");

“CodingFox” prints after printing of “Hello World” by 1second delay.

kbhit() in Turbo C:

It is a function defined in conio.h, used when some thing has to continue until any key is hit on the keyboard. It returns true (1) on hitting any key on the keyboard otherwise returns false(0)

int i=1;
do{
  printf("\t%d",i);
  i++;
}while(!kbhit());  /* not false is true */

It is the loop to print natural numbers from 1.
In the conditional statement kbhit() gives false (0), !kbhit() gives true (1). As the condition is always true, loop prints natural numbers continuously.
Loop terminates only on hitting any key on the keyboard because on hitting the key kbhit() gives true (1), !kbhit() gives false (0) which results termination of loop.

Simple animation program:

#include<conio.h>
#include<stdio.h>
#include<dos.h>
int main()
{
 int i,x,y;
 clrscr();
 textcolor(CYAN);  /* setting the active window color */
 do{
     x=5;                       /* setting column */
     y=3;                       /* setting row    */
     for(i=1;i<=15;i++)         /* creates one animation in 15 steps*/
     {
        clrscr();               /* clearing previous screen */
        gotoxy(x,y);            /* setting cursor to next row */
        cprintf("Codingfox");   /* printing codingfox */
        delay(100);             /* waiting for some time */
        y++;                    /* changing row */
     }
  }while(!kbhit());             /* going for next animation */
  return 0;
}

Output:
Animation in C language

Example explained:
Here the word “codingfox” scrolls vertically until any key on the keyboard is pressed

Printing a string with changing colors

#include<stdio.h>
#include<conio.h>
int main()
{
 int i;
 clrscr();
 for(i=0;i<15;i++) /* loop to change the colors */
 {
  gotoxy(35,13);
  textcolor(i);          /* changing text color         */
  cprintf("CodingFox");
  delay(1000);
 }
 return 0;
}

Print a string by assembling characters come from different directions

printing good

#include<stdio.h>
#include<conio.h>
int main()
{
 int i;
 clrscr();
 textcolor(YELLOW);
 /* Moving G from left to center*/
 for(i=1;i<=36;i++)
 {
  gotoxy(i,13);
  cprintf("G");             /* printing character           */
  gotoxy(i-1,13);
  cprintf(" ");                /* Erasing printed character */
  delay(20);
 }
 /* Moving D from right to center*/
 for(i=79;i>=39;i--)
 {
  gotoxy(i,13);
  cprintf("D");             /* printing character           */
  gotoxy(i+1,13);
  cprintf(" ");                /* Erasing printed character */
  delay(20);
 }
 /* Moving O from top to center*/
 for(i=1;i<=13;i++)
 {
  gotoxy(37,i);
  cprintf("O");            /* printing character           */
  gotoxy(37,i-1);
  cprintf(" ");               /* Erasing printed character */
  delay(50);
 }
 /* Moving O from bottom to center*/
 for(i=24;i>=13;i--)
 {
  gotoxy(38,i);
  cprintf("O");                   /* printing character            */
  gotoxy(38,i+1);
  cprintf(" ");                      /* Erasing printed character  */
  delay(50);
 }
 getch();
 return 0;
}

Previous post:

Next post: