Computer Graphics Program source codes with full description.

Study of Various C Graphics Functions

Implementation and Using mouse in DOS

Implementation of DDA line algorithm with source code in C/C++.

Implementation of Bresenham Line algorithm with source code in C/C++.

Implementation of Midpoint Line algorithm with source code in C/C++.

Implementation of Bresenham Circle algorithm with source code in C/C++.

Implementation of Mid-point Circle algorithm with source code in C/C++.

Implementation of Mid-point Ellipse algorithm with source code in C/C++.

Implementation of Polygon Filling using Scan Fill with source code in C/C++.

Implementation of Polygon Filling using Flood Fill with source code in C/C++.

Implementation of Polygon Filling using Boundary Fill Algorithms.

Implementation of algorithm of 2D Transformation of an Object with source code in C/C++.

Implementation of Line Clipping using Cohen- Sutherland algorithm with source code in C/C++.

Implementation of Line Clipping using Liang-Barky algorithm with source code in C/C++.

Implementation of Polygon clipping using Sutherland-hodgeman algorithm with source code in C/C++.

Search

Saturday 9 April 2011

Study of Various Graphics Functions


  • Initgraph:-
    • Initializes the graphics system
    • Declaration:
    void far initgraph (int far *graphdriver, int far *graphmode, char far athtodriver);
    • Remarks:
      To start the graphics system, you must first call initgraph.
    • initgraph initializes the graphics system by loading a graphics driver
      from disk (or validating a registered driver) then putting the system into graphics mode.
    • initgraph also resets all graphics settings (color, palette, current position, viewport, etc.) to their defaults, then resets graphresult to 0.

     
Argument What It Is/Does 
*graphdriver 
  • Integer that specifies the graphics 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 graphics 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 graphics drivers (*.BGI) first.
  • If they're not there, initgraph looks in the current directory.
  • If pathtodriver is null, the driver files must be in the current directory.
  • This is also the path settextstyle searches for the stroked character font files (*.CHR).

 
  • *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 graphicsdriver, and graphmode is set to the current graphics mode.
  • You can tell initgraph to use a particular graphics 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 graphics driver and mode.
  • Normally, initgraph loads a graphics 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 graphics driver file (or several of them) directly into your executable program file.

     
  • Return Value:
    initgraph always sets the internal error code.

    • On success, initgraph sets the code to 0.
    • On error, initgraph sets *graphdriver to -2, -3, -4, or -5, and graphresult returns the same value.

     
  • See the enumeration graphics_errors for definitions of error codes and
    graphresult return values.


     
  • Portability:
DOS

Yes 
UNIX ANSI C++ Only 

 

 
Example:

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

 
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;

 
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "");

 
/* read result of initialization */
errorcode = graphresult();

 
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics 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;
}

 
  • closegraph:-

 

  • Shuts down the graphics system
  • Declaration: void far closegraph(void);
  • Remarks: closegraph deallocates all memory allocated by the graphics system.
  • It then restores the screen to the mode it was in before you called initgraph.
  • The graphics system deallocates memory, such as the drivers, fonts, and an internal buffer, through a call to _graphfreemem.

 


 
  • Return Value: None

 


 
  • Portability:
DOS

Yes 
UNIX ANSI C C++ Only 

 
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int x, y;

 
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "");

 
/* read result of initialization */
errorcode = graphresult();

 
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
x = getmaxx() / 2;
y = getmaxy() / 2;

 
/* output a message */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(x, y, "Press a key to close the graphics system:");

 
/* wait for a key */
getch();

 
/* closes down the graphics system */
closegraph();

 
printf("We're now back in text mode.\n");
printf("Press any key to halt:");
getch();
return 0;
}
  • getgraphmode, setgraphmode:-

 

  • getgraphmode returns the current graphics mode
  • setgraphmode sets the system to graphics mode, clears the screen
  • Declaration:
  • int far getgraphmode(void);
  • void far setgraphmode(int mode);

     
  • Remarks: getgraphmode returns the current graphics mode.

 

  • NOTE: Your program must make a successful call to initgraph or setgraphmode BEFORE calling getgraphmode.
  • setgraphmode selects a graphics mode different than the default one set by initgraph. It clears the screen and resets all graphics settings to their defaults.
  • mode must be a valid mode for the current device driver.
  • The enumeration graphics_modes, defined in GRAPHICS.H, gives names for the predefined graphics modes.

 

  • Return Value:
    • getgraphmode returns the graphics mode set by initgraph or setgraphmode
    • setgraphmode does not return.

 

  • If you give setgraphmode an invalid mode for the current device driver, graphresult returns -10 (grInvalidMode).

 

  • Portability:

 

