CrystalPoint.cpp 2.9 KB

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