123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- #include "Control.h"
- #include <GLFW/glfw3.h>
- typedef struct Key {
- int key;
- int isDown;
- int downTime;
- } Key;
- static Key keys[GLFW_KEY_LAST + 2];
- typedef struct MouseButton {
- int button;
- int isDown;
- int downTime;
- int fastClick;
- } MouseButton;
- static MouseButton buttons[GLFW_MOUSE_BUTTON_LAST + 1];
- void controlInit() {
- for(int i = 0; i < GLFW_KEY_LAST + 2; i++) {
- keys[i].key = 0;
- keys[i].isDown = 0;
- keys[i].downTime = 0;
- }
- for(int i = 0; i < GLFW_MOUSE_BUTTON_LAST + 1; i++) {
- buttons[i].button = 0;
- buttons[i].isDown = 0;
- buttons[i].downTime = 0;
- buttons[i].fastClick = 0;
- }
- }
- void keyPress(int key) {
- keys[key + 1].isDown = 1;
- }
- void keyRelease(int key) {
- keys[key + 1].isDown = 0;
- keys[key + 1].downTime = 0;
- }
- void controlTick() {
- for(int i = 0; i < GLFW_KEY_LAST + 2; i++) {
- keys[i].downTime += keys[i].isDown;
- }
- for(int i = 0; i < GLFW_MOUSE_BUTTON_LAST + 1; i++) {
- buttons[i].downTime += buttons[i].isDown;
- buttons[i].fastClick--;
- }
- }
- int keyIsDown(int key) {
- return keys[key + 1].isDown;
- }
- int keyIsJustDown(int key) {
- return keys[key + 1].downTime == 1;
- }
- void mousePress(int button) {
- buttons[button].isDown = 1;
- buttons[button].fastClick = 2;
- }
- void mouseRelease(int button) {
- buttons[button].isDown = 0;
- buttons[button].downTime = 0;
- }
- int mouseIsDown(int button) {
- return buttons[button].isDown;
- }
- int mouseIsJustDown(int button) {
- return buttons[button].downTime == 1 || buttons[button].fastClick > 0;
- }
|