DOS

Yes 
UNIX ANSI C C++ Only 

 

 
Examples:

 
1) getgraphmode example:-

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

 
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy, mode;
char numname[80], modename[80];

 
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

 
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
/* terminate with an error code */
exit(1);
}

 
midx = getmaxx() / 2;
midy = getmaxy() / 2;

 
/* get mode number and name strings */
mode = getgraphmode();
sprintf(numname, "%d is the current mode number.", mode);
sprintf(modename, "%s is the current graphics mode", getmodename(mode));

 
/* display the information */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, numname);
outtextxy(midx, midy+2*textheight("W"), modename);

 
/* clean up */
getch();
closegraph();
return 0;
}

 

 
2) setgraphmode example:-

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

 
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int x, y;

 
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

 
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}

 
x = getmaxx() / 2;
y = getmaxy() / 2;

 
/* output a message */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(x, y, "Press any key to exit graphics:");
getch();

 
/* restore system to text mode */
restorecrtmode();
printf("We're now in text mode.\n");
printf("Press any key to return to graphics mode:");
getch();

 
/* return to graphics mode */
setgraphmode(getgraphmode());

 
/* output a message */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(x, y, "We're back in graphics mode.");
outtextxy(x, y+textheight("W"), "Press any key to halt:");

 
/* clean up */
getch();
closegraph();
return 0;
}

 
  • getdefaultpalette, getpalette and setallpalette :-

 

  • getdefaultpalette returns the palette definition structure
  • getpalette gets information about the current palette
  • setallpalette changes all palette colors as specified

 

  • Declaration:
    • struct palettetype *far getdefaultpalette(void);
    • void far getpalette(struct palettetype far *palette);
    • void far setallpalette(struct palettetype far *palette);

 

  • Remarks:
    • getdefaultpalette finds the palettetype structure that contains the palette initialized by the driver during initgraph.
    • getpalette fills the palettetype structure *palette with information about the current palette's size and colors.
    • setallpalette sets the current palette to the values given in the palettetype structure *palette. setallpalette can partially (or completely) change the colors in the EGA/VGA palette.

 

  • NOTE: getpalette and setallpalette can NOT be used with the IBM-8514 driver. See setrgbpalette.

 

  • Valid colors depend on the current graphics driver and current graphics mode.

 

  • Changes made to the palette appear onscreen immediately. Each time a palette color changes, all occurrences of that color change to the new color value.

 


 

  • Return Value:
    • getdefault returns a pointer to the default palette set up by the current driver when that driver was initialized.
    • getpalette and setallpalette do not return.

 

  • If invalid input is passed to setallpalette, graphresult returns -11 (grError), and the current palette remains unchanged.

 

  • Portability:

 

DOS

Yes 
UNIXANSI C C++ Only 

 

