Search This Blog

Most Viewed

Wednesday, January 26, 2011

Begining C/C++ Graphics........

 
  Usually the C and C++ which is studied at institutes contains thing that is useless for an engineer or belonging to science field student.
  So here is interesting way of learning the C programming with graphics. There are 2 kind of C graphics. Although both are primitive graphics but they must be learned before one can go to more virtual graphic enviornment nsuch as Visual C/C++ or VB.
  I am giving brief idea about programming with C/C++.
  One must know the following things before going to the graphics programming in C/C++.


1. The way to write code into C/C++, compiling and execution of code with C/C++ compilers. Here is brief idea about primitive programs in C/C++. { I am using Turbo C++ 3.0 Compiler. }

// Program to Print Hello and your name on the output screen...

// The Header File Goes here.
#include<stdio.h>
#inlcude<conio.h>

// Main function goes here

void main()
{
   Char a[30]="";    // Make a character string of 30 character to hold your input
   scanf("%s", a);  // Takes input from standard output  and save it to string a
   printf("Hello");    // Print Hello on the standard output device i.e. Monitor   
   printf(" %s",a);    // Put the string entered by you write next to 'Hello'
   getch();               // Wait for single character input from keyboard
                               // Here this function is used to hold the screen until any key is pressed on the
                              // key board
}

2. Function calls and returns. In the above example the void main() is also a function called by compiler at first in execution or run time. A function is basically that code of program which is rapidly reputed in program. There are two method of making function bot do one and same thing with some slightly changs. First one is call/return by value and another is call/return by reference. A function call is code which move the execution of program to the code written inside a function and return is to go back to main function or any function which calls the function. The function call by value is take value of variable for manipulation and return value to the calling function. The value call can't change the value of actual variable, but a reference call can. A reference call will load the address where the main variable is present and change the original value. Example of both type of function call is as follows.

// Function calling

#include<stdio.h>
#include<conio.h>

int get_by_val(int);
int get_by_ref(int);

void main()
{
    int a,b,c,d;
    a=10;
    b=20;

// Function calls
    c=get_by_val(a);     // a=10, c=30
    d=get_by_ref(b);     // d=40, b=40

    printf("%d\n%d\n%d\n%d\n",a,b,c,d);
    getch();
}

int get_by_val(int a)    //Makes a new integer locally named a, different from a as in main
{
    a+=20;         // Increment in a by 20
    return a;
}

int get_by_ref(int *x)    // x represent same as b any change in x will change the b
{
   x+=20                 // Increment in x or b by 20
   return x;
}

3. Logical operation and looping. The logical statement is "if else" and "switch case". and looping statements are "for", "while ", "do while".
4. Overview of object oriented programming.

  Now that's enough to learn the graphic programming in C/C++. The following program will illustrate to draw a point on the screen, using Borland Graphics.


// Point Drawing

#include<stdio.h>         // Standard input output header file
#include<conio.h>        // Console input output and formatting functions
#include<math.h>         // Mathematical operations definition function
#include<graphics.h>  // Graphics drawing function definition


main()
{


// Detect and select the mode and driver for standard output device
// Driver is a program which tells the computer how the monitor is working
// and mode is resolution of device, in this program both are auto set.
// driver is IBM and mode is of 640x480

 int driver=DETECT,mode;

// Initialization of graphics mode

 initgraph(&driver,&mode,"Pankaj");

// Point drawing Function
// putpixel(<column no>, <row no>, <color>)

 putpixel(100,50,RED);    // Draw a red point at 100th column and 50th row
 putpixel(50,100,BLUE);  // Draw a blue point at 50th column and 100th row

 printf("PUSHPKANT YADAV");
 

 getch();
 closegraph();
 return(0);
}


Graphics drivers, Generally use auto detect mode, if doesn't work one can use the following driver, which is suitable

  DETECT,         /* requests auto detection */
  CGA,

  MCGA,
  EGA,
  EGA64,
  EGAMONO,
  IBM8514,    /* 1 - 6 */
  HERCMONO,

  ATT400,
  VGA,
  PC3270,          /* 7 - 10 */
  CURRENT_DRIVER = -1


There are following graphics mode

    CGAC0      = 0,  /* 320x200 palette 0; 1 page   */
    CGAC1      = 1,  /* 320x200 palette 1; 1 page   */
    CGAC2      = 2,  /* 320x200 palette 2: 1 page   */
    CGAC3      = 3,  /* 320x200 palette 3; 1 page   */
    CGAHI      = 4,  /* 640x200 1 page          */
    MCGAC0     = 0,  /* 320x200 palette 0; 1 page   */
    MCGAC1     = 1,  /* 320x200 palette 1; 1 page   */
    MCGAC2     = 2,  /* 320x200 palette 2; 1 page   */
    MCGAC3     = 3,  /* 320x200 palette 3; 1 page   */
    MCGAMED    = 4,  /* 640x200 1 page          */
    MCGAHI     = 5,  /* 640x480 1 page          */
    EGALO      = 0,  /* 640x200 16 color 4 pages    */
    EGAHI      = 1,  /* 640x350 16 color 2 pages    */
    EGA64LO    = 0,  /* 640x200 16 color 1 page     */
    EGA64HI    = 1,  /* 640x350 4 color  1 page     */
    EGAMONOHI  = 0,  /* 640x350 64K on card, 1 page - 256K on card, 4 pages */
    HERCMONOHI = 0,  /* 720x348 2 pages         */
    ATT400C0   = 0,  /* 320x200 palette 0; 1 page   */
    ATT400C1   = 1,  /* 320x200 palette 1; 1 page   */
    ATT400C2   = 2,  /* 320x200 palette 2; 1 page   */
    ATT400C3   = 3,  /* 320x200 palette 3; 1 page   */
    ATT400MED  = 4,  /* 640x200 1 page          */
    ATT400HI   = 5,  /* 640x400 1 page          */
    VGALO      = 0,  /* 640x200 16 color 4 pages    */
    VGAMED     = 1,  /* 640x350 16 color 2 pages    */
    VGAHI      = 2,  /* 640x480 16 color 1 page     */
    PC3270HI   = 0,  /* 720x350 1 page          */
    IBM8514LO  = 0,  /* 640x480 256 colors      */
    IBM8514HI  = 1   /*1024x768 256 colors      */


Colors are As follows

    BLACK,          /* dark colors */
    BLUE,
    GREEN,
    CYAN,
    RED,
    MAGENTA,
    BROWN,
    LIGHTGRAY,
    DARKGRAY,       /* light colors */
    LIGHTBLUE,
    LIGHTGREEN,
    LIGHTCYAN,
    LIGHTRED,
    LIGHTMAGENTA,
    YELLOW,
    WHITE


                                                                   To be Continued...............

No comments:

Post a Comment

If the contents is insufficient or if there any error, please write here....


Suggestion are welcome:

Popular Posts