2023年4月27日 星期四

要叫什麼丫 Week 11

鍵盤控制 

  • 新增glut專案
  • 輸入程式碼

#include <GL/freeglut.h>

      void display()
        {
              glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
                glutSolidTeapot(0.3);
                  glutSwapBuffers();
                }

                    void keyboard(unsigned char key,int x,int y)
                      {
                            if (key==27) ///ESC=27
                                  exit(1234); ///按下ESC關閉視窗在小黑顯示1234
                            }
                              int main(int argc,char** argv)
                                {
                                      glutInit(&argc,argv);
                                        glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
                                          glutCreateWindow("week11");

                                              glutDisplayFunc(display);
                                                glutKeyboardFunc(keyboard);///鍵盤

                                                    glutMainLoop();
                                                  }
                                                    執行結果




                                                    音效播放

                                                    • 新增glut專案
                                                    • 複製week11_keyboard程式碼
                                                    • 在#include <GL/freeglut.h>上方新增#include <windows.h>


                                                    • 修改keyboard程式碼

                                                      void keyboard(unsigned char key,int x,int y)
                                                      {
                                                          if (key=='1') PlaySound("C:\\Users\\Administrator\\Desktop\\do-re-mi\\do.wav",NULL,SND_ASYNC);
                                                          if (key=='2') PlaySound("C:\\Users\\Administrator\\Desktop\\do-re-mi\\re.wav",NULL,SND_ASYNC);
                                                          if (key=='3') PlaySound("C:\\Users\\Administrator\\Desktop\\do-re-mi\\mi.wav",NULL,SND_ASYNC);
                                                          if (key=='4') PlaySound("C:\\Users\\Administrator\\Desktop\\do-re-mi\\fa.wav",NULL,SND_ASYNC);
                                                          if (key=='5') PlaySound("C:\\Users\\Administrator\\Desktop\\do-re-mi\\so.wav",NULL,SND_ASYNC);
                                                      }


                                                    • 執行結果:按下鍵盤對應的數字1~5會發出對應聲音

                                                    CPP播放聲音

                                                    • 新增一個empty file
                                                    • 檔案格式cpp
                                                    • 輸入程式碼
                                                      #include <windows.h>
                                                      #include <stdio.h>

                                                      int main()
                                                      {
                                                          PlaySound("C:\\Users\\Administrator\\Desktop\\do-re-mi\\re.wav",NULL,SND_SYNC);
                                                          printf("Hello World");
                                                      }

                                                      SND_SYNC與前面用GLUT的SND_ASYNC差在同步與非同步


                                                    • setting-compiler-linker setting-add-winmm


                                                    • 將PlaySound以相對路徑寫
                                                      PlaySound("do-re-mi/re.wav",NULL,SND_SYNC);

                                                    播放MP3格式(好處可以同時播很多檔)

                                                    • 新增glut 專案,不刪程式碼
                                                    • 將CMP3_MCI.h 放入程式碼位置
                                                    • 新增 #include "CMP3_MCI.h" ///印號引進外掛,檔案要放在跟程式檔同目錄


                                                    • 在int main()附近新增程式碼
                                                    #include "CMP3_MCI.h" ///印號引進外掛,檔案要放在跟程式檔同目錄

                                                        CMP3_MCI myMP3;

                                                            int main(int argc, char *argv[])
                                                              {
                                                                    char filename[]="C:/Users/Administrator/Desktop/do-re-mi/suzume.mp3";
                                                                      myMP3.Load(filename);
                                                                        myMP3.Play();

                                                                    ...
                                                                    }


                                                                    • save everything
                                                                    • 將freeglut-bin-freeglut.dll複製貼上到程式碼目錄
                                                                    • 將音檔移到程式碼目錄
                                                                    • project-properties-build target-execution working dir 改成小數點 .




                                                                    • 回到程式碼,將
                                                                    char filename[]="C:/Users/Administrator/Desktop/do-re-mi/suzume.mp3";
                                                                    改成myMP3.Load("suzume.mp3");



                                                                    解決dll檔會被github刪掉的問題
                                                                    • 下載github資料夾
                                                                    • 找到.gitignore
                                                                    • 用notepad++打開
                                                                    • 找到dll,註解掉


                                                                    • 存檔,完成,以後github備份就不會刪掉dll檔案

                                                                    沒有留言:

                                                                    張貼留言