Examples:

 
1) getdefaultpalette example:-

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

 
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int i;

 
/* structure for returning palette copy */
struct palettetype far *pal=(void *) 0;

 
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

 
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
/* terminate with an error code */
exit(1);
}

 
setcolor(getmaxcolor());

 
/* return a pointer to the default palette */
pal = getdefaultpalette();

 
for (i=0; i<16; i++)
{
printf("colors[%d] = %d\n", i, pal->colors[i]);
getch();
}

 
/* clean up */
getch();
closegraph();
return 0;
}

 
2) getpalette example:-

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

 
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
struct palettetype pal;
char psize[80], pval[20];
int i, ht;
int y = 10;

 
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

 
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
/* terminate with an error code */
exit(1);
}

 
/* grab a copy of the palette */
getpalette(&pal);

 
/* convert palette info. into strings */
sprintf(psize, "The palette has %d modifiable entries.", pal.size);

 
/* display the information */
outtextxy(0, y, psize);
if (pal.size != 0)
{
ht = textheight("W");
y += 2*ht;
outtextxy(0, y, "Here are the current values:");
y += 2*ht;
for (i=0; i<pal.size; i++, y+=ht)
{
     sprintf(pval, "palette[%02d]: 0x%02X", i, pal.colors[i]);
     outtextxy(0, y, pval);
}
}

 
/* clean up */
getch();
closegraph();
return 0;
}

 
3) setallpalette example:-

 

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

 
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
struct palettetype pal;
int color, maxcolor, ht;
int y = 10;
char msg[80];

 
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

 
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}

 
maxcolor = getmaxcolor();
ht = 2 * textheight("W");

 
/* grab a copy of the palette */
getpalette(&pal);

 
/* display the default palette colors */
for (color=1; color<=maxcolor; color++)
{
setcolor(color);
sprintf(msg, "Color: %d", color);
outtextxy(1, y, msg);
y += ht;
}
/* wait for a key */
getch();

 
/* black out the colors one by one */
for (color=1; color<=maxcolor; color++)
{
setpalette(color, BLACK);
getch();
}

 
/* restore the palette colors */
setallpalette(&pal);

 
/* clean up */
getch();
closegraph();
return 0;
}

 
  • getdrivername:-

 

  • Returns a pointer to the name of the current graphics driver
  • Declaration: char *far getdrivername(void);
  • Remarks: After a call to initgraph, getdrivername returns the name of the driver that is currently loaded.

 

  • Return Value:
    • Returns a pointer to a string with the name of the currently loaded graphics driver.

 

  • Portability:

 

DOS

Yes 
UNIX ANSI C C++ Only 

 


 

Example:

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

 
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;

 
/* stores the device driver name */
char *drivername;

 
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

 
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
/* terminate with an error code */
exit(1);
}

 
setcolor(getmaxcolor());

 
/* get name of the device driver in use */
drivername = getdrivername();

 
/* for centering text on the screen */
settextjustify(CENTER_TEXT, CENTER_TEXT);

 
/* output the name of the driver */
outtextxy(getmaxx() / 2, getmaxy() / 2,
drivername);

 
/* clean up */
getch();
closegraph();
return 0;
}

 

 
  • Getmoderange:-

 

  • Gets the range of modes for a given graphics driver
  • Declaration: void far getmoderange(int graphdriver, int far *lomode, int far*himode);
  • Remarks: getmoderange gets the range of valid graphics modes for the given graphics driver.

 


 
Argument What It Is/Does 
graphdriver Specified graphics driver:-

  • If graphdriver = -1, getmoderange gets the currently loaded driver modes.
  • If graphdriver specifies an invalid graphics driver,both *lomode and *himode are set to -1.
lomode 
  • Points to location where lowest permissible mode value is returned.
himode 
  • Points to location where highest permissible mode value is returned.

 

 
  • Return Value: None

 

  • Portability:

 

DOS

Yes 
UNIX ANSI C C++ Only 

 

 
Example:

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

 
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int low, high;
char mrange[80];

 
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

 
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}

 
midx = getmaxx() / 2;
midy = getmaxy() / 2;

 
/* get the mode range for this driver */
getmoderange(gdriver, &low, &high);

 
/* convert mode range info. into strings */
sprintf(mrange, "This driver supports modes %d..%d", low, high);

 
/* display the information */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, mrange);

 
/* clean up */
getch();
closegraph();
return 0;
}

 

 
  • graphdefaults:-

 

  • Resets all graphics settings to their defaults
  • Declaration: void far graphdefaults(void);

 

  • Remarks:
