Object Oriented Programming Lab-12 (Graphics in C++ Using WinBGIm)
Object Oriented Programming Lab-12 (Graphics in C++ Using WinBGIm)
Lab Manual
Topic:
Graphics in C++ using WinBGIm
1
What is WinBGIm
WinBGIm is a framework for working with graphics in C++ environment. It was initially
composed by Borland with the name of Borland Graphics Interface and is usually available for16
bit DOS applications. It can be configured with MinGW or with Dev-C++. Here in this Lab manual
we’ll try to configure it with MinGW.
Graphics Libraries
Graphic libraries required can be downloaded from:
http://codecutter.org/tools/winbgim/
There are three file which are also available with this Lab-Manual:
Two header file
graphics.h
winbgim.h
And one lib file:
libbgi.a
And some Linker Code Lines
-lbgi
-lgdi32
-lcomdlg32
-luuid
-loleaut32
-lole32
BGI Documentation
2
o C:\Program Files\CodeBlocks\MinGW\lib
3. Copy the Linker Code lines given above as:
o Goto the Settings option in Code::Blocks Manu.
o Select the Compiler option under the Settings Menu.
o Select the Compiler as GNU GCC Compiler.
o Select the Linker settings Tab.
o And Copy Linker Code lines Under the space available in Other Linker
Option.
3
Sample Task 1: Drawing an Arc
Description
Arc draws a circular arc in the current drawing color centered at (x,y) with a radius given
by radius. The arc travels from stangle to endangle. If stangle equals 0 and endangle equals 360,
the call to arc draws a complete circle. The angle for arc is reckoned counterclockwise, with 0
degrees at 3 o'clock, 90 degrees at 12 o'clock, and so on.
Syntax
#include <graphics.h>
void arc(int x, int y, int stangle, int endangle, int radius);
Code
#include <graphics.h>
int main()
{
/* request autodetection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int stangle = 0, endangle = 180;
int radius = 50;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
/* draw arc */
arc(midx, midy, stangle, endangle, radius);
/* clean up */
getch();
closegraph();
return 0;
}
4
Sample Task 2: Drawing a rectangular, two-dimensional bar.
Description
bar() draws a filled-in, rectangular, two-dimensional bar. The bar is filled using the
current fill pattern and fill color. bar does not outline the bar; to draw an outlined twodimensional
bar, use bar3d with depth equal to 0. The upper left and lower right corners of the rectangle are
given by (left, top) and (right, bottom), respectively. The coordinates refer to pixels.
Syntax
#include <graphics.h>
void bar(int left, int top, int right, int bottom);
Code
#include <graphics.h>
int main()
{
/* request autodetection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy, i;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* loop through the fill patterns */
for (i=SOLID_FILL; i<USER_FILL; i++) {
/* set the fill style */
setfillstyle(i, getmaxcolor());
/* draw the bar */
bar(midx-50, midy-50, midx+50, midy+50);
getch();
}
/* clean up */
closegraph();
return 0;
}
5
Sample Task 3: Erasing and redrawing the entire graphics screen.
Description
cleardevice() is a function which erases the entire graphics screen and moves the CP (current
position) to home (0,0).
settextjustify() is used to set text alignment.
outtextxy() is used to display a message on graphics screen.
Syntax
#include <graphics.h>
void cleardevice(void);
void settextjustify(CENTER_TEXT, CENTER_TEXT);
void outtextxy(midx, midy, "Press any key to clear the screen:");
Code
#include <graphics.h>
int main(void)
{
/* request autodetection */
int gdriver = DETECT, gmode;
int midx, midy;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
/* for centering screen messages */
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* output a message to the screen */
outtextxy(midx, midy, "Press any key to clear the screen:");
getch(); /* wait for a key */
cleardevice(); /* clear the screen */
/* output another message */
outtextxy(midx, midy, "This way we can animate the objects: Now press for next screen...");
getch(); /* wait for a key */
6
cleardevice(); /* clear the screen */
7
Sample Task 4: Drawing different graphical objects simultaneously.
Description
This program draw different shapes like Circle, Rectangle, Lines, 3D Rectangular Bar and text
messages etc. This program also initializes a new window of user desired size(resolution).
Syntax
initwindow(800, 600, "Hello"); // Create a window with 800x600 resolution with “Hello” title
circle(x, y, radius);
setfillstyle(SOLID_FILL, BLUE);
floodfill(x, y, WHITE);
Code
#include "graphics.h"
int main()
{
// Create a window for 800 x 600 resolution, with a title 'Hello World'
initwindow(800, 600, "Hello");
8
// bar3d is filled by default
setfillstyle(HATCH_FILL, GREEN);
bar3d(80, 100, 180, 300, 15, 1);
// Draw lines of different color (total colors are 16, 0 = BLACK ... 15 = WHITE)
for (int i = 0; i <= 15; ++i)
{
setcolor(i);
line(430 + i*10, 100, 80, 430 + i*10);
}
getch();
// Closes the graphics mode
closegraph();
return 0;
}
9
Lab Tasks
Task 1:
Draw a circle in C++ using WinBGIm function arc(). You are not allowed to use the function circle().
Task 2
Draw the following shape in C++, you can only use the WinBGIm function line().
Task 3
Draw the following shape in C++ using WinBGIm functions.
Task 4
Draw the following shape in C++ using WinBGIm functions.
10