2023年4月26日 星期三

DTang---Week11_Keyboard

step01-1_鍵盤 

新增專案,專案裡有預設的keyboard的程式範例

下為最基本範例,設定為按下esc便會關閉視窗

#include <GL/glut.h>

void display(){

    glutSolidTeapot(0.3);

    glutSwapBuffers();

}

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

    if(key==27)

        exit(886); /// 視窗會顯示輸入的數字

}///按esc就會關閉

int main(int argc, char**argv){

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week11");

    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);

    glutMainLoop();

}

--------------------------------------------------------------------------------------------------------------

week11-2 鍵盤、wav檔跟PlaySound()函式

程式設定可撥放音樂的檔案有兩種wav跟mp3

wav--檔案大,無壓縮,可隨時撥放

mp3--檔案小,有壓縮,撥放較為困難

PlaySound(檔名, NULL, SND_ASYNC);

NULL表示撥放音檔沒有在任何物件下

SND_ASYNC為"不等待同步",亦為馬上撥放

#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') /// 播放音樂檔的方法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);

}

int main(int argc, char**argv){

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week11");

    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);

    glutMainLoop();

}

-----------------------------------------------------------------------------------------------------------------

week11-3 playsound.cpp

在單獨(新增Empty file)下的狀況,直接執行PlaySound()

這樣會無法執行
要手動加入winmm 這個lib



#include <windows.h>

/// week11-3_PlaySound.cpp 如果沒有手動加上lib就無法正常執行

///setting-compiler-->linker setting --> add "winmm" 這個lib

int main()

{                                                                 ///要等待同步

    PlaySound("do_re_mi\\do.wav", NULL, SND_SYNC);

    PlaySound("do_re_mi\\re.wav",NULL,SND_SYNC);

    PlaySound("do_re_mi\\mi.wav",NULL,SND_SYNC);

    PlaySound("do_re_mi\\do.wav",NULL,SND_SYNC);

}


///Checking for existence: C:\Users\Administrator\Desktop\week11-3_PlaySound.exe

///Executing: '"C:\Program Files (x86)\CodeBlocks/cb_console_runner.exe" "C:\Users\Administrator\Desktop\week11-3_PlaySound.exe"' (in 'C:\Users\Administrator\Desktop')

///由上列的資訊可以得知,這個執行檔的執行目錄是在C:\Users\Administrator\Desktop下面,所以在這種情況下要撥放音樂可以使用"相對路徑"

----------------------------------------------------------------------------------------------------------------

week11-4_glut mp3

將外掛檔(CMP3_MCI.h)放到相同專案目錄下


使用外掛的class宣告一個變數

在來使用他進行Load(讀檔)、Play(撥放)


///新增專案

///在原來的範例程式裡,插入一個外掛(要放在week11-4_glut_mp3相同目錄下)

#include "CMP3_MCI.h"


CMP3_MCI myMP3;

...

                            /// 字串裡\\或/都可以當目錄

    char filename [] = "C:\\Users\\Administrator\\Desktop\\do_re_mi\\suzumi.mp3";

    myMP3.Load(filename);

    myMP3.Play();

...


第二種用法: 工作執行目錄,修改.cbp檔內的working_dir

工作執行目錄(working_dir)在C:/Users/Administrator/Desktop/freeglut/bin下

為了能更簡單的套用目錄下的音樂,可透過修改working_dir的所在位置來實現

                                                          /// 字串裡\\或/都可以當目錄

    ///char filename [] = "C:\\Users\\Administrator\\Desktop\\do_re_mi\\suzumi.mp3";

    ///myMP3.Load(filename);///此為絕對路徑absoulute path 一定可以運作,但很長,且在另一台電腦上執行就會有問題


    ///如果要在更簡單的目錄裡,要把專案的工作執行目錄(working_dir),改成"."(現在目錄)

    myMP3.Load("suzumi.mp3");///相對路徑

    myMP3.Play();



---------------------------------------------------------------------------------------------------------------------

gitignore










沒有留言:

張貼留言