graphdefaults resets all graphics settings to their defaults:

  • sets the viewport to the entire screen.
  • moves the current position to (0,0).
  • sets the default palette colors, background color, and drawing color.
  • sets the default fill style and pattern.
  • sets the default text font and justification.

 

  • Return Value: None

 

  • Portability:

 

DOS

Yes 
UNIXANSI C C++ Only 

 

 
  • Example:

     

    #include <graphics.h>

    #include <stdlib.h>

    #include <stdio.h>

    #include <conio.h>


     

    int main(void)

    {

    /* request auto detection */

    int gdriver = DETECT, gmode, errorcode;

    int maxx, maxy;


     

    /* initialize graphics and local variables */

    initgraph(&gdriver, &gmode, "c:\\bor\\turbo5\\bgi");


     

    /* read result of initialization */

    errorcode = graphresult();

    if (errorcode != grOk) /* an error occurred */

    {

    printf("Graphics error: %s\n", grapherrormsg(errorcode));

    printf("Press any key to halt:");

    getch();

    exit(1); /* terminate with an error code */

    }


     

    maxx = getmaxx();

    maxy = getmaxy();


     

    /* output line with non-default settings */

    setlinestyle(DOTTED_LINE, 0, 3); line(0, 0, maxx, maxy);

    outtextxy(maxx/2, maxy/3, "Before default values are restored.");

    getch();


     

    /* restore default values for everything */

    graphdefaults();


     

    /* clear the screen */

    cleardevice();


     

    /* output line with default settings */

    line(0, 0, maxx, maxy);

    outtextxy(maxx/2, maxy/3, "After restoring default values.");


     

    /* clean up */

    getch();

    closegraph();

    return 0;

    }


     


     
  • Installuserdriver:-

 

  • Installs a vendor-added device driver to the BGI device driver table
  • Declaration: int far installuserdriver(char far *name, int huge (*detect)(void));
  • Remarks: With installuserdriver, you can add a vendor-added device driver to the BGI internal table.

 

Argument What It Is/Does 
Name detect 
  • Name of the new device driver file (filename.BGI) Points to an optional autodetect function that can accompany the new driver. This autodetect function takes no parameters and returns an integer value.

 

 
  • You can install up to 10 drivers at one time.

 

  • There are two ways to use the vendor-supplied driver:

     

    1) passing the driver number directly to initgraph:-

        Assume you have a new video card called the Spiffy Graphics Array (SpGA) and that the SpGA manufacturer provided you with a BGI device driver (SPGA.BGI).


     

    The easiest way to use this driver is to install it by calling installuserdriver and then passing the return value (the assigned driver number) directly to initgraph.


     

    2) linking in an autodetect function:-

       

    The more general way to use this hypothetical SpGA driver is to link in an autodetect function that will be called by initgraph as part of its hardware-detection logic. (Presumably, the manufacturer of the SpGA gave you this autodetect function).

 

When you install the driver (by calling installuserdriver), you pass the address of this autodetect function, along with the device driver's file name.

 
After you install the device driver's file name and the SpGA autodetect function, call initgraph and let it go through its normal autodetection process.

 
Before initgraph calls its own built-in autodetection function (detectgraph), it first calls the SpGA autodetect function.

 
  • If the SpGA autodetect function doesn't find the SpGA hardware, it returns a value of -11 (grError), and initgraph proceeds with its normal hardware detection logic.(This can include calling any other vendor-supplied autodetection functions in the order in which they were "installed").
  • If, however, the autodetect function determines that an SpGA is present, it returns a non-negative mode number.

 

  • initgraph then:

 


1) locates and loads SPGA.BGI
2) puts the hardware into the default graphics mode recommended by the autodetect function
3) returns control to your program.

 
  • Return Value: Returns the driver number you pass to initgraph to manually select the newly installed driver.

 

  • Portability:
DOS

