Week11
開啟codeblocks開啟一個新的GLUT專案
名叫week11_keyboard
利用 ctrl + f 搜尋 keyboard
尋找相關的程式碼
glutKeyboardFunc(keyboard)
接著把全部的程式碼刪掉
新增以下程式碼
#include <GL/glut.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 ) exit(1234);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
執行後的結果為下列圖片
前面的範例裡面有提到按esc會結束也會有相關訊息
而27這個值代表的就是鍵盤上的Esc
所以下面的執行結果就是按了鍵盤上的esc後
會把程式結束 並出現相關代碼 1234
接著新增一個新的glut專案命名
week11_keyboard_PlaySound
要利用程式按鍵讓聲音出來
利用老師給我們的do re mi的音檔來播放
將程式打成下面這些
#include <windows.h>
#include <GL/glut.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 == '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_PlaySound.cpp 注意檔名是.cpp
接著將程式碼打成下列
#include <windows.h>
#include <stdio.h>
int main()
{
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);
printf("Hello World\n");
}
執行後會依序撥出 do re mi
接著出現Hello World
結果如下圖所示
接著可以將路徑改為相對路徑
程式碼就可以縮短
#include <windows.h>
#include <stdio.h>
int main()
{
///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);
PlaySound("do-re-mi/do.wav", NULL, SND_SYNC);
printf("Hello World\n");
}
新增一個新的glut專案
命名week11-4_CMP3_MCI_Load_Play
新增框框內的程式碼
執行程式碼後會出現下圖
就會播放我們所選的音樂
接著試著把目錄很長很長改成很短很短
先到project properties裡面
接著到下圖紅框的地方的路徑改成
一個 .
再把 freeglut.dll 跟 要執行的音檔suzume.mp3
移動到week11-4的專案資料夾
沒有留言:
張貼留言