39

Introduction to graphics programming in c

Embed Size (px)

Citation preview

Page 1: Introduction to graphics programming in c
Page 2: Introduction to graphics programming in c

Date:27/03/2015

[email protected]

facebook.com/baabtra

twitter.com/baabtra

in.linkedin.com/in/baabtra

Introduction to Graphics

Programming in

Team: Golden Girlzz

Page 3: Introduction to graphics programming in c

Contents

❏ Setting Up graphics using gcc

❏ Basic Idea About Graphics Programming in C

❏ Some functions in Graphics

❏ Working Example

Page 4: Introduction to graphics programming in c

❏ Computer Graphics is one of the most powerful and interesting aspect

of computers.

❏ There are many things we can do in graphics apart from drawing

figures of various shapes.

❏ All video games, animation, multimedia predominantly works using

computer graphics.

Page 5: Introduction to graphics programming in c

❏ There is a large number of functions in C which are used for putting pixel

on a graphic screen to form lines, shapes and patterns.

❏ The Default output mode of C language programs is “Text” mode.

❏ We have to switch to “Graphic” mode before drawing any graphical shape

like line, rectangle, circle etc.

Page 6: Introduction to graphics programming in c

❏ First of all, we must include the ”graphics.h” header file in our source

program

❏ GCC compiler doesn’t provides inbuilt facility to run graphics.h library.

❏ So you are not able to run graphics in C language

❏ To run "graphics.h" library in Ubuntu, following are some simple steps.

Graphics in C

Page 7: Introduction to graphics programming in c

Make sure that you have basic compilers installed. For this run the command:

Install few packages that required. Run the command:

Run “graphics.h” in Ubuntu

#1

sudo apt-get install build-essential

#2

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

Page 8: Introduction to graphics programming in c

Now download libgraph

Copy libgraph-1.0.2.tar.gz to your home folder. Right click on it and select "Extract

Here".

Then run following commands one by one.

Now Ubuntu is ready to run graphics program. To do that add

#include<graphics.h> in your C program.

Here is a sample program to test it's working or not. sample.c

#3

cd libgraph-1.0.2

./configure

sudo make

sudo make install

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

#4

#include<graphics.h>

main() {

int gd=DETECT,gm;

initgraph(&gd,&gm,NULL);

closegraph();

}

Page 9: Introduction to graphics programming in c

❏ Run it with following command:

If you get following screen then graphics.h is working

gcc sample.c -o sample -lgraph

./sample

#5

Page 10: Introduction to graphics programming in c

Initializing Graphics Mode

❏ The initgraph function is used to switch the output from text mode to

graphics mode

❏ The initgraph function takes three arguments

❏ Syntax: –

intigraph(&graphics_driver,&graphics_mode,Path _to_driver);

Page 11: Introduction to graphics programming in c

❏ Example:

If you are working on Turbo C, use the path “c:\\tc\\bgi”

initgraph(&dr, &md, NULL );

Graphics Driver

Type

Initial Graphics

Mode

Directory Path of

Graphics Driver

Page 12: Introduction to graphics programming in c

Closing Graphics Mode

❏ Graphics mode must be closed at the end

❏ For that function closegraph() is used

❏ Syntax: –

closegraph();

Page 13: Introduction to graphics programming in c

Some Functions in

“graphics.h”

Page 14: Introduction to graphics programming in c

void line(int x1, int y1, int x2, int y2);

❏ Remarks

➔ line draws a line from (x1, y1) to (x2, y2) using the

current color

line()❏ line function is used to draw a line from a point(x1,y1) to

point(x2,y2)

❏ Syntax :-

Page 15: Introduction to graphics programming in c
Page 16: Introduction to graphics programming in c

void circle(int x, int y, int radius);

● (x , y) -> Center point of circle

● radius-> Radius of circle

circle()

❏ Circle function is used to draw a circle with center (x,y) and

third parameter specifies the radius of the circle.

❏ Syntax :-

Page 17: Introduction to graphics programming in c
Page 18: Introduction to graphics programming in c

void rectangle(int left, int top, int right, int

bottom);

rectangle()❏ Coordinates of left top and right bottom corner are required

to draw the rectangle.

❏ Syntax:-

Page 19: Introduction to graphics programming in c
Page 20: Introduction to graphics programming in c

void bar( left, top, right, bottom);

bar( )

❏ bar function draws a bar

❏ bar draws a filled-in, rectangular, two-dimensional bar.

❏ žSyntax:

Page 21: Introduction to graphics programming in c
Page 22: Introduction to graphics programming in c

void ellipse(int x, int y, int stangle, int endangle, int

xradius, int yradius);

ellipse()❏ Ellipse function is used to draw an ellipse, (x,y) are

coordinates of center of the ellipse, stangle is the

starting angle, end angle is the ending angle, and fifth

and sixth parameters specifies the X and Y radius of

the ellipse.

❏ Syntax :-

Page 23: Introduction to graphics programming in c
Page 24: Introduction to graphics programming in c

Note the Point:

In this, color can be represented

using the name of colors in