Yes 
UNIX ANSI C C++ Only 

 
Example:

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

 
/* function prototypes */
int huge detectEGA(void);
void checkerrors(void);

 
int main(void)
{
int gdriver, gmode;

 
/* install a user written device driver */
gdriver = installuserdriver("EGA", detectEGA);

 
/* must force use of detection routine */
gdriver = DETECT;

 
/* check for any installation errors */
checkerrors();

 
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

 
/* check for any initialization errors */
checkerrors();

 
/* draw a line */
line(0, 0, getmaxx(), getmaxy());

 
/* clean up */
getch();
closegraph();
return 0;
}

 
/* detects EGA or VGA cards */
int huge detectEGA(void)
{
int driver, mode, sugmode = 0;

 
detectgraph(&driver, &mode);
if ((driver == EGA) || (driver == VGA))
/* return suggested video mode number */
return sugmode;
else
/* return an error code */
return grError;
}

 
/* check for and report any graphics errors */
void checkerrors(void)
{
int errorcode;

 
/* read result of last graphics operation */
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
}

 

 

 
  • registerbgidriver, registerfarbgidriver:-

 

  • Registers linked-in graphics driver
  • Declaration:
  • int registerbgidriver(void (*driver)(void));
  • int far registerfarbgidriver(void far *driver);
  • Remarks:
  • registerbgidriver enables a user to load a driver file and "register" the driver.
  • registerfarbgidriver is used to register far drivers.

     
  • Once the driver's memory location has been passed to registerbgidriver, initgraph uses the registered driver.

     
  • A user-registered driver can be loaded from disk onto the heap, or converted to an .OBJ file (using BINOBJ.EXE) and linked into the .EXE.
  • Calling registerbgidriver informs the BGI graphics system that the driver *driver was included at link time.
  • registerbgidriver checks the linked-in code for the specified driver. If the code is valid, it registers the code in internal tables.
  • Linked-in drivers are discussed in detail in UTIL.DOC.
  • By using the name of a linked-in driver in a call to registerbgidriver, you also tell the compiler (and linker) to link in the object file with that public name.

     
  • Far drivers
  • Far drivers are created with the /F switch of the BGIOBJ utility. This switch is described in UTIL.DOC (an online text file included with your distribution disks).

     
  • Return Value
  • On success, returns a negative graphics error code if the specified driver or font is invalid.
  • Otherwise, returns the driver number.

     
  • If you register a user-supplied driver, you MUST pass the result of registerbgidriver to initgraph as the drive number to be used.

     
  • Portability:


    DOS

    Yes 
    UNIX ANSI C C++ Only 




    Example (registerbgidriver only):



    #include <graphics.h>

    #include <stdlib.h>

    #include <stdio.h>

    #include <conio.h>



    int main(void)

    {

    /* request auto detection */

    int gdriver = DETECT, gmode, errorcode;



    /* register a driver that was added into graphics.lib */

    /* For information on adding the driver, see the

    /* BGIOBJ section of UTIL.DOC */

    errorcode = registerbgidriver(EGAVGA_driver);



    /* report any registration errors */

    if (errorcode < 0)

    {

    printf("Graphics error: %s\n", grapherrormsg(errorcode));

    printf("Press any key to halt:");

    getch();

    exit(1); /* terminate with an error code */

    }



    /* initialize graphics and local variables */

    initgraph(&gdriver, &gmode, "");



    /* read result of initialization */

    errorcode = graphresult();

    if (errorcode != grOk) /* an error occurred */

    {

    printf("Graphics error: %s\n", grapherrormsg(errorcode));

    printf("Press any key to halt:");

    getch();

    exit(1); /* terminate with an error code */

    }

    /* draw a line */

    line(0, 0, getmaxx(), getmaxy());



    /* clean up */

    getch();

    closegraph();

    return 0;

    }

 


 

 
  • Registerbgifont:-

 

  • Registers linked-in stroked-font code
  • Declaration: int registerbgifont(void (*font)(void));
    int registerfarbgifont(void far *font);

 

  • Remarks:
    • Calling registerbgifont informs the graphics system that the font *font was included at link time.
    • registerfarbgifont is used to register far fonts.

 

  • registerbgidriver checks the linked-in code for the specified font. If the code is valid, it registers the code in internal tables.
  • Linked-in fonts are discussed in detail under BGIOBJ in UTIL.DOC (an online text file included with your distribution disks).
  • By using the name of a linked-in font in a call to registerbgifont, you also tell the compiler (and linker) to link in the object file with that public name.
  • If you register a user-supplied font, you MUST pass the result of registerbgifont to settextstyle as the font number to be used.
  • Far fonts are created with the /F switch of the BGIOBJ utility. This switch is described in UTIL.DOC.

 

  • Return Value
    • On success, returns a negative graphics error code if the specified font is invalid.
    • Otherwise, returns the font number of the registered font.

       
  • Portability:

 

DOS

Yes 
UNIX ANSI CC++ Only 

 

 
Example (registerbgifont only):

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

 
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;

 
/* register a font file that was added into graphics.lib */
/* For information on adding the font, see the
/* BGIOBJ section of UTIL.DOC */
errorcode = registerbgifont(triplex_font);

 
/* report any registration errors */
if (errorcode < 0)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}

 
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

 
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}

 
midx = getmaxx() / 2;
midy = getmaxy() / 2;

 
/* select the registered font */
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4);

 
/* output some text */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, "The TRIPLEX FONT");

 
/* clean up */
getch();
closegraph();
return 0;
}

 

 

 

 

 
  • restorecrtmode:-

 

  • Restores screen mode to pre-initgraph setting
  • Declaration: void far restorecrtmode(void);

 

  • Remarks: restorecrtmode restores the original video mode detected by initgraph.

 

  • This function can be used in conjunction with setgraphmode to switch back and forth between text and graphics modes.
  • NOTE: Do NOT use textmode for this purpose.
  • Use textmode only when the screen is in text mode, to change to a different text mode.

 

  • Return Value:None

 

  • Portability:

 

DOS

Yes 
UNIX ANSI C C++ Only 

 

 
Example:

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

 
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int x, y;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

 
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
x = getmaxx() / 2;
y = getmaxy() / 2;

 
/* output a message */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(x, y, "Press any key to exit graphics:");
getch();

 
/* restore system to text mode */
restorecrtmode();
printf("We're now in text mode.\n");
printf("Press any key to return to graphics mode:");
getch();

 
/* return to graphics mode */
setgraphmode(getgraphmode());

 
/* output a message */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(x, y, "We're back in graphics mode.");
outtextxy(x, y+textheight("W"), "Press any key to halt:");

 
/* clean up */
getch();
closegraph();
return 0;
}

 

 
  • Setgraphbufsize:-

 

  • Changes the size of the internal graphics buffer.
  • Declaration: unsigned far setgraphbufsize(unsigned bufsize);

 

  • Remarks: Some of the graphics routines (such as floodfill) use a memory buffer that is allocated when initgraph is called, and released when closegraph is called.
  • The default size of this buffer, allocated by _graphgetmem, is 4,096 bytes.
  • You can make this buffer smaller (to save memory space) or bigger (if, for example, a call to floodfill produces error -7: Out of flood memory).
  • setgraphbufsize tells initgraph how much memory to allocate for this internal graphics buffer when it calls _graphgetmem.
  • You must call setgraphbufsize before calling initgraph. Once initgraph has been called, all calls to setgraphbufsize are ignored until after the next call to closegraph.

 

  • Return Value: setgraphbufsize returns the previous size of the internal buffer.

 

  • Portability:

 

DOS

Yes 
UNIX ANSI C C++ Only 

 
Example:

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

 
#define BUFSIZE 1000 /* internal graphics
buffer size */

 
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int x, y, oldsize;
char msg[80];

 
/* set the size of the internal graphics buffer */
/* before making a call to initgraph. */
oldsize = setgraphbufsize(BUFSIZE);

 
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

 
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}

 
x = getmaxx() / 2;
y = getmaxy() / 2;

 
/* output some messages */
sprintf(msg, "Graphics buffer size: %d", BUFSIZE);
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(x, y, msg);
sprintf(msg, "Old graphics buffer size: %d", oldsize);
outtextxy(x, y+textheight("W"), msg);
/* clean up */
getch();
closegraph();
return 0;
}

 

 
  • getgraphmode, setgraphmode:-

 

  • getgraphmode returns the current graphics mode
  • setgraphmode sets the system to graphics mode, clears the screen

 

  • Declaration:
    • int far getgraphmode(void);
    • void far setgraphmode(int mode);

 

  • Remarks:
    • getgraphmode returns the current graphics mode.

 

  • NOTE: Your program must make a successful call
  • to initgraph or setgraphmode BEFORE calling getgraphmode.
  • setgraphmode selects a graphics mode different than the default one set by initgraph. It clears the screen and resets all graphics settings to their defaults.
  • mode must be a valid mode for the current device driver.
  • The enumeration graphics_modes, defined in GRAPHICS.H, gives names for the predefined graphics modes.
  • You can use setgraphmode in conjunction with restorecrtmode to switch back and forth between text and graphics modes.

 

  • Return Value:
    • getgraphmode returns the graphics mode set by initgraph or setgraphmode
    • setgraphmode does not return.

 

  • If you give setgraphmode an invalid mode for the current device driver, graphresult returns -10 (grInvalidMode).

 

  • Portability:

 

DOS

Yes 
UNIX ANSI C C++ Only 

 
Examples:
  1. getgraphmode example:-

     

    #include <graphics.h>

    #include <stdlib.h>

    #include <stdio.h>

    #include <conio.h>


     

    int main(void)

    {

    /* request auto detection */

    int gdriver = DETECT, gmode, errorcode;

    int midx, midy, mode;

    char numname[80], modename[80];


     

    /* initialize graphics and local variables */

    initgraph(&gdriver, &gmode, "");


     

    /* read result of initialization */

    errorcode = graphresult();

    /* an error occurred */

    if (errorcode != grOk)

    {

    printf("Graphics error: %s\n", grapherrormsg(errorcode));

    printf("Press any key to halt:");

    getch();

    /* terminate with an error code */

    exit(1);

    }


     

    midx = getmaxx() / 2;

    midy = getmaxy() / 2;


     

    /* get mode number and name strings */

    mode = getgraphmode();

    sprintf(numname, "%d is the current mode number.", mode);

    sprintf(modename, "%s is the current graphics mode", getmodename(mode));


     

    /* display the information */

    settextjustify(CENTER_TEXT, CENTER_TEXT);

    outtextxy(midx, midy, numname);

    outtextxy(midx, midy+2*textheight("W"), modename);


     

    /* clean up */

    getch();

    closegraph();

    return 0;

    }


     
  2. setgraphmode example:-


    #include <graphics.h>

    #include <stdlib.h>

    #include <stdio.h>

    #include <conio.h>



    int main(void)

    { /* request auto detection */

    int gdriver = DETECT, gmode, errorcode;

    int x, y;



    /* initialize graphics and local variables */

    initgraph(&gdriver, &gmode, "");



    /* read result of initialization */

    errorcode = graphresult();

    if (errorcode != grOk) /* an error occurred */

    {

    printf("Graphics error: %s\n", grapherrormsg(errorcode));

    printf("Press any key to halt:");

    getch();

    exit(1); /* terminate with an error code */

    }

    x = getmaxx() / 2;

    y = getmaxy() / 2;



    /* output a message */

    settextjustify(CENTER_TEXT, CENTER_TEXT);

    outtextxy(x, y, "Press any key to exit graphics:");

    getch();

    /* restore system to text mode */

    restorecrtmode();

    printf("We're now in text mode.\n");

    printf("Press any key to return to graphics mode:");

    getch();



    /* return to graphics mode */

    setgraphmode(getgraphmode());



    /* output a message */

    settextjustify(CENTER_TEXT, CENTER_TEXT);

    outtextxy(x, y, "We're back in graphics mode.");

    outtextxy(x, y+textheight("W"), "Press any key to halt:");



    /* clean up */

    getch();

    closegraph();

    return 0;

    }

No comments:

Post a Comment

Copyright Text

Copyright @ LDRP Student Community, webmaster Rahul Bhadauriya