Here I am posting a C program to Create a Star field effect in C. Also I have well Documented the code so that you can under Stand it well.
download the executable and source code file form exe form here
NOTE this will work only in widows XP or windows 2000 ... for VISTA or other OS you will need dosbox. download it form here http://www.dosbox.com/download.php?main=1
the code :-
/////////////////////////////////////////////////////////////////////
// 3D star field effect
// RITESH RANJAN .... 16.03.2008
//
// See guideline at http://programing-tutorial.blogspot.com/
// for compiling using TuboC++
// Find more Programing Hacking and Tutorial stuff at
// http://programing-tutorial.blogspot.com/
/////////////////////////////////////////////////////////////////////
#include< stdio.h >
#include< stdlib.h >
#include< time.h >
#include< dos.h >
#include< conio.h >
// include my graphics library
#include< ritgraph.h >
// user defined function for writing the pixel values
// to virtual screen (double buffer)
void virpixel(int , int, byte);
// structure to define position of star and it color by c
typedef struct star
{
int x,y,z;
byte c; // note byte if defined in ritgraph.h
};
byte * virscr=NULL; /// virtual screen pointer
int main()
{
int x,y,z,i,sx,sy; // sx and sy screen x and y positions
byte pal[256][3],temp[3]; // for pallet and color of star
time_t t;
srand((unsigned) time(&t)); // randomisation
randomize();
star st[70]; // take 70 stars
settextmode();
printf(" Another creation by Ritesh Ranjan ");
printf("\n \n\t This star field effect is created by ");
printf("\n\t by using double buffering for faster and smooth effect ");
printf("\n\n I would like to hear ur comments at... :- ritesh_ranjan007@yahoo.com");
printf("Find more Programing Hacking and Tutorial stuff at--\n");
printf("http://programing-tutorial.blogspot.com/");
printf("\n\npress any key to exit");
getch();
setvgamode(); // set the screen mode form text mode to vga mode in ritgraph.h
//////////////////// create virtual screen //////////////////////////////
virscr=(byte *)(calloc(64000,1));
if(virscr==NULL)
{
printf(" error in calloc ");
getch();
exit(1);
}
/////////////// Craete the white shade for star ////////////////
for(i=10;i>0;i--)
setpal(i,55-3*i,55-3*i,55-3*i); /// white shade
for(i=25;i>10;i--)
setpal(i,25,25,25);
for(i=0;i<70;i++)
st[i].z=0;
/////////////// create stars //////////////////////
for(i=0;i<70;i++)
{
if(st[i].z<1)
{
st[i].x=(rand()%300)-150;
st[i].y=(rand()%199)-100;
st[i].z=256;
/////////////////// colouring of stars //////////////
if(st[i].x>0 && st[i].y>0)
st[i].c= (st[i].x+st[i].y)/25 ;
else if(st[i].x<0 && st[i].y>0)
st[i].c= ((-1)*st[i].x+st[i].y)/25;
else if(st[i].x>0 && st[i].y<0)
st[i].c=(st[i].x+(-1)*st[i].y)/25;
else
st[i].c=(byte)( ( (-1)*(st[i].x+st[i].y) ) /25);
}
// transformation of 3d cordinate to 2d system for showing in screen
// note sx and sy and real x,y coodinate of screen calculated for each star
sx=((st[i].x*256)/st[i].z + 150);
sy=((st[i].y*256)/st[i].z + 100);
// if inside screen of the write the pixel to virtual screen
if(sx>0 && sx<320 && sy>0 && sy<200)
virpixel(sx,sy,st[i].c);
}
// after calculating the whole screen copy to main VGA memory
_fmemcpy(vga,virscr,64000);
///////////////////////// move b/w stars ///////////////
// start infinite loop
while(!kbhit())
{
for(i=0;i<70;i++)
{
if(st[i].z<1)
{
st[i].x=(rand()%300)-150;
st[i].y=(rand()%199)-100;
st[i].z=256;
if(st[i].x>0 && st[i].y>0)
st[i].c=(st[i].x+st[i].y)/12;
else if(st[i].x<0 && st[i].y>0)
st[i].c=((-1)*st[i].x+st[i].y)/12;
else if(st[i].x>0 && st[i].y<0)
st[i].c=(st[i].x+(-1)*st[i].y)/12;
else
st[i].c=(-1)*(st[i].x+st[i].y)/12;
}
else
st[i].z=st[i].z-2;
sx=((st[i].x*256)/st[i].z + 150);
sy=((st[i].y*256)/st[i].z + 100);
if(sx>0 && sx<320 && sy>0 && sy<200)
virpixel(sx,sy,st[i].c);
else
st[i].z=0;
}
////////////// copy from virtual screen to real ///////
_fmemcpy(vga,virscr,64000);
memset(virscr,0,64000); // clear the virtual screen
waitretrace(); // wait for the screen to draw defined in ritgraph.h
}
free(virscr);
settextmode();
return 0;
}
///////////// fuction to write in virtual memory ///////////////////////
void virpixel(int x,int y,unsigned char col)
{
//this is x+y*2^8+y*2^6 = x+y*320
// remeber bit wise letfshit is faster than mutiplication
virscr[x+(y<<8)+(y<<6)]=col;
}
// 3D star field effect
// RITESH RANJAN .... 16.03.2008
//
// See guideline at http://programing-tutorial.blogspot.com/
// for compiling using TuboC++
// Find more Programing Hacking and Tutorial stuff at
// http://programing-tutorial.blogspot.com/
/////////////////////////////////////////////////////////////////////
#include< stdio.h >
#include< stdlib.h >
#include< time.h >
#include< dos.h >
#include< conio.h >
// include my graphics library
#include< ritgraph.h >
// user defined function for writing the pixel values
// to virtual screen (double buffer)
void virpixel(int , int, byte);
// structure to define position of star and it color by c
typedef struct star
{
int x,y,z;
byte c; // note byte if defined in ritgraph.h
};
byte * virscr=NULL; /// virtual screen pointer
int main()
{
int x,y,z,i,sx,sy; // sx and sy screen x and y positions
byte pal[256][3],temp[3]; // for pallet and color of star
time_t t;
srand((unsigned) time(&t)); // randomisation
randomize();
star st[70]; // take 70 stars
settextmode();
printf(" Another creation by Ritesh Ranjan ");
printf("\n \n\t This star field effect is created by ");
printf("\n\t by using double buffering for faster and smooth effect ");
printf("\n\n I would like to hear ur comments at... :- ritesh_ranjan007@yahoo.com");
printf("Find more Programing Hacking and Tutorial stuff at--\n");
printf("http://programing-tutorial.blogspot.com/");
printf("\n\npress any key to exit");
getch();
setvgamode(); // set the screen mode form text mode to vga mode in ritgraph.h
//////////////////// create virtual screen //////////////////////////////
virscr=(byte *)(calloc(64000,1));
if(virscr==NULL)
{
printf(" error in calloc ");
getch();
exit(1);
}
/////////////// Craete the white shade for star ////////////////
for(i=10;i>0;i--)
setpal(i,55-3*i,55-3*i,55-3*i); /// white shade
for(i=25;i>10;i--)
setpal(i,25,25,25);
for(i=0;i<70;i++)
st[i].z=0;
/////////////// create stars //////////////////////
for(i=0;i<70;i++)
{
if(st[i].z<1)
{
st[i].x=(rand()%300)-150;
st[i].y=(rand()%199)-100;
st[i].z=256;
/////////////////// colouring of stars //////////////
if(st[i].x>0 && st[i].y>0)
st[i].c= (st[i].x+st[i].y)/25 ;
else if(st[i].x<0 && st[i].y>0)
st[i].c= ((-1)*st[i].x+st[i].y)/25;
else if(st[i].x>0 && st[i].y<0)
st[i].c=(st[i].x+(-1)*st[i].y)/25;
else
st[i].c=(byte)( ( (-1)*(st[i].x+st[i].y) ) /25);
}
// transformation of 3d cordinate to 2d system for showing in screen
// note sx and sy and real x,y coodinate of screen calculated for each star
sx=((st[i].x*256)/st[i].z + 150);
sy=((st[i].y*256)/st[i].z + 100);
// if inside screen of the write the pixel to virtual screen
if(sx>0 && sx<320 && sy>0 && sy<200)
virpixel(sx,sy,st[i].c);
}
// after calculating the whole screen copy to main VGA memory
_fmemcpy(vga,virscr,64000);
///////////////////////// move b/w stars ///////////////
// start infinite loop
while(!kbhit())
{
for(i=0;i<70;i++)
{
if(st[i].z<1)
{
st[i].x=(rand()%300)-150;
st[i].y=(rand()%199)-100;
st[i].z=256;
if(st[i].x>0 && st[i].y>0)
st[i].c=(st[i].x+st[i].y)/12;
else if(st[i].x<0 && st[i].y>0)
st[i].c=((-1)*st[i].x+st[i].y)/12;
else if(st[i].x>0 && st[i].y<0)
st[i].c=(st[i].x+(-1)*st[i].y)/12;
else
st[i].c=(-1)*(st[i].x+st[i].y)/12;
}
else
st[i].z=st[i].z-2;
sx=((st[i].x*256)/st[i].z + 150);
sy=((st[i].y*256)/st[i].z + 100);
if(sx>0 && sx<320 && sy>0 && sy<200)
virpixel(sx,sy,st[i].c);
else
st[i].z=0;
}
////////////// copy from virtual screen to real ///////
_fmemcpy(vga,virscr,64000);
memset(virscr,0,64000); // clear the virtual screen
waitretrace(); // wait for the screen to draw defined in ritgraph.h
}
free(virscr);
settextmode();
return 0;
}
///////////// fuction to write in virtual memory ///////////////////////
void virpixel(int x,int y,unsigned char col)
{
//this is x+y*2^8+y*2^6 = x+y*320
// remeber bit wise letfshit is faster than mutiplication
virscr[x+(y<<8)+(y<<6)]=col;
}
No comments:
Post a Comment