CrystalPoint.cpp 2.5 KB

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