2020年5月27日 星期三

Week14 - 07161045

開啟檔案
  • 至 CodeBlocks 開啟一個 Empty File 
  • 程式碼如下:
#include <stdio.h>
    int main()
      {
            FILE * fout=fopen("檔名.txt", "w+");

                 printf(    "Hello world\n");
                  fprintf(fout,"Hello world in file\n")
                }
                • 輸入完上面程式碼按下齒輪(Build)
                • 資料夾中會顯示出".o"跟".exe"檔
                • 再按齒輪加播放(Build and Run)
                • 資料夾中會顯示出".txt"檔
                     
                • 更改一下程式碼
                • 程式碼如下:
                #include <stdio.h>
                  int main()
                    {
                          FILE * fout=fopen("檔名2.txt", "w+");

                              for(int i=1;i<20;i++)
                                {
                                      printf(     "Hello %d\n",i);
                                       fprintf(fout,"Hello %d\n",i);
                                      }
                                    }
                                    • 將上述程式碼紅色部分更改名字
                                    • 再按下按齒輪加播放(Build and Run)
                                    • 資料夾中將會出現新的".txt"檔
                                         
                                    • 新增一些關節,並印出來
                                    • 程式碼如下
                                    #include <stdio.h>
                                      int angel[10];///有很多個關節
                                        int main()
                                          {
                                                FILE * fout=fopen("motion.txt", "w+");

                                                    for(int i=1;i<10;i++)
                                                      {
                                                            printf(     "%d \n",angel[i]);
                                                             fprintf(fout,"%d \n",angel[i]);
                                                            }
                                                               printf("\n");
                                                                fprintf(fout, "\n");
                                                              }

                                                                   

                                                              讀檔、動作
                                                              • 先開啟"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(0.5,0.4,0.3);///棕色
                                                                                                  glutSolidTeapot(0.2);///右上手臂

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

                                                                                                                    glColor3f(0.3,0.6,0.7);///淺藍色
                                                                                                                      glutSolidTeapot(0.2);///右下手臂

                                                                                                                      glPopMatrix();
                                                                                                                    glPopMatrix();///右側結束

                                                                                                                        glPushMatrix();///左側開始
                                                                                                                              glTranslatef(-0.2, 0,0 );
                                                                                                                                glRotatef(angle[2], 0,0,1);
                                                                                                                                  glTranslatef(-0.2, 0,0 );

                                                                                                                                      glColor3f(0.8,1,0.6);///淺綠色
                                                                                                                                        glutSolidTeapot(0.2);///左上手臂

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

                                                                                                                                                          glColor3f(0.4,0.1,0.6);///紫色
                                                                                                                                                            glutSolidTeapot(0.2);///左下手臂

                                                                                                                                                            glPopMatrix();
                                                                                                                                                          glPopMatrix();///左側結束
                                                                                                                                                            glutSwapBuffers();
                                                                                                                                                          }
                                                                                                                                                            int main(int argc, char**argv)
                                                                                                                                                              {
                                                                                                                                                                    glutInit(&argc, argv);
                                                                                                                                                                      glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
                                                                                                                                                                        glutCreateWindow("Week14");

                                                                                                                                                                            glutDisplayFunc(display);
                                                                                                                                                                              glutMainLoop();
                                                                                                                                                                            }

                                                                                                                                                                                 
                                                                                                                                                                            • 新增滑鼠程式碼,讓關節可透過滑鼠移動
                                                                                                                                                                            • 程式碼如下:
                                                                                                                                                                            #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(0.5,0.4,0.3);///棕色
                                                                                                                                                                                                                glutSolidTeapot(0.2);///右上手臂

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

                                                                                                                                                                                                                                  glColor3f(0.3,0.6,0.7);///淺藍色
                                                                                                                                                                                                                                    glutSolidTeapot(0.2);///右下手臂

                                                                                                                                                                                                                                    glPopMatrix();
                                                                                                                                                                                                                                  glPopMatrix();///右側結束

                                                                                                                                                                                                                                      glPushMatrix();///左側開始
                                                                                                                                                                                                                                            glTranslatef(-0.2, 0,0 );
                                                                                                                                                                                                                                              glRotatef(angle[2], 0,0,1);
                                                                                                                                                                                                                                                glTranslatef(-0.2, 0,0 );

                                                                                                                                                                                                                                                    glColor3f(0.8,1,0.6);///淺綠色
                                                                                                                                                                                                                                                      glutSolidTeapot(0.2);///左上手臂

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

                                                                                                                                                                                                                                                                        glColor3f(0.4,0.1,0.6);///紫色
                                                                                                                                                                                                                                                                          glutSolidTeapot(0.2);///左下手臂

                                                                                                                                                                                                                                                                          glPopMatrix();
                                                                                                                                                                                                                                                                        glPopMatrix();///左側結束
                                                                                                                                                                                                                                                                          glutSwapBuffers();
                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                          int angleID=0;///0,1,2,3
                                                                                                                                                                                                                                                                            int oldX, oldY;
                                                                                                                                                                                                                                                                              void mouse(int button, int state, int x, int y)
                                                                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                                                                      oldX=x; oldY=y;///備份(old)mouse位置
                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                      void motion(int x, int y)
                                                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                                                              angle[angleID] += (x-oldX);///滑動,更新 angleID對應的angle[]
                                                                                                                                                                                                                                                                                                oldX=x;
                                                                                                                                                                                                                                                                                                  glutPostRedisplay();///重畫畫面
                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                  void keyboard(unsigned char key, int x, int y)
                                                                                                                                                                                                                                                                                                    {///鍵盤,選angleID
                                                                                                                                                                                                                                                                                                          if(key=='0') angleID=0;
                                                                                                                                                                                                                                                                                                            if(key=='1') angleID=1;
                                                                                                                                                                                                                                                                                                              if(key=='2') angleID=2;
                                                                                                                                                                                                                                                                                                                if(key=='3') angleID=3;
                                                                                                                                                                                                                                                                                                              }
                                                                                                                                                                                                                                                                                                                int main(int argc, char**argv)
                                                                                                                                                                                                                                                                                                                  {
                                                                                                                                                                                                                                                                                                                        glutInit(&argc, argv);
                                                                                                                                                                                                                                                                                                                          glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
                                                                                                                                                                                                                                                                                                                            glutCreateWindow("Week14 file");

                                                                                                                                                                                                                                                                                                                                glutDisplayFunc(display);
                                                                                                                                                                                                                                                                                                                                  glutMouseFunc(mouse);///備份(old)mouse位置
                                                                                                                                                                                                                                                                                                                                    glutMotionFunc(motion);///滑動,更新 angleID對應的angle[]
                                                                                                                                                                                                                                                                                                                                      glutKeyboardFunc(keyboard);///鍵盤,選angleID
                                                                                                                                                                                                                                                                                                                                        glutMainLoop();
                                                                                                                                                                                                                                                                                                                                      }

                                                                                                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                                                                                      • 利用程式碼讓旋轉數值印出來
                                                                                                                                                                                                                                                                                                                                      • 程式碼如下:
                                                                                                                                                                                                                                                                                                                                      #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(0.5,0.4,0.3);///棕色
                                                                                                                                                                                                                                                                                                                                                                        glutSolidTeapot(0.2);///右上手臂

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

                                                                                                                                                                                                                                                                                                                                                                                          glColor3f(0.3,0.6,0.7);///淺藍色
                                                                                                                                                                                                                                                                                                                                                                                            glutSolidTeapot(0.2);///右下手臂

                                                                                                                                                                                                                                                                                                                                                                                            glPopMatrix();
                                                                                                                                                                                                                                                                                                                                                                                          glPopMatrix();///右側結束

                                                                                                                                                                                                                                                                                                                                                                                              glPushMatrix();///左側開始
                                                                                                                                                                                                                                                                                                                                                                                                    glTranslatef(-0.2, 0,0 );
                                                                                                                                                                                                                                                                                                                                                                                                      glRotatef(angle[2], 0,0,1);
                                                                                                                                                                                                                                                                                                                                                                                                        glTranslatef(-0.2, 0,0 );

                                                                                                                                                                                                                                                                                                                                                                                                            glColor3f(0.8,1,0.6);///淺綠色
                                                                                                                                                                                                                                                                                                                                                                                                              glutSolidTeapot(0.2);///左上手臂

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

                                                                                                                                                                                                                                                                                                                                                                                                                                glColor3f(0.4,0.1,0.6);///紫色
                                                                                                                                                                                                                                                                                                                                                                                                                                  glutSolidTeapot(0.2);///左下手臂

                                                                                                                                                                                                                                                                                                                                                                                                                                  glPopMatrix();
                                                                                                                                                                                                                                                                                                                                                                                                                                glPopMatrix();///左側結束
                                                                                                                                                                                                                                                                                                                                                                                                                                  glutSwapBuffers();
                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                  int angleID=0;///0,1,2,3
                                                                                                                                                                                                                                                                                                                                                                                                                                    int oldX, oldY;
                                                                                                                                                                                                                                                                                                                                                                                                                                      void mouse(int button, int state, int x, int y)
                                                                                                                                                                                                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                                                                                                                                                                                                              oldX=x; oldY=y;///備份(old)mouse位置
                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                              #include <stdio.h> ///外掛
                                                                                                                                                                                                                                                                                                                                                                                                                                                FILE * fout=NULL;
                                                                                                                                                                                                                                                                                                                                                                                                                                                  void saveAll()///(自己寫的函式)全部存檔
                                                                                                                                                                                                                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                                                                                                                                                                                                                          if(fout==NULL) fout=fopen("motion.txt", "w+");///TODO3:開檔
                                                                                                                                                                                                                                                                                                                                                                                                                                                            for(int i=0; i<10; i++){///for迴圈,把陣列全用
                                                                                                                                                                                                                                                                                                                                                                                                                                                                   printf(      "%d ", angle[i]);///印畫面
                                                                                                                                                                                                                                                                                                                                                                                                                                                                    fprintf(fout, "%d ", angle[i]);///寫檔案
                                                                                                                                                                                                                                                                                                                                                                                                                                                                  }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                     printf(      "\n");///跳行, 畫面不會亂
                                                                                                                                                                                                                                                                                                                                                                                                                                                                      fprintf(fout, "\n");///跳行, 畫面不會亂
                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                      void motion(int x, int y)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                              angle[angleID] += (x-oldX);///滑動,更新 angleID對應的angle[]
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                oldX=x;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  saveAll();///(自己寫的函式)全部存檔
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    glutPostRedisplay();///重畫畫面
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    void keyboard(unsigned char key, int x, int y)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      {///鍵盤,選angleID
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if(key=='0') angleID=0;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              if(key=='1') angleID=1;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if(key=='2') angleID=2;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  if(key=='3') angleID=3;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  int main(int argc, char**argv)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          glutInit(&argc, argv);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              glutCreateWindow("Week14 file");

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  glutDisplayFunc(display);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    glutMouseFunc(mouse);///備份(old)mouse位置
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      glutMotionFunc(motion);///滑動,更新 angleID對應的angle[]
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        glutKeyboardFunc(keyboard);///鍵盤,選angleID
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          glutMainLoop();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        • 新增按鍵(1、2、3、4、r),讓每個關節有相對的按鍵
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        • 程式碼如下:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        #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(0.5,0.4,0.3);///棕色
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            glutSolidTeapot(0.2);///右上手臂

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

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              glColor3f(0.3,0.6,0.7);///淺藍色
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                glutSolidTeapot(0.2);///右下手臂

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                glPopMatrix();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              glPopMatrix();///右側結束

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  glPushMatrix();///左側開始
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        glTranslatef(-0.2, 0,0 );
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          glRotatef(angle[2], 0,0,1);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            glTranslatef(-0.2, 0,0 );

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                glColor3f(0.8,1,0.6);///淺綠色
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  glutSolidTeapot(0.2);///左上手臂

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

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    glColor3f(0.4,0.1,0.6);///紫色
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      glutSolidTeapot(0.2);///左下手臂

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      glPopMatrix();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    glPopMatrix();///左側結束
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      glutSwapBuffers();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      int angleID=0;///0,1,2,3
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        int oldX, oldY;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          void mouse(int button, int state, int x, int y)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  oldX=x; oldY=y;///備份(old)mouse位置
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  #include <stdio.h> ///外掛
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    FILE * fout=NULL;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      void saveAll()///(自己寫的函式)全部存檔
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              if(fout==NULL) fout=fopen("motion.txt", "w+");///TODO3:開檔
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                for(int i=0; i<10; i++){///for迴圈,把陣列全用
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       printf(      "%d ", angle[i]);///印畫面
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        fprintf(fout, "%d ", angle[i]);///寫檔案
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         printf(      "\n");///跳行, 畫面不會亂
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          fprintf(fout, "\n");///跳行, 畫面不會亂
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          void motion(int x, int y)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  angle[angleID] += (x-oldX);///滑動,更新 angleID對應的angle[]
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    oldX=x;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      saveAll();///(自己寫的函式)全部存檔
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        glutPostRedisplay();///重畫畫面
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        FILE * fin=NULL;///宣告檔案指標
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          void keyboard(unsigned char key, int x, int y)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {///鍵盤,選angleID
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  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);///備份(old)mouse位置
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              glutMotionFunc(motion);///滑動,更新 angleID對應的angle[]
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                glutKeyboardFunc(keyboard);///鍵盤,選angleID
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  glutMainLoop();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                沒有留言:

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                張貼留言