Input and tick
This commit is contained in:
@ -19,10 +19,10 @@ target_include_directories(${DUSK_TARGET_NAME}
|
|||||||
target_sources(${DUSK_TARGET_NAME}
|
target_sources(${DUSK_TARGET_NAME}
|
||||||
PRIVATE
|
PRIVATE
|
||||||
game.c
|
game.c
|
||||||
|
input.c
|
||||||
)
|
)
|
||||||
|
|
||||||
# Subdirs
|
# Subdirs
|
||||||
add_subdirectory(assert)
|
add_subdirectory(assert)
|
||||||
add_subdirectory(console)
|
|
||||||
add_subdirectory(entity)
|
add_subdirectory(entity)
|
||||||
add_subdirectory(display)
|
add_subdirectory(display)
|
@ -14,6 +14,11 @@ void gameInit() {
|
|||||||
|
|
||||||
void gameUpdate() {
|
void gameUpdate() {
|
||||||
inputUpdate();
|
inputUpdate();
|
||||||
|
|
||||||
|
if(inputWasPressed(INPUT_MENU)) {
|
||||||
|
// Quit
|
||||||
|
printf("Quit\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void gameDispose() {
|
void gameDispose() {
|
||||||
|
@ -8,6 +8,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "dusk.h"
|
#include "dusk.h"
|
||||||
|
|
||||||
|
#define GAME_TICK_RATE 60
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the game.
|
* Initializes the game.
|
||||||
*/
|
*/
|
||||||
|
37
src/dusk/input.c
Normal file
37
src/dusk/input.c
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "input.h"
|
||||||
|
|
||||||
|
inputstate_t INPUT_CURRENT;
|
||||||
|
inputstate_t INPUT_LAST_FRAME;
|
||||||
|
|
||||||
|
void inputInit() {
|
||||||
|
INPUT_CURRENT = 0;
|
||||||
|
INPUT_LAST_FRAME = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void inputUpdate() {
|
||||||
|
INPUT_LAST_FRAME = INPUT_CURRENT;
|
||||||
|
INPUT_CURRENT = inputPlatformState();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool_t inputWasPressed(const inputstate_t state) {
|
||||||
|
return (INPUT_LAST_FRAME & state) == 0 && (INPUT_CURRENT & state) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool_t inputWasReleased(const inputstate_t state) {
|
||||||
|
return (INPUT_LAST_FRAME & state) != 0 && (INPUT_CURRENT & state) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool_t inputIsDown(const inputstate_t state) {
|
||||||
|
return (INPUT_CURRENT & state) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool_t inputIsUp(const inputstate_t state) {
|
||||||
|
return (INPUT_CURRENT & state) == 0;
|
||||||
|
}
|
@ -6,9 +6,18 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#include "dusk.h"
|
||||||
|
|
||||||
typedef uint16_t inputstate_t;
|
typedef uint16_t inputstate_t;
|
||||||
|
|
||||||
|
#define INPUT_MENU (1 << 0)
|
||||||
|
#define INPUT_UP (1 << 1)
|
||||||
|
#define INPUT_DOWN (1 << 2)
|
||||||
|
#define INPUT_LEFT (1 << 3)
|
||||||
|
#define INPUT_RIGHT (1 << 4)
|
||||||
|
#define INPUT_ACCEPT (1 << 5)
|
||||||
|
#define INPUT_BACK (1 << 6)
|
||||||
|
|
||||||
extern inputstate_t INPUT_CURRENT;
|
extern inputstate_t INPUT_CURRENT;
|
||||||
extern inputstate_t INPUT_LAST_FRAME;
|
extern inputstate_t INPUT_LAST_FRAME;
|
||||||
|
|
||||||
@ -16,4 +25,47 @@ extern inputstate_t INPUT_LAST_FRAME;
|
|||||||
* Initializes the input system
|
* Initializes the input system
|
||||||
*/
|
*/
|
||||||
void inputInit();
|
void inputInit();
|
||||||
void inputUpdate();
|
|
||||||
|
/**
|
||||||
|
* Updates the input system for this tick.
|
||||||
|
*/
|
||||||
|
void inputUpdate();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Requests the platform let us know the current state of each input.
|
||||||
|
*
|
||||||
|
* @return Current input state.
|
||||||
|
*/
|
||||||
|
inputstate_t inputPlatformState();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the input was pressed this frame, but not last frame.
|
||||||
|
*
|
||||||
|
* @param state Inputs to check, typically a single input.
|
||||||
|
* @return True if input was pressed this frame but not last frame.
|
||||||
|
*/
|
||||||
|
bool_t inputWasPressed(const inputstate_t state);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the input was released this frame, but pressed last frame.
|
||||||
|
*
|
||||||
|
* @param state Inputs to check, typically a single input.
|
||||||
|
* @return True if input was released this frame but was pressed last frame.
|
||||||
|
*/
|
||||||
|
bool_t inputWasReleased(const inputstate_t state);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the input is currently pressed.
|
||||||
|
*
|
||||||
|
* @param state Inputs to check, typically a single input.
|
||||||
|
* @return True if input is currently pressed.
|
||||||
|
*/
|
||||||
|
bool_t inputIsDown(const inputstate_t state);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the input is currently released.
|
||||||
|
*
|
||||||
|
* @param state Inputs to check, typically a single input.
|
||||||
|
* @return True if input is currently released.
|
||||||
|
*/
|
||||||
|
bool_t inputIsUp(const inputstate_t state);
|
@ -7,24 +7,11 @@
|
|||||||
|
|
||||||
#include "assert/assertgl.h"
|
#include "assert/assertgl.h"
|
||||||
#include "render.h"
|
#include "render.h"
|
||||||
#include "console/console.h"
|
|
||||||
|
|
||||||
render_t RENDER;
|
render_t RENDER;
|
||||||
|
|
||||||
void renderInit() {
|
void renderInit() {
|
||||||
memset(&RENDER, 0, sizeof(render_t));
|
memset(&RENDER, 0, sizeof(render_t));
|
||||||
|
|
||||||
// Get OpenGL version
|
|
||||||
const GLubyte *version = glGetString(GL_VERSION);
|
|
||||||
const GLubyte *renderer = glGetString(GL_RENDERER);
|
|
||||||
const GLubyte *vendor = glGetString(GL_VENDOR);
|
|
||||||
const GLubyte *glslVersion = glGetString(GL_SHADING_LANGUAGE_VERSION);
|
|
||||||
|
|
||||||
consolePrint("OpenGL Renderer");
|
|
||||||
consolePrint("Version: %s", version);
|
|
||||||
consolePrint("Renderer: %s", renderer);
|
|
||||||
consolePrint("Vendor: %s", vendor);
|
|
||||||
consolePrint("GLSL: %s", glslVersion);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void renderUpdate() {
|
void renderUpdate() {
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint16_t glfw;
|
uint16_t glfw;
|
||||||
inputbind_t bind;
|
inputstate_t bind;
|
||||||
} glfwkeybindmap_t;
|
} glfwkeybindmap_t;
|
||||||
|
|
||||||
glfwkeybindmap_t GLFW_KEY_BIND_MAP[] = {
|
glfwkeybindmap_t GLFW_KEY_BIND_MAP[] = {
|
||||||
@ -30,23 +30,27 @@ glfwkeybindmap_t GLFW_KEY_BIND_MAP[] = {
|
|||||||
{GLFW_KEY_E, INPUT_ACCEPT},
|
{GLFW_KEY_E, INPUT_ACCEPT},
|
||||||
{GLFW_KEY_SPACE, INPUT_ACCEPT},
|
{GLFW_KEY_SPACE, INPUT_ACCEPT},
|
||||||
|
|
||||||
{GLFW_KEY_ESCAPE, INPUT_BACK},
|
|
||||||
{GLFW_KEY_BACKSPACE, INPUT_BACK},
|
{GLFW_KEY_BACKSPACE, INPUT_BACK},
|
||||||
{GLFW_KEY_Q, INPUT_BACK},
|
{GLFW_KEY_Q, INPUT_BACK},
|
||||||
|
|
||||||
|
{GLFW_KEY_ESCAPE, INPUT_MENU},
|
||||||
|
|
||||||
{0, 0}
|
{0, 0}
|
||||||
};
|
};
|
||||||
|
|
||||||
inputvalue_t inputStateGet(const inputbind_t input) {
|
inputstate_t inputPlatformState() {
|
||||||
|
inputstate_t state = 0;
|
||||||
|
|
||||||
// Handle keybinds
|
// Handle keybinds
|
||||||
glfwkeybindmap_t *map = GLFW_KEY_BIND_MAP;
|
glfwkeybindmap_t *map = GLFW_KEY_BIND_MAP;
|
||||||
do {
|
do {
|
||||||
if(map->bind == input && glfwGetKey(window, map->glfw) == GLFW_PRESS) {
|
if(glfwGetKey(window, map->glfw) != GLFW_PRESS) {
|
||||||
return 1;
|
map++;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
state |= map->bind;
|
||||||
map++;
|
map++;
|
||||||
} while(map->glfw != 0);
|
} while(map->glfw != 0);
|
||||||
|
|
||||||
return 0;
|
return state;
|
||||||
}
|
}
|
@ -10,7 +10,6 @@
|
|||||||
#include "display/render.h"
|
#include "display/render.h"
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include "asset.h"
|
#include "asset.h"
|
||||||
#include "console/console.h"
|
|
||||||
|
|
||||||
char_t EXECUTABLE_PATH[FILENAME_MAX];
|
char_t EXECUTABLE_PATH[FILENAME_MAX];
|
||||||
char_t EXECUTABLE_DIRECTORY[FILENAME_MAX];
|
char_t EXECUTABLE_DIRECTORY[FILENAME_MAX];
|
||||||
@ -34,17 +33,13 @@ int32_t main(int32_t argc, char_t **argv) {
|
|||||||
}
|
}
|
||||||
gameInit();
|
gameInit();
|
||||||
|
|
||||||
// Print OS Information
|
|
||||||
const char* glfwVersionString = glfwGetVersionString();
|
|
||||||
consolePrint("GLFW: %s", glfwVersionString);
|
|
||||||
|
|
||||||
// Init asset and render systems.
|
// Init asset and render systems.
|
||||||
assetInit();
|
assetInit();
|
||||||
renderInit();
|
renderInit();
|
||||||
|
|
||||||
// Prepare for time tracking
|
// Prepare for time tracking
|
||||||
double_t time, newTime;
|
double_t time, newTime;
|
||||||
float_t fDelta;
|
float_t fDelta, fTimeSinceLastFrame = 1.0f;
|
||||||
int32_t updateResult;
|
int32_t updateResult;
|
||||||
|
|
||||||
// Main loop
|
// Main loop
|
||||||
@ -53,10 +48,15 @@ int32_t main(int32_t argc, char_t **argv) {
|
|||||||
newTime = glfwGetTime();
|
newTime = glfwGetTime();
|
||||||
fDelta = (float_t)(newTime - time);
|
fDelta = (float_t)(newTime - time);
|
||||||
time = newTime;
|
time = newTime;
|
||||||
|
fTimeSinceLastFrame += fDelta;
|
||||||
|
|
||||||
gameUpdate(fDelta);
|
// Tick according to the game tick rate.
|
||||||
|
if(fTimeSinceLastFrame >= (1.0f / GAME_TICK_RATE)) {
|
||||||
|
gameUpdate();
|
||||||
|
fTimeSinceLastFrame = 0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
// Draw
|
// Draw. In future may be tick based.
|
||||||
renderUpdate();
|
renderUpdate();
|
||||||
windowUpdate();
|
windowUpdate();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user