Basics of Graphics programming
• We are going to see the basic syntax which must be followed while programming the graphics in turbo C++
1) By default are output screen or the terminal (Black screen were we get the output of programs) is in text mode.
2) So to initialize the terminal in graphic mode(x-axis and y-axis) we use initgraph function in our program.
3) 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.
Terminal in Graphics mode
4) 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.
5) This function is present in "graphics.h" header line in turbo C.
Sample graphics program:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
int main()
{
int gd= DETECT, gm;
initgraph (&gd, &gm, "C:\\TC\\BCI");
getch();
closegraph();
return 0;
}
OUTPUT: This program initialise graphic mode and then closes it after a key is pressed.
Explanation:
• To begin with we have declared two variables of int type gd and gm for graphics driver and graphics mode respectively, you can choose any other variable name as well.
• DETECT is a macro defined in "graphics.h" header file.
• we have passed three arguments to initgraph function:
1) First is the address of gd.
2) second is the address of gm.
3) Third is the path where your BGI files are present (you have to adjust this accordingly where you Turbo C compiler is installed).
4) Initgraph function automatically decides an appropriate graphics driver and mode such that maximum screen resolution is set.
5) getch helps us to wait until a key is pressed, closegraph function closes the graphics mode, and finally return statement returns a value 0 to main indicating successful execution of the program.
• Once you have understood initgraph function then you can use functions to draw shapes such as circle, line , rectangle, etc.
Remember:
1) All the programs that we are going to execute in are other post, work only in Turbo C platform because functions like gotoxy(), clrscr(), textcolor(), cprintf(), window(), rand(), kbhit() and delay() are not available in other platforms.
*You can leave a comment if you have doubt in it.
Comments
Post a Comment