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