1.練習開檔、寫檔、關檔
#include <stdio.h>
int main()
{
FILE*fout=fopen("file.txt", "w+");
printf("Hello world\n");
fprintf(fout, "Hello world\n");
fclose(fout);
}
2.練習開檔、讀檔、關檔
#include <stdio.h>
int main()
{
FILE*fout=fopen("file2.txt", "w+");
fprintf(fout,"angle1 %d\n", 999);
fclose(fout);
char line[200];
int a;
FILE*fin=fopen("file2.txt", "r");
fscanf(fin, "%s %d", line, &a);
printf("讀到字串:%s及整數%d\n", line, a);
fclose(fin);
}
3.開啟上禮拜最後的檔案,並新增程式
#include <GL/glut.h>
#include <stdio.h>
float angle[20], oldX=0; ///角度歸零
int angleID=0; ///0為第0個關節...以此類推
FILE*fout=NULL;
void myWrite()
{
if(fout==NULL) fout=fopen("file.txt", "w+");
for(int i=0; i<20; 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(); ///右半邊
glTranslatef(0.3, 0.4, 0);
glRotatef(angle[0], 0, 0, 1);
glTranslatef(-0.3, -0.4, 0);
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(); ///左半邊
glTranslatef(-0.3, 0.4, 0);
glRotatef(angle[2], 0, 0, 1);
glTranslatef(0.3, -0.4, 0);
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_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week13_rect_TRT");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display); ///不放Idle
glutMainLoop();
}
4.按r動作恢復初始
#include <GL/glut.h>
#include <stdio.h>
float angle[20], oldX=0; ///角度歸零
int angleID=0; ///0為第0個關節...以此類推
FILE*fout=NULL, *fin=NULL;
void myWrite()
{
if(fout==NULL) fout=fopen("file.txt", "w+");
for(int i=0; i<20; i++){
fprintf(fout, "%.2f", angle[i]);
}
}
void myRead()
{
if(fout!=NULL){fclose(fout); fout=NULL;}
if(fin==NULL) fin=fopen("file.txt", "r");
for(int i=0; i<20; i++){
fscanf(fin, "%f", &angle[i]);
}
glutPostRedisplay();///重畫畫面
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='r'){
myRead();
}
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(); ///右半邊
glTranslatef(0.3, 0.4, 0);
glRotatef(angle[0], 0, 0, 1);
glTranslatef(-0.3, -0.4, 0);
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(); ///左半邊
glTranslatef(-0.3, 0.4, 0);
glRotatef(angle[2], 0, 0, 1);
glTranslatef(0.3, -0.4, 0);
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_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week13_rect_TRT");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display); ///不放Idle
glutMainLoop();
}
5.做一個鬧鐘
#include <GL/glut.h>
#include <stdio.h>
void timer(int t)
{
printf("鬧鐘%d,起床了\n", t);
printf("設定下一個鬧鐘\n");
glutTimerFunc(1000, timer, t+1);
printf("設定好鬧鐘, 回去睡回籠覺\n");
}
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();
}
6.會響的鬧鐘
#include <GL/glut.h>
#include <mmsystem.h>
#include <stdio.h>
void timer(int t)
{
printf("鬧鐘%d,起床了\n", t);
PlaySound("do.wav", NULL, SND_ASYNC);
printf("設定下一個鬧鐘\n");
glutTimerFunc(1000, timer, t+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();
}






沒有留言:
張貼留言