CrystalPoint.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "CrystalPoint.h"
  2. #include <GL/freeglut.h>
  3. #include <cmath>
  4. #include <cstring>
  5. #include "WorldHandler.h"
  6. #include "Player.h"
  7. int CrystalPoint::width = 0;
  8. int CrystalPoint::height = 0;
  9. SoundSystem CrystalPoint::sound_system;
  10. void CrystalPoint::init()
  11. {
  12. player = Player::getInstance();
  13. worldhandler = WorldHandler::getInstance();
  14. //cursor = Cursor::getInstance();
  15. lastFrameTime = 0;
  16. glClearColor(0.7f, 0.7f, 1.0f, 1.0f);
  17. mousePosition = Vec2f(width / 2, height / 2);
  18. }
  19. void CrystalPoint::draw()
  20. {
  21. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  22. //Draw world
  23. glEnable(GL_LIGHTING);
  24. glEnable(GL_DEPTH_TEST);
  25. glMatrixMode(GL_PROJECTION);
  26. glLoadIdentity();
  27. gluPerspective(70, width / (float)height, 0.1f, 15000);
  28. glMatrixMode(GL_MODELVIEW);
  29. glLoadIdentity();
  30. worldhandler->draw();
  31. //cursor->draw();
  32. glutSwapBuffers();
  33. }
  34. void CrystalPoint::update()
  35. {
  36. float frameTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
  37. float deltaTime = frameTime - lastFrameTime;
  38. lastFrameTime = frameTime;
  39. if (keyboardState.special[GLUT_KEY_LEFT] && !prevKeyboardState.special[GLUT_KEY_LEFT])
  40. worldhandler->PreviousWorld();
  41. if (keyboardState.special[GLUT_KEY_RIGHT] && !prevKeyboardState.special[GLUT_KEY_RIGHT])
  42. worldhandler->NextWorld();
  43. if (keyboardState.keys[27])
  44. exit(0);
  45. Player* player = Player::getInstance();
  46. player->rotation.y += mouseOffset.x / 10.0f;
  47. player->rotation.x += mouseOffset.y / 10.0f;
  48. if (player->rotation.x > 90)
  49. player->rotation.x = 90;
  50. if (player->rotation.x < -90)
  51. player->rotation.x = -90;
  52. float speed = 10;
  53. Vec3f oldPosition = player->position;
  54. if (keyboardState.keys['a']) player->setPosition(0, deltaTime*speed, false);
  55. if (keyboardState.keys['d']) player->setPosition(180, deltaTime*speed, false);
  56. if (keyboardState.keys['w']) player->setPosition(90, deltaTime*speed, false);
  57. if (keyboardState.keys['s']) player->setPosition(270, deltaTime*speed, false);
  58. if (keyboardState.keys['q']) player->setPosition(1, deltaTime*speed, true);
  59. if (keyboardState.keys['e']) player->setPosition(-1, deltaTime*speed, true);
  60. if (!worldhandler->isPlayerPositionValid())
  61. player->position = oldPosition;
  62. player->position.y = worldhandler->getHeight(player->position.x, player->position.z) + 1.7f;
  63. worldhandler->update(deltaTime);
  64. mousePosition = mousePosition + mouseOffset;
  65. //cursor->update(mousePosition);
  66. mouseOffset = Vec2f(0, 0);
  67. prevKeyboardState = keyboardState;
  68. glutPostRedisplay();
  69. sound_system.SetListener(player->position, Vec3f(), Vec3f());
  70. }
  71. KeyboardState::KeyboardState()
  72. {
  73. memset(keys, 0, sizeof(keys));
  74. memset(special, 0, sizeof(special));
  75. }