2023年4月27日 星期四

KZNK Week11 鍵盤、滑鼠、音樂

 Week11-1 鍵盤  

OpenCV 3咒語
安裝 OpenCV, 要記得 Add PATH
1.加上 -Search Directiories  咒語: C:\OpenCV2.1\include
2.加上 -Search Directiories  咒語: C:\OpenCV2.1\lib
3.加上 -Linker Settings        咒語: cv210 cxcore210 highgui210
Settings-Compiler-

開啟新的GLUT專案,命名week11-1 keyboard
輸入以下程式碼
用鍵盤來操作程式,if(key==27)這個鍵是Esc鍵,按下後可離開程式




















Week11-2 音樂
  
開啟新的GLUT專案,命名week11-2 keyboard_PlaySound
老師傳送的音樂資料夾放在桌面上
輸入以下程式碼
#include <windows.h>
#include <GL/glut.h>
void display()
{
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
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);
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week11");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}

輸入1 2 3發出DO RE MI的聲音

















Week11-3 音樂撥放
  
開啟New File並取名為week11-3 PlaySound.cpp
設定咒語 SettingsCompilerLinker Settings  winmm
輸入程式碼
#include <windows.h>
#include <GL/glut.h>
int main(int argc,char**argv)
{
    PlaySound("C:\\Users\\Administrator\\Desktop\\do-re-mi\\do.wav",NULL,SND_ASYNC);
    PlaySound("C:\\Users\\Administrator\\Desktop\\do-re-mi\\re.wav",NULL,SND_ASYNC);
    PlaySound("C:\\Users\\Administrator\\Desktop\\do-re-mi\\mi.wav",NULL,SND_ASYNC);
    printf("Hello World\n");
}
一樣可以撥放DO RE MI











絕對路徑與相對路徑












Week11-4 自動撥放音樂

開啟新的GLUT專案,命名week11-4 CMP3_MCI_Load_Play
從第137行開始更改程式碼如以下

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

要將CMP3_MCI.h也放在同個專案資料夾裡
Ctrl C+V 放入week11-4 CMP3_MCI_Load_Play資料夾
執行會有音樂



















將freeglut裡bin中的freeglut.dll放入同個專案資料夾裡(week11-4)
專案上按右鍵出現選單點最下面的Properties...
接著點第二欄的Build targets
再來找到Execution working dir 工作執行目錄 將路徑改成" . "





















音樂就會自動撥放了

沒有留言:

張貼留言