graphics programming in c under gnu linux (ubuntu distribution)

22
Tushar B Kute Nashik Linux User Group http://snashlug.org Graphics Programming in C under GNU / Linux (Ubuntu Distribution)

Upload: tushar-b-kute

Post on 15-May-2015

10.200 views

Category:

Technology


7 download

DESCRIPTION

How to do Graphics Programming in C under GNU Linux (Ubuntu distribution)

TRANSCRIPT

Page 1: Graphics Programming in C under GNU Linux (Ubuntu distribution)

Tushar B KuteNashik Linux User Group

http://snashlug.org

Graphics Programming in C under GNU / Linux (Ubuntu Distribution)

Page 2: Graphics Programming in C under GNU Linux (Ubuntu distribution)

Graphics Programming in C

● OpenGL● Gtk+● SDL Libgraph

Page 3: Graphics Programming in C under GNU Linux (Ubuntu distribution)

OpenGL

● OpenGL (Open Graphics Library) is a cross-language, multi-platform application programming interface (API) for rendering 2D and 3D computer graphics. The API is typically used to interact with a Graphics processing unit (GPU), to achieve hardware-accelerated rendering.

● OpenGL was developed by Silicon Graphics Inc. (SGI) from 1991 and released in January 1992 and is widely used in CAD, virtual reality, scientific visualization, information visualization, flight simulation, and video games. OpenGL is managed by the non-profit technology consortium Khronos Group.

Page 4: Graphics Programming in C under GNU Linux (Ubuntu distribution)

gtk+

● GTK+ (GIMP Toolkit) is a cross-platform widget toolkit for creating graphical user interfaces. It is licensed under the terms of the GNU LGPL, allowing both free and proprietary software to use it. It is one of the most popular toolkits for the X Window System, along with Qt.

● The name GTK+ originates from GTK; the plus was added to distinguish an enhanced version. It was originally created for the GNU Image Manipulation Program (GIMP), a free software raster graphics editor in 1997.

Page 5: Graphics Programming in C under GNU Linux (Ubuntu distribution)

SDL

● Simple DirectMedia Layer (SDL) is a cross-platform, free and open source multimedia library written in C that presents a simple interface to various platforms' graphics, sound, and input devices. It is widely used due to its simplicity. Over 700 games, 180 applications, and 120 demos have been posted on its website.

● SDL has the word "layer" in its title because it is actually a wrapper around operating-system-specific functions. The main purpose of SDL is to provide a common framework for accessing these functions. For further functionality beyond this goal, many libraries have been created to work on top of SDL.

Page 6: Graphics Programming in C under GNU Linux (Ubuntu distribution)

libgraph

● This project is not part of the GNU Project.● libgraph is an implementation of the TurboC graphics API (graphics.h)

on GNU/Linux using SDL. The library requires SDL for primitive graphics and SDL_image (to blit fonts). Functions for text display are based heavily on code "borrowed" from Karl Bartel's SFont library.

● The library is not very powerful or flexible. It is probably not suitable for use in production-quality applications. I see it more as a simple, easy-to-use 2D graphics interface - could be used for simple prototyping, visualization or studying graphics algorithms.

● It is simplified library to run graphics programs in C.

Page 7: Graphics Programming in C under GNU Linux (Ubuntu distribution)

SDL-Libgraph

● It works efficiently with open source C compiler gcc.

● It is required to add the sdl-libgraph library to current existing gcc.

● Let's see how to do it in GNU/Linux Ubuntu distribution. [Next slide]

Page 8: Graphics Programming in C under GNU Linux (Ubuntu distribution)

Step-1

● Make sure you have the basic compiler installed.● You need the build-essential package. For this, run

the command on terminal:

sudo apt-get install build-essential

● You must have internet connection for this command to work.

Page 9: Graphics Programming in C under GNU Linux (Ubuntu distribution)

Actual Demo

Page 10: Graphics Programming in C under GNU Linux (Ubuntu distribution)

Step-2

● You need to install a hand full of packages. You can simply run the following command and get it all done.

● sudo apt-get install libsdl-image1.2● libsdl-image1.2-dev● guile-1.8● guile-1.8-dev● libart-2.0-dev● libaudiofile-dev● libesd0-dev● libdirectfb-dev

● libdirectfb-extra

● libxext-dev

● x11proto-xext-dev

● libfreetype6

● libaa1

● libaa1-dev

● libslang2-dev

● libasound2

● libasound2-dev

Page 11: Graphics Programming in C under GNU Linux (Ubuntu distribution)

Single stroke command

● sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-1.8 guile-1.8-dev 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

Page 12: Graphics Programming in C under GNU Linux (Ubuntu distribution)

Actual Demo

Page 13: Graphics Programming in C under GNU Linux (Ubuntu distribution)

Step-3

● Now, download libgraph from: (http://download.savannah.gnu.org/releases/libgraph/libgraph-1.0.2.tar.gz)

● Then copy the file libgraph-1.0.2.tar.gz to our home folder. Right click on the file and select Extract here.

● Open a terminal and run the following commands, one by one.

cd libgraph-1.0.2

./configure

sudo make

sudo make install

sudo cp /usr/local/lib/libgraph.* /usr/lib

Page 14: Graphics Programming in C under GNU Linux (Ubuntu distribution)

Actual Demo

Page 15: Graphics Programming in C under GNU Linux (Ubuntu distribution)

Step-4

● Now you are ready to compile your C program!● Write your C program using any editor and save.● Open the terminal for the specified folder and then

run with:

gcc MyProg.c -lgraph

./a.out● Once all this installation process is done, you don't

need to follow this process again and again. Just compile and execute the program.

Page 16: Graphics Programming in C under GNU Linux (Ubuntu distribution)

Example: MyProg.c

#include<graphics.h>

int main()

{

int gd = DETECT, gm = 0;

initgraph(&gd, &gm,""); // initialize graphics

circle(300, 300, 150); // graphics function for circle

getch(); // this is must to hold output!

closegraph();

return 0;

}

Page 17: Graphics Programming in C under GNU Linux (Ubuntu distribution)

Execution of program

Page 18: Graphics Programming in C under GNU Linux (Ubuntu distribution)

Output Window

You may use print screen (PrtSc) button to take output.

By default, this image get stored in /home/Pictures.

Page 19: Graphics Programming in C under GNU Linux (Ubuntu distribution)

Final work

● You may use “Shotwell Photo Viewer” to crop the image output!

● Generally making background White and foreground Black create good effect in output for taking printout.

Page 20: Graphics Programming in C under GNU Linux (Ubuntu distribution)

Sample Program

int gd = DETECT, gm = 0;

initgraph(&gd, &gm,"");

setbkcolor(WHITE);

setcolor(BLACK);

circle(300, 300, 150);

getch();

closegraph();

Page 21: Graphics Programming in C under GNU Linux (Ubuntu distribution)

Final Output

Page 22: Graphics Programming in C under GNU Linux (Ubuntu distribution)

Thank you

[email protected]

http://tusharkute.com

This presentation is created using LibreOffice Impress 4.0.2.2 Use open source, be safe and secure