CrystalPoint.cpp 2.7 KB

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