JancoProject.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "JancoProject.h"
  2. #include <GL/freeglut.h>
  3. #include <cstring>
  4. int CrystalPoint::width = 0;
  5. int CrystalPoint::height = 0;
  6. bool state = false;
  7. void CrystalPoint::init()
  8. {
  9. player = Player::getInstance();
  10. world = World::getInstance();
  11. lastFrameTime = 0;
  12. state = true;
  13. glClearColor(0.7f, 0.7f, 1.0f, 1.0f);
  14. }
  15. void CrystalPoint::draw()
  16. {
  17. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  18. //Draw world
  19. glEnable(GL_DEPTH_TEST);
  20. glMatrixMode(GL_PROJECTION);
  21. glLoadIdentity();
  22. gluPerspective(70, width / (float)height, 0.1f, 15000);
  23. glMatrixMode(GL_MODELVIEW);
  24. glLoadIdentity();
  25. world->draw();
  26. glutSwapBuffers();
  27. }
  28. void CrystalPoint::update()
  29. {
  30. float frameTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
  31. float deltaTime = frameTime - lastFrameTime;
  32. lastFrameTime = frameTime;
  33. if (keyboardState.keys[27] && !prevKeyboardState.keys[27])
  34. state = !state;
  35. Player* player = Player::getInstance();
  36. if (keyboardState.keys[27])
  37. exit(0);
  38. player->rotation.y += mouseOffset.x / 10.0f;
  39. player->rotation.x += mouseOffset.y / 10.0f;
  40. float speed = 30;
  41. Vec3f oldPosition = player->position;
  42. if (keyboardState.keys['a']) player->setPosition(0, deltaTime*speed, false);
  43. if (keyboardState.keys['d']) player->setPosition(180, deltaTime*speed, false);
  44. if (keyboardState.keys['w']) player->setPosition(90, deltaTime*speed, false);
  45. if (keyboardState.keys['s']) player->setPosition(270, deltaTime*speed, false);
  46. if (keyboardState.keys['q']) player->setPosition(1, deltaTime*speed, true);
  47. if (keyboardState.keys['e']) player->setPosition(-1, deltaTime*speed, true);
  48. if (keyboardState.keys['z']) world->objects[1].scale+=1*deltaTime;
  49. if (keyboardState.keys['x']) world->objects[1].scale-=1*deltaTime;
  50. if (player->rotation.x > 90)
  51. player->rotation.x = 90;
  52. if (player->rotation.x < -90)
  53. player->rotation.x = -90;
  54. world->update(deltaTime);
  55. mouseOffset = Vec2f(0, 0);
  56. prevKeyboardState = keyboardState;
  57. glutPostRedisplay();
  58. }
  59. KeyboardState::KeyboardState()
  60. {
  61. memset(keys, 0, sizeof(keys));
  62. memset(special, 0, sizeof(special));
  63. }