2023年4月26日 星期三

SangMo 電腦圖學 Week11 鍵盤聲音滑鼠

 

Step01-1 鍵盤程式

開啟codeblock 開啟一個GLUT專案 檔名week11-1_keyboard

在原本茶壺的程式上再加上keyboard相關程式

#include <GL/glut.h>

void display()

{

    glutSolidTeapot(0.3);

    glutSwapBuffers();

}

void keyboard(unsigned char key, int x,int y)

{

    if(key==27)exit(0);

}

int main(int argc, char**argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week11");


    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);


    glutMainLoop();

}

Step01-2 聲音程式

開啟一個新GLUT專案 檔名week11-2_keyboard_wav_PlaySound
複製week11-1_keyboard程式
#include<windows.h>///放最上面
#include <GL/glut.h>
void display()
{
    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);
}2個斜線=1個斜線
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week11");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);

    glutMainLoop();
}
開啟後有聲音即為成功

Step02-1 聲音程式

先把剛剛的GLUT專案save everything重開codeblock 
File-New-Empty 存成week11-3_PlaySound.cpp
之後在開頭打上
#include<windows.h>
接著打
int main()
{
    PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\do.wav", NULL, SND_SYNC);
    PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\do.wav", NULL, SND_SYNC);
    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);
}
然後打開Setting-Compiler -> add winmm -ok
OpenCV要3咒語
GLUT要5咒語
PlaySound要1咒語
執行後需要等待SND_SYNC一下聲音 出現do的音為成功

Step02-1.5筆記對自身的重要性

筆記的好處,使用HackMD作筆記,觀看老師介紹Feetcode程式筆記


Step02-2 MP3音樂程式

開啟的GLUT專案 名稱week11-4_glut_mp3
在第20打入下方程式#include"CMP3_MCI.h"

接著把老師給的檔案複製過去
到140行打入下方程式
#include"CMP3_MCI.h"
CMP3_MCI myMP3;///宣告變數

int main(int argc, char *argv[])
{
    char filename[]="C:\\Users\\Administrator\\Desktop\\do_re_mi\\suzumi.mp3";(\\=/)
    myMP3.Load(filename);
    ///myMP3.Load("檔名.mp3");
    myMP3.Play();
執行會播放音樂

Step03-1絕對路徑 相對路徑

用Notepad++打開然後變成下方圖的樣子
一定要存檔(旁邊檔案變藍即可)

然後把mp3檔丟到glut資料夾裡
同理放入dlll檔案
就能把程式精簡
#include"CMP3_MCI.h"
CMP3_MCI myMP3;

int main(int argc, char *argv[])
{
    myMP3.Load("suzumi.mp3");
    myMP3.Play();
}
執行後播放音樂就成功

Step03-2 Git上傳時的.gitignore 哪些檔案不備份

檔名相同的dlll檔案若重複出現會導致後面的檔案都會消失
所以需要備份它
需要備份的檔案前面加上#字號並儲存



沒有留言:

張貼留言