2020年5月12日 星期二

發出聲音拉

今天讓程式可以發出聲音:
一樣是用GLUT專案,並且要把wav檔案放在GLUT檔案夾裡的bin中。
如果要更改成使用滑鼠按鍵來發出聲音的話就要另外宣告一些東西:
#include<GL/glut.h>
#include<stdio.h>
#include<windows.h>///定義一些DWORD
#include<mmsystem.h>///為了PlaySoundA()
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSwapBuffers();
}
void mouse(int button,int state, int x,int y)
{/// *1.Mouse要發出聲音
    if(state==GLUT_DOWN)
    {
        PlaySoundA("Gun.wav",NULL,SND_SYNC);
    }
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Bang!!");

    glutMouseFunc(mouse);/// *1
    glutDisplayFunc(display);
    glutMainLoop();
}
--------------------------------------------------------------------------------------------------------------------------
改為播放MP3檔案

#include<GL/glut.h>
#include<windows.h>///定義一些DWORD
#include<mmsystem.h>///為了PlaySoundA()
#include"CMP3_MCI.h"///*2.雙引號,表示要在同一個專案目錄找
CMP3_MCI myMP3;///*2.宣告
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSwapBuffers();
}
void mouse(int button,int state, int x,int y)
{/// *1.Mouse要發出聲音
    if(state==GLUT_DOWN)
    {
        PlaySoundA("Gun.wav",NULL,SND_SYNC);
    }
}
int main(int argc,char**argv)
{
    myMP3.Load("Sunday.mp3");///*2.讀入檔案
    myMP3.Play();///*2.撥放
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Bang!!");

    glutMouseFunc(mouse);/// *1
    glutDisplayFunc(display);
    glutMainLoop();
}
--------------------------------------------------------------------------------------------------------------------------
讓全部的東西在自己的專案檔裡面運行:
將自己的專案.cbp用notepad++開啟後,將 Option working_dir 的部分更改成".",再把freeglut.dll和自己的音樂檔全部丟到自己的專案夾即可。
--------------------------------------------------------------------------------------------------------------------------

沒有留言:

張貼留言