Added input manager final version

This commit is contained in:
2021-02-25 00:00:49 +11:00
parent 700a1b8c7f
commit 90fa9d8314
9 changed files with 84 additions and 40 deletions

View File

@ -23,8 +23,8 @@ game_t * gameInit(platform_t *platform) {
return game;
}
void gameUpdate(game_t *game) {
engineUpdate(game->engine);
uint32_t gameUpdate(game_t *game) {
return engineUpdate(game->engine);
}
void gameDispose(game_t *game) {

View File

@ -23,7 +23,7 @@ engine_t * engineInit(platform_t *platform, char *name, uint32_t inputCount) {
}
// Setup the input manager.
engine->input = inputInit(inputCount);
engine->input = inputInit(inputCount, platform->inputSourceCount);
if(engine->input == NULL) {
free(engine->render);
free(engine);
@ -33,9 +33,11 @@ engine_t * engineInit(platform_t *platform, char *name, uint32_t inputCount) {
return engine;
}
void engineUpdate(engine_t *engine) {
uint32_t engineUpdate(engine_t *engine) {
renderFrame(engine->render);
inputUpdate(engine->input);
return 0;
}
bool engineDispose(engine_t *engine) {

View File

@ -39,8 +39,9 @@ engine_t * engineInit(platform_t *platform, char *name, uint32_t inputCount);
* Start the main engine loop.
*
* @param engine The game to start the loop for.
* @return 0 if success (and loop to continue), 1 for non error terminate.
*/
void engineUpdate(engine_t *engine);
uint32_t engineUpdate(engine_t *engine);
/**
* Cleanup a previously constructed game engine instance.

View File

@ -27,8 +27,9 @@ game_t * gameInit(platform_t *platform);
* Start the main game loop.
*
* @param game The game to start the loop for.
* @return Refer to engineUpdate. 0 for loop continue, 1 for safe exit.
*/
void gameUpdate(game_t *game);
uint32_t gameUpdate(game_t *game);
/**
* Cleanup a previously constructed.

View File

@ -7,7 +7,7 @@
#include "input.h"
input_t * inputInit(uint32_t inputBindCount) {
input_t * inputInit(uint32_t inputBindCount, uint32_t inputSourceCount) {
uint32_t i;
// Create the input manager
@ -22,27 +22,45 @@ input_t * inputInit(uint32_t inputBindCount) {
}
// Create the current & previous input states
input->current = malloc(sizeof(float) * inputBindCount);
input->previous = malloc(sizeof(float) * inputBindCount);
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);
// Create the buffer, zero all the values out.
size_t size = sizeof(inputval_t) * inputSourceCount;
input->buffer = malloc(size);
memset(input->buffer, 0, size);
// Pass off the input manager
return input;
}
void inputUpdate(input_t *input) {
uint32_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;
// Now look at each bind, get the state from the platform
// Now look at each bind...
for(i = 0; i < input->inputBindCount; i++) {
// For this bind, get the sources.
// Now get the list of input sources bound to this input
item = input->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];
item = item->next;
}
// Set to the current state.
input->current[i] = value;
}
}
@ -54,6 +72,7 @@ void inputDispose(input_t *input) {
listDispose(input->bindMap[i], false);
}
free(input->buffer);
free(input->times);
free(input->previous);
free(input->current);
@ -61,13 +80,11 @@ void inputDispose(input_t *input) {
free(input);
}
void inputBind(input_t *input, inputbind_t bind, inputsource_t *source) {
void inputBind(input_t *input, inputbind_t bind, inputsource_t source) {
listAdd(input->bindMap[bind], source);
}
void inputUnbind(input_t *input, inputbind_t bind,
inputsource_t *source
) {
void inputUnbind(input_t *input, inputbind_t bind, inputsource_t source) {
listRemove(input->bindMap[bind], source);
}
@ -87,13 +104,10 @@ bool inputIsPressed(input_t *input, inputbind_t binding) {
}
bool inputIsReleased(input_t *input, inputbind_t binding) {
return (
input->current[binding] == 0 &&
input->previous[binding] != 0
);
return input->current[binding] == 0 && input->previous[binding] != 0;
}
float inputGetAxis(input_t *input, inputbind_t binding) {
inputval_t inputGetAxis(input_t *input, inputbind_t binding) {
return input->current[binding];
}

View File

@ -7,12 +7,12 @@
#include <stdbool.h>
#include <stdint.h>
#include <malloc.h>
#include <string.h>
#include "../util/list/list.h"
#include "../platform.h"
/////////////////////////////////// CONSTANTS //////////////////////////////////
#define INPUT_NULL (inputbind_t)0x00
#define INPUT_INPUTS_PER_BIND
/////////////////////////////// Type Definitions ///////////////////////////////
/**
@ -20,7 +20,19 @@
* e.g. "Jump" or "Walk Forward".
*/
typedef uint8_t inputbind_t;
typedef platforminput_t inputsource_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.
@ -30,10 +42,16 @@ typedef struct {
uint32_t inputBindCount;
/** Float of the input between 0 and 1. For teh current frame */
float *current;
inputval_t *current;
/** Float of the input between 0 and 1. For the last frame */
float *previous;
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;
@ -51,9 +69,10 @@ typedef struct {
* 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);
input_t * inputInit(uint32_t inputBindCount, uint32_t inputSourceCount);
/**
* Tick the input manager.
@ -75,7 +94,7 @@ void inputDispose(input_t *input);
* @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(input_t *input, inputbind_t bind, inputsource_t source);
/**
* Unbind a previously bound input source from a binding. This method is costly.
@ -84,7 +103,7 @@ void inputBind(input_t *input, inputbind_t bind, inputsource_t *source);
* @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(input_t *input, inputbind_t bind, inputsource_t source);
/**
* Is the current input "down", not being pressed, being moved, not in a state
@ -130,9 +149,9 @@ bool inputIsReleased(input_t *input, inputbind_t binding);
*
* @param input The input manager to check against.
* @param binding The previously bound input binding.
* @return A float between 0 and 1 representing the action force of the input.
* @return Input state of the axis.
*/
float inputGetAxis(input_t *input, inputbind_t binding);
inputval_t inputGetAxis(input_t *input, inputbind_t binding);
/**
* Returns a raw input value between -1 and 1 between two axis. This would be

View File

@ -26,7 +26,6 @@ typedef struct {
/** Dimensions of the screen (in pixels) */
uint32_t screenWidth, screenHeight;
int32_t inputSourceCount;
float *inputValues;
platforminput_t *inputSource;
/** Count of input sources that exist in engine. */
uint32_t inputSourceCount;
} platform_t;

View File

@ -5,19 +5,18 @@
#include "glwfwplatform.h"
GLFWwindow *window;
game_t *runningGame;
GLFWwindow *window = NULL;
game_t *runningGame = NULL;
int32_t main() {
// Create out platform context
platform_t platform = {
.name = "glfw",
.screenWidth = WINDOW_WIDTH_DEFAULT,
.screenHeight = WINDOW_HEIGHT_DEFAULT,
.inputSourceCount = 0,
.inputValues = NULL,
.inputSource = NULL
.inputSourceCount = 400
};
// Attempt to init GLFW
@ -46,9 +45,13 @@ int32_t main() {
engine_t *engine = gameGetEngine(runningGame);
glfwSetWindowTitle(window, engine->name);
// Bind inputs
inputBind(engine->input, INPUT_NULL, (inputsource_t *)GLFW_KEY_ESCAPE);
// Main Render Loop
while(!glfwWindowShouldClose(window)) {
gameUpdate(runningGame);
glfwSwapBuffers(window);
glfwPollEvents();
}
@ -73,8 +76,12 @@ void glfwOnResize(GLFWwindow *window, int32_t width, int32_t height) {
void glfwOnKey(GLFWwindow *window,
int32_t key, int32_t scancode, int32_t action, int32_t mods
) {
float force = action == GLFW_PRESS ? 1 : 0;
if(runningGame == NULL) return;
engine_t *engine = gameGetEngine(runningGame);
engine->input->current[scancode] = force;
if(action == GLFW_PRESS) {
engine->input->buffer[key] = 1;
} else if(action == GLFW_RELEASE) {
engine->input->buffer[key] = 0;
}
}

View File

@ -13,6 +13,7 @@
#include <stdint.h>
#include "../../engine/platform.h"
#include "../../engine/game/game.h"
#include "../../engine/input/input.h"
#include "../../engine/display/render.h"
#define WINDOW_WIDTH_DEFAULT 480