2020年5月13日 星期三

月光

主題: 聲音、音效、音樂
用glut來放出聲音
#include <windows.h>
#include <mmsystem.h>
int main()
{
    PlaySoundA("nier.wav",NULL,SND_SYNC);

}
#include <mmsystem.h>這行是播音樂
我們還能用它來彈鋼琴
其中要注意的一點
if(key=='1') PlaySoundA("do.wav",NULL,SND_ASYNC);
和上面不同的是要加A
我們也加一些程式碼讓他能放mp3檔

#include "CMP3_MCI.h"
CMP3_MCI mp3;
mp3.Load("whoyou.mp3");
mp3.Play();
主要加這4行就能夠播放
注意的一點是要把CMP3_MCI.h放到你程式的資料夾才可以
我們把音階放在開頭當變數,這樣就能變成一起按不同的音

 接下來,我們要改專案檔,把sound.cbp中資料位置都改成 . ,之後再把GLUT裡bin的wav檔跟mp3檔還有freeglut.dll放到專案中的資料夾就可以
float ballX=0,ballY=0,dx=0.05,dy=0.03;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(ballX,ballY,0);
        glutSolidSphere(0.1,30,30);
    glPopMatrix();
    glutSwapBuffers();
}
void timer(int t)
{
    glutTimerFunc(50,timer,t+1);
    ballX+=dx; ballY+=dy;
    if(ballX>=1)
    {
        Do.Play(); dx=-dx;
    }
    if(ballX<=-1)
    {
        Re.Play(); dx=-dx;
    }
    if(ballY>=1)
    {
        Mi.Play(); dy=-dy;
    }
    if(ballY<=-1)
    {
        Fa.Play(); dy=-dy;
    }
    glutPostRedisplay();
}
void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN) PlaySoundA("shot.wav",NULL,SND_ASYNC);
}
然後在main中加入
glutTimerFunc(50,timer,0);
glutMouseFunc(mouse);





沒有留言:

張貼留言