Cursor.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "Cursor.h"
  2. #include <GL/freeglut.h>
  3. #include <cmath>
  4. #include "CrystalPoint.h"
  5. Cursor* Cursor::instance = NULL;
  6. Cursor::Cursor()
  7. {
  8. enabled = false;
  9. mousePosition = Vec2f(CrystalPoint::width / 2, CrystalPoint::height / 2);
  10. clicked = false;
  11. }
  12. Cursor::~Cursor()
  13. {
  14. }
  15. Cursor* Cursor::getInstance(void)
  16. {
  17. if (instance == nullptr)
  18. instance = new Cursor();
  19. return instance;
  20. }
  21. void Cursor::enable(bool enable)
  22. {
  23. enabled = enable;
  24. }
  25. bool Cursor::isEnabled(void)
  26. {
  27. return enabled;
  28. }
  29. void Cursor::draw(void)
  30. {
  31. //Draw Cursor
  32. glMatrixMode(GL_PROJECTION);
  33. glLoadIdentity();
  34. glOrtho(0, CrystalPoint::width, CrystalPoint::height, 0, -10, 10);
  35. glMatrixMode(GL_MODELVIEW);
  36. glLoadIdentity();
  37. glDisable(GL_LIGHTING);
  38. glDisable(GL_DEPTH_TEST);
  39. glColor4f(1, cos(glutGet(GLUT_ELAPSED_TIME) / 1000.0f), sin(glutGet(GLUT_ELAPSED_TIME) / 1000.0f), 1);
  40. glBegin(GL_TRIANGLES);
  41. glVertex2f(mousePosition.x, mousePosition.y);
  42. glVertex2f(mousePosition.x + 15, mousePosition.y + 15);
  43. glVertex2f(mousePosition.x + 5, mousePosition.y + 20);
  44. glEnd();
  45. }
  46. void Cursor::update(Vec2f newPosition)
  47. {
  48. if (newPosition.x < 0)
  49. newPosition.x = 0;
  50. if (newPosition.y < 0)
  51. newPosition.y = 0;
  52. if (newPosition.x > CrystalPoint::width)
  53. newPosition.x = CrystalPoint::width;
  54. if (newPosition.y > CrystalPoint::height)
  55. newPosition.y = CrystalPoint::height;
  56. mousePosition = newPosition;
  57. if (clicked)
  58. clicked = !clicked;
  59. if (state != prev)
  60. if(state == GLUT_UP)
  61. clicked = true;
  62. prev = state;
  63. }