CrystalPoint.cpp 2.9 KB

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