How to include graphics.h in C?
How to include graphics.h in code blocks?
Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to a basic graphics library. <graphics.h> is a predefine header file in C, which allows you to use the graphics function in your C program. You need to use graphics.h header file into your C program.
You can draw geometrical shapes, design simple animations etc.
graphics.h is a header file containing functions that help you create diagrams such as circles, rectangles and other 2D figures. We can even do some basic animations, such as a moving car.
#include<graphics.h>
WinBGIM (Windows 7) or C graphics using the graphics.h functions can be used to draw, display text in various fonts, change colours, and do various other things. Using the graphics.h functions in the turbo c compiler, you can create graphics programs, animations, projects, and games. You can create circles, lines, rectangles, bars, and other geometrical shapes. You can fill them and change their colours using the available functions. The functions of the graphics.h header files are listed below. Each function is discussed in detail, including the arguments it requires, its description, potential errors when using that function, and a sample C graphics program with its output.
The header file specified in graphics.h is a header file that contains graphical functions. It is included using the preprocessor directive #include. Some of these functions include put pixel(), which plots a pixel with the specified color on a given point, and line(), which draws a line between two points.
How do I add graphics.h to Code Blocks?
Please complete the steps below in order to include "graphics.h" in code Blocks and successfully compile graphics code on Code blocks.
Step 1: Before creating "graphics.h" in Code Blocks, install the winBGIm graphics library. Windham can be downloaded from http://winbgim.codecutter.org/ or by following this link.
UPDATE: The library building at http://winbgim.codecutter.org/ is old, built around 2006. A linking error occurs when linking against that with a 64-bit compiler toolchain. A recent build of the library with 64-bit compatibility is available at here.
Step 2: Extract the downloaded file in step two. Three files will be present:
graphics.h
winbgim.h
libbgi.a
Step 3: Insert the files graphics.h and winbgim.h into the compiler directory's include folder. (If your computer's C drive has Code::Blocks installed, follow these steps: Disk C >> Program Files >> Code Blocks >> MinGW >> include. Add these two files, thereby pasting.)
Step 4: Copy and paste libbgi.an into the compiler directory's lib folder.
Step 5: Open Code: Blocks in step 5. Select Options >> Compiler >> Linker settings from the menu.
Step 6: Select "Link libraries" in that window and click the Add button. Go to the lib folder where the libbgi.a file was copied in step 4.
Step 7: Paste commands in the appropriate section (other linker options).
Step 8: lbgi lgdi32 lcomdlg32 luuid loleaut32 lole32
Step 9: There will still be an error if you attempt to compile graphics.h application in C or C++. To fix it, open the graphics.h file in Notepad++ (copied into the included folder in step 3). Line 302 should be changed to read, "int left=0, int top=0, int right=INT MAX, int bottom=INT MAX,"
Save the file in step ten. Done!
Note: Any C or C++ application that contains the graphics.h header file can now be built. You will still see the "fatal error: stream: no such file directory" issue even if you build C codes.
Overview
Visit link, to download WinBGIm.
Take it out to line 302 of graphics.h, open it. To make int top=0, modify int right=0.
Place the files graphics.h and winbgim.h in the compiler directory's include folder.
Add libbgi.a to your compiler directory's lib folder.
Using code: blocks access to the linker settings menu under Settings, Compiler and Debugger.
Libby.a file can be selected by clicking the Add button in the Link Libraries section.
Paste commands in the right part (i.e., other linker options).
-luuid -loleaut32 -lole32 -lbgi -lgdi32 -lcomdlg32
Press "OK"
How to setup in Dev C++:
Download graphics.h to the Dev-C++ folders' include/ subfolder.
To use the WinBGIm subdirectory of the Dev-C++ directories, download libbgi.a to the lib/ directory.
When a programme uses the #include graphics.h> directive, the linker must be told to link in specific libraries. Alt-P is the Dev-C++ command to perform this action. Type the following into the Linker box of the Parameters tab in the pop-up window:
-lbgi\s-lgdi32\s-lcomdlg32\s-luuid\s-loleaut32\s-lole32
Programs that use the WinBGIm graphics library can now be compiled and run.
C graphics examples
1. Drawing concentric circles
1. #include <graphics.h>
2. int main()
4. {
5. int gd = DETECT, gm;
6. int x = 320, y = 240, radius;
7.
8. initgraph(&gd, &gm, "C:\\TC\\BGI");
9.
10. for ( radius = 25; radius <= 125 ; radius = radius + 20)
11. circle(x, y, radius);
12.
13. getch();
14. closegraph();
15. return 0;
16. }
2. C graphics program moving car
1. #include <graphics.h>
2. #include <dos.h>
3.
4. int main()
5. {
6. int i, j = 0, gd = DETECT, gm;
7.
8. initgraph(&gd,&gm,"C:\\TC\\BGI");
9.
10. settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
11. outtextxy(25,240," Press any key on the keyboard to view the moving car");
12.
13. getch();
14.
15. for( i = 0 ; i <= 420 ; i = i + 10, j++ )
16. {
17. rectangle(50+i,275,150+i,400);
18. rectangle(150+i,350,200+i,400);
19. circle(75+i,410,10);
20. circle(175+i,410,10);
21. setcolor(j);
22. delay(100);
23.
24. if( i == 420 )
25. break;
26. if ( j == 15 )
27. j = 2;
28.
29. cleardevice(); // clear screen
30. }
31.
32. getch();
33. closegraph();
34. return 0;
35. }
Windows 7 or Vista graphics
Except for bar3d, which draws a three-dimensional bar, most of the functions are two-dimensional; you can also implement these functions using pre-existing algorithms. These functions can also be used in C++ programmes. These functions can be used to develop Windows 7 and Vista programmes using the Dev C++ compiler. You will need to download an additional package WinBGIm to do so.
Launch the Dev C++ compiler, navigate to Tools->Package Manager, click the install button, and browse the package location. Create a new project and choose WinBGIm. This library also includes many functions for image manipulation, such as opening image files, creating bitmaps and printing images, RGB colours, and mouse handling.
Steps:
When compiling graphics code in code Blocks IDE, an error message appears: "Cannot find graphics.h." This is because graphics.h runs do not exist in the code Blocks library folder. Set up the winBGIm library to successfully compile graphics code on code Blocks.
Uses:
The primary use of <graphics.h> library is to include and facilitate graphical operations in your program. For example, WinBGIM (Windows 7) or the graphics.h functions in C can create various shapes, show text in various fonts, alter colours, and more. You can create graphics-based projects, animations, games, and programmes using the functions in graphics.h provided by the turbo c compiler. You can draw many geometrical figures, including circles, lines, rectangles, and bars. Utilizing the offered functions, you can fill them and alter their hue. The functions of the graphics.h header files are listed below. Each function is explained together with the arguments it requires, its explanation, potential pitfalls, and an example C program.
C graphics can be used in the following:
- Functions
- Programs
C graphics apply to:
Programs Functions is not a typical header. At best, it needs to be updated and most frequently refers to Borland's BGI API header for DOS.
However, it is relatively straightforward; WinBGIm is a Win32 implementation of the BGI interface. The lowest-level Windows graphics interface, Win32 GDI calls, is used in its implementation. Given that the source code is available, it might be a straightforward method of learning how GDI functions.
However, WinBGIm is not at all cross-platform. Most higher-level GUI libraries, like wxWidgets and Qt, include simple graphics primitives if that is all you are after. There are suggested more straightforward libraries.