BGI (Borland Graphics Interface) is a very old graphics library available in Borland C++ and Turbo C++ IDE. Borland c++ and Turbo C++ both were discontinued. BGI graphics library was very popular, and still, students in our schools and colleges want to use it in their projects. Codeblocks IDE with MinGW compiler is widely used as C/C++ especially to develop cross-platform open-source software but BGI is not available in Codeblocks. Thanks to Michael Main, who created WinBGIm at the University of Colorado to use the BGI graphics library in windows with other compilers. There are many alternatives to winBGIm, But BGI is good for beginners because you don’t have to worry about whole new fancy functions, classes, etc.
However, the latest version of WinBGIm was released in Nov 2005 which is very old and no longer supported in a newer version of the MinGW compiler. We have to compile source code to make it run in the latest Codeblocks. In this article, I will provide instructions to compile source code and configure the library in Codeblocks IDE. I have also created a video about it. You can watch below:
If you don’t want to compile source code and fix errors by yourself then copy project files from my github https://github.com/smokindinesh/WinBGIm and go to configure library section.
Lets get started: Create Static Library Project
- First download WinBGIm Source Code from http://winbgim.codecutter.org/
- Extract it. (Note: Make sure you have download source code)
- Open Codeblocks and create a new project. (File -> New -> Project)
- From the Project template window select "Static Library" and fillup all necessary information to finish the project creation wizard.
- Go to project properties and select "Build targets" option and rename output filename with libbgi.a for both debug and release targets.
- Copy all source files that you have extracted in the earlier step and paste them to the project directory.
- Remove "main.c" file from the project.
- Add source files in the project (Select project in Workspace -> Right Click -> Add Files recursively). Select both Build and Release options in the Target window which pops-up after adding files.
- Now, a project is ready to build but there are errors in the source code. Please follow the instructions provided below to fix errors.
Instruction to fix errors:
- Source file "bgiout.cxx"
- Line 1: Replace '#include <stringstream>' with '#include <sstream>'
- Line 2: Remove ': virtual std::basic_ostringstream'
- Source file "winbgim.h"
- Line 302: Replace "right=0" with "top=0"
- Source file "graphics.h"
- Line 302: Replace "right=0" with "top=0"
- Source file "misc.cxx"
- Line 595 to 599: Remove "~". e.g Replace ~0xE0 with 0xE0.
- Source file "winthread.cxx"
- Line 103: Replace "SetWindowLong( hWindow, GWL_USERDATA, (LONG)pWndData )" with "SetWindowLongPtr( hWindow, GWLP_USERDATA, (LONG_PTR)pWndData )"
- Source file "drawing.cxx"
- Line 66: Replace "return (WindowData*)GetWindowLong( hWnd, GWL_USERDATA )" with "return (WindowData*)GetWindowLongPtr( hWnd, GWLP_USERDATA );"
- Note: In above steps 5 and 6, if GWLP_USERDATA didnot work then try with GWL_USERDATA
- Finally, compile project in Release target. I hope you will not encounter any other errors.
Configure winbgim library in Codeblocks.
- Create a folder in any directory and give the name "WinBGIm" and create include and lib folder inside it. (eg. c:\WinBGIm\include and c:\WinBGIm\lib).
- From the project created above, copy "graphics.h" and "winbgim.h" header files and paste it inside include directory, and "libbgi.a" file from bin/release project folder and paste it inside lib directory that you have created in step 2. (eg. c:\WinBGIm\include and c:\WinBGIm\lib).
- Open the code::blocks and go to File -> New -> Project or click create a new project link from the start page.
- From the project wizard select "empty project" and click "Go" button and provide all necessary information. Also, do not forget to add main.cpp file.
- Go to menu bar and select project-> Project properties. A dialogue box will appear and click "Project's build options". Another dialogue box will appear and click the "Search directories" tab. Under "Compiler" tab provide the path of WinBGIm include folder (e.g. c:\WinBGIm\include) and under "Linker" tab provide the path of WinBGIm lib folder (e.g. c:\WinBGIm\lib).
- Select "Linker Setting" tab and add "libbgi.a" file e.g path c:\WinBGIm\lib\libbgi.a. In "Other linker options" text box add linkers -lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32
- Now click ok.
Finally, you can now compile source code using "graphics.h". To test configuration please copy source code from the sample winBGIm graphics program from here. I hope, you found this guide helpful, and in case you need my help just leave a message.
Thanks for this Howto. WinBGIm is still the easiest 2D graphics library available.
ReplyDeleteThe alternative to installing it yourself is to use the current version of Code::Blocks EDU-Portable (Codeblocks-EP) from http://codeblocks.codecutter.net
It has templates for WinBGIm projects installed and all the necessary libraries pre-installed.
in Codeblocks-EP just start [File]->[New]->[Project]->[WinBGIm Project]. You'll be able to choose a graphics-only project type, or one with an extra console for standard input/output if you wish.
I get an issue with my code blocks: after i compile the code, i get the window but i doesnt draw normally. i mean, i draws the first line and then, if i resize the window with the mouse i can see the other lines. but with no delay. and i can not figure out what to do :( please help!
DeleteThanks y again. reply here so i will get notified! :D
DeleteCant get the output.... its opening the graphics.h header file
Deletefake bgi
DeleteThanks for information.Now I will install current version of code::blocks
ReplyDeletethank you very much.................
ReplyDeleteHi did this but got an error in building: "fatal error: sstream: No such file or directory||=== Build finished: 1 errors, 0 warnings ===|, will be looking this up on the internet...
ReplyDeleteMe again, now not so anonymous. sstream is there! My Hello world file has the extension c and not cpp. Does this mean that I can only use graphics.h with c++ files?
DeleteWell its not the "Hello world" example of course but this one:
Delete#include
int main()
{
int gd = DETECT, gm;
int x = 320, y = 240, radius;
initgraph(&gd, &gm, "C:\\TC\\BGI");
for ( radius = 25; radius <= 125 ; radius = radius + 20)
circle(x, y, radius);
getch();
closegraph();
return 0;
}
By the way, I don't know why but above the include does not mention graphics.h, and I'm using the mingw32-gcc.exe c compiler
DeleteMy program is terminated unexpectedly after giving a warning saying that :
DeleteDeprecated conversion from string constant to 'char*' [-Wwrite-strings]
What can i do?
I did this but it opened the graphics.h with error in it
ReplyDeleteyou may done some mistake in using graphics.h function so this error. compile this program http://codeincodeblock.blogspot.com/2011/09/analog-clock-source-code-using-c-and.html if error occurs then problem in your setup otherwise its works.
Deletein build messengers here a problem, like "sstream: no such file or directory" , how to slove it.
ReplyDeletecan you show sample code
Deletei too getting this error "sstream: no such file or directory". please help. how to solve it.thanks in advance.
DeleteWrite using a cpp file.
Deletepaste the .h file in this path : C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.7.1\include\c++
ReplyDeletebut me there is an error at line 302 of graphics.h . it says redefinition of int right and when i look in the code there are two int right and no int top at the spécified line is it an error of the new version ?
i have facing the same problem at line 302. if u could manage this problem let me know .
DeleteI tried the following steps after being suggested from
Deletehttp://stackoverflow.com/questions/20313534/how-to-use-graphics-h-in-codeblocks
1. Open the file graphics.h using either of Sublime Text Editor or Notepad++,from the include folder where you have installed Codeblocks.
2. Goto line no 302
3. Delete the line and paste
int left=0, int top=0, int right=INT_MAX, int bottom=INT_MAX,
in that line.
4. Save the file and start Coding.
To learn c graphics programming visit programmingcampus.
ReplyDeletei'm also getting the error "sstream: no such file or directory"
ReplyDeletegraphics.h use in source.cpp
DeleteHi Everybody,
ReplyDeletei've a problem with winbgim but i don't understand where i mistake.
i've installed following the procedure but with any kind of example code the run stop at initgraph(&gdriver, &gmode, ""). i think probably it can't reveal something. i tried also using initwindow(300,200) and always the .exe stop at this line...someone has idea?
thanks!
Paolo
I have no Idea what happen to you. You can download codeblocks-EP version from this link http://codeblocks.codecutter.org/ .In this version template for winBgim graphics is already installed so you don't have to setup winBgim. Just create the winBgim project and run the code.
DeleteThank you Dinesh, but i don't like that versione of CB...i'd prefer to setup the latest version or directly change graphics library (as OpenGL)
ReplyDeleteThanks a lot
Paolo
At me (on Code::Blocks) stop work when i want to int in graph mode
ReplyDeleteIs possible to work with graphics in c with ARM GNU GCC compiler..?? by using code blocks?? and using Winbgim library??
ReplyDeletei am getting some errors refering to missing references , such as 'settextalign@8'. Here is the source code that I am wanting to work:
ReplyDelete#include
#include "graphics.h"
#include "koolplot.h"
using namespace std;
int main()
{
initwindow(800,600);
int k;
float QT,Q1, Q2, Q3,Q4,Q5,Te,v1[200],v2[200],v3[200];
cout<<"Q1=";cin>>Q1;
cout<<"Q2=";cin>>Q2;
cout<<"Q3=";cin>>Q3;
cout<<"Q4=";cin>>Q4;
cout<<"Q5=";cin>>Q5;
cout<<"Te=";cin>>Te;
for(k=1;k<=200;k++)
{
if(k==1)
{v1[k]=1;
v2[k]=0.5;
v3[k]=1;
}
{if((v1[k]<=0.1) && (Q1=9.9)&&(Q1>Q2))
Q1=Q2;
if ((v1[k]<=0.1) && (Q3=9.9) && (Q3>Q4))
Q3=Q4;
QT=Q2+Q4;
if ((v1[k]<=0.1) && (QT=9.9) && (QT>Q5))
QT=Q5;
}v1[k+1]=v1[k]+Te*(Q1-Q2);
v2[k+1]=v2[k]+Te*(Q3-Q4);
v3[k+1]=v3[k]+Te*(Q2+Q4-Q5);}
for(k=1;k<=200;k++)
{cout<<v1[k]<<" "<<v2[k]<<" "<<v3[k];
plotdata x(0,int(v1));
plotdata y(0,int(v2));
plotdata z(0,int(v3));
plot(x, y);
plot(x,z);
plot(y,z);}}
this line: for(k=1;k<=200;k++) will access beyond the end of the v1, v2, v3 arrays. suggest: for(k=1;k<200;k++) In C, an array offset is in the range 0,,,(numberOfArrayEntries -1) so for a v1[200] the valid offsets are 0...199.
Deleteregarding this kind of line v1[k+1]=v1[k]+Te*(Q1-Q2);, v1[k+1] accesses the v1[] array beyond the end of the array, even if my prior comment is implemented. Accessing beyond the end of the upper bound of an array is undefined behaviour and can lead to a seg fault event.
Deletehey, i got everything up 'n' runnin' but it shows me an error in the graphics.h - it says:redefinition of int right
ReplyDeletethe other is:int right previously declared here
it opens graphic.h file when i built it.
ReplyDeleteThere must be some error in a function you are using.
DeletePost error properly.
getting error…:(
ReplyDeleteplease help…
error: sstream: No such file or directory
Change your file extension to .cpp from .c
DeleteOR Always select C++ while making new project in Code Blocks.
I hope that helps.
What should be the third parameter in initgraph() ? As it cannot be C:\\TC\\BGI for Codeblocks
ReplyDeleteDoesn't matter. Just put C:\\TC\\BGI in there and it will work.
DeleteIf it cannot find drivers in this directory, it looks up in current directory by default.
It still doesn't work for me... what should I install in order to get C:\\TC\\BGI?
Deletesame is with me please tell a solution
DeleteMy problem is different, it says: " cannot find -lole32? " and it comes from ld.exe
ReplyDeleteFile Line Message
ld.exe cannot find -lol32?
same problem of -lole32 :(
Deletei try but is show agin error
ReplyDelete----------------------------------------------------
i try basic program using graphics.h
---------------------------------------------------------
=====================================
#include
void main()
{
}
=======================================================
c:\program files (x86)\codeblocks\mingw\include\graphics.h|30|fatal error: sstream: No such file or directory|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 3 second(s)) ===|
Change your file extension to .cpp if it is .c
DeleteChange it to .cpp
It worked for me.
my problem is when I click on (Build and Run) Button my program generate some errors like as..
ReplyDeletec:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\graphics.h|302|error: redefinition of 'int right'|
but when I click on (Run) Button my program executed correctly...
help me:
ReplyDeleteld.exe cannot find -lbgi|
=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
to solve graphics.h error. U can see there is two times declared 'right'. One of those need to be changed into 'top' and that is it.
ReplyDeletei have some error in DEV c++ . when i wanna complie a program i see these error
ReplyDelete$(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)
above error for MAKEFile.win in the 25 line and section compiler write thise massage
[Error] id returned 1 exit statuse
plz help me
Is there a way to use initwindow() to open the window in the *center of the screen*? It is possible to specify the coords of the top-left corner of the window but how to read the size of the whole screen to be able to calculate the parameters?
ReplyDeleteuse the functions: getmaxx() and getmaxy() and divide the results by 2 to get the center of the screen.
DeleteIm using the new code blocks when i try to run my code in the graphic project i get this error : linking stage skipped (build target has no object file to link )
ReplyDeleteNothing to be done ( all items are up to date )
Im using the new code blocks when i try to run my code in the graphic project i get this error : linking stage skipped (build target has no object file to link )
ReplyDeleteNothing to be done ( all items are up to date )
All other programmes are running fine on mu codeblock 13.12 but when I runs a simple graphic programme compiler crashes giving pop up window that says "abc.exe has stopped working window is checking solution".
ReplyDeleteIt also giving a warning message on compilation
warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|
How to resolve it.
Advance Thanks
I have the same problem - also using c::b 13.12.
DeleteDid you find a solution?
I do have the same problem. Could anybody help us please?!
ReplyDelete