/*! -------------------------------------------------------------------- \file Input.cpp \brief Class to encapsulate keyboard and mouse input \author 15-493 Instructors \date 2002-09-04 $Updated: qsb $ $Date: 2002/12/04 07:29:14 $ -------------------------------------------------------------------- */ #include #include #include #include #include #include #include "input.h" #include "camera.h" #include "game.h" //! initialize singleton static variable Input *Input::s_pInput = NULL; //! name of the jpg file snapshots are saved to const char SNAPSHOT_IMAGE[50] = "snapshot.jpg"; //--------------------------------------------------------------------- // Input // Method: Destroy // //! cleans up the singleton when it is no longer needed // //--------------------------------------------------------------------- void Input::Destroy() { delete s_pInput; } //--------------------------------------------------------------------- // Input // Method: Constructor // //--------------------------------------------------------------------- Input::Input() { // initialize arrays memset(m_bButtonPressed, 0, sizeof(m_bButtonPressed)); memset(m_bKeyPressed, 0, sizeof(m_bKeyPressed)); memset(m_bSpecialPressed, 0, sizeof(m_bSpecialPressed)); // memset(m_iMousePos, 0, sizeof(m_iMousePos)); m_iMousePos[0] = glutGet(GLUT_WINDOW_WIDTH) / 2; m_iMousePos[1] = glutGet(GLUT_WINDOW_HEIGHT) / 2; _dx = m_iMousePos[0]; _dy = m_iMousePos[1]; glutWarpPointer(m_iMousePos[0], m_iMousePos[1]); glutSetCursor(GLUT_CURSOR_NONE); } //--------------------------------------------------------------------- // Input // Method: MouseMotion // //! Process mouse motion events // //--------------------------------------------------------------------- void Input::MouseMotion(int x, int y) { #if 0 // EDIT CODE HERE TO CHANGE CAMERA cout << "Mouse movin" << endl; if (m_bButtonPressed[LEFT_BUTTON]) { if ((dy != 0) || (dx != 0)) { // theCamera.SetPosition(dx*3,dy*3); } } else if (m_bButtonPressed[RIGHT_BUTTON]) { if ((dy != 0) || (dx != 0)) { //theCamera.SetPosition(dx*3, -dy*3); } } #endif MousePassiveMotion(x,y); } //--------------------------------------------------------------------- // Input // Method: MousePassiveMotion // //! keep track of passive motion // //--------------------------------------------------------------------- void Input::MousePassiveMotion(int x, int y) { #if 0 // GetCursorPos(mousePos); glutWarpPointer(m_iMousePos[0], m_iMousePos[1]); // calculate change in mouse pointer position int dx = x - m_iMousePos[0]; int dy = y - m_iMousePos[1]; //cout << "Dx: " << dx << ", Dy: " << dy << endl; int limit = 50; if (_dx + dx > limit) _dx = limit; else if (_dx + dx < -limit) _dx= -limit; else _dx += dx; if (_dy + dy > limit) _dy = limit; else if (_dy + dy < -limit) _dy= -limit; else _dy += dy; // cout << "cool" << endl; #else _dx = x; _dy = y; #endif } //--------------------------------------------------------------------- // Input // Method: MouseButton // //! process mouse button clicks // //--------------------------------------------------------------------- void Input::MouseButton(int button, int state, int x, int y) { switch (button) { case GLUT_LEFT_BUTTON: m_bButtonPressed[LEFT_BUTTON] = (state==GLUT_DOWN); break; case GLUT_MIDDLE_BUTTON: m_bButtonPressed[MIDDLE_BUTTON] = (state==GLUT_DOWN); break; case GLUT_RIGHT_BUTTON: m_bButtonPressed[RIGHT_BUTTON] = (state==GLUT_DOWN); break; default: cerr << "Input::mouseButton : unknown button" << endl; return; } m_iMousePos[0] = x; m_iMousePos[1] = y; } //--------------------------------------------------------------------- // Input // Method: Keyboard // //! process key down events // //--------------------------------------------------------------------- void Input::Keyboard(unsigned char key, int x, int y) { switch (key) { case 27: // ESCAPE is pressed exit(0); // quit case 'c': theGame._CaptureImage(SNAPSHOT_IMAGE); default: // store which key is pressed m_bKeyPressed[(int)key] = true; } } //--------------------------------------------------------------------- // Input // Method: Keyboard // //! process key up events // //--------------------------------------------------------------------- void Input::KeyboardUp(unsigned char key, int x, int y) { m_bKeyPressed[(int)key] = false; } //--------------------------------------------------------------------- // Input // Method: KeyboardSpecial // //! process special key down events // //--------------------------------------------------------------------- void Input::KeyboardSpecial(int key, int x, int y) { switch (key) { case GLUT_KEY_UP: m_bSpecialPressed[KEY_UP] = true; break; case GLUT_KEY_DOWN: m_bSpecialPressed[KEY_DOWN] = true; break; case GLUT_KEY_LEFT: m_bSpecialPressed[KEY_LEFT] = true; break; case GLUT_KEY_RIGHT: m_bSpecialPressed[KEY_RIGHT] = true; break; } } //--------------------------------------------------------------------- // Input // Method: KeyboardSpecialUp // //! process special key up events // //--------------------------------------------------------------------- void Input::KeyboardSpecialUp(int key, int x, int y) { switch (key) { case GLUT_KEY_UP: m_bSpecialPressed[KEY_UP] = false; break; case GLUT_KEY_DOWN: m_bSpecialPressed[KEY_DOWN] = false; break; case GLUT_KEY_LEFT: m_bSpecialPressed[KEY_LEFT] = false; break; case GLUT_KEY_RIGHT: m_bSpecialPressed[KEY_RIGHT] = false; break; } }