2023年4月26日 星期三

愛喝奶茶的石靈-week11-鍵盤滑鼠跟音樂

 要來塞音樂囉 不知道塞個nyan cat會不會很好笑

第一節課

程式碼:

#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

2.出現茶壺圖片後按下ESC,測試是否關閉






第二節課
程式碼:
#include <windows.h>
#include <GL/glut.h>
{
    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();
}
3. 執行結果(出現茶壺圖案,按數字鍵1、2、3,會出現Do,Re,Mi的聲音)


最後一節課

建立新的File並取名為week11-3_PlaySound.cpp

程式碼:
#include <windows.h>
int main()
{
    PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\do.wav",NULL,SND_SYNC);
    PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\do.wav",NULL,SND_SYNC);
    PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\do.wav",NULL,SND_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);
}

又出問題了
在Setting--Compiler的Linker Setting加上winmm,因為實際上PlaySound是在winmm的函式庫裡

最後弄出音樂擋


3. 將CMP3_MCI.h檔案加入week11-4_glut_mp3資料夾目錄中



在Notepad++開啟week11-4_glut_mp3目錄中的cbp檔,並將音檔目錄更改為「.」,更改完成後,將檔案儲存,開啟codeblocks檔案,並按下 "Yes"

5. 將freeglutbinfreeglut.dll、suzumi音檔加入week11-4_glut_mp3目錄中


6.執行即可





下課~





















沒有留言:

張貼留言