Moved some code around and cleaned things a bit.

This commit is contained in:
2021-02-22 08:08:12 +11:00
parent a456eb1008
commit 11ca3d96e3
14 changed files with 405 additions and 77 deletions

View File

@ -7,91 +7,108 @@
#include "input.h"
input_t * inputInit(uint32_t inputBindCount) {
input_t * inputInit(platform_t *platform, uint32_t inputBindCount) {
// Create the input manager
input_t *input = malloc(sizeof(input_t));
if(!input) return NULL;
// Setup the binds
input->inputBindCount = inputBindCount;
input->inputsCurrent = malloc(sizeof(float) * inputBindCount);
if(input->inputsCurrent == NULL) {
input->binds = malloc(sizeof(inputbind_t) * inputBindCount);
if(input->binds == NULL) {
free(input);
return NULL;
}
input->inputsPrevious = malloc(sizeof(float) * inputBindCount);
if(input->inputsPrevious == NULL) {
free(input->inputsCurrent);
// Create the current input states
input->current = malloc(sizeof(float) * inputBindCount);
if(input->current == NULL) {
free(input->binds);
free(input);
return NULL;
}
input->inputTime = malloc(sizeof(float) * inputBindCount);
if(input->inputTime == NULL) {
free(input->inputsPrevious);
free(input->inputsCurrent);
// Create the previous input states.
input->previous = malloc(sizeof(float) * inputBindCount);
if(input->previous == NULL) {
free(input->current);
free(input->binds);
free(input);
return NULL;
}
// Create the input actuate times array.
input->times = malloc(sizeof(float) * inputBindCount);
if(input->times == NULL) {
free(input->previous);
free(input->current);
free(input->binds);
free(input);
return NULL;
}
// Pass off the input manager
return input;
}
void inputUpdate(input_t *input) {
void inputUpdate(platform_t *platform, input_t *input) {
// Flip the states to save memory.
float * currentCurrent = input->inputsCurrent;
input->inputsCurrent = input->inputsPrevious;
input->inputsPrevious = currentCurrent;
float * currentCurrent = input->current;
input->current = input->previous;
input->previous = currentCurrent;
// Now look at each bind, get the state from the platform
for(uint32_t i = 0; i < input->inputBindCount; i++) {
// For this bind, get the sources.
}
}
void inputDispose(input_t *input) {
free(input->inputTime);
free(input->inputsPrevious);
free(input->inputsCurrent);
free(input->times);
free(input->previous);
free(input->current);
free(input->binds);
free(input);
}
inputbindmap_t inputBind(input_t *input, inputbind_t bind,
inputsource_t *source
platforminputsource_t *source
) {
return NULL;
}
float inputGetState(inputbind_t binding, inputsource_t *source) {
return 0;
}
bool inputIsDown(input_t *input, inputbind_t binding) {
return input->inputsCurrent[binding] != 0;
return input->current[binding] != 0;
}
bool inputIsUp(input_t *input, inputbind_t binding) {
return input->inputsCurrent[binding] == 0;
return input->current[binding] == 0;
}
bool inputIsPressed(input_t *input, inputbind_t binding) {
return (
input->inputsPrevious[binding] == 0 &&
input->inputsCurrent[binding] != 0
input->previous[binding] == 0 &&
input->current[binding] != 0
);
}
bool inputIsReleased(input_t *input, inputbind_t binding) {
return (
input->inputsCurrent[binding] == 0 &&
input->inputsPrevious[binding] != 0
input->current[binding] == 0 &&
input->previous[binding] != 0
);
}
float inputGetAxis(input_t *input, inputbind_t binding) {
return input->inputsCurrent[binding];
return input->current[binding];
}
float inputGetFullAxis(input_t *input, inputbind_t positive,
inputbind_t negative
) {
return input->inputsCurrent[negative] + input->inputsCurrent[positive];
return input->current[negative] + input->current[positive];
}
float inputGetAccuated(input_t *input, inputbind_t binding) {
return input->inputTime[binding];
return input->times[binding];
}

View File

@ -7,13 +7,13 @@
#include <stdbool.h>
#include <stdint.h>
#include <malloc.h>
#include "../platform/platform.h"
/////////////////////////////////// CONSTANTS //////////////////////////////////
#define INPUT_NULL (inputbind_t)0x00
#define INPUT_INPUTS_PER_BIND
/////////////////////////////// Type Definitions ///////////////////////////////
/**
* Input Source, can be a button or an axis. Currently a void type.
*/
typedef void inputsource_t;
/**
* Input Bind, a specific action bind reference for the game engine to use.
* e.g. "Jump" or "Walk Forward".
@ -21,7 +21,7 @@ typedef void inputsource_t;
typedef uint8_t inputbind_t;
/**
* Input Bind Map, contains the bound link between an inputsource_t and an
* Input Bind Map, contains the bound link between an input source and an
* inputbind_t. Bind Maps can be added and removed.
*/
typedef uint16_t inputbindmap_t;
@ -34,28 +34,21 @@ typedef struct {
uint32_t inputBindCount;
/** Float of the input between 0 and 1. For teh current frame */
float *inputsCurrent;
float *current;
/** Float of the input between 0 and 1. For the last frame */
float *inputsPrevious;
float *previous;
/** Float of the GameTime that the input was actuated last. */
float *inputTime;
/**
* Array of pointers to all of the input sources. This must be set up by the
* platform to register each known input to poll on. Essentially the engine is
* going to query the platform for the current state of each input based on the
* entries within this list.
*/
inputsource_t **bindingSources;
float *times;
/**
* Array of input bindings. This is the list of "possible actions" that the
* user can perform. This would contain "jump", "walk forward", "run"... etc.
*/
inputbind_t *INPUT_BINDING_BINDS;
inputbind_t *binds;
} input_t;
//////////////////////////////////// Methods ///////////////////////////////////
/**
@ -64,13 +57,13 @@ typedef struct {
* @param inputCount The input binding counts to allow for.
* @return The input manager
*/
input_t * inputInit(uint32_t inputBindCount);
input_t * inputInit(platform_t *platform, uint32_t inputBindCount);
/**
* Tick the input manager.
* @param input The input to update.
*/
void inputUpdate(input_t *input);
void inputUpdate(platform_t *platform, input_t *input);
/**
* Destroy the input manager and cleanup.
@ -88,19 +81,10 @@ void inputDispose(input_t *input);
* @return The map between the bind and the source, needed to unbind later.
*/
inputbindmap_t inputBind(input_t *input, inputbind_t bind,
inputsource_t *source
platforminputsource_t *source
);
/**
* Platform-centric method. Method will be polled for each known input source to
* request the current state from the device. Values returned will be defined
* within the 0 to 1 range.
*
* @param binding The binding that has been created.
* @param source The input source, this was created when the binding was defined
* @return Input state between 0 and 1.
*/
float inputGetState(inputbind_t binding, inputsource_t *source);
float inputGetState(inputbind_t binding, platforminputsource_t *source);
/**
* Is the current input "down", not being pressed, being moved, not in a state