A Little Help to Start Computer Graphics Designing using C

If you have the basic knowledge of C programming and you wish to develop graphics using C, well, this is for you. 
Age or experience are not the constraints for graphics designing. Learning to develop computer graphics comes out of interest for animation. Starting with C is pretty easy, really. And the best part is that Turbo C comes full of programs meant to help you understand the building block of designing (like drawing a line or an oval). The syntax is right there. All you need to do is implement it while creating your own program. You don't need any formal training, like most designing software. You teach yourself, out of interest. You experiment, you learn. For me, making objects move with for loops and delay() for the first time was a real joy. So here are the basic steps you need to follow to drive your passion ahead.

Sample program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT, gm, i;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
setlinestyle(1,0,1);
rectangle(0,0,636,476);
rectangle(10,10,624,464);
setlinestyle(0,0,0);
settextstyle(10,0,6);
setcolor(10);
for(i=0;i<10;i++)
{
outtextxy(260,250,"WELCOME");
 //"outtextxy" is used for putting text in specific co-ordinates, you can also use "printf"
delay(10);
}
getch();
closegraph();
}
(Copy the above program and paste to Notepad. Save with the extension " .c ". Open in C)
For more sample programs and help, when you have written " #include<graphics.h> " in C, just bring your cursor to it and press Ctrl+F1. It'll open up a list of all C graphics functions. Then, just go to the one you want to use and press ENTER or, simply click on it. You will get the details on how to use that function.

Explanation:

1. The basic header files a program would require are:
#include<stdio.h>
#include<conio.h>
#include<graphics.h> //"graphics.h" is for including the graphics library

2. int gd=DETECT, gm;
  • Here, "gd" is for *graphdriver, it is the integer that specifies the graphics driver to be used
  • "gd=DETECT" is given for requesting auto detection
  • "gm" is for *graphmode, which specifies the initial graphics mode
  • *graphdriver and *graphmode must be set to valid graphics_drivers and graphics_mode
3. initgraph(&gd,&gm,"c:\\turboc3\\bgi"); 
(This is very important, so be careful never to forget this.)
 
Syntax:
void initgraph(int far *graphdriver, int far *graphmode, char far *pathtodriver);
 
"initgraph" initializes the graphics system by validating a registered driver or loading a graphics driver from disk. It also resets all graphics settings to their  default. The system is then put to graphics mode.
"initgraph" loads a graphics driver by allocating memory for the driver. You can also link a graphics driver file without this dynamic loading scheme, by linking one or more graphics driver files directly into your executable program file.
The path provided here,"c:\\turboc3\\bgi" links the .bgi file from the BGI folder in turboc, which for me is located in my C drive. You need to set you own path here which takes it to the BGI folder.

4. closegraph();

This function is used to end the graphics mode. If you don't use this function, the screen will remain in graphics mode. This means that when you come back to DOS mode your screen would not be in text mode.
It left me so confused the first time I made that mistake. I actually thought something is wrong with the loops I have used in the program.

I hope this helps. If you are able to successfully execute your program using the above points, do share it here. Also, contact via comment section for further clarification of doubts. I would try my best to clear them for you.
Happy Designing!




No comments:

Post a Comment

Your questions and opinions matter. So go ahead..