1.讀,寫,新增檔案
int main()
{
FILE * Fp=fopen("file.txt","w+");//開檔案
fprintf(Fp,"angle1 %d\n",999);//輸出文字,
fclose(Fp);//關檔案
char line[200];
int a;
FILE * Fs=fopen("file.txt","r");//開啟剛剛寫入文字的檔案
fscanf(Fs,"%s %d",line,&a);//將檔案內的文字輸入line,a
printf("讀到了字串:%s 及整數%d\n",line,a);
fclose(Fs);
}
2.glut結合讀寫檔案
float angle[20],oldx=0;
int angleID=0;
FILE * fout=NULL;
void myWrite(){
if (fout==NULL)fout=fopen("file.txt","w+");
for(int i=0;i<20;i++){
printf("%.2f",angle[i]);
fprintf(fout,"%.2f",angle[i]);
}
}
void keyboard(unsigned char key,int x,int y){
if(key=='0')angleID=0;//右手臂
if(key=='1')angleID=1;//右手肘
if(key=='2')angleID=2;//左手臂
if(key=='3')angleID=3;//左手肘
}
void mouse(int button,int state ,int x,int y){
oldx=x;
}
void motion(int x,int y){
angle[angleID]+=(x-oldx);
myWrite();
oldx=x;
glutPostRedisplay();//重畫畫面
}
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf(0.3,0.5,-0.3,-0.2);//白色身體
glPushMatrix();///right
glTranslatef(0.3, 0.5, 0);
glRotatef( angle[0], 0,0,1 );//2.旋轉他
glTranslatef(-0.3,-0.4, 0);//1.把旋轉中心放中心點
glColor3f(1,0,0);
glRectf(0.3,0.5,0.8,0.3);//紅手臂
glPushMatrix();
glTranslatef(0.8,0.4,0);//3.把下手肘掛在關節上
glRotatef(angle[1],0,0,1);//2.旋轉
glTranslatef(-0.8,-0.4,0);//1.把下手肘的旋轉中心,放在正中心
glColor3f(0,1,0);//綠色的
glRectf(0.8,0.5,1.1,0.3);//下手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();///left
glTranslatef(-0.3, 0.5, 0);
glRotatef( angle[2], 0,0,1 );//2.旋轉他
glTranslatef(0.3,-0.4, 0);//1.把旋轉中心放中心點
glColor3f(1,0,0);
glRectf(-0.3,0.5,-0.8,0.3);//左手臂
glPushMatrix();
glTranslatef(-0.8,0.4,0);//3.把下手肘掛在關節上
glRotatef(angle[3],0,0,1);//2.旋轉
glTranslatef(0.8,-0.4,0);//1.把左下手肘的旋轉中心,放在正中心
glColor3f(0,1,0);//綠色的
glRectf(-0.8,0.5,-1.1,0.3);//左下手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc , char **argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
///glutInitWindowSize(600,600);
glutCreateWindow("Week14 TRT angle Write" );
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMouseFunc(mouse);/////mouse
glutMotionFunc(motion);//配合motion
glutMainLoop();
}
3.修改txt輸出位置
4.timer跟PlaySound函式
需要用到 #include <stdio.h>timer+PlaySound setup:void timer(int t){///t單位:ms printf("鬧鐘%d, 起床\n", t); PlaySound("do.wav", NULL, SND_ASYNC); printf("下一個鬧鐘\n"); glutTimerFunc( 1000, timer, t+1);///1秒之後 }total:
void timer(int t){///t單位:ms printf("鬧鐘%d, 起床\n", t); PlaySound("do.wav", NULL, SND_ASYNC); printf("下一個鬧鐘\n"); glutTimerFunc( 1000, timer, t+1);///1秒之後 }void display() { } int main(int argc, char**argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH); glutCreateWindow("week14 timer"); glutTimerFunc(3000, timer, 0);///3秒後,叫timer() glutDisplayFunc(display); glutMainLoop(); }
沒有留言:
張貼留言