Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
165 views

Object Oriented Programming Lab-12 (Graphics in C++ Using WinBGIm)

The document is a lab manual for graphics programming in C++ using WinBGIm. It provides documentation and sample code for drawing basic graphical shapes and manipulating graphics screens using WinBGIm functions. The document explains how to configure WinBGIm in Code::Blocks and includes 4 sample tasks that demonstrate drawing shapes like circles, rectangles, and 3D bars using functions like arc(), line(), bar3d(), and cleardevice().

Uploaded by

Ahmad Abduhu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
165 views

Object Oriented Programming Lab-12 (Graphics in C++ Using WinBGIm)

The document is a lab manual for graphics programming in C++ using WinBGIm. It provides documentation and sample code for drawing basic graphical shapes and manipulating graphics screens using WinBGIm functions. The document explains how to configure WinBGIm in Code::Blocks and includes 4 sample tasks that demonstrate drawing shapes like circles, rectangles, and 3D bars using functions like arc(), line(), bar3d(), and cleardevice().

Uploaded by

Ahmad Abduhu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Object Oriented Programming

Lab Manual

Topic:
Graphics in C++ using WinBGIm

Lab Instructor: Ahmad Abduhu

Session: Spring 2020


School of Systems and Technology
UMT Lahore Pakistan

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

The complete documentation of WinBGIm is available at:


http://www.cs.colorado.edu/~main/cs1300/doc/bgi/index.html
or
http://www.csie.nuu.edu.tw/~nschou/Teaching/C+
+/Doc/Quincy/html/programmerHelp/winbgim/bgi/doc/index.html

Configuring WinBGIm with Code::Blocks


We must add the above described header file (graphics.h and winbgim.h) in the following
directory of Code::Blocks installation. Map the mingw folder in your Code::Blocks installation
folder .
1. Copy both graphics.h and winbgim.h files in:.
o C:\Program Files\CodeBlocks\MinGW\include
2. Copy libbgi.a file in

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 */

outtextxy(midx, midy, "Press any key to quit:");


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

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

outtextxy(x, x, "Message to Display");

circle(x, y, radius);

setfillstyle(SOLID_FILL, BLUE);

floodfill(x, y, WHITE);

setfillstyle( HATCH_FILL, GREEN);

bar3d(x1, y1, x2, y2, depth, 1);

rectangle(x1, y1, x2, y2); // Draw a hollow rectangle

Code

#include "graphics.h"
int main()
{
// Create a window for 800 x 600 resolution, with a title 'Hello World'
initwindow(800, 600, "Hello");

// Display some text


outtextxy(420, 290, "BGI is so easy!");

// Draw a circle (by default it will be drawn in WHITE color)


circle(300, 300, 100);
// Fill the circle with solid blue color
setfillstyle(SOLID_FILL, BLUE);
// Start filling from (301, 301) and fill untill WHITE color is found
// NOTE: Point given in flood fill MUST BE INSIDE the shape to fill
// AND the shap MUST BE a CLOSED SHAPE.
floodfill(301, 301, WHITE);

8
// bar3d is filled by default
setfillstyle(HATCH_FILL, GREEN);
bar3d(80, 100, 180, 300, 15, 1);

// Draw a hollow rectangle


rectangle(400, 350, 500, 500);

// 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

You might also like