2015年4月9日 星期四

week07_加入音效,鍵盤,滑鼠_呂登祐

01.鍵盤輸入 可輸入印出

#include <GL/glut.h>
#include <stdio.h>

void display()
{

    glutSolidTeapot(0.3);
    glFlush();
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='a') printf("aaaaa\n");
    if(key=='b') printf("bbbbb\n");
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("林周罵尬李拚了!");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
    return 0;
}

02.鍵盤輸入 可輸入印出音效

#include <GL/glut.h>
#include <stdio.h>
#include <mmsystem.h>  ///多媒體

void display()
{

    glutSolidTeapot(0.3);
    glFlush();
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='a') {
        PlaySound("gos.wav",NULL,SND_ASYNC); ///音效,音樂檔路徑要放在freeglut\bin裡面
        printf("aaaaa\n");
    }
    if(key=='b') printf("bbbbb\n");
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("林周罵尬李拚了!");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
    return 0;
}


03.增加鋼琴音階

#include <GL/glut.h>
#include <stdio.h>
#include <mmsystem.h>

void display()
{

    glutSolidTeapot(0.3);
    glFlush();
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='a') {
        PlaySound("gos.wav",NULL,SND_ASYNC);
        printf("aaaaa\n");
    }
    if(key=='1') PlaySound("Do.wav",NULL,SND_ASYNC);  //音階DO
    if(key=='2') PlaySound("Re.wav",NULL,SND_ASYNC);
    if(key=='3') PlaySound("Mi.wav",NULL,SND_ASYNC);
    if(key=='4') PlaySound("Fa.wav",NULL,SND_ASYNC);
    if(key=='5') PlaySound("Sol.wav",NULL,SND_ASYNC);
    if(key=='6') PlaySound("La.wav",NULL,SND_ASYNC);
    if(key=='7') PlaySound("Sia.wav",NULL,SND_ASYNC);
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("林周罵尬李拚了!");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
    return 0;
}

04.滑鼠控制方向

#include <GL/glut.h>
#include <stdio.h>
#include <mmsystem.h>
float teaY=0,rotX=0,rotY=0,oldX,oldY=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPopMatrix();
    glTranslatef(0,teaY,0);
    glRotatef(rotX,1,0,0);
    glRotatef(rotY,0,1,0);
    glutSolidTeapot(0.3);
    glPopMatrix();
    glFlush();
}
void mouse (int button,int state,int x,int y)
{
    oldX=x; oldY=y;
    if(GLUT_LEFT_BUTTON==button&&GLUT_DOWN==state)
        printf("now left down\n");
    if(GLUT_RIGHT_BUTTON==button&&GLUT_DOWN==state)
        printf("now right down\n");
}
void motion(int x,int y)
{
    rotX +=y-oldY;
    rotY +=x-oldX;
    oldX=x;
    oldY=y;
    glutPostRedisplay();
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("林周罵尬李拚了!");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
    return 0;
}

05.鍵盤控制方向

#include <GL/glut.h>
#include <stdio.h>
#include <mmsystem.h>
float teaY=0,rotX=0,rotY=0,oldX,oldY=0;

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPopMatrix();
    glTranslatef(0,teaY,0);
    glRotatef(rotX,1,0,0);
    glRotatef(rotY,0,1,0);
    glutSolidTeapot(0.3);
    glPopMatrix();
    glFlush();
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='a')
    {
        PlaySound("gos.wav",NULL,SND_ASYNC);
        printf("aaaaa\n");
    }
    {
        if(key=='1') PlaySound("Do.wav",NULL,SND_ASYNC);
        if(key=='2') PlaySound("Re.wav",NULL,SND_ASYNC);
        if(key=='3') PlaySound("Mi.wav",NULL,SND_ASYNC);
        if(key=='4') PlaySound("Fa.wav",NULL,SND_ASYNC);
        if(key=='5') PlaySound("Sol.wav",NULL,SND_ASYNC);
        if(key=='6') PlaySound("La.wav",NULL,SND_ASYNC);
        if(key=='7') PlaySound("Sia.wav",NULL,SND_ASYNC);
    }
}
void mouse (int button,int state,int x,int y)
{
    oldX=x;
    oldY=y;
    if(GLUT_LEFT_BUTTON==button&&GLUT_DOWN==state)
        printf("now left down\n");
    if(GLUT_RIGHT_BUTTON==button&&GLUT_DOWN==state)
        printf("now right down\n");
}
void motion(int x,int y)
{
    rotX +=y-oldY;
    rotY +=x-oldX;
    oldX=x;
    oldY=y;
    glutPostRedisplay();
}
void special(int key,int x,int y)
{
    if(key==GLUT_KEY_DOWN)
    {
        teaY-=0.2;
        printf("down\n");
    }
    else if(key==GLUT_KEY_UP)
    {
        teaY+=0.2;
        printf("up\n");
    }
    glutPostRedisplay();
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("ªhello!");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutSpecialFunc(special);
    glutMainLoop();
    return 0;
}



沒有留言:

張貼留言