2015年4月9日 星期四

FreeGLUT Test6




●今日嘗試:
          按鍵盤撥放音效!!!
          鍵盤控制上下!!!


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

void display(void);
void keyboard(unsigned char, int, int);
void mouse(int, int, int, int);
void motion(int, int);
void special(int key, int x, int y);
float rotX=0,rotY=0,oldX=0,oldY=0,teaX=0,teaY=0;

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("02160295");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutSpecialFunc(special);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(0,teaY, 0);
        glRotatef(rotX,1,0,0);
        glRotatef(rotY,0,1,0);
        glutSolidTeapot(0.5);
    glPopMatrix();
    glFlush();
}

void keyboard(unsigned char key, int x, int y)
{
    if(key=='a') PlaySound("a.wav",NULL,SND_ASYNC), printf("key=a");
    if(key=='1') PlaySound("1_C.wav",NULL,SND_ASYNC), printf("Sound=Do\n");
    if(key=='2') PlaySound("2_D.wav",NULL,SND_ASYNC), printf("Sound=Re\n");
    if(key=='3') PlaySound("3_E.wav",NULL,SND_ASYNC), printf("Sound=Mi\n");
    if(key=='4') PlaySound("4_F.wav",NULL,SND_ASYNC), printf("Sound=Fa\n");
    if(key=='5') PlaySound("5_G.wav",NULL,SND_ASYNC), printf("Sound=Sol\n");
    if(key=='6') PlaySound("6_A.wav",NULL,SND_ASYNC), printf("Sound=La\n");
    if(key=='7') PlaySound("7_B.wav",NULL,SND_ASYNC), printf("Sound=Si\n");
}

void motion(int x, int y)
{
    rotX += y-oldY;
    rotY += x-oldX;
    oldX=x; oldY=y;
    glutPostRedisplay();
}

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 special(int key,int x, int y)
{
    if(key==GLUT_KEY_DOWN)
    {
        teaY-=0.1; printf("down\n");
    }
    else if(key==GLUT_KEY_UP)
    {
        teaY+=0.1; printf("up\n");
    }
    glutPostRedisplay();
}


沒有留言:

張貼留言