2020年5月27日 星期三

Week14

 ◮FILE fopen(), fprintf(), fscanf()
STEP1 : 開啟codeblocks新增一個專案 ⟶ File ⟶ NEW ⟶ Empty file ⟶ 命名專案為file.cpp


Step2 : 實作讀檔

程式碼 : #include<stdio.h>

int main()
{
    FILE * fout=fopen("檔名2.txt","w+");///檔案指標fout是開啟檔(....write+)
    for(int i=0; i<20; i++){
        printf("Hello %d\n", i);
        fprintf( fout,"Hello %d\n", i);
    }
}

執行後如下圖 : 


Step3 : 改程式碼

#include<stdio.h>
int angle[10];///要有很多關節
int main()
{
    FILE * fout=fopen("motion.txt", "w+");

    for(int i=0; i<10; i++){
        printf( "%d", angle[i]);
        fprintf( fout,"%d", angle[i]);
    }///數字黏在一起不對
    printf("\n");///印一行後,跳行
    fprintf(fout, "\n");

}

執行後如下圖 : 



Step4 :新增命名為Week14_file的GLUT專案

程式碼 : 

#include <GL/glut.h>
int angle[10]={0,0,0,0,0, 0,0,0,0,0};
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glutSolidTeapot(0.2);

    glPushMatrix();
        glTranslatef(0.2, 0,0 );
        glRotatef(angle[0], 0,0,1);
        glTranslatef(0.2, 0,0 );

        glColor3f(1,0,0);
        glutSolidTeapot(0.2);

        glPushMatrix();
            glTranslatef(0.2, 0,0);
            glRotatef(angle[1], 0,0,1);
            glTranslatef(0.2, 0,0);

            glColor3f(1,1,0);
            glutSolidTeapot(0.2);

        glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.2, 0,0 );
        glRotatef(angle[2], 0,0,1);
        glTranslatef(-0.2, 0,0 );

        glColor3f(0,1,0);
        glutSolidTeapot(0.2);

        glPushMatrix();
            glTranslatef(-0.2, 0,0);
            glRotatef(angle[3], 0,0,1);
            glTranslatef(-0.2, 0,0);

            glColor3f(0,0,1);
            glutSolidTeapot(0.2);

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

    glutDisplayFunc(display);
    glutMainLoop();


執行後如下圖 :

 

Step5 : 動作

#include <GL/glut.h>
int angle[10]={0,0,0,0,0, 0,0,0,0,0};
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glutSolidTeapot(0.2);

    glPushMatrix();
        glTranslatef(0.2, 0,0 );
        glRotatef(angle[0], 0,0,1);
        glTranslatef(0.2, 0,0 );

        glColor3f(1,0,0);
        glutSolidTeapot(0.2);

        glPushMatrix();
            glTranslatef(0.2, 0,0);
            glRotatef(angle[1], 0,0,1);
            glTranslatef(0.2, 0,0);

            glColor3f(1,1,0);
            glutSolidTeapot(0.2);

        glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.2, 0,0 );
        glRotatef(angle[2], 0,0,1);
        glTranslatef(-0.2, 0,0 );

        glColor3f(0,1,0);
        glutSolidTeapot(0.2);

        glPushMatrix();
            glTranslatef(-0.2, 0,0);
            glRotatef(angle[3], 0,0,1);
            glTranslatef(-0.2, 0,0);

            glColor3f(0,0,1);
            glutSolidTeapot(0.2);

        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();

}
int angleID=0;
int oldX, oldY;
void mouse(int button, int state, int x, int y)
{
    oldX=x; oldY=y;
}
#include <stdio.h>
FILE * fout=NULL;
void saveAll()
{
    if(fout==NULL) fout=fopen("motion.txt", "w+");

        for(int i=0; i<10; i++){
        printf( "%d ", angle[i]);
        fprintf( fout,"%d ", angle[i]);

    }///數字黏在一起不對

    printf("\n");///印一行後,跳行

    fprintf(fout, "\n");
}
void motion(int x, int y)
{
    angle[angleID] += (x-oldX);
    oldX=x;
    saveAll();
    glutPostRedisplay();
}
FILE * fin=NULL;
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') {
        if(fin==NULL) fin=fopen("motion.txt", "r");
        for(int i=0; i<10; i++){
            fscanf(fin, "%d", &angle[i]);
        }
        glutPostRedisplay();
    }
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week14 file");

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

執行後如下圖 :

沒有留言:

張貼留言