week15
1. 講解播放聲音的參數和對應的lib
- 開啟CodeBlocks 檔名:week15-1_PlaySound.cpp
- 到Setting-Compile-Linker Setting-Add "winmm"
- 小黑視窗會播出錯誤音效
```c
///#include <mmsystem.h>//上週教
#include <windows.h>///另一種寫法,結果一樣
int main()
{ ///先放不存在的檔 上週教SND_ASYNC
PlaySound("badbadbad.wav", NULL, SND_SYNC);
}///Compile成功,但是link失敗
/// .c .cpp =compile=> .o =link=> .exe
```
2. 播放想要的音效
SND_SYNC 要等待同步
SND_ASYNC 不等待,不同步
- 開啟CodeBlocks 檔名:week15-2_SND_SYNC_SND_ASYNC
```c
#include <windows.h>
#include <stdio.h>///printf()
int main()
{
printf("現在在PlaySound()前\n");
PlaySound("07042111.wav", NULL, SND_ASYNC);
printf("現在在PlaySound()後\n");
int N;
scanf("%d", &N);///等你輸入數字
}
```
- 執行則播放音檔,若輸入數字則停止
5. 接續上週 week14_angles_TRT_write_and_read
- File-New-Project, GLUT檔
- 複製上週程式碼
- 原本 motion() 裡一直呼叫 myWrite() ,把它刪掉
- keyboard函數 按下r 會讀進一行 按下 s 來儲存位置
```c
#include <GL/glut.h>
#include <stdio.h> ///為了 printf, fprintf, fopen, fclose
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<20; 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<20; i++){
fscanf(fin, "%f", &angle[i]);
}
}
void keyboard( unsigned char key, int x, int y){
if( key=='r' ) myRead();
if( key=='s' ) myWrite();///save
if( key=='0' ) angleID=0;
if( key=='1' ) angleID=1;
if( key=='2' ) angleID=2;
if( key=='3' ) angleID=3;
}///用keyboard的按鍵來決定
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);///(3) 把下手肘掛在關節上
glRotatef(angle[1], 0, 0 ,1);///(2) 旋轉
glTranslatef(-0.8, -0.4, 0);///(1) 把下手肘的旋轉中心放正中心
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);///(3) 把下手肘掛在關節上
glRotatef(angle[3], 0, 0 ,1);///(2) 旋轉
glTranslatef(+0.8, -0.4, 0);///(1) 把下手肘的旋轉中心放正中心
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 );
glutInitWindowSize(600, 600);
glutCreateWindow("week15 TRT angles again");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutDisplayFunc( display );
glutMainLoop();
return 0;
}
```
6. 機器人跳舞 期末作業講解












沒有留言:
張貼留言