Tiny refactor
This commit is contained in:
@@ -167,14 +167,14 @@ Input_State :: struct {
|
||||
}
|
||||
|
||||
init_input :: () {
|
||||
input = .{};
|
||||
input.has_char = false;
|
||||
engine.input = .{};
|
||||
engine.input.has_char = false;
|
||||
|
||||
init_sdl_input();
|
||||
}
|
||||
|
||||
update_key_state :: (key: Key_Code, down: bool) {
|
||||
using input;
|
||||
using engine.input;
|
||||
flags := keys[key];
|
||||
|
||||
if down {
|
||||
@@ -187,7 +187,7 @@ update_key_state :: (key: Key_Code, down: bool) {
|
||||
|
||||
keys[key] = flags;
|
||||
|
||||
input.last_touched = .KEYBOARD_AND_MOUSE;
|
||||
engine.input.last_touched = .KEYBOARD_AND_MOUSE;
|
||||
}
|
||||
|
||||
update_gamepad_state :: (gamepad: *Gamepad, button: Gamepad_Button, down: bool) {
|
||||
@@ -196,7 +196,7 @@ update_gamepad_state :: (gamepad: *Gamepad, button: Gamepad_Button, down: bool)
|
||||
if down {
|
||||
if !(flags & Key_Flags.DOWN) {
|
||||
flags = Key_Flags.DOWN | Key_Flags.START;
|
||||
input.last_touched = .GAMEPAD;
|
||||
engine.input.last_touched = .GAMEPAD;
|
||||
}
|
||||
} else if flags & Key_Flags.DOWN {
|
||||
flags = Key_Flags.END;
|
||||
@@ -207,11 +207,11 @@ update_gamepad_state :: (gamepad: *Gamepad, button: Gamepad_Button, down: bool)
|
||||
}
|
||||
|
||||
update_input :: () {
|
||||
input.mouse.delta_x = 0.0;
|
||||
input.mouse.delta_y = 0.0;
|
||||
input.mouse.wheel = 0.0;
|
||||
input.has_char = false;
|
||||
remove_all_temp_key_flags(*input);
|
||||
engine.input.mouse.delta_x = 0.0;
|
||||
engine.input.mouse.delta_y = 0.0;
|
||||
engine.input.mouse.wheel = 0.0;
|
||||
engine.input.has_char = false;
|
||||
remove_all_temp_key_flags(*engine.input);
|
||||
|
||||
update_sdl_input();
|
||||
|
||||
@@ -245,11 +245,11 @@ eat_mouse_input :: (using input_state: *Input_State) {
|
||||
}
|
||||
|
||||
eat_key :: (key: Key_Code) {
|
||||
input.keys[key] |= Key_Flags.EATEN;
|
||||
engine.input.keys[key] |= Key_Flags.EATEN;
|
||||
}
|
||||
|
||||
action_down :: (action: Action) -> bool {
|
||||
mapping := input.action_mappings[action];
|
||||
mapping := engine.input.action_mappings[action];
|
||||
for 0..mapping.key_mapping_count-1 {
|
||||
if key_down(mapping.keys[it]) {
|
||||
return true;
|
||||
@@ -266,7 +266,7 @@ action_down :: (action: Action) -> bool {
|
||||
}
|
||||
|
||||
action_pressed :: (action: Action) -> bool {
|
||||
mapping := input.action_mappings[action];
|
||||
mapping := engine.input.action_mappings[action];
|
||||
for 0..mapping.key_mapping_count-1 {
|
||||
if key_pressed(mapping.keys[it]) {
|
||||
return true;
|
||||
@@ -283,7 +283,7 @@ action_pressed :: (action: Action) -> bool {
|
||||
}
|
||||
|
||||
action_up :: (action: Action) -> bool {
|
||||
mapping := input.action_mappings[action];
|
||||
mapping := engine.input.action_mappings[action];
|
||||
for 0..mapping.key_mapping_count-1 {
|
||||
if key_up(mapping.keys[it]) {
|
||||
return true;
|
||||
@@ -300,11 +300,11 @@ action_up :: (action: Action) -> bool {
|
||||
}
|
||||
|
||||
get_mouse_delta_x :: inline () -> float {
|
||||
return input.mouse.delta_x;
|
||||
return engine.input.mouse.delta_x;
|
||||
}
|
||||
|
||||
get_mouse_delta_y :: inline () -> float {
|
||||
return input.mouse.delta_y;
|
||||
return engine.input.mouse.delta_y;
|
||||
}
|
||||
|
||||
get_mouse_delta :: () -> float, float {
|
||||
@@ -312,11 +312,11 @@ get_mouse_delta :: () -> float, float {
|
||||
}
|
||||
|
||||
get_mouse_wheel_input :: () -> float {
|
||||
return input.mouse.wheel;
|
||||
return engine.input.mouse.wheel;
|
||||
}
|
||||
|
||||
get_horizontal_axis :: () -> float {
|
||||
if #complete input.last_touched == {
|
||||
if #complete engine.input.last_touched == {
|
||||
case .KEYBOARD_AND_MOUSE;
|
||||
if key_pressed(.A) || key_pressed(.LEFT) return -1.0;
|
||||
else if key_pressed(.D) || key_pressed(.RIGHT) return 1.0;
|
||||
@@ -324,12 +324,12 @@ get_horizontal_axis :: () -> float {
|
||||
case .GAMEPAD;
|
||||
if gamepad_pressed(0, .DPAD_LEFT) return -1.0;
|
||||
else if gamepad_pressed(0, .DPAD_RIGHT) return 1.0;
|
||||
return input.gamepads[0].left_stick_x;
|
||||
return engine.input.gamepads[0].left_stick_x;
|
||||
}
|
||||
}
|
||||
|
||||
get_vertical_axis :: () -> float {
|
||||
if #complete input.last_touched == {
|
||||
if #complete engine.input.last_touched == {
|
||||
case .KEYBOARD_AND_MOUSE;
|
||||
if key_pressed(.S) || key_pressed(.DOWN) return -1.0;
|
||||
else if key_pressed(.W) || key_pressed(.UP) return 1.0;
|
||||
@@ -337,51 +337,51 @@ get_vertical_axis :: () -> float {
|
||||
case .GAMEPAD;
|
||||
if gamepad_pressed(0, .DPAD_DOWN) return -1.0;
|
||||
else if gamepad_pressed(0, .DPAD_UP) return 1.0;
|
||||
return -input.gamepads[0].left_stick_y;
|
||||
return -engine.input.gamepads[0].left_stick_y;
|
||||
}
|
||||
}
|
||||
|
||||
get_right_horizontal_axis :: () -> float {
|
||||
if #complete input.last_touched == {
|
||||
if #complete engine.input.last_touched == {
|
||||
case .KEYBOARD_AND_MOUSE;
|
||||
return input.mouse.delta_x;
|
||||
return engine.input.mouse.delta_x;
|
||||
case .GAMEPAD;
|
||||
return input.gamepads[0].right_stick_x;
|
||||
return engine.input.gamepads[0].right_stick_x;
|
||||
}
|
||||
}
|
||||
|
||||
get_right_vertical_axis :: () -> float {
|
||||
if #complete input.last_touched == {
|
||||
if #complete engine.input.last_touched == {
|
||||
case .KEYBOARD_AND_MOUSE;
|
||||
return input.mouse.delta_y;
|
||||
return engine.input.mouse.delta_y;
|
||||
case .GAMEPAD;
|
||||
return -input.gamepads[0].right_stick_y;
|
||||
return -engine.input.gamepads[0].right_stick_y;
|
||||
}
|
||||
}
|
||||
|
||||
key_down :: (key: Key_Code) -> bool {
|
||||
flags := input.keys[key];
|
||||
flags := engine.input.keys[key];
|
||||
return flags & .START && !(flags & .EATEN);
|
||||
}
|
||||
|
||||
key_pressed :: (key: Key_Code) -> bool {
|
||||
flags := input.keys[key];
|
||||
flags := engine.input.keys[key];
|
||||
return flags & .DOWN && !(flags & .EATEN);
|
||||
}
|
||||
|
||||
key_up :: (key: Key_Code) -> bool {
|
||||
flags := input.keys[key];
|
||||
flags := engine.input.keys[key];
|
||||
return flags & .END && !(flags & .EATEN);
|
||||
}
|
||||
|
||||
gamepad_down :: (index: s32, button: Gamepad_Button) -> bool {
|
||||
return cast(bool)(input.gamepads[index].buttons[button] & .START);
|
||||
return cast(bool)(engine.input.gamepads[index].buttons[button] & .START);
|
||||
}
|
||||
|
||||
gamepad_pressed :: (index: s32, button: Gamepad_Button) -> bool {
|
||||
return cast(bool)(input.gamepads[index].buttons[button] & .DOWN);
|
||||
return cast(bool)(engine.input.gamepads[index].buttons[button] & .DOWN);
|
||||
}
|
||||
|
||||
gamepad_up :: (index: s32, button: Gamepad_Button) -> bool {
|
||||
return cast(bool)(input.gamepads[index].buttons[button] & .END);
|
||||
return cast(bool)(engine.input.gamepads[index].buttons[button] & .END);
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ update_sdl_input :: () {
|
||||
while SDL_PollEvent(*event) {
|
||||
if event.type == {
|
||||
case SDL_QUIT; {
|
||||
input.exit = true;
|
||||
engine.input.exit = true;
|
||||
}
|
||||
case SDL_KEYDOWN; {
|
||||
keycode, success := table_find(*key_mappings, event.key.keysym.sym);
|
||||
@@ -97,17 +97,17 @@ update_sdl_input :: () {
|
||||
}
|
||||
}
|
||||
case SDL_TEXTINPUT; {
|
||||
input.current_char = event.text.text[0];
|
||||
input.has_char = true;
|
||||
engine.input.current_char = event.text.text[0];
|
||||
engine.input.has_char = true;
|
||||
}
|
||||
case .SDL_MOUSEMOTION; {
|
||||
input.mouse.delta_x += xx event.motion.xrel;
|
||||
input.mouse.delta_y += xx event.motion.yrel;
|
||||
input.mouse.x = xx event.motion.x;
|
||||
input.mouse.y = xx event.motion.y;
|
||||
engine.input.mouse.delta_x += xx event.motion.xrel;
|
||||
engine.input.mouse.delta_y += xx event.motion.yrel;
|
||||
engine.input.mouse.x = xx event.motion.x;
|
||||
engine.input.mouse.y = xx event.motion.y;
|
||||
}
|
||||
case .SDL_MOUSEWHEEL; {
|
||||
input.mouse.wheel += event.wheel.y;
|
||||
engine.input.mouse.wheel += event.wheel.y;
|
||||
}
|
||||
case .SDL_CONTROLLERAXISMOTION; {
|
||||
sdl_gamepad_axis_update(event.caxis);
|
||||
@@ -143,27 +143,27 @@ update_sdl_input :: () {
|
||||
|
||||
//mouse_x, mouse_y : s32;
|
||||
//SDL_GetRelativeMouseState(*mouse_x, *mouse_y);
|
||||
//input.mouse.delta_x = xx mouse_x;
|
||||
//input.mouse.delta_y = xx mouse_y;
|
||||
//engine.input.mouse.delta_x = xx mouse_x;
|
||||
//engine.input.mouse.delta_y = xx mouse_y;
|
||||
}
|
||||
|
||||
sdl_connect_gamepad :: (input : *Input_State, id : SDL_JoystickID) {
|
||||
if !SDL_IsGameController(id) {
|
||||
return;
|
||||
} else if input.num_gamepads >= MAX_GAMEPADS {
|
||||
} else if engine.input.num_gamepads >= MAX_GAMEPADS {
|
||||
return;
|
||||
}
|
||||
|
||||
// @Incomplete: Is this necessary?
|
||||
//if input.gamepads[id].controller != null {
|
||||
//if engine.input.gamepads[id].controller != null {
|
||||
// print("Already connected gamepad with id %\n", id);
|
||||
// return;
|
||||
//}
|
||||
|
||||
gamepad : Gamepad;
|
||||
controller := xx SDL_GameControllerOpen(id);
|
||||
input.gamepads[id] = gamepad;
|
||||
input.num_gamepads += 1;
|
||||
engine.input.gamepads[id] = gamepad;
|
||||
engine.input.num_gamepads += 1;
|
||||
}
|
||||
|
||||
sdl_disconnect_gamepad :: (input : *Input_State, id : SDL_JoystickID) {
|
||||
@@ -171,18 +171,18 @@ update_sdl_input :: () {
|
||||
return;
|
||||
}
|
||||
|
||||
gamepad := input.gamepads[id];
|
||||
gamepad := engine.input.gamepads[id];
|
||||
//if !gamepad.controller {
|
||||
// return;
|
||||
//}
|
||||
|
||||
//input.gamepads[id].controller = null;
|
||||
//engine.input.gamepads[id].controller = null;
|
||||
}
|
||||
|
||||
update_gamepad_input :: () {
|
||||
// Deadzone checks
|
||||
for 0..input.num_gamepads-1 {
|
||||
gamepad := *input.gamepads[it];
|
||||
for 0..engine.input.num_gamepads-1 {
|
||||
gamepad := *engine.input.gamepads[it];
|
||||
|
||||
if abs(gamepad.left_stick_x) + abs(gamepad.left_stick_y) < LEFT_STICK_DEADZONE {
|
||||
gamepad.left_stick_x = 0.0;
|
||||
@@ -197,10 +197,10 @@ update_gamepad_input :: () {
|
||||
}
|
||||
|
||||
sdl_gamepad_axis_update :: (using event : SDL_ControllerAxisEvent) {
|
||||
if which < 0 || which >= input.num_gamepads {
|
||||
if which < 0 || which >= engine.input.num_gamepads {
|
||||
return;
|
||||
}
|
||||
gamepad := *input.gamepads[which];
|
||||
gamepad := *engine.input.gamepads[which];
|
||||
|
||||
if cast(SDL_GameControllerAxis) axis == {
|
||||
case SDL_CONTROLLER_AXIS_LEFTX; {
|
||||
@@ -233,13 +233,13 @@ sdl_gamepad_axis_update :: (using event : SDL_ControllerAxisEvent) {
|
||||
}
|
||||
|
||||
sdl_gamepad_button_update :: (using event : SDL_ControllerButtonEvent) {
|
||||
if which < 0 || which >= input.num_gamepads {
|
||||
if which < 0 || which >= engine.input.num_gamepads {
|
||||
return;
|
||||
}
|
||||
down := state == SDL_PRESSED;
|
||||
gamepad_button := sdl_button_map[button];
|
||||
|
||||
gamepad := *input.gamepads[which];
|
||||
gamepad := *engine.input.gamepads[which];
|
||||
|
||||
update_gamepad_state(gamepad, gamepad_button, down);
|
||||
}
|
||||
@@ -251,10 +251,10 @@ sdl_gamepad_device_event :: (using event : SDL_ControllerDeviceEvent) {
|
||||
|
||||
if type == {
|
||||
case SDL_CONTROLLERDEVICEADDED; {
|
||||
sdl_connect_gamepad(*input, which);
|
||||
sdl_connect_gamepad(*engine.input, which);
|
||||
}
|
||||
case SDL_CONTROLLERDEVICEREMOVED; {
|
||||
sdl_disconnect_gamepad(*input, which);
|
||||
sdl_disconnect_gamepad(*engine.input, which);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user