Control.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "Control.h"
  2. #include <GLFW/glfw3.h>
  3. typedef struct Key {
  4. int key;
  5. int isDown;
  6. int downTime;
  7. } Key;
  8. static Key keys[GLFW_KEY_LAST + 2];
  9. typedef struct MouseButton {
  10. int button;
  11. int isDown;
  12. int downTime;
  13. int fastClick;
  14. } MouseButton;
  15. static MouseButton buttons[GLFW_MOUSE_BUTTON_LAST + 1];
  16. void controlInit() {
  17. for(int i = 0; i < GLFW_KEY_LAST + 2; i++) {
  18. keys[i].key = 0;
  19. keys[i].isDown = 0;
  20. keys[i].downTime = 0;
  21. }
  22. for(int i = 0; i < GLFW_MOUSE_BUTTON_LAST + 1; i++) {
  23. buttons[i].button = 0;
  24. buttons[i].isDown = 0;
  25. buttons[i].downTime = 0;
  26. buttons[i].fastClick = 0;
  27. }
  28. }
  29. void keyPress(int key) {
  30. keys[key + 1].isDown = 1;
  31. }
  32. void keyRelease(int key) {
  33. keys[key + 1].isDown = 0;
  34. keys[key + 1].downTime = 0;
  35. }
  36. void controlTick() {
  37. for(int i = 0; i < GLFW_KEY_LAST + 2; i++) {
  38. keys[i].downTime += keys[i].isDown;
  39. }
  40. for(int i = 0; i < GLFW_MOUSE_BUTTON_LAST + 1; i++) {
  41. buttons[i].downTime += buttons[i].isDown;
  42. buttons[i].fastClick--;
  43. }
  44. }
  45. int keyIsDown(int key) {
  46. return keys[key + 1].isDown;
  47. }
  48. int keyIsJustDown(int key) {
  49. return keys[key + 1].downTime == 1;
  50. }
  51. void mousePress(int button) {
  52. buttons[button].isDown = 1;
  53. buttons[button].fastClick = 2;
  54. }
  55. void mouseRelease(int button) {
  56. buttons[button].isDown = 0;
  57. buttons[button].downTime = 0;
  58. }
  59. int mouseIsDown(int button) {
  60. return buttons[button].isDown;
  61. }
  62. int mouseIsJustDown(int button) {
  63. return buttons[button].downTime == 1 || buttons[button].fastClick > 0;
  64. }