CrystalJohan.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "CrystalJohan.h"
  2. #include <GL/freeglut.h>
  3. #include "World.h"
  4. void CrystalJohan::init()
  5. {
  6. world = new World();
  7. lastFrameTime = 0;
  8. glClearColor(0.7, 0.7, 1.0, 1.0);
  9. glEnable(GL_DEPTH_TEST);
  10. glEnable(GL_LIGHTING);
  11. glEnable(GL_LIGHT0);
  12. mousePosition = Vec2f(width / 2, height / 2);
  13. }
  14. void CrystalJohan::draw()
  15. {
  16. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  17. //Draw world
  18. glEnable(GL_LIGHTING);
  19. glEnable(GL_DEPTH_TEST);
  20. glMatrixMode(GL_PROJECTION);
  21. glLoadIdentity();
  22. gluPerspective(70, width / (float)height, 0.1f, 100);
  23. glMatrixMode(GL_MODELVIEW);
  24. glLoadIdentity();
  25. world->draw();
  26. //Draw Cursor
  27. glMatrixMode(GL_PROJECTION);
  28. glLoadIdentity();
  29. glOrtho(0,width, height,0,-10,10);
  30. glMatrixMode(GL_MODELVIEW);
  31. glLoadIdentity();
  32. /*glDisable(GL_LIGHTING);
  33. glDisable(GL_DEPTH_TEST);
  34. glColor4f(1, cos(glutGet(GLUT_ELAPSED_TIME) / 1000.0f), sin(glutGet(GLUT_ELAPSED_TIME) / 1000.0f), 1);
  35. glBegin(GL_TRIANGLES);
  36. glVertex2f(mousePosition.x, mousePosition.y);
  37. glVertex2f(mousePosition.x+15, mousePosition.y+15);
  38. glVertex2f(mousePosition.x+5, mousePosition.y+20);
  39. glEnd();*/
  40. glutSwapBuffers();
  41. }
  42. void CrystalJohan::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. if (keyboardState.keys[27])
  49. exit(0);
  50. world->player.rotation.y += mouseOffset.x / 10.0f;
  51. world->player.rotation.x += mouseOffset.y / 10.0f;
  52. if (world->player.rotation.x > 90)
  53. world->player.rotation.x = 90;
  54. if (world->player.rotation.x < -90)
  55. world->player.rotation.x = -90;
  56. Vec3f oldPosition = world->player.position;
  57. if (keyboardState.keys['a']) world->player.setPosition(0, deltaTime, false);
  58. if (keyboardState.keys['d']) world->player.setPosition(180, deltaTime, false);
  59. if (keyboardState.keys['w']) world->player.setPosition(90, deltaTime, false);
  60. if (keyboardState.keys['s']) world->player.setPosition(270, deltaTime, false);
  61. if (keyboardState.keys['q']) world->player.setPosition(1, deltaTime, true);
  62. if (keyboardState.keys['e']) world->player.setPosition(-1, deltaTime, true);
  63. if (!world->isPlayerPositionValid())
  64. world->player.position = oldPosition;
  65. world->update(deltaTime);
  66. mousePosition = mousePosition + mouseOffset;
  67. mouseOffset = Vec2f(0, 0);
  68. prevKeyboardState = keyboardState;
  69. glutPostRedisplay();
  70. }
  71. KeyboardState::KeyboardState()
  72. {
  73. memset(keys, 0, sizeof(keys));
  74. memset(special, 0, sizeof(special));
  75. }