vrshoot
annotate src/input.cc @ 2:334d17aed7de
visual studio project files
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 02 Feb 2014 18:36:38 +0200 |
parents | |
children |
rev | line source |
---|---|
nuclear@0 | 1 #include <vector> |
nuclear@0 | 2 #include <bitset> |
nuclear@0 | 3 #include "input.h" |
nuclear@0 | 4 #include "logger.h" |
nuclear@0 | 5 |
nuclear@0 | 6 #define NUM_KEYS 65536 |
nuclear@0 | 7 static std::bitset<NUM_KEYS> keystate; |
nuclear@0 | 8 |
nuclear@0 | 9 void set_key_state(int key, bool state) |
nuclear@0 | 10 { |
nuclear@0 | 11 if(key < 0 || key >= NUM_KEYS) { |
nuclear@0 | 12 error_log("set_key_state: key (%d) out of bounds\n", key); |
nuclear@0 | 13 return; |
nuclear@0 | 14 } |
nuclear@0 | 15 keystate[key] = state; |
nuclear@0 | 16 } |
nuclear@0 | 17 |
nuclear@0 | 18 bool get_key_state(int key) |
nuclear@0 | 19 { |
nuclear@0 | 20 if(key < 0 || key >= NUM_KEYS) { |
nuclear@0 | 21 error_log("get_key_state: key (%d) out of bounds\n", key); |
nuclear@0 | 22 return false; |
nuclear@0 | 23 } |
nuclear@0 | 24 return keystate[key]; |
nuclear@0 | 25 } |
nuclear@0 | 26 |
nuclear@0 | 27 struct ButtonState { |
nuclear@0 | 28 bool pressed; |
nuclear@0 | 29 int press_x, press_y; |
nuclear@0 | 30 }; |
nuclear@0 | 31 static std::vector<ButtonState> bnstate(16); |
nuclear@0 | 32 #define MAX_BNSTATE 128 |
nuclear@0 | 33 static int mouse_x, mouse_y; |
nuclear@0 | 34 |
nuclear@0 | 35 void set_mouse_state(int x, int y, int bn, bool state) |
nuclear@0 | 36 { |
nuclear@0 | 37 mouse_x = x; |
nuclear@0 | 38 mouse_y = y; |
nuclear@0 | 39 |
nuclear@0 | 40 if(bn < 0 || bn >= MAX_BNSTATE) { |
nuclear@0 | 41 return; |
nuclear@0 | 42 } |
nuclear@0 | 43 if((size_t)bn >= bnstate.size()) { |
nuclear@0 | 44 bnstate.resize(bn + 1); |
nuclear@0 | 45 } |
nuclear@0 | 46 |
nuclear@0 | 47 bnstate[bn].pressed = state; |
nuclear@0 | 48 if(state) { |
nuclear@0 | 49 bnstate[bn].press_x = x; |
nuclear@0 | 50 bnstate[bn].press_y = y; |
nuclear@0 | 51 } |
nuclear@0 | 52 } |
nuclear@0 | 53 |
nuclear@0 | 54 bool mouse_button(int bn) |
nuclear@0 | 55 { |
nuclear@0 | 56 if(bn < 0 || (size_t)bn >= bnstate.size()) { |
nuclear@0 | 57 return false; |
nuclear@0 | 58 } |
nuclear@0 | 59 return bnstate[bn].pressed; |
nuclear@0 | 60 } |
nuclear@0 | 61 |
nuclear@0 | 62 bool mouse_dragging(int bn) |
nuclear@0 | 63 { |
nuclear@0 | 64 if(bn < 0 || (size_t)bn >= bnstate.size()) { |
nuclear@0 | 65 return false; |
nuclear@0 | 66 } |
nuclear@0 | 67 |
nuclear@0 | 68 return bnstate[bn].pressed && mouse_x != bnstate[bn].press_x && |
nuclear@0 | 69 mouse_y != bnstate[bn].press_y; |
nuclear@0 | 70 } |
nuclear@0 | 71 |
nuclear@0 | 72 bool mouse_drag_origin(int bn, int *xorig, int *yorig) |
nuclear@0 | 73 { |
nuclear@0 | 74 if(!mouse_dragging()) { |
nuclear@0 | 75 // this also handles bound issues, no need to recheck |
nuclear@0 | 76 *xorig = mouse_x; |
nuclear@0 | 77 *yorig = mouse_y; |
nuclear@0 | 78 return false; |
nuclear@0 | 79 } |
nuclear@0 | 80 |
nuclear@0 | 81 *xorig = bnstate[bn].press_x; |
nuclear@0 | 82 *yorig = bnstate[bn].press_y; |
nuclear@0 | 83 return true; |
nuclear@0 | 84 } |