/* Copyright (c) 2020 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "gamemath/MoveInput.h" #include bool MoveInput_start(MoveInputState * ioState, MoveInput_axis axis, MoveInput_direction direction) { assert(axis < MOVE_AXIS_COUNT_MAX); assert(direction == MOVE_DIRECTION_NEGATIVE || direction == MOVE_DIRECTION_POSITIVE); MoveInputState state = *ioState, lastState = state; state |= (direction == MOVE_DIRECTION_NEGATIVE ? 1 : 2) << axis * MOVE_INPUT_BITS_PER_AXIS; if (direction == MOVE_DIRECTION_NEGATIVE) { state &= ~(4 << axis * MOVE_INPUT_BITS_PER_AXIS); } else { state |= 4 << axis * MOVE_INPUT_BITS_PER_AXIS; } *ioState = state; return state != lastState; } bool MoveInput_stop(MoveInputState * ioState, MoveInput_axis axis, MoveInput_direction direction) { assert(axis < MOVE_AXIS_COUNT_MAX); assert(direction == MOVE_DIRECTION_NEGATIVE || direction == MOVE_DIRECTION_POSITIVE); MoveInputState state = *ioState, lastState = state; state &= ~((direction == MOVE_DIRECTION_NEGATIVE ? 1 : 2) << axis * MOVE_INPUT_BITS_PER_AXIS); *ioState = state; return state != lastState; } void MoveInput_clear(MoveInputState * ioState, MoveInput_axis axis) { assert(axis < MOVE_AXIS_COUNT_MAX); *ioState &= ~(3 << axis * MOVE_INPUT_BITS_PER_AXIS); } bool MoveInput_isInput(MoveInputState state, MoveInput_axis axis, MoveInput_direction direction) { assert(axis < MOVE_AXIS_COUNT_MAX); assert(direction == MOVE_DIRECTION_NEGATIVE || direction == MOVE_DIRECTION_POSITIVE); return (state >> axis * MOVE_INPUT_BITS_PER_AXIS) & (direction == MOVE_DIRECTION_NEGATIVE ? 1 : 2); } MoveInput_direction MoveInput_combinedDirection(MoveInputState state, MoveInput_axis axis) { assert(axis < MOVE_AXIS_COUNT_MAX); bool negative = state & 1 << axis * MOVE_INPUT_BITS_PER_AXIS; bool positive = state & 2 << axis * MOVE_INPUT_BITS_PER_AXIS; if (negative == positive) { return MOVE_DIRECTION_NEUTRAL; } return negative ? MOVE_DIRECTION_NEGATIVE : MOVE_DIRECTION_POSITIVE; } MoveInput_direction MoveInput_mostRecentDirection(MoveInputState state, MoveInput_axis axis) { assert(axis < MOVE_AXIS_COUNT_MAX); bool negative = state & 1 << axis * MOVE_INPUT_BITS_PER_AXIS; bool positive = state & 2 << axis * MOVE_INPUT_BITS_PER_AXIS; if (!negative && !positive) { return MOVE_DIRECTION_NEUTRAL; } if (!positive) { return MOVE_DIRECTION_NEGATIVE; } if (!negative) { return MOVE_DIRECTION_POSITIVE; } return state & 4 << axis * MOVE_INPUT_BITS_PER_AXIS ? MOVE_DIRECTION_POSITIVE : MOVE_DIRECTION_NEGATIVE; }