Cursor.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. }
  11. Cursor::~Cursor()
  12. {
  13. }
  14. Cursor* Cursor::getInstance(void)
  15. {
  16. if (instance == nullptr)
  17. instance = new Cursor();
  18. return instance;
  19. }
  20. void Cursor::enable(bool enable)
  21. {
  22. enabled = enable;
  23. }
  24. bool Cursor::isEnabled(void)
  25. {
  26. return enabled;
  27. }
  28. void Cursor::draw(void)
  29. {
  30. //Draw Cursor
  31. glMatrixMode(GL_PROJECTION);
  32. glLoadIdentity();
  33. glOrtho(0, CrystalPoint::width, CrystalPoint::height, 0, -10, 10);
  34. glMatrixMode(GL_MODELVIEW);
  35. glLoadIdentity();
  36. glDisable(GL_LIGHTING);
  37. glDisable(GL_DEPTH_TEST);
  38. glColor4f(1, cos(glutGet(GLUT_ELAPSED_TIME) / 1000.0f), sin(glutGet(GLUT_ELAPSED_TIME) / 1000.0f), 1);
  39. glBegin(GL_TRIANGLES);
  40. glVertex2f(mousePosition.x, mousePosition.y);
  41. glVertex2f(mousePosition.x + 15, mousePosition.y + 15);
  42. glVertex2f(mousePosition.x + 5, mousePosition.y + 20);
  43. glEnd();
  44. }
  45. void Cursor::update(Vec2f newPosition)
  46. {
  47. if (newPosition.x < 0)
  48. newPosition.x = 0;
  49. if (newPosition.y < 0)
  50. newPosition.y = 0;
  51. if (newPosition.x > CrystalPoint::width)
  52. newPosition.x = CrystalPoint::width;
  53. if (newPosition.y > CrystalPoint::height)
  54. newPosition.y = CrystalPoint::height;
  55. mousePosition = newPosition;
  56. }