C and C++ Programming Resources

Share/Save/Bookmark
Custom Search

Graphics in C Language

Posted on September 10th, 2008

in C Language

We will restrict our discussion on in C Language to 16 bit and MS DOS environment. In a C Program first of all you need to initialize the drivers on the computer. This is done using the initgraph method provided in .h library. In the next few pages we will discuss .h library in details. Important functions in graphic.h library will be discuees in details and samples programmes will be provided to show the power of language.

mode Initialization

First of all we have to call the initgraph function that will intialize the mode on the computer. initigraph have the following prototype.

 void initgraph(int far *graphdriver, int far *graphmode, char far *pathtodriver);

Initgraph initializes the system by loading a driver from disk (or validating a registered driver) then putting the system into

mode.Initgraph also resets all settings (color, palette, current position, viewport, etc.) to their defaults, then resets graphresult to 0.

*graphdriver

Integer that specifies the driver to be used. You can give graphdriver a value using a constant of the graphics_drivers enumeration type.

*graphmode

Integer that specifies the initial mode (unless *graphdriver = DETECT). If *graphdriver = DETECT, initgraph sets *graphmode to the highest resolution available for the detected driver. You can give *graphmode a value using a constant of the graphics_modes enumeration type.

*pathtodriver

Specifies the directory path where initgraph looks for drivers (*.BGI) first.

  1. If they’re not there, initgraph looks in the current directory.
  2. If pathtodriver is null, the driver files must be in the current directory.

*graphdriver and *graphmode must be set to valid graphics_drivers and graphics_mode values or you’ll get unpredictable results. (The exception is graphdriver = DETECT.)

After a call to initgraph, *graphdriver is set to the current driver, and *graphmode is set to the current mode. You can tell initgraph to use a particular driver and mode, or to autodetect the attached video adapter at run time and pick the corresponding driver. If you tell initgraph to autodetect, it calls detectgraph to select a driver and mode.

Normally, initgraph loads a driver by allocating memory for the driver (through _graphgetmem), then loading the appropriate .BGI file from disk.As an alternative to this dynamic loading scheme, you can link a driver file (or several of them) directly into your executable program file.

Here is a sample program that initializes the mode in C Language.

#include <.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
  int gdriver = DETECT, gmode, errorcode;
/* initialize  mode */
  initgraph(&gdriver, &gmode, "");
/* read result of initialization */
  errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
  {
    printf(" error: %s\n", grapherrormsg(errorcode));
    printf("Press any key to halt:");
    getch();
    exit(1); /* return with error code */
  }
/* draw a line */
  line(0, 0, getmaxx(), getmaxy());
/* clean up */
  getch();
  closegraph();
  return 0;
}

The programming in c language is discussed in brief to provide an over view to the beginner.

/* Sample program to draw a circle*/
#include<.h>
#include<conio.h>
main()
{
  int gd=DETECT,gm;
  initgraph(&gd,&gm,""); /* initialization of graphic mode */
  circle(150,150,100);
  getch();
  closegraph(); /* Restore orignal screen mode */
}
/* End of program */

Normally the screen which u view in DOS is in the text mode which means it is meant for text. And for u need to initialize mode. And for this to happen u need to include .h?.

circle(x coordinate ,y coordinate , radius);

The circle command takes a X coordinate which means Vertical axis and Y coordinate which means Horizontal axis. And the last one is the radius of the circle. closegraph();

With out this function the screen mode will still remain in graphic mode and when u come out, to DOS u will see a different screen, which is not in the text mode.

/*A program to draw a space with stars*/
#include<.h>
main()
{
  int gd=DETECT,gm;
  int i,x,y;
  initgraph(&gd,&gm,"");
  line(0,0,640,0);
  line(0,0,0,480);
  line(639,0,639,480);
  line(639,479,0,479);
  for(i=0;i<=1000;i++)
  {
    x=rand()%639;
    y=rand()%480;
    putpixel(x,y,15);
  }
  getch();
  closegraph();
}
/* End of program */
/*Here a sample program to illustrate how to use BARS which are used for visual statistics */
#include<.h>
main() {
  int gd=DETECT,gm,maxx,maxy,x,y,button;
  initgraph(&gd,&gm,"");
  line(80,150,200,150);
  line(80,150,80,50);
  settextstyle(1,HORIZ_DIR,1);
  outtextxy(100,153,"<-X axis");
  settextstyle(1,VERT_DIR,1);
  outtextxy(60,50,"<-Y axis");
  bar(100,100,120,150);
  bar(130,120,150,150);
  getch();
  closegraph();
}

Pages: [Page - 1] [Page - 2] [Page - 3] [Page - 4]

Tags: , ,

Like What you See?

Become one of the regulars by subscribing! You'll be the first to know when we add more great posts just like this. Join up by either RSS Feeds or Email Updates today!

There are 130 Comments to this post. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response or TrackBack from your own site.


Leave a Reply

You must be logged in to post a comment.