2022年5月23日 星期一

09163051 上課筆記 Week14

 1.讀,寫,新增檔案

利用 FILE *Fp=fopen("file1.txt","w+");寫入新檔案
         FILE *Fs=fopen("file2.txt","r+");讀入新檔案
w代表 寫檔案,r代表讀檔案,+代表更新.

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結合讀寫檔案

讀:

開啟新的glut檔案,把上週week13_rect_many_TRT的程式碼貼上去並做修改:
#include <GL/glut.h>
#include <stdio.h>

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();
}

新增的檔案會在\freeglut\bin裡面.

讀加寫:

#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldx=0;
int angleID=0;
FILE * fout=NULL;
FILE * fin=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 myRead(){
    if(fout!=NULL){
        fclose(fout);///還在讀的檔案要關
        fout==NULL;
    }
    if(fin==NULL)fin=fopen("flie.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=='0')angleID=0;//右手臂
    if(key=='1')angleID=1;//右手肘
    if(key=='2')angleID=2;//左手臂
    if(key=='3')angleID=3;//左手肘
    if(key=='r'){
        myRead();
    }
}
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 angle write and read");

    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);
    glutMouseFunc(mouse);/////mouse
    glutMotionFunc(motion);//配合motion
    glutMainLoop();
}

3.修改txt輸出位置

用NotePad++ or VSCode 開啟 cbp檔案.
<Option working_dir="D:/freeglut/bin" />改成<Option working_dir="." />
將freeglut\bin 裡的 freeglut.dll丟到專案檔內部.
然後再reload一次project.

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(); }

沒有留言:

張貼留言