/* * TinyBlocks - tb_input.c * Estado de entrada em coordenadas do palco (ver tb_input.h). */ #define _POSIX_C_SOURCE 200809L #include #include #include "tinyblocks/tb_input.h" #include "tinyblocks/tb_stage.h" #define HALF_W 240.f #define HALF_H 180.f #define RAD2DEG 57.29577951308232f typedef struct { float mx, my; /* palco, sem prender */ bool over, left, right; float wheel; unsigned char keys[TB_INPUT_MAX_KEYS]; int last_key; int touch_n; float tx[TB_INPUT_MAX_TOUCHES], ty[TB_INPUT_MAX_TOUCHES]; bool has_accel, has_gyro; float accel[3], gyro[3]; double timer_t0; bool timer_started; } InputState; static InputState s_in; static double now_s(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (double)ts.tv_sec + (double)ts.tv_nsec * 1e-9; } static float clampf(float v, float lo, float hi) { return v < lo ? lo : v > hi ? hi : v; } /* -- Alimentacao --- */ void tb_input_begin_frame(void) { s_in.wheel = 0.f; } void tb_input_set_mouse(float vx, float vy, bool left, bool right) { s_in.over = tb_stage_view_to_stage(vx, vy, &s_in.mx, &s_in.my); s_in.left = left; s_in.right = right; } void tb_input_add_wheel(float dy) { s_in.wheel += dy; } void tb_input_clear_keys(void) { memset(s_in.keys, 0, sizeof(s_in.keys)); } void tb_input_set_key(int key, bool held) { if (key > 0 && key < TB_INPUT_MAX_KEYS) s_in.keys[key] = held ? 1 : 0; } void tb_input_key_pressed(int key) { if (key > 0 && key < TB_INPUT_MAX_KEYS) s_in.last_key = key; } void tb_input_set_touches(int n, const float* vx, const float* vy) { if (n < 0) n = 0; if (n > TB_INPUT_MAX_TOUCHES) n = TB_INPUT_MAX_TOUCHES; s_in.touch_n = n; for (int i = 0; i < n; i++) tb_stage_view_to_stage(vx[i], vy[i], &s_in.tx[i], &s_in.ty[i]); } void tb_input_set_accel(bool has, float x, float y, float z) { s_in.has_accel = has; s_in.accel[0] = has ? x : 0.f; s_in.accel[1] = has ? y : 0.f; s_in.accel[2] = has ? z : 0.f; } void tb_input_set_gyro(bool has, float x, float y, float z) { s_in.has_gyro = has; s_in.gyro[0] = has ? x : 0.f; s_in.gyro[1] = has ? y : 0.f; s_in.gyro[2] = has ? z : 0.f; } void tb_input_reset(void) { memset(&s_in, 0, sizeof(s_in)); } /* -- Leitura --- */ float tb_input_mouse_x(void) { return clampf(s_in.mx, -HALF_W, HALF_W); } float tb_input_mouse_y(void) { return clampf(s_in.my, -HALF_H, HALF_H); } bool tb_input_mouse_over_stage(void) { return s_in.over; } bool tb_input_mouse_down(void) { return s_in.left; } bool tb_input_mouse_right_down(void) { return s_in.right; } float tb_input_wheel(void) { return s_in.wheel; } bool tb_input_key_held(int key) { return key > 0 && key < TB_INPUT_MAX_KEYS && s_in.keys[key]; } bool tb_input_any_key_held(void) { for (int k = 1; k < TB_INPUT_MAX_KEYS; k++) if (s_in.keys[k]) return true; return false; } int tb_input_last_key(void) { return s_in.last_key; } int tb_input_touch_count(void) { return s_in.touch_n; } float tb_input_touch_x(int i) { return (i >= 0 && i < s_in.touch_n) ? s_in.tx[i] : 0.f; } float tb_input_touch_y(int i) { return (i >= 0 && i < s_in.touch_n) ? s_in.ty[i] : 0.f; } bool tb_input_has_accel(void) { return s_in.has_accel; } float tb_input_accel(int axis) { return (axis >= 0 && axis < 3) ? s_in.accel[axis] : 0.f; } bool tb_input_has_gyro(void) { return s_in.has_gyro; } float tb_input_gyro(int axis) { return (axis >= 0 && axis < 3) ? s_in.gyro[axis] * RAD2DEG : 0.f; } double tb_input_timer(void) { if (!s_in.timer_started) { s_in.timer_t0 = now_s(); s_in.timer_started = true; } return now_s() - s_in.timer_t0; } void tb_input_timer_reset(void) { s_in.timer_t0 = now_s(); s_in.timer_started = true; }