闹钟程序

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include <gl/glut.h>
#include <windows.h>
#include <math.h>
#include<string.h>
#include<sstream>
#include<fstream>
#include <stdlib.h>
#include <time.h>//定义头文件
#define N 3000
#define changecolor 1
#define resumecolor 2
#define quit 3
int winheight=700,winwidth=500;
int cx=400,cy=200,radius=0;
int hradius=150,mradius=150,sradius=300;
float sangle,mangle,hangle;
char timebuffer[100];
int ww=0,hh=0;
int tt=1,ll=0;
int distances=0;
char a[]="定义函数";
int second,miniter,hour;
int second1,miniter1,hour1;
float br=0.0,bg=.0,bb=255.0;
float sr=0.0,sg=255.0,sb=0.0; //定义函数
bool bLight=false;
void drawcrpot();
void drawline();
void hand(int cx,int cy,int r,float thleta);
void Bresenham_Circle(int x0,int y0,double radius); void CirPot(int x0,int y0,int x,int y);
void drawother();
void createGLUTMenus();
void processMenuEvents(int option);
void Myinit(void)
{ glClearColor(1.0,1.0,1.0,1.0);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); glLineWidth(10.0);
glPointSize(2); _
strtime(timebuffer);
hour1=50;
miniter1=60;
second1=70;
tt=2; }
void distimbuffer(char *strings)
{ int lengths;
lengths = (int) strlen(strings);
glRasterPos2i(cx-50,cy+50);
for (int mm=0;mm<lengths;mm++)
{ glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, strings[mm]); } } void disclock(char *string)
{ int length;
length = (int) strlen(string);
glRasterPos2i(cx-20,cy-10);
for (int m=0;m<length;m++)
{ glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, string[m]); } } void dis()
{ disclock(a);
distimbuffer(timebuffer); }
void Display(void) { glClear(GL_COLOR_BUFFER_BIT); drawcrpot();//划4个数字drawline();//三个指针
drawother();
dis();
glutSwapBuffers(); }//画图
void drawline()
{ hradius=radius/2.2;
mradius=radius/1.8;
sradius=radius/1.5;
cx=100+radius;
cy=cx;
glColor3f(sr,sg,sb);
hand(cx,cy,sradius,sangle);
glLineWidth(3.0);
hand(cx,cy,mradius,mangle);
glLineWidth(5.0);
hand(cx,cy,hradius,hangle); }
void drawcrpot()
{ if(ww>hh) distances=hh;
else distances=ww;
radius=distances/2-100;glClear(GL_COLOR_BUFFER_BIT);
glClearColor(br,bg,bb,30.0);//背景色
glColor3f(255.0,50.0,110.0);
glRasterPos2i(100+radius, 100); //光标定位,窗口左下角为(0,0)glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'6'); g lRasterPos2i(100, 100+radius);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'9');
glRasterPos2i(100+2*radius,100+radius);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'3');
glRasterPos2i(100+radius,100+2*radius);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'1');
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, '2'); }//画出表的圆盘void hand(int cx,int cy,int r,float thleta)
{ float harr[2];
harr[0]=cx+r*cos(thleta);
harr[1]=cy+r*sin(thleta);
glBegin(GL_LINES);
glVertex2f(harr[0],harr[1]);
glVertex2f(cx,cy);
glEnd(); }
void drawother()
{ glColor3f(0.0,255.0,105.0);
Bresenham_Circle(cx,cy,double(radius+20)); }
void Bresenham_Circle(int x0,int y0,double radius)
{ int x,y,d;
x=0;
y=int(radius);
d=(int)3-2*radius;
while (x<=y)
{ CirPot(x0,y0,x,y);
if (d<0) d+=4*x+1;
else { d+=4*(x-y)-2;
y--; }
x++; } }
void CirPot(int x0,int y0,int x,int y)
{ glBegin(GL_POINTS);
glVertex2f(x0+x,y0+y);
glVertex2f(x0+x,y0-y);
glVertex2f(x0-x,y0+y);
glVertex2f(x0-x,y0-y);
glVertex2f(x0+y,y0+x);
glVertex2f(x0+y,y0-x);
glVertex2f(x0-y,y0+x);
glVertex2f(x0-y,y0-x); glEnd(); }
void myidle() { ::Sleep(1000); _strtime(timebuffer);//取系统时间到timebuffer数组中for(int i=0;i<9;i++)
hour=((int)timebuffer[0]-48)*10+((int)timebuffer[1]-48);
miniter=((int)timebuffer[3]-48)*10+((int)timebuffer[4]-48);
second=((int)timebuffer[6]-48)*10+((int)timebuffer[7]-48);
sangle=(float)(3.1415/2-(float)second*2*3.1415/60);
mangle=(float)3.1415/2-(float)(miniter+(float)second/60)/60*2*3.1415;
hangle=(float)3.1415/2-(float)(hour+(float)(miniter+(float)second/60)/60)*2*3.1415/12; glutPostRedisplay();
void Reshape(int w, int h)
{
ww=w;
hh=h;
glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-20, (GLdouble) w, -180.0, (GLdouble) h); glMatrixMode(GL_MODELVIEW); glLoadIdentity();
}
void change() //改变颜色
{ br=0.0,bg=255.0,bb=0.0;
sr=0.0,sg=255.0,sb=0.0;}
void resume() //恢复颜色
{ br=0.0,bg=0.0,bb=0.0;
sr=1.0,sg=1.0,sb=0.0;
}
void processMenuEvents(int option)
{
switch (option)
{
case changecolor:
change();
break;
case resumecolor:
resume();
break;
case quit:
exit(0);
break;}
}
void createGLUTMenus()
{
int menu;
menu = glutCreateMenu(processMenuEvents); glutAddMenuEntry("交换颜色",changecolor); glutAddMenuEntry("恢复颜色",resumecolor); glutAddMenuEntry("退出",quit);
glutAttachMenu(GLUT_RIGHT_BUTTON);
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowPosition(0, 0);
glutInitWindowSize(winheight,winwidth); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutCreateWindow("计算机-11--2---蔡丰");
Myinit();
glutDisplayFunc(Display);
glutIdleFunc(myidle);
glutReshapeFunc(Reshape);
createGLUTMenus();
glutMainLoop();
return 0;
}。

相关文档
最新文档