2022年5月30日 星期一

Yiting_week15

 1、

1-1:播放聲音的另一個方式

#include <windows.h>///另一種寫法

int main()

{

    PlaySound("badbadbad.wav",NULL,SND_SYNC);///先放不存在的檔

}///此時compiler成功,但link失敗

所以此時要在setting設定

路徑:setting/compiler/Linker settings/add winmm

即可播放聲音(若檔案放的是不存在的檔,也至少有錯誤的聲音)

執行後,下面bulid log會告知聲音檔放哪可以播放,
再將聲音檔放在資料夾中即可。



1-2:

#include <windows.h>
#include <stdio.h>
int main()
{
    printf("現在在PlaySound()前\n");
    PlaySound("badbadbad.wav",NULL,SND_ASYNC);///播出後不等待
    printf("現在在PlaySound()後\n");
    int N;
    scanf("%d",&N);///等你輸入數字,輸入即結束程式碼
}


1-3:播放MP3

#include "CMP3_MCI.h"
#include <stdio.h>
CMP3_MCI mp3;
int main()
{
    mp3.Load("MJQZZY.mp3");
    mp3.Play();
    printf("現在播放聲音\n");
    int N;///卡住不結束
    scanf("%d",&N);
}

2、動畫

2-1:

#include <GL/glut.h>
#include <stdio.h>///為了能用printf、fprintf、fopen......
float angle[20],oldX=0;
int angleID=0; ///0:第0個關節,1:第1個關節
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;
    }///如果fout正在跑其他東西,把其他東西關掉
    if(fin==NULL)fin=fopen("file.txt","r");
    for(int i=0;i<20;i++){
        fscanf(fin,"%f",&angle[i]);
    }
    glutPostRedisplay();///重畫畫面
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='r') myRead();///按r讀檔
    if(key=='s') myWrite();///存檔
    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");

    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}







沒有留言:

張貼留言