October 7, 2011

Graphic Programming in C - Part 1

If in your everyday programming you are afraid of coding large algos. Then change your mind from now coz graphic programming demands more patience and hard work so if you were rusty at long coding till now then change your view from now!! and please don't be afraid of reading long posts also!

What are we going to do here?
Here i will tell you some basics of graphics.
The language will be TURBO C v 3.0 second WE WONT BE USING BGI!!!

Which graphic mode to use?
I am assuming windows platform but the same thing applies to any platform.
There is lot of fuss on tip topic since every machine config is diff some can get higher resolution while others cant…
so whats the way out?
Mode 13h(320x200x256) of course it is available on every platform.it is independent mode that means you are the one who will define every function like line, circle, ellipse etc..

ENTERING MODE 13h

Since there is no bgi you might wonder how to set screen to some resolution. But its quite easy use interrupts buddy. Here is what to do in turbo c

Lets talk about this code, Rainbow Screen

Code:

/*Rainbow Screen Code In C - MyCFiles.com */
#include<conio.h>
#include<stdio.h>
void setMCGA() {
asm mov ax,013h
asm int 10h
}
void putpixel(int x,int y,unsigned char col) {
asm {
mov ah , 0Ch
mov al , col
mov cx , x
mov dx , y
mov bh ,0
int 10h
}
}
void restoreNORMAL() {
asm {
mov ax,003h
int 10h
}
}
int main()
{
int i,j,k,h;
printf("graphic programming part 1");
printf("hit any key");
getch();
setMCGA();
for(i = 1;i <= 200; i++)
for(j = 1,h = 0;j <= 320; j++,h++) {
if(h>256) h = 0;
putpixel(j,i,h) ;
}
getch();
restoreNORMAL() ;
printf("ALL DONE!!");
getch();
return 0;
}
Output:




Now you can run your graphic codes in windows 7
Refer: Turbo C++ for windows Vista and Windows7 32 bit, 64 bit

Code Analysis:
here
_AX = 0x13;
geninterrupt(0x10);

or even better(and faster) in assembly

void setMCGA() {
asm mov ax,013h
asm int 10h
}
on calling this function will set screen to desired resolution of 13h.

Writing Something On Screen
as said in 13h you are going to decide how to do and what to do(thats the best thing to do with it).
Here again we will use interrupts.

_AH = 0Ch; //compulsory
_AL = pixel color
_CX = horizontal pos(column)
_DX = vertical pos(row)
_BH = page no(0 for us)
if i want the color 5 pixel to be put at 50,50pos (the top left coordinate is 0,0 remember)then in assembly its like

void putpixel(int x,int y,unsigned char col) {
asm {
mov ah , 0Ch
mov al , col
mov cx , x
mov dx , y
mov bh ,0
int 10h
}
}

putpixel(50,50,5);

here x is x coordinate while y is y coordinate and col is the color of pixel.
this is a sample procedure. Later we will amazingly reduce it to one line only.

Getting Back to Normal Screen 
when you are done with graphics to return back to normal console screen. Use this

void restoreNORMAL() {
asm {
mov ax,003h
int 10h
}
}

Now this will get back to normal console screen.

Searching for Colors
while at this is just begining you have to hunt for new color. . Later i will tell you how to control it via pallet..


Hope you enjoyed it!
Don’t forget to ask queries..

Related Posts :



0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Related Posts with Thumbnails