2023年4月26日 星期三

Week11_10160863_鍵盤、滑鼠、音樂

 Week11_鍵盤、滑鼠、音樂

開啟CodeBlock GLUT專案命名week11-1_KeyBoard (鍵盤

#include <GL/glut.h>

void display()

{

    glutSolidTeapot(0.3);

    glutSwapBuffers();

}

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

{

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

}

int main(int argc,char**argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week11");

    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);///今天新教的


    glutMainLoop();

}

按下Esc結束就會回傳值

開新專案命名week11-2_keyboard_wav_PlaySound (音樂


#include <windows.h>

#include <GL/glut.h>

void display()

{

    glutSolidTeapot(0.3);

    glutSwapBuffers();

}

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

{///step01-1今天教的  step01-2音樂檔,先用絕對路徑,要小心\\代表1個反斜線

    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);

}

int main(int argc,char**argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week11");

    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);

    glutMainLoop();

}

執行按鍵盤1,2,3可以發出聲音


重開codeblock->File->New->Empty File 命名為week11-3_PlaySound.cpp

#include <windows.h>

int main()

{                                                          ///要等待同步    SND_ASYNC

    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);

}

Settings->Compiler

Linker Settings->Add->新增winmm 後重開CodeBlock
由絕對路徑變成相對路徑

開啟新專案命名week11-4_glut_mp3
執行後會播音樂
#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.Play();

將week11-4_glut_mp3資料夾中的week11-4_glut_mp3.cbp檔案用nopad++開起來

11&21行改為"."

將freeglut->bin->freeglut.dll拉到week11-4_glut_mp3內
do_re_mi->suzumi.mp3拉到week11-4_glut_mp3內








沒有留言:

張貼留言