2023年4月26日 星期三

Code Talker Week11 鍵盤 音樂

 STEP01

1.開啟一個新的GLUT專案

2.打上以下程式碼:

#include <GL/glut.h>

void display()

{

    glutSolidTeapot(0.3);

    glutSwapBuffers();

}

void keyboard(unsigned char key, int x, int y)

{

    if(key==27) exit(0);//關掉

}//按ESC鍵關掉

int main(int argc, char**argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("Week11");


    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);


    glutMainLoop();

}

用 void keyboard(), 呼叫 exit(0) 程式就結束,並return 0

3.執行後按下ESC,測試是否關閉

STEP02
1.想要播放音樂,建立一個新的專案11-2
2.將keyboard內程式碼替換成:
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);
註:這裡是用絕對路徑
3.標頭檔最前頭加上函式庫#include <windows.h>,讓PlaySound得以啟用

STEP03
1.我們發現如果把放音樂的部分,改成Main函式的話,音樂會放不出來
2.關掉重開CodeBlock,並開啟一個新的空白檔案11-3
3.打上下列程式碼:
#include <windows.h>

int main()
{
    PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\do.wav", NULL, SND_SYNC);//SYNC等待同步
    PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\re.wav", NULL, SND_SYNC);
    PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\mi.wav", NULL, SND_SYNC);
    PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\fa.wav", NULL, SND_SYNC);
    PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\so.wav", NULL, SND_SYNC);
}
無法成功執行
4.在Setting--Compiler的Linker Setting加上winmm,因為實際上PlaySound是在winmm的函式庫裡
5.加入後成功執行

STEP04
1.開啟一個新的專案11-4
2.做出下圖的樣子

3.執行聽有沒有音樂

STEP05

1.絕對路徑有個缺點,就是換電腦就完蛋了,所以想使用相對路徑
2.我們用notepad++打開11-4的cbp檔案,把11行21行的工作目錄換成.
3.把filename兩行都註解掉
4.將MP3檔案加入到11-4的資料夾
5.資料夾中加入freeglut.dll
6.執行即可


沒有留言:

張貼留言