Showing posts with label C. Show all posts

graphics.h working in Linux - Ubuntu

Working on Linux distribution is a fun way, tricky and ....... (rest you know, if you r a linux user).

Although Ubuntu come pre-added programming language support for The Coders like you. But, it miss some dependencies which is required to add manualy.

Lets see how to get graphics.h header file working in Ubuntu.



There are several option available to do graphics programming using Ubuntu.

Using SDL

If you want to use graphics.h on Ubuntu platform you need to compile and install libgraph. It is the implementation of turbo c graphics API on Linux using SDL.

It is not very powerful and suitable for production quality application, but it is simple and easy to use for learning purpose.

You can download it from here.


  • Open terminal window by pressing Ctrl+Alt+T
  • First install build-essential by typing in terminal
sudo apt-get install build-essential

  •  Install some additional packages by typing

sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-1.8 \
  guile-1.8-dev libsdl1.2debian libart-2.0-dev libaudiofile-dev \
  libesd0-dev libdirectfb-dev libdirectfb-extra libfreetype6-dev \
  libxext-dev x11proto-xext-dev libfreetype6 libaa1 libaa1-dev \
  libslang2-dev libasound2 libasound2-dev

  • Now extract the downloaded libgraph-1.0.2.tar.gz file.
  • Goto extracted folder and run following command
 ./configure
make
sudo make install
sudo cp /usr/local/lib/libgraph.* /usr/lib

  • Now you can use #include<graphics.h> on ubuntu platform
  • Use following line in your program
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
Here is a sample program using graphics.h

/*  demo.c*/
#include<graphics.h>
int main()
{
   int gd = DETECT,gm,left=100,top=100,right=200,bottom=200,x= 300,y=150,radius=50;
   initgraph(&gd,&gm,NULL);
   rectangle(left, top, right, bottom);
   circle(x, y, radius);
   bar(left + 300, top, right + 300, bottom);
   line(left - 10, top + 150, left + 410, top + 150);
   ellipse(x, y + 200, 0, 360, 100, 50);
   outtextxy(left + 100, top + 325, "C Graphics Program");
   delay(5000);
   closegraph();
   return 0;
}

  • To compile it use
gcc demo.c -o demo -lgraph

  • To run type
./demo

Using OpenGL (via GLUT)

Although OpenGL is basically made for 3D programming, drawwing 2D shapes gives the basic outline and introduction to OpenGL and gives the idea about how to start drawing objects in OpenGL.

  • To install GLUT, open terminal and type sudo apt-get install freeglut3-dev
  • Here is a simple graphics program using GLUT
/*  demo.c*/
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/glu.h>
void setup() {   glClearColor(1.0f, 1.0f, 1.0f, 1.0f); }
void display()
   {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glColor3f(0.0f, 0.0f, 0.0f);
      glRectf(-0.75f,0.75f, 0.75f, -0.75f);
      glutSwapBuffers();
   }
int main(int argc, char *argv[])
  {
     glutInit(&argc, argv);
     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
     glutInitWindowSize(800,600);
     glutCreateWindow("Hello World");
     setup();
     glutDisplayFunc(display);
     glutMainLoop();
     return 0;
  }

  • Compile it using 
gcc demo.c -o demo -lglut -lGL 

  • Run is using 
 ./demo


Learn To Compile And Run C/C++ Code In Linux: 10 Steps To Follow

Are you interested in writing C or C++ programs on Ubuntu Linux instead of MS-Windows? Using the bash Terminal application you can compile a C or C++ program on any Linux distro, like Ubuntu, Red Hat, Fedora, Debian and others. You just need to install GNU C and C++ compiler collection, development tools, development libraries and IDE or text editor to write programs. Let's discuss the process step-wise:




Step #1: Install C/C++ compiler and related tools: 

Use the following yum command to install GNU C/C++ compiler, on Fedora, Red Hat, CentOS or Scientific Linux:
 # yum groupinstall 'Development Tools'

In case you are using Debian or Ubuntu Linux then you need the following apt-get command to install the same:
 $ sudo apt-get update
$ sudo apt-get install build-essential manpages-dev

Step #2: Verify installation: 

Type the following command to display the version number and location of the compiler on Linux:
$ whereis gcc
$ which gcc
$ gcc –version

Here is a sample of the output:



Step #3: How to Compile and Run C/C++ program on Linux: 

Create a file called demo.c using a text editor such as vi, emacs or joe:
#include
/* demo.c: My first C program on a Linux */
int main(void)
{
printf("Hello! This is a test prgoram.\n");
return 0;

Step #4: How to Compile the program on Linux? 

Use any one of the following syntax to compile the program called demo.c:

cc program-source-code.c -o executable-file-name

OR
gcc program-source-code.c -o executable-file-name

OR ## assuming that executable-file-name.c exists ##

cc demo.c -o demo

OR
## assuming demo.c exists in the current directory ##
make demo

If your code or C program has no error, then an executable file called demo will be successfully created by the compiler in the current directory, otherwise the code can be fixed. To verify this, type:
$ ls -l demo*

Step #5:How to run or execute the program called demo on Linux? 

Simply type the the program name:
$ ./demo
OR
$ /path/to/demo

Compiling and running a simple C++ program
Create a program called demo2.C as follows:
#include "iostream"
// demo2.C - Sample C++ prgoram
int main(void)
{
std::cout << "Hello! This is a C++ program.\n";
return 0;
}
To compile this program, enter:

make demo2


To run this program, type:

./demo2


Step #6: How to generate symbolic information for gdb and warning messages? 

The syntax is as follows C compiler:
cc -g -Wall input.c -o executable
The syntax is as follows C++ compiler:
g++ -g -Wall input.C -o executable

Step #7: How to generate optimized code on a Linux machine? 

The syntax is as follows C compiler:
cc -O input.c -o executable
The syntax is as follows C++ compiler:
g++ -O -Wall input.C -o executable
Step #8: How to compile a C program that uses math functions? 

The syntax is as follows when need pass the -lm option with gcc to link with the math libraries:
cc myth1.c -o executable -lm

Step #9: How to compile a C++ program that uses Xlib graphics functions? 

The syntax is as follows when need pass the -lX11 option with gcc to link with the Xlib libraries:
g++ fireworks.C -o executable -lX11

Step #10: How to compile a program with multiple source files? 

The syntax is as follows if the source code is in several files (such as light.c, sky.c, fireworks.c):
cc light.c sky.c fireworks.c -o executable
C++ syntax is as follows if the source code is in several files:
g++ ac.C bc.C file3.C -o my-program-name

Color-Table in C

Color-Table in C Programming Language

Following colors are available for use in c graphics programming.




Color Table :


COLORVALUE
BLACK0
BLUE1
GREEN2
CYAN3
RED4
MAGENTA5
BROWN6
LIGHTBLUE7
DARKGRAY8
LIGHTBLUE9
LIGHTGREEN10
LIGHTCYAN11
LIGHTRED12
LIGHTMAGENTA13
YELLOW14
WHITE15

Total number of colors available depend on current graphics driver and mode. Use colors name in capital letters, for example use setcolor(RED) not setcolor(red) the latter will give you an error. You may use number instead of color for example setbkcolor(GREEN) or setbkcolor(2) are same, but you are advised to use color name as it will improve readability of program.

Changing Text Color In C/C++ Programming Language

Are you a newbie programmer learning C/C++ Programming Languages. Here is a new trick for you to make others and your classmates surprise. This trick will tell you, how you can change the Text Color, Blink a Text, or Change the Background Color.


textcolor function is used to change the color of drawing text in c programs.

Declaration :- void textcolor(int color);
where color is an integer variable. For example 0 means BLACK color, 1 means BLUE, 2 means GREEN and soon. You can also use write appropriate color instead of integer. For example you can write textcolor(YELLOW); to change text color to YELLOW. But use colors in capital letters only.

A List of Color codes can be found here : Color-Table

1. C programming code to change text color

#include<stdio.h>
#include<conio.h>
 
void main()
{
   textcolor(RED);
   cprintf("C programming");
 
   getch();
   
}

2. C programming code to BLINK the Text

#include<stdio.h>
#include<conio.h>
 
void main()
{
   textcolor(MAGENTA+BLINK);
   cprintf("C programming");
 
   getch();
   
}

Note that we have used cprintf function instead of printf. This is because cprintf send formatted output to text window on screen and printf sends it to stdin.