Week 11
Week11-1.1 關閉按鍵
Step 1:先開啟CodeBlock並開啟GLUT project
Step 2:按下Ctrl+F 可開啟【搜尋字功能】
Step 3:看95行程式碼,Block為【按鍵偵測】
程式碼:
#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();
}
程式碼解釋:
key-27:ESC的按鍵編號
Week 11-1.2 WAV檔
Step 1:開啟CodeBlock並開啟GLUT project
Step 2:將下列程式碼複製並貼上
程式碼:
#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 == 27)exit(1234);
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);
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
PlaySound()第一個參數要放【目標檔案的位置】
Week 11-02.1 播放後顯示訊息
Step 1:開新的GLUT project
將下列程式碼複製進project當中
程式碼:
#include<windows.h>
#include<stdio.h>
int main()
{
PlaySound("do.wav",NULL,SND_SYNC);
printf("Hello World\n");
}
切記:#include<windows.h>要include在最前面
如果不指定目錄,則【音樂檔】要放在與此程式執行檔相同目錄中
Step 3:觀看程式執行結果
Week 11-2.2 MP3檔
Step 1:開新的GLUT project,這次留下程式碼
Step 2:在main()函式〔大概100多行附近〕,要先#include "CMP3_MCI.h",並先宣告CMP3_MCI物件
Step 3:宣告一個字串,儲存欲撥放的檔案路徑
詳細程式碼:
#include "CMP3_MCI.h"
CMP3_MCI myMP3; //宣告mp3函數
main()://要打在在main函式中
char filename[]="C:/Users/Administrator/Desktop/do-re-mi/thoseyear.wav";
myMP3.Load(filename);
myMP3.Play();
Load:載入檔案
Play:播放檔案
mp3物件可以為wav檔案
Step 4:看程式執行結果,完成!
Week 11_3.1
Step 1:開啟CodeBlock並且開啟GLUT project
Step 2:點選Project-Properties
Step 3:點選 Build targets
Step 4:選取下面的Execution working dir,可以修改程式的執行路徑
沒有留言:
張貼留言