先來複習怎麼發出聲音吧
1.創建資料夾(week15_1)
2.開codeblocks創建新檔案
3.存成week15_1.cpp
4.放上程式碼:
#include <windows.h>
int main()
{
PlaySound("TestSound.wav",NULL,SND_SYNC);
}
5.將TestSound.wav放在week15_1裡面
6.在執行之前別忘了確認winmm資料庫在不在(Settings>>Complier>>Linker Settings),如果沒有,只需要按add即可
7.執行
一小段聲音就這樣被播放了
PlaySound("TestSound.wav",NULL,SND_SYNC); 可以換成:
PlaySound("TestSound.wav",NULL,SND_ASYNC);
為了比較一下,我們需要<stdio.h>
#include <windows.h>
#include <stdio.h>
int main()
{
printf("Start\n");
PlaySound("TestSound.wav",NULL,SND_SYNC);
printf("End\n");
}
使用SND_SYNC時,Start會先跑出來,播放聲音後,在顯示End。
換成SND_ASYNC後,Start跟End同時出現,不等待聲音播完。如果程式碼太短導致聲音來不及播放,可以用個scanf拖延一下時間。
目前這個資料庫只能播放wav檔,想播放mp3檔案時...
1.保留現在這個檔案
2.下載CMP3_MCI.h(應該有個地方可以下載,當時老師沒有特別解釋)
3.跟音檔一樣,放在同一個資料夾裡面
4.將TestSound.wav轉成TestSound.mp3,放在同一個資料夾裡面
不要直接改檔按形式(在.wav 後面按BackSpace),會出錯!
#include <stdio.h>
#include "CMP3_MCI.h"
CMP3_MCI mp3;
int main()
{
printf("Start\n");
mp3.Load("TestSound.mp3");
mp3.Play();
int n;
scanf("%d",&n);
printf("End\n");
}
這東西沒有同步的問題,所以必須用scanf,我們也暫時不會需要n
接著,回到上周的存檔讀檔... 最後的檔案應該會像這樣子
#include <GL/glut.h>
#include <stdio.h>
float angle[20]={},oldX=0;
int angleID=0;
FILE*fout=NULL,*fin=NULL;
void myWrite(){
if(fout==NULL) fout=fopen("file.txt","w+");
for(int i=0;i<8;i++){
printf("%.2f ",angle[i]);
fprintf(fout,"%.2f",angle[i]);
}
printf("\n");
fprintf(fout,"\n");
}
void myRead(){
if(fout!=NULL){fclose(fout),fout=NULL;}
if(fin==NULL) fin=fopen("file.txt","r");
for(int i=0;i<8; i++){
fscanf(fin,"%f",&angle[i]);
}
glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='s')myWrite(); ///加這一行
if(key=='r')myRead();
if(key=='0')angleID=0;
if(key=='1')angleID=1;
if(key=='2')angleID=2;
if(key=='3')angleID=3;
}
void mouse(int button,int state,int x,int y){
oldX=x;
}
void motion(int x,int y){
angle[angleID]+=(x-oldX);
myWrite(); ///刪掉這一行
oldX=x;
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf(0.3,0.5,-0.3,-0.2);
glPushMatrix();
glTranslatef(0.3,0.5,0);
glRotatef(angle[0],0,0,1);
glTranslatef(-0.3,-0.4,0);
glColor3f(1,0,0);
glRectf(0.3,0.5,0.8,0.3);
glPushMatrix();
glTranslatef(0.8,0.4,0);
glRotatef(angle[1],0,0,1);
glTranslatef(-0.8,-0.4,0);
glColor3f(0,1,0);
glRectf(0.8,0.5,1.1,0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.3,0.5,0);
glRotatef(angle[2],0,0,1);
glTranslatef(0.3,-0.4,0);
glColor3f(1,0,0);
glRectf(-0.3,0.5,-0.8,0.3);
glPushMatrix();
glTranslatef(-0.8,0.4,0);
glRotatef(angle[3],0,0,1);
glTranslatef(0.8,-0.4,0);
glColor3f(0,1,0);
glRectf(-0.8,0.5,-1.1,0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main (int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week14_2");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}



沒有留言:
張貼留言