Refactored.
This commit is contained in:
@ -7,114 +7,96 @@
|
||||
|
||||
#include "input.h"
|
||||
|
||||
input_t * inputInit(uint32_t inputBindCount, uint32_t inputSourceCount) {
|
||||
uint32_t i;
|
||||
input_t INPUT_STATE;
|
||||
|
||||
// Create the input manager
|
||||
input_t *input = malloc(sizeof(input_t));
|
||||
if(!input) return NULL;
|
||||
void inputInit() {
|
||||
int32_t i;
|
||||
|
||||
// Setup the bind lists
|
||||
input->inputBindCount = inputBindCount;
|
||||
input->bindMap = malloc(sizeof(list_t) * inputBindCount);
|
||||
for(i = 0; i < inputBindCount; i++) {
|
||||
input->bindMap[i] = listCreate();
|
||||
for(i = 0; i < INPUT_BIND_COUNT; i++) {
|
||||
INPUT_STATE.bindMap[i] = listCreate();
|
||||
}
|
||||
|
||||
// Create the current & previous input states
|
||||
input->current = malloc(sizeof(inputval_t) * inputBindCount);
|
||||
input->previous = malloc(sizeof(inputval_t) * inputBindCount);
|
||||
|
||||
// Create the input actuate times array.
|
||||
input->times = malloc(sizeof(float) * inputBindCount);
|
||||
INPUT_STATE.current = INPUT_STATE.inputsA;
|
||||
INPUT_STATE.previous = INPUT_STATE.inputsB;
|
||||
|
||||
// Create the buffer, zero all the values out.
|
||||
input->buffer = calloc(inputSourceCount, sizeof(inputval_t));
|
||||
|
||||
// Pass off the input manager
|
||||
return input;
|
||||
memset(&INPUT_STATE.buffer, 0, sizeof(inputval_t)*INPUT_SOURCE_COUNT);
|
||||
}
|
||||
|
||||
void inputUpdate(input_t *input) {
|
||||
uint32_t i;
|
||||
void inputUpdate() {
|
||||
int32_t i;
|
||||
listentry_t *item;
|
||||
inputval_t value;
|
||||
|
||||
// Flip the states to save memory.
|
||||
float * currentCurrent = input->current;
|
||||
input->current = input->previous;
|
||||
input->previous = currentCurrent;
|
||||
inputval_t *currentCurrent = INPUT_STATE.current;
|
||||
INPUT_STATE.current = INPUT_STATE.previous;
|
||||
INPUT_STATE.previous = currentCurrent;
|
||||
|
||||
// Now look at each bind...
|
||||
for(i = 0; i < input->inputBindCount; i++) {
|
||||
for(i = 0; i < INPUT_BIND_COUNT; i++) {
|
||||
// Now get the list of input sources bound to this input
|
||||
item = input->bindMap[i]->start;
|
||||
item = INPUT_STATE.bindMap[i]->start;
|
||||
value = 0;
|
||||
|
||||
// For each input source, add the value from the buffer
|
||||
while(item != NULL) {
|
||||
value += input->buffer[(inputsource_t)item->data];
|
||||
value += INPUT_STATE.buffer[(inputsource_t)item->data];
|
||||
item = item->next;
|
||||
}
|
||||
|
||||
// Set to the current state.
|
||||
input->current[i] = value;
|
||||
INPUT_STATE.current[i] = value;
|
||||
}
|
||||
}
|
||||
|
||||
void inputDispose(input_t *input) {
|
||||
uint32_t i;
|
||||
void inputDispose() {
|
||||
int32_t i;
|
||||
|
||||
// Free up the bind lists
|
||||
for(i = 0; i < input->inputBindCount; i++) {
|
||||
listDispose(input->bindMap[i], false);
|
||||
for(i = 0; i < INPUT_BIND_COUNT; i++) {
|
||||
listDispose(INPUT_STATE.bindMap[i], false);
|
||||
}
|
||||
|
||||
free(input->buffer);
|
||||
free(input->times);
|
||||
free(input->previous);
|
||||
free(input->current);
|
||||
free(input->bindMap);
|
||||
free(input);
|
||||
}
|
||||
|
||||
void inputBind(input_t *input, inputbind_t bind, inputsource_t source) {
|
||||
listAdd(input->bindMap[bind], source);
|
||||
void inputBind(inputbind_t bind, inputsource_t source) {
|
||||
listAdd(INPUT_STATE.bindMap[bind], (void *)source);
|
||||
}
|
||||
|
||||
void inputUnbind(input_t *input, inputbind_t bind, inputsource_t source) {
|
||||
listRemove(input->bindMap[bind], source);
|
||||
void inputUnbind(inputbind_t bind, inputsource_t source) {
|
||||
listRemove(INPUT_STATE.bindMap[bind], (void *)source);
|
||||
}
|
||||
|
||||
bool inputIsDown(input_t *input, inputbind_t binding) {
|
||||
return input->current[binding] != 0;
|
||||
bool inputIsDown(inputbind_t binding) {
|
||||
return INPUT_STATE.current[binding] != 0;
|
||||
}
|
||||
|
||||
bool inputIsUp(input_t *input, inputbind_t binding) {
|
||||
return input->current[binding] == 0;
|
||||
bool inputIsUp(inputbind_t binding) {
|
||||
return INPUT_STATE.current[binding] == 0;
|
||||
}
|
||||
|
||||
bool inputIsPressed(input_t *input, inputbind_t binding) {
|
||||
bool inputIsPressed(inputbind_t binding) {
|
||||
return (
|
||||
input->previous[binding] == 0 &&
|
||||
input->current[binding] != 0
|
||||
INPUT_STATE.previous[binding] == 0 &&
|
||||
INPUT_STATE.current[binding] != 0
|
||||
);
|
||||
}
|
||||
|
||||
bool inputIsReleased(input_t *input, inputbind_t binding) {
|
||||
return input->current[binding] == 0 && input->previous[binding] != 0;
|
||||
bool inputIsReleased(inputbind_t binding) {
|
||||
return INPUT_STATE.current[binding]==0 && INPUT_STATE.previous[binding] != 0;
|
||||
}
|
||||
|
||||
inputval_t inputGetAxis(input_t *input, inputbind_t binding) {
|
||||
return input->current[binding];
|
||||
inputval_t inputGetAxis(inputbind_t binding) {
|
||||
return INPUT_STATE.current[binding];
|
||||
}
|
||||
|
||||
float inputGetFullAxis(input_t *input, inputbind_t positive,
|
||||
float inputGetFullAxis(inputbind_t positive,
|
||||
inputbind_t negative
|
||||
) {
|
||||
return input->current[negative] + input->current[positive];
|
||||
return INPUT_STATE.current[negative] + INPUT_STATE.current[positive];
|
||||
}
|
||||
|
||||
float inputGetAccuated(input_t *input, inputbind_t binding) {
|
||||
return input->times[binding];
|
||||
float inputGetAccuated(inputbind_t binding) {
|
||||
return INPUT_STATE.times[binding];
|
||||
}
|
@ -1,158 +1,86 @@
|
||||
// Copyright (c) 2021 Dominic Msters
|
||||
// Copyright (c) 2021 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <malloc.h>
|
||||
#include <memory.h>
|
||||
#include <string.h>
|
||||
#include <dawn/dawn.h>
|
||||
#include "../util/list.h"
|
||||
#include "../platform.h"
|
||||
|
||||
/////////////////////////////////// CONSTANTS //////////////////////////////////
|
||||
#define INPUT_NULL (inputbind_t)0x00
|
||||
|
||||
/////////////////////////////// Type Definitions ///////////////////////////////
|
||||
/**
|
||||
* Input Bind, a specific action bind reference for the game engine to use.
|
||||
* e.g. "Jump" or "Walk Forward".
|
||||
*/
|
||||
typedef uint8_t inputbind_t;
|
||||
|
||||
/**
|
||||
* Input source identifier. It's up to the platform itself to decide what the
|
||||
* hell this number refers to. For most platforms it will be an input, such as a
|
||||
* keyboard scancode or a (pad number * button count) + button.
|
||||
*/
|
||||
typedef uint32_t inputsource_t;
|
||||
|
||||
/**
|
||||
* Value that represents the state of an input. Defined as 0-1 where 0 is set
|
||||
* to be completely off / netural state, and 1 is completely on / full state.
|
||||
*/
|
||||
typedef float inputval_t;
|
||||
|
||||
/**
|
||||
* Structure for the entire input mapping.
|
||||
*/
|
||||
typedef struct {
|
||||
/** How many input bindings are in the input managers' scope */
|
||||
uint32_t inputBindCount;
|
||||
|
||||
/** Float of the input between 0 and 1. For teh current frame */
|
||||
inputval_t *current;
|
||||
|
||||
/** Float of the input between 0 and 1. For the last frame */
|
||||
inputval_t *previous;
|
||||
|
||||
/**
|
||||
* Input buffer array. Keeps track of raw values from the inputs.
|
||||
* The engine will read from the buffer when necessary.
|
||||
*/
|
||||
inputval_t *buffer;
|
||||
|
||||
/** Float of the GameTime that the input was actuated last. */
|
||||
float *times;
|
||||
|
||||
/**
|
||||
* Binding Map, Array of lists where index = binding and entry is a list of
|
||||
* input sources.
|
||||
*/
|
||||
list_t **bindMap;
|
||||
} input_t;
|
||||
|
||||
//////////////////////////////////// Methods ///////////////////////////////////
|
||||
|
||||
/**
|
||||
* Initializes the input manager.
|
||||
*
|
||||
* @param inputCount The input binding counts to allow for.
|
||||
* @param inputSourceCount The input source count to allow for.
|
||||
* @return The input manager
|
||||
*/
|
||||
input_t * inputInit(uint32_t inputBindCount, uint32_t inputSourceCount);
|
||||
void inputInit();
|
||||
|
||||
/**
|
||||
* Tick the input manager.
|
||||
* @param input The input to update.
|
||||
*/
|
||||
void inputUpdate(input_t *input);
|
||||
void inputUpdate();
|
||||
|
||||
/**
|
||||
* Destroy the input manager and cleanup.
|
||||
* @param input The input to destroy.
|
||||
*/
|
||||
void inputDispose(input_t *input);
|
||||
void inputDispose();
|
||||
|
||||
/**
|
||||
* Binds the given input binding to the input source. Essentially allowing any
|
||||
* time we fetch the state of bind, we will read the value from source.
|
||||
*
|
||||
* @param input The input manager to bind for.
|
||||
* @param bind The binding to bind against.
|
||||
* @param source The source that is being bound.
|
||||
*/
|
||||
void inputBind(input_t *input, inputbind_t bind, inputsource_t source);
|
||||
void inputBind(inputbind_t bind, inputsource_t source);
|
||||
|
||||
/**
|
||||
* Unbind a previously bound input source from a binding. This method is costly.
|
||||
*
|
||||
* @param input The input manager to bind for.
|
||||
* @param bind The binding to unbind from.
|
||||
* @param source The source that is being unbound.
|
||||
*/
|
||||
void inputUnbind(input_t *input, inputbind_t bind, inputsource_t source);
|
||||
void inputUnbind(inputbind_t bind, inputsource_t source);
|
||||
|
||||
/**
|
||||
* Is the current input "down", being pressed, being moved, not in a state
|
||||
* of rest.
|
||||
*
|
||||
* @param state The input state to check against.
|
||||
* @param binding The previously bound input binding.
|
||||
* @return True if the input vector is non-zero.
|
||||
*/
|
||||
bool inputIsDown(input_t *state, inputbind_t binding);
|
||||
bool inputIsDown(inputbind_t binding);
|
||||
|
||||
/**
|
||||
* Is the current input "up", in a state of rest, not being actioned, moved.
|
||||
*
|
||||
* @param state The input state to check against.
|
||||
* @param binding The previously bound input binding.
|
||||
* @return True if input vector is zero
|
||||
*/
|
||||
bool inputIsUp(input_t *state, inputbind_t binding);
|
||||
bool inputIsUp(inputbind_t binding);
|
||||
|
||||
/**
|
||||
* Returns true on the first tick that an input was actioned/downed.
|
||||
*
|
||||
* @param input The input manager.
|
||||
* @param binding The previously bound input binding.
|
||||
* @return True if the input vector was non-zeroed this tick but not last.
|
||||
*/
|
||||
bool inputIsPressed(input_t *input, inputbind_t binding);
|
||||
bool inputIsPressed(inputbind_t binding);
|
||||
|
||||
/**
|
||||
* Returns true on the first tick that an input was released/upped.
|
||||
*
|
||||
* @param input The input manager.
|
||||
* @param binding The previously bound input binding.
|
||||
* @return True if the input vector was zeroed this tick but not last.
|
||||
*/
|
||||
bool inputIsReleased(input_t *input, inputbind_t binding);
|
||||
bool inputIsReleased(inputbind_t binding);
|
||||
|
||||
/**
|
||||
* Returns the raw input value as a float between 0 and 1. For digital (buttons)
|
||||
* this will typicall be 0 or 1 only. Other analogue inputs will have anywhere
|
||||
* within the range.
|
||||
*
|
||||
* @param input The input manager to check against.
|
||||
* @param binding The previously bound input binding.
|
||||
* @return Input state of the axis.
|
||||
*/
|
||||
inputval_t inputGetAxis(input_t *input, inputbind_t binding);
|
||||
inputval_t inputGetAxis(inputbind_t binding);
|
||||
|
||||
/**
|
||||
* Returns a raw input value between -1 and 1 between two axis. This would be
|
||||
@ -160,21 +88,17 @@ inputval_t inputGetAxis(input_t *input, inputbind_t binding);
|
||||
* for a positive input and another for a negative input, typically a game
|
||||
* controller's analogue sticks.
|
||||
*
|
||||
* @param input The input manager to check against.
|
||||
* @param postitive The positive axis binding.
|
||||
* @param negative The negative axis binding.
|
||||
* @return A float between -1 and 1 representing the result of both.
|
||||
*/
|
||||
float inputGetFullAxis(input_t *input, inputbind_t positive,
|
||||
inputbind_t negative
|
||||
);
|
||||
float inputGetFullAxis(inputbind_t positive, inputbind_t negative);
|
||||
|
||||
/**
|
||||
* Returns the time that an input was actuated at. Actuate would count as a
|
||||
* non-zero input for analogue inputs.
|
||||
*
|
||||
* @param input The input manager to check against.
|
||||
* @param binding The previously bound input binding.
|
||||
* @return Game Engine time that an input was non-zeroed
|
||||
*/
|
||||
float inputGetAccuated(input_t *input, inputbind_t binding);
|
||||
float inputGetAccuated(inputbind_t binding);
|
Reference in New Issue
Block a user