C programming graphics animation tutorials can help you learn about some cool stuff in just a few minutes. These examples will show you how to create various effects in c visual studio with some animations. Learning more about graphics c programming examples can help you create stunning programs through many examples. They also offer good source code for your own projects so if you want to create some animation or graphics for your project, all you have to do is search these c program code library for their example codes.
If you are looking for c program code to create stunning 3d animation effects, you are in the right place. If you are keen on implementing jaw-dropping 3d animation easily, professionally and innovatively using c program, then this is the place for you.
We always want to do something new so that we can improve ourselves in every field of our lives.Here i have created a program of 3d animation in C language .I have used turbo C++ compiler for this and also recommend you to use if you are running this program.Its ouput is pretty much cool and you can customize it your own so enjoy it….Here is the code…..
#include<conio.h>#include<stdio.h>#include<graphics.h>void happy();int main() { happy(); return 0; }void happy(){ int x,y, gd = DETECT, gm; char arr[]=”HAPPY TEACHER’S DAY 2016″,arr1[]=”They say that…’Experience is the best teacher'”; int i; initgraph(&gd,&gm,”c:\\tc\\bgi”);//used to initialised the graphics settextstyle(SCRIPT_FONT,HORIZ_DIR,3);//used to change the text style and size setcolor(4); // used to set the color of the output text outtextxy(50,170,arr1); //used to print msg at the given position cursor outtextxy(50,190,”But for us…”); outtextxy(50,210,”Having you as our teacher is the best experience!”); settextstyle(TRIPLEX_FONT,HORIZ_DIR,1); setcolor(GREEN); outtextxy(300,350,”Created By :: Shashi Kumar”); settextstyle(TRIPLEX_FONT,HORIZ_DIR,5); while(!kbhit()){ for(i=100;i<110;i++) { outtextxy(50,i,arr); setcolor(i); delay(200); // pause the program }} getch(); system(“cls”); // substitution for CLRSCR(); closegraph();}
It initializes the graphics system by loading the passed graphics driver then changing the system into graphics mode. It also resets or initializes all graphics settings like color, palette, current position etc, to their default values. Below is the description of input parameters of initgraph function.
- graphicsDriver : It is a pointer to an integer specifying the graphics driver to be used. It tells the compiler that what graphics driver to use or to automatically detect the drive. In all our programs we will use DETECT macro of graphics.h library that instruct compiler for auto detection of graphics driver.
- graphicsMode : It is a pointer to an integer that specifies the graphics mode to be used. If *graphdriver is set to DETECT, then initgraph sets *graphmode to the highest resolution available for the detected driver.
- driverDirectoryPath : It specifies the directory path where graphics driver files (BGI files) are located. If directory path is not provided, then it will seach for driver files in current working directory directory. In all our sample graphics programs, you have to change path of BGI directory accordingly where you turbo C compiler is installed.
Colors in C Graphics Programming
There are 16 colors declared in C Graphics. We use colors to set the current drawing color, change the color of background, change the color of text, to color a closed shape etc. To specify a color, we can either use color constants like setcolor(RED), or their corresponding integer codes like setcolor(4). Below is the color code in increasing order.
COLOR MACRO | INTEGER VALUE |
---|---|
BLACK | 0 |
BLUE | 1 |
GREEN | 2 |
CYAN | 3 |
RED | 4 |
MAGENTA | 5 |
BROWN | 6 |
LIGHTGRAY | 7 |
DARKGRAY | 8 |
LIGHTBLUE | 9 |
LIGHTGREEN | 10 |
LIGHTCYAN | 11 |
LIGHTRED | 12 |
LIGHTMAGENTA | 13 |
YELLOW | 14 |
WHITE | 15 |
At the end of our graphics program, we have to unloads the graphics drivers and sets the screen back to text mode by calling closegraph function. Here is our first C Graphics program to draw a straight line on screen.
123456789101112 | /* C graphics program to draw a line */ #include<graphics.h> #include<conio.h> int main() { int gd = DETECT, gm; /* initialization of graphic mode */ initgraph(&gd, &gm, "C:\\TC\\BGI" ); line(100,100,200, 200); getch(); closegraph(); return 0; } |
In this program initgraph function auto detects an appropriate graphics driver and sets graphics mode maximum possible screen resolution. Then line function draws a straight line from coordinate (100, 100) to (200, 200). Then we added a call to getch function to avoid instant termination of program as it waits for user to press any key. At last, we unloads the graphics drivers and sets the screen back to text mode by calling closegraph function.
CIRCLE
C Program for Moving Car Animation
#include <stdio.h> #include <graphics.h> #include <conio.h> #include <dos.h> int main() { int gd = DETECT, gm; int i, maxx, midy; /* initialize graphic mode */ initgraph(&gd, &gm, "X:\\TC\\BGI"); /* maximum pixel in horizontal axis */ maxx = getmaxx(); /* mid pixel in vertical axis */ midy = getmaxy()/2; for (i=0; i < maxx-150; i=i+5) { /* clears screen */ cleardevice(); /* draw a white road */ setcolor(WHITE); line(0, midy + 37, maxx, midy + 37); /* Draw Car */ setcolor(YELLOW); setfillstyle(SOLID_FILL, RED); line(i, midy + 23, i, midy); line(i, midy, 40 + i, midy - 20); line(40 + i, midy - 20, 80 + i, midy - 20); line(80 + i, midy - 20, 100 + i, midy); line(100 + i, midy, 120 + i, midy); line(120 + i, midy, 120 + i, midy + 23); line(0 + i, midy + 23, 18 + i, midy + 23); arc(30 + i, midy + 23, 0, 180, 12); line(42 + i, midy + 23, 78 + i, midy + 23); arc(90 + i, midy + 23, 0, 180, 12); line(102 + i, midy + 23, 120 + i, midy + 23); line(28 + i, midy, 43 + i, midy - 15); line(43 + i, midy - 15, 57 + i, midy - 15); line(57 + i, midy - 15, 57 + i, midy); line(57 + i, midy, 28 + i, midy); line(62 + i, midy - 15, 77 + i, midy - 15); line(77 + i, midy - 15, 92 + i, midy); line(92 + i, midy, 62 + i, midy); line(62 + i, midy, 62 + i, midy - 15); floodfill(5 + i, midy + 22, YELLOW); setcolor(BLUE); setfillstyle(SOLID_FILL, DARKGRAY); /* Draw Wheels */ circle(30 + i, midy + 25, 9); circle(90 + i, midy + 25, 9); floodfill(30 + i, midy + 25, BLUE); floodfill(90 + i, midy + 25, BLUE); delay(100); } getch(); closegraph(); return 0; }
Output
Let see the C code for Rocket Animation,
#include <stdio.h> //Giving some delay void delay( unsigned int value) { unsigned int count1 =0; unsigned int count2 = 0; for(count1 = 0; count1 < value ; count1++ ) { for(count2 = 0; count2 < count1 ; count2++ ) { } } } // string to display Rocket const char rocket[] = " ^ \n\ /^\\\n\ |-|\n\ | |\n\ |I|\n\ |S|\n\ |R|\n\ |O|\n\ /| |\\\n\ / | | \\\n\ | | | |\n\ `-\"\"\"-`\n\ "; int main() { int jumpControlAtBottom = 0; const int someDelay = 6000; int shifControl = 0; //jump to bottom of console for (jumpControlAtBottom = 0; jumpControlAtBottom < 30; ++jumpControlAtBottom) { printf("\n"); } //Print rocket fputs(rocket,stdout); for (shifControl = 0; shifControl < 30; ++shifControl) { // Rocket move on the basis of delay delay(someDelay); // move rocket a line upward printf("\n"); } return 0; }
Code Analysis:
We have to first jump to the bottom of the console, so in the for loop, I am executing printf with a new line (‘\n’).
for (jumpControlAtBottom = 0; jumpControlAtBottom < 30; ++jumpControlAtBottom) { printf("\n"); }
Now times to display the rocket, so using the fputs I am printing the rocket.
fputs(rocket,stdout);
After displaying the rocket, I am using a for loop in which I have given some delay. You can change the delay as per your requirement. I have also displayed the new line using the printf to move the rocket upward,
for (shifControl = 0; shifControl < 30; ++shifControl) { // Rocket move on the basis of delay delay(someDelay); // move rocket a line upward printf("\n"); }
Conclusion:
C is a great first language for a new programmer to learn. These animation programs can help beginners–or anyone who wants to learn how to do animation. But the trick is finding the right C animation code for your specific needs, rather than taking all of them!