هذا برنامج وصل عبر بريدي ولم اجربه ولن اجربه

قد يستفيد البعض منه وقد يكون مقلب لأن وصلني من شخص مجهول .
// Clock6 Project
#include <GL/glut.h>
#include <time.h>
#include <stdlib.h>
//structur containing all values needed for clock
struct tm *time_tm;
float hour_float,min_float,sec_float;
char * time_char;
//time_t is a predefined class in time.h
//ltime is used as a parameter for the function localtime
time_t ltime;
//a boolean variable later used in the program
bool analog=true;
void bitmap_text(float x,float y,char * string,void *font,int start,int
len)
{
int i;
glRasterPos2f(x,y);
for(i=start;i<start+len;i++)
glutBitmapCharacter(font,string[i]);
}
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
//Displaying the clock
void display(void)
{
int i;
//char one_char[2];
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
glPushMatrix();
//for 3D use
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
//to specify the clock type, if true then clock is analog
//otherwise it's a digital clock
if(analog){
//drawing the outside shap of the clock
glutSolidTorus(0.3,2.3,50,12) ;
glutSolidSphere(1.8,120,120);
//we could use "glutWireSphere(1.8,120,120);" instead
}
else{ // digital
//we slightly rotate the cube so that it's fully displayed
glRotatef ( 45, 1.0, 1.0, 0.0);
glutSolidCube(2.0);
}
//disabling the 3d features so that we could use colors
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
if(analog){
// drawing the minute markers
glColor3f(1.0, 1.0, 1.0);
for(i=0;i<60;i++){
//we rotate around the Z-axis
glRotatef ( 6.0, 0.0, 0.0, 1.0); //minutes, 360/60=6
glRectf(1.9,0.015,1.7,-0.015);
}
// drawing the hour markers
glColor3f(1.0, 1.0, 0.0);
for(i=0;i<12;i++){
glRotatef (30.0, 0.0, 0.0, 1.0); //hours, 360/12=30
glRectf(1.9,0.04,1.6,-0.04);
}
// drawing the four quarter markers, hours 3,6,9,12
glColor3f(1.0, 0.5, 0.7);
for(i=0;i<4;i++){
glRotatef ( 90, 0.0, 0.0, 1.0);
glRectf(1.9,0.055,1.6,-0.055);
}
glColor3f(1.0, 1.0, 1.0);
char x[1]; //bitmap takes a string parameter
for( char cc='1';cc<='9';cc++){
glRotatef (-30.0, 0.0, 0.0, 1.0);
x[0]=cc;
//Numbering hour markers
bitmap_text(0.0,1.5,x,GLUT_BITMAP_HELVETICA_18,0,2);
}
glRotatef (-30.0, 0.0, 0.0, 1.0);
bitmap_text(0.0,1.5,"10",GLUT_BITMAP_HELVETICA_18,0,2);
glRotatef (-30.0, 0.0, 0.0, 1.0);
bitmap_text(0.0,1.5,"11",GLUT_BITMAP_HELVETICA_18,0,2);
glRotatef (-30.0, 0.0, 0.0, 1.0);
bitmap_text(0.0,1.5,"12",GLUT_BITMAP_HELVETICA_18,0,2);
glColor3f(1.0, 0.0, 0.0);
// drawing the hour hand
glPushMatrix();
//the drawing angle
glRotatef ((GLfloat) hour_float, 0.0, 0.0, 1.0);
//casting is optional
glBegin(GL_POLYGON);
glVertex2f(-0.05,0.0);
glVertex2f(-0.15,1.2);
glVertex2f(0.0,1.4);
glVertex2f(0.15,1.2);
glVertex2f(0.05,00);
glEnd();
glPopMatrix(); //every push has to have a pop assosiated with it
glPushMatrix();
// drawing the minute hand
glRotatef ( min_float, 0.0, 0.0, 1.0); //without casting
glColor3f(1.0, 0.3, 0.0);
glBegin(GL_POLYGON);
glVertex2f(-0.03,0.0);
glVertex2f(-0.1,1.5);
glVertex2f(0.0,1.7);
glVertex2f(0.1,1.5);
glVertex2f(0.03,0.0);
glEnd();
glPopMatrix();
// drawing the seconds hand
glRotatef ( sec_float, 0.0, 0.0, 1.0);
glColor3f(1.0, 0.6, 0.0);
glBegin(GL_POLYGON);
glVertex2f(-0.01,0.0);
glVertex2f(-0.07,1.6);
glVertex2f(0.0,1.8);
glVertex2f(0.07,1.6);
glVertex2f(0.01,0.0);
glEnd();
}
else // digital
{
//drawing the strings containing the time on the cube
glColor3f(1.0, 0.0, 0.2);
bitmap_text(-1.1,0.0,time_char,GLUT_BITMAP_TIMES_ROMAN_24,0,3);
glColor3f(1.0, 0.0, 0.4);
bitmap_text(-0.7,0.9,time_char,GLUT_BITMAP_TIMES_ROMAN_24,4,6);
glColor3f(1.0, 0.0, 0.6);
bitmap_text(0.3,0.85,time_char,GLUT_BITMAP_TIMES_ROMAN_24,20,4);
glColor3f(1.0, 0.0, 0.8);
bitmap_text(0.0,-0.5,time_char,GLUT_BITMAP_TIMES_ROMAN_24,11,8);
glColor3f(1.0, 0.0, 1.0);
bitmap_text(0.0,-0.8,"(Local Time)",GLUT_BITMAP_HELVETICA_18,0,12);
}
glPopMatrix();
glutSwapBuffers();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (0.0, 0.0, -5.0);
}
//creating the menu
void menu_choice(int selection) {
switch (selection) {
case 0 :
exit(0);
break;
case 1:
analog=true;
break;
case 2:
analog=false;
}
}
void timer1( int anyvalue){
//filling the structure with values from the system's clock
time(<ime);
//localtime returns the current local time of the system
time_tm=localtime(<ime);
time_char=ctime( <ime );
//calculating angles
//between second markers
sec_float=(float) time_tm->tm_sec*6; //360/60=6
//casting is a must because we need float values not integers
//between minute markers
min_float=(float) time_tm->tm_min*6 + sec_float/60.0;
//between hour markers
hour_float=(float) time_tm->tm_hour*30+ min_float/12.0; //360/12=30
//negetive value because we want it to rotate clockwise
sec_float=-sec_float;
min_float=-min_float;
hour_float=-hour_float;
glutPostRedisplay();
//time call back function
glutTimerFunc(500,timer1,0);
//every half a second the systems clock is checked and display
//is called again
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (600, 600);
glutInitWindowPosition (0, 0);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
int menu = glutCreateMenu(menu_choice);
glutAddMenuEntry("analog", 1);
glutAddMenuEntry("digital",2);
glutAddMenuEntry("quit",0);
glutAttachMenu(GLUT_LEFT_BUTTON);
glutReshapeFunc(reshape);
glutTimerFunc(3,timer1,0);
glutMainLoop();
return 0;
}