January 12, 2018

How to create graphical applications using Turbo C++ 3.0 IDE (BGI graphics library)



   The Borland Graphics Interface, also known as BGI, is a graphics library which provides several functions which can be used to create graphical applications. It is a 16-bit library and the programs based on this library will work on 16-bit and 32-bit systems.





How to Create Graphical Applications using Turbo C++ 3.0 IDE


Turbo C++ is one of the very common environments that you might be habituated in for your programming in C/C++. Here, we’ll show you how to create graphical applications using Turbo C++ 3.0 IDE and the BGI library. The header file “graphics.h” will be used to access the functions of this library. Using functions of graphics.h in turbo C++ compiler you can make graphics programs, animations, projects and games. You can draw circles, lines, rectangles, bars and many other geometrical figures.


Before we discuss more, you can see some of our applications created using BGI.



Structure of a typical graphics program using BGI


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

// Other header files (if any required)
int main()
{
    // request auto detection
    int gd=DETECT, gm, errorcode;
   
    // initialize graphics and local variables
    initgraph(&gd,&gm,"c://turboc3//bgi");
   
    // read result of initialization
    errorcode = graphresult();

    if (errorcode != grOk)// an error occurred
    {
        cout<<"Graphics error :: "<<grapherrormsg(errorcode);
        cout <<"\n press any key to halt: ";
        getch();
        exit(1);// terminate with an error code
    }

    // Insert the code to perform the actual tasks here
    closegraph();
    restorecrtmode();
    return 0;
}






A brief description of the above program


  • void initgraph(int *graphdriver, int *graphmode, char *pathtodriver) :


The function initgraph() switches over to the graphics mode. 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 setting (color, palette, current position, viewport, etc.) to their defaults, then resets graphresult to 0.

The first argument graphdriver indicates the address of the graphics driver, which directly communicates with the hardware.


Following table lists all the graphics drivers available :


Graphics Drivers
Graphics Drivers




The second parameter i.e. graphmode specifies the initial graphics mode. The graphmode number tells about the monitor, resolution, colors etc. If graphmode = DETECT, initgraph() sets the graphics mode to the highest resolution available for the detected driver.



Following table lists all the graphics modes available :



Graphics Modes
Graphics Modes



The last parameter i.e. 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).


  • int graphresult(void) :

The function graphresult() returns the error code for the last graphics operation that reported an error, then resets the error level to grOk. Therefore we must store the value returned by graphresult() in a variable and then test it to know whether an error was returned (and if there was an error, to know more about it).

The enumerated type graphics_errors defines the error codes.

Following table lists all the graphics errors :


Graphics Error Code
Graphics Error Code

  • char *grapherrormsg(int errorcode) :

The function grapherrormsg() takes errorcode as its argument and returns a pointer to the error message string associated with that value of errorcode.

For example, a sample error message is displayed below :



Graphics Error Message
Graphics Error Message

  • void closegraph(void) :

The function closegraph() closes the graphics mode and deallocates all the memory space allocated by this mode.

  • void restorecrtmode(void) : 

The function restorecrtmode() restores the screen mode to the initial setting i.e. the one before using initgraph().



My first graphics program


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


int main()
{
    // request auto detection
    int gd=DETECT, gm, errorcode;
    int midX,midY,radius=150;
    int color[]={RED,CYAN,YELLOW,GREEN};
                                          
    // initialize graphics and local variables
    initgraph(&gd,&gm,"c://turboc3//bgi");
   
    // read result of initialization
    errorcode = graphresult();

    if (errorcode != grOk)// an error occurred
    {
        cout<<"Graphics error :: "<<grapherrormsg(errorcode);
        cout <<"\n press any key to halt: ";
        getch();
        exit(1);// terminate with an error code
    }

                                                                
    midX=getmaxx()/2;
    midY=getmaxy()/2;


    for(int i=0;i<4;i++)
    {
        setfillstyle(SOLID_FILL,color[i]);
        fillellipse(midX,midY,radius,radius);
        radius-=40;
    }

    setcolor(WHITE);
    outtextxy(220,400,"My first graphics program");
                                                                                                    
    getch();
    closegraph();
    restorecrtmode();
    return 0;
}



Output


My first graphics program
My first graphics program








No comments:

Post a Comment