vrshoot

diff src/input.cc @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/input.cc	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,84 @@
     1.4 +#include <vector>
     1.5 +#include <bitset>
     1.6 +#include "input.h"
     1.7 +#include "logger.h"
     1.8 +
     1.9 +#define NUM_KEYS	65536
    1.10 +static std::bitset<NUM_KEYS> keystate;
    1.11 +
    1.12 +void set_key_state(int key, bool state)
    1.13 +{
    1.14 +	if(key < 0 || key >= NUM_KEYS) {
    1.15 +		error_log("set_key_state: key (%d) out of bounds\n", key);
    1.16 +		return;
    1.17 +	}
    1.18 +	keystate[key] = state;
    1.19 +}
    1.20 +
    1.21 +bool get_key_state(int key)
    1.22 +{
    1.23 +	if(key < 0 || key >= NUM_KEYS) {
    1.24 +		error_log("get_key_state: key (%d) out of bounds\n", key);
    1.25 +		return false;
    1.26 +	}
    1.27 +	return keystate[key];
    1.28 +}
    1.29 +
    1.30 +struct ButtonState {
    1.31 +	bool pressed;
    1.32 +	int press_x, press_y;
    1.33 +};
    1.34 +static std::vector<ButtonState> bnstate(16);
    1.35 +#define MAX_BNSTATE		128
    1.36 +static int mouse_x, mouse_y;
    1.37 +
    1.38 +void set_mouse_state(int x, int y, int bn, bool state)
    1.39 +{
    1.40 +	mouse_x = x;
    1.41 +	mouse_y = y;
    1.42 +
    1.43 +	if(bn < 0 || bn >= MAX_BNSTATE) {
    1.44 +		return;
    1.45 +	}
    1.46 +	if((size_t)bn >= bnstate.size()) {
    1.47 +		bnstate.resize(bn + 1);
    1.48 +	}
    1.49 +
    1.50 +	bnstate[bn].pressed = state;
    1.51 +	if(state) {
    1.52 +		bnstate[bn].press_x = x;
    1.53 +		bnstate[bn].press_y = y;
    1.54 +	}
    1.55 +}
    1.56 +
    1.57 +bool mouse_button(int bn)
    1.58 +{
    1.59 +	if(bn < 0 || (size_t)bn >= bnstate.size()) {
    1.60 +		return false;
    1.61 +	}
    1.62 +	return bnstate[bn].pressed;
    1.63 +}
    1.64 +
    1.65 +bool mouse_dragging(int bn)
    1.66 +{
    1.67 +	if(bn < 0 || (size_t)bn >= bnstate.size()) {
    1.68 +		return false;
    1.69 +	}
    1.70 +
    1.71 +	return bnstate[bn].pressed && mouse_x != bnstate[bn].press_x &&
    1.72 +		mouse_y != bnstate[bn].press_y;
    1.73 +}
    1.74 +
    1.75 +bool mouse_drag_origin(int bn, int *xorig, int *yorig)
    1.76 +{
    1.77 +	if(!mouse_dragging()) {
    1.78 +		// this also handles bound issues, no need to recheck
    1.79 +		*xorig = mouse_x;
    1.80 +		*yorig = mouse_y;
    1.81 +		return false;
    1.82 +	}
    1.83 +
    1.84 +	*xorig = bnstate[bn].press_x;
    1.85 +	*yorig = bnstate[bn].press_y;
    1.86 +	return true;
    1.87 +}