讓GLUT可以播聲音
#include <stdio.h>
#include <windows.h>///為了定義DWORD
#include <mmsystem.h>///為了PlaySoundA(),必須在windows.h後面
int main()
{/// WAV only 放在freeglut\bin
PlaySoundA("Birds.wav",NULL, SND_SYNC);///For Sound
printf("Hello World\n");
}
---------------------------------------------------------------------------------------------------------
按下滑鼠發出聲音
#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>
#include <GL/glut.h>
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y){///當按下滑鼠,發出聲音
if(state==GLUT_DOWN) {
PlaySoundA("Birds.wav",NULL, SND_SYNC);
printf("hi\n");
}
}
int main(int argc,char**argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("haha");
glutMouseFunc(mouse);
glutDisplayFunc(display);
glutMainLoop();
}
---------------------------------------------------------------------------------------------------
讓GLUT可以播mp3並把他當BGM
先把CMP3_MCI.h程式放在專案檔裡面
#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>
#include <GL/glut.h>
#include "CMP3_MCI.h"///雙引號,表示要在同一個專案檔中
CMP3_MCI myMP3;///宣告一個物件變數
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y){
if(state==GLUT_DOWN) {
PlaySoundA("Birds.wav",NULL, SND_SYNC);
printf("hi\n");
}
}
int main(int argc,char**argv){
myMP3.Load("bgmAC.mp3");///讀入檔案,放在freeglut\bin
myMP3.Play();///播放
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("haha");
glutMouseFunc(mouse);
glutDisplayFunc(display);
glutMainLoop();
}
---------------------------------------------------------------------------------------------------------------
PlaySoundA("Birds.wav",NULL, SND_ASYNC);///不用等聲音播完,就可以播下一次
-----------------------------------------------------------------------------------------------------------
讓音樂檔可以存在專案檔的位置
把箭頭所指的地方的地址改成"."
並且在codeblock讓他Reload Project
-----------------------------------------------------------------------------------------------
讓他播出鋼琴聲
#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>
#include <GL/glut.h>
#include "CMP3_MCI.h"
CMP3_MCI myMP3;
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y){
if(state==GLUT_DOWN) {
PlaySoundA("Birds.wav",NULL, SND_ASYNC);
printf("hi\n");
}
}
void keyboard(unsigned char key,int x,int y){
///利用鍵盤1,2,3,4,5來播出C,D,E,F,G的聲音
if(key=='1') PlaySoundA("do.wav",NULL, SND_ASYNC);
if(key=='2') PlaySoundA("re.wav",NULL, SND_ASYNC);
if(key=='3') PlaySoundA("mi.wav",NULL, SND_ASYNC);
if(key=='4') PlaySoundA("fa.wav",NULL, SND_ASYNC);
if(key=='5') PlaySoundA("so.wav",NULL, SND_ASYNC);
}
int main(int argc,char**argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("haha");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutDisplayFunc(display);
glutMainLoop();
}
-------------------------------------------------------------------------------------------------------
讓鋼琴聲可以同時播放
#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>
#include <GL/glut.h>
#include "CMP3_MCI.h"
CMP3_MCI myMP3;
CMP3_MCI C,D,E,F,G;///加入變數
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y){
if(state==GLUT_DOWN) {
PlaySoundA("Birds.wav",NULL, SND_ASYNC);
printf("hi\n");
}
}
void keyboard(unsigned char key,int x,int y){
if(key=='1') C.Play();
if(key=='2') D.Play();
if(key=='3') E.Play();
if(key=='4') F.Play();
if(key=='5') G.Play();
}
int main(int argc,char**argv){
C.Load("do.wav");
D.Load("re.wav");
E.Load("mi.wav");
F.Load("fa.wav");
G.Load("so.wav");
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("haha");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutDisplayFunc(display);
glutMainLoop();
}








沒有留言:
張貼留言