CAPITAL LETTER and also using

the numbers

Eg:

code 0 means BLACK

code 1 means BLUE

code 2 means GREEN

Page 25: Introduction to graphics programming in c

putpixel() and getpixel()

❏ getpixel() returns the color of pixel present at point(x, y)

❏ Syntax: –

❏ putpixel plots a pixel at a point(x, y) of specified color

❏ Syntax:-

int getpixel(int x, int y);

void putpixel(int x, int y, int color);

Page 26: Introduction to graphics programming in c

setbkcolor()

❏ setbkcolor function changes current background color e.g.

setbkcolor(YELLOW) changes the current background color to

YELLOW.

❏ Remember that default drawing color is WHITE and background color

is BLACK.

❏ Syntax :-

void setbkcolor(int color);

Page 27: Introduction to graphics programming in c

setcolor()

❏ setcolor() function changes current drawing color

❏ Syntax :-

❏ Example:

changes the current drawing color to YELLOW.

void setcolor(int color);

setcolor(YELLOW);

Page 28: Introduction to graphics programming in c

Text with Graphics

❏ There are functions in C language that draw text characters in

graphics mode.

❏ These functions can be used to mix text and graphics in the same

image.

❏ These functions also make it possible to change text font and very the

size of text.

Page 29: Introduction to graphics programming in c

outtext()

❏ displays a string in the viewport (graphics mode)

❏ Declaration:

❏ outtext display a text string, using the current justification settings and

the current font, direction, and size

❏ outtext outputs textstring at the current position (CP)

void outtext(char *textstring);

Page 30: Introduction to graphics programming in c

outtextxy()

❏ outtextxy displays a string at the specified location (graphics mode)

❏ Declaration:

❏ Remarks: outtextxy() display a text string, using the current

justification settings and the current font, direction, and size.(CP)

outtextxy() displays textstring in the viewport at the position (x, y)

void outtextxy(x, y, char *textstring);

Page 31: Introduction to graphics programming in c

Working

Page 32: Introduction to graphics programming in c

#include<graphics.h>

#include<stdio.h>

main() {

int gd = DETECT,gm,left=100,top=100,right=200,bottom=200;

int x=300,y=150,radius=50,color;

initgraph(&gd, &gm,NULL);

printf("Press any key to change the back color");

getch();

setbkcolor(2);

setcolor(RED);

outtext("Introduction to Graphics Programming....\n");

outtextxy(100,100,"Text at position x=100 and y=100");

color=getpixel(0,0);

printf("color of pixel at (0,0) = %d",color);

putpixel(25,25,YELLOW);

putpixel(25,26,YELLOW);

putpixel(25,27,YELLOW);

rectangle(left, top, right, bottom);

circle(x, y, radius);

bar(left+100,top+100,right+100,bottom+100);

line(left - 10, top + 150, left + 410, top + 150);

ellipse(x, y + 200, 0, 360, 100, 50);

outtextxy(left + 100, top + 325, "My First C Graphics Program");

getch();

closegraph();

return 0;

}

Page 33: Introduction to graphics programming in c

and the output is..

Page 34: Introduction to graphics programming in c
Page 35: Introduction to graphics programming in c
Page 36: Introduction to graphics programming in c

US UK UAE

7002 Hana Road,

Edison NJ 08817,

United States of America.

90 High Street,

Cherry Hinton,

Cambridge, CB1 9HZ,

United Kingdom.

Suite No: 51, Oasis Center,

Sheikh Zayed Road, Dubai,

UAE

Email to [email protected] or Visit baabtra.com

Looking for learning more about the above

topic?

Page 37: Introduction to graphics programming in c

India Centres

Emarald Mall (Big Bazar Building)

Mavoor Road, Kozhikode,

Kerala, India.

Ph: + 91 – 495 40 25 550

NC Complex, Near Bus Stand

Mukkam, Kozhikode,

Kerala, India.

Ph: + 91 – 495 40 25 550

Cafit Square IT Park,

Hilite Business Park,

Kozhikode

Kerala, India.

Email: [email protected]

TBI - NITC

NIT Campus, Kozhikode.

Kerala, India.

Start up Village

Eranakulam,

Kerala, India.

Start up Village

UL CC

Kozhikode, Kerala

Page 38: Introduction to graphics programming in c

Follow us @ twitter.com/baabtra

Like us @ facebook.com/baabtra

Subscribe to us @ youtube.com/baabtra

Become a follower @ slideshare.net/BaabtraMentoringPartner

Connect to us @ in.linkedin.com/in/baabtra

Give a feedback @ massbaab.com/baabtra

Thanks in advance

www.baabtra.com | www.massbaab.com |www.baabte.com

Page 39: Introduction to graphics programming in c

Want to learn more about programming or Looking to become a good programmer?

Are you wasting time on searching so many contents online?

Do you want to learn things quickly?

Tired of spending huge amount of money to become a Software professional?

Do an online course @ baabtra.com

We put industry standards to practice. Our structured, activity based courses are so designedto make a quick, good software professional out of anybody who holds a passion for coding.