2022年5月23日 星期一

⚝ YI-ZHEN的筆記 week14

 

  • 寫檔 關檔 讀檔
1. 建立新空白檔案(Week14-1)
2. 最原始的Hello World檔案再加上 開檔、寫檔及關檔的程式

#include <stdio.h>
int main()
{
    FILE * fout = fopen("file.txt","w+");  ///開啟檔案,write+順便新增
         
    printf("Hello World\n");
    fprintf(fout, "Hello World\n");  ///印出來 寫出來

    fclose(fout); ///關閉檔案
}


3. 在開新檔(Week14-2)
4. 除了開檔、關檔外,現在再加入讀檔

#include <stdio.h>
int main()
{
    FILE * fout = fopen("file2.txt","w+");
    fprintf(fout, "angle %d\n", 999);
    fclose(fout);

    FILE * fin = fopen("file2.txt", "r");
    char line[200]; /// 宣告字串
    int a; /// 宣告整數a
    fscanf(fin, "%s %d", line, &a);
    printf("讀到了字串%s 及整數%d", line, a);
    fclose(fin);
}
5. 開啟新專案
6. 貼上上週最後一個檔案的程式碼
7. 加上 開檔 寫檔的函式, 執行移動後會出現一堆數字

#include <GL/glut.h>
#include <stdio.h> ///為了 printf , fprintf, fopen, fclose
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++){
        printf("%.2f ", angle[i]);
        fprintf(fout, "%.2f ", angle[i]);
    }
    printf("\n");
    fprintf(fout, "\n");
    ///少了fclose,因為不想印一行就結束,要多行一點
}
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("Week14_TRT_angle_write");

    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display); ///不放Idle

    glutMainLoop();
}

8. 增加一個 myRead函式來進行讀檔,運用keyboard把動作一行一行讀進去

#include <GL/glut.h>
#include <stdio.h> ///為了 printf , fprintf, fopen, fclose
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++){
        printf("%.2f ", angle[i]);
        fprintf(fout, "%.2f ", angle[i]);
    }
    printf("\n");
    fprintf(fout, "\n");
    ///少了fclose,因為不想印一行就結束,要多行一點
}
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 == '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(); ///右半邊
        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("Week14_TRT_angle_write");

    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display); ///不放Idle

    glutMainLoop();
}
9. 最好加一個警告

10. 修改工作目錄(C:\Users\user\Desktop\freeglut\bin)至程式碼的目錄
    10-1 先用NotePad++來修改.cbp檔案
    10-2 把檔案中所有working_dir="   ",雙引號中都改為 "."
    10-3 在手動把(C:\Users\user\Desktop\freeglut\bin) 裡的freeglut.dll複製進專案目錄中
    10-4 CodeBlocks 中 Reload 就可改變工作目錄了!


  • 計時器
#include <GL/glut.h>
#include <stdio.h>
void timer(int t){ ///t 單位為ms 1000代表1秒
    printf("鬧鐘%d, 起床了!\n", t); ///起床做事情
    printf("再設下一個鬧鐘\n");
    glutTimerFunc(2000, timer, t+1); ///2秒後
    printf("設好鬧鐘,再繼續睡!\n");
}
void display()
{

}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week_timer");

    glutTimerFunc(3000, timer, 0); ///3秒後,叫timer() ///只會播一個鬧鐘
    glutDisplayFunc(display);
    glutMainLoop();
}
1.  加入聲音
    1-1 要先將音檔放進freeglut的bin中

#include <GL/glut.h>
#include <stdio.h>
#include <mmsystem.h>
void timer(int t){ ///t 單位為ms 1000代表1秒
    printf("鬧鐘%d, 起床了!\n", t);
    PlaySound("do.wav", NULL, SND_ASYNC); ///發出聲音

    printf("再設下一個鬧鐘\n");
    glutTimerFunc(2000, timer, t+1); ///2秒後
    ///printf("設好鬧鐘,再繼續睡!\n");
}
void display()
{

}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week_timer");

    glutTimerFunc(3000, timer, 0); ///3秒後,叫timer()
    glutDisplayFunc(display);
    glutMainLoop();
}



沒有留言:

張貼留言