核心热路径(数学/内存/字符串/性能分析)用MASM汇编(SSE/AVX2) 外围系统(窗口/渲染/ECS/音频/UI/脚本)用C语言 含自研编辑器、VisualScript积木编程、中文渲染(glyph_atlas)
138 lines
4.0 KiB
C
138 lines
4.0 KiB
C
#include "input.h"
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
static float normalize_trigger(BYTE value) {
|
|
return value / 255.0f;
|
|
}
|
|
|
|
static float normalize_axis(SHORT value) {
|
|
if (value > XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) {
|
|
return (value - XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) / (32767.0f - XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE);
|
|
} else if (value < -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) {
|
|
return (value + XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) / (32767.0f - XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE);
|
|
}
|
|
return 0.0f;
|
|
}
|
|
|
|
Input* input_create(void) {
|
|
Input* input = (Input*)calloc(1, sizeof(Input));
|
|
input->deadzone = 0.2f;
|
|
return input;
|
|
}
|
|
|
|
void input_destroy(Input* input) {
|
|
free(input);
|
|
}
|
|
|
|
void input_update(Input* input) {
|
|
memcpy(input->key_prev, input->key_states, sizeof(int) * MAX_KEYS);
|
|
memcpy(input->mouse_prev, input->mouse_buttons, sizeof(int) * 3);
|
|
|
|
input->mouse_dx = 0;
|
|
input->mouse_dy = 0;
|
|
input->mouse_scroll = 0;
|
|
|
|
memcpy(&input->gamepad_prev, &input->gamepad_state, sizeof(XINPUT_STATE));
|
|
input->gamepad_connected = (XInputGetState(0, &input->gamepad_state) == ERROR_SUCCESS);
|
|
}
|
|
|
|
void input_process_key(Input* input, int key, int down) {
|
|
if (key >= 0 && key < MAX_KEYS) {
|
|
input->key_states[key] = down;
|
|
}
|
|
}
|
|
|
|
void input_process_mouse_move(Input* input, int x, int y) {
|
|
input->mouse_dx = x - input->mouse_x;
|
|
input->mouse_dy = y - input->mouse_y;
|
|
input->mouse_x = x;
|
|
input->mouse_y = y;
|
|
}
|
|
|
|
void input_process_mouse_button(Input* input, int button, int down) {
|
|
if (button >= 0 && button < 3) {
|
|
input->mouse_buttons[button] = down;
|
|
}
|
|
}
|
|
|
|
void input_process_mouse_wheel(Input* input, int delta) {
|
|
input->mouse_scroll += delta;
|
|
}
|
|
|
|
int input_key_down(Input* input, int key) {
|
|
return (key >= 0 && key < MAX_KEYS) ? input->key_states[key] : 0;
|
|
}
|
|
|
|
int input_key_pressed(Input* input, int key) {
|
|
return (key >= 0 && key < MAX_KEYS) ? (input->key_states[key] && !input->key_prev[key]) : 0;
|
|
}
|
|
|
|
int input_key_released(Input* input, int key) {
|
|
return (key >= 0 && key < MAX_KEYS) ? (!input->key_states[key] && input->key_prev[key]) : 0;
|
|
}
|
|
|
|
int input_mouse_button(Input* input, int button) {
|
|
return (button >= 0 && button < 3) ? input->mouse_buttons[button] : 0;
|
|
}
|
|
|
|
int input_mouse_button_pressed(Input* input, int button) {
|
|
return (button >= 0 && button < 3) ? (input->mouse_buttons[button] && !input->mouse_prev[button]) : 0;
|
|
}
|
|
|
|
void input_get_mouse_pos(Input* input, int* x, int* y) {
|
|
if (x) *x = input->mouse_x;
|
|
if (y) *y = input->mouse_y;
|
|
}
|
|
|
|
void input_get_mouse_delta(Input* input, int* dx, int* dy) {
|
|
if (dx) *dx = input->mouse_dx;
|
|
if (dy) *dy = input->mouse_dy;
|
|
}
|
|
|
|
int input_get_mouse_scroll(Input* input) {
|
|
return input->mouse_scroll;
|
|
}
|
|
|
|
int input_gamepad_connected(Input* input) {
|
|
return input->gamepad_connected;
|
|
}
|
|
|
|
float input_gamepad_axis_lx(Input* input) {
|
|
float v = normalize_axis(input->gamepad_state.Gamepad.sThumbLX);
|
|
return fabsf(v) < input->deadzone ? 0.0f : v;
|
|
}
|
|
|
|
float input_gamepad_axis_ly(Input* input) {
|
|
float v = normalize_axis(input->gamepad_state.Gamepad.sThumbLY);
|
|
return fabsf(v) < input->deadzone ? 0.0f : v;
|
|
}
|
|
|
|
float input_gamepad_axis_rx(Input* input) {
|
|
float v = normalize_axis(input->gamepad_state.Gamepad.sThumbRX);
|
|
return fabsf(v) < input->deadzone ? 0.0f : v;
|
|
}
|
|
|
|
float input_gamepad_axis_ry(Input* input) {
|
|
float v = normalize_axis(input->gamepad_state.Gamepad.sThumbRY);
|
|
return fabsf(v) < input->deadzone ? 0.0f : v;
|
|
}
|
|
|
|
float input_gamepad_trigger_l(Input* input) {
|
|
return normalize_trigger(input->gamepad_state.Gamepad.bLeftTrigger);
|
|
}
|
|
|
|
float input_gamepad_trigger_r(Input* input) {
|
|
return normalize_trigger(input->gamepad_state.Gamepad.bRightTrigger);
|
|
}
|
|
|
|
int input_gamepad_button(Input* input, uint16_t button) {
|
|
return (input->gamepad_state.Gamepad.wButtons & button) != 0;
|
|
}
|
|
|
|
int input_gamepad_button_pressed(Input* input, uint16_t button) {
|
|
return (input->gamepad_state.Gamepad.wButtons & button) != 0 &&
|
|
(input->gamepad_prev.Gamepad.wButtons & button) == 0;
|
|
}
|