Add console.
This commit is contained in:
@ -14,8 +14,10 @@ target_sources(${DUSK_TARGET_NAME}
|
|||||||
PRIVATE
|
PRIVATE
|
||||||
game.c
|
game.c
|
||||||
input.c
|
input.c
|
||||||
|
gametime.c
|
||||||
)
|
)
|
||||||
|
|
||||||
# Subdirs
|
# Subdirs
|
||||||
add_subdirectory(assert)
|
add_subdirectory(assert)
|
||||||
|
add_subdirectory(console)
|
||||||
add_subdirectory(display)
|
add_subdirectory(display)
|
12
src/dusk/console/CMakeLists.txt
Normal file
12
src/dusk/console/CMakeLists.txt
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
# Copyright (c) 2025 Dominic Masters
|
||||||
|
#
|
||||||
|
# This software is released under the MIT License.
|
||||||
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
# Sources
|
||||||
|
target_sources(${DUSK_TARGET_NAME}
|
||||||
|
PRIVATE
|
||||||
|
console.c
|
||||||
|
)
|
||||||
|
|
||||||
|
# Subdirs
|
59
src/dusk/console/console.c
Normal file
59
src/dusk/console/console.c
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "console.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
|
||||||
|
console_t CONSOLE;
|
||||||
|
|
||||||
|
void consoleInit() {
|
||||||
|
memset(&CONSOLE, 0, sizeof(console_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
const consolecmd_t * consoleCommand(const char_t *cmd) {
|
||||||
|
assertTrue(
|
||||||
|
CONSOLE.commandCount < CONSOLE_COMMAND_COUNT_MAX,
|
||||||
|
"Command buffer full."
|
||||||
|
);
|
||||||
|
assertStrLen(cmd, CONSOLE_CMD_LENGTH_MAX, "Command too long.");
|
||||||
|
|
||||||
|
consolecmd_t *command = &CONSOLE.commands[CONSOLE.commandCount++];
|
||||||
|
strcpy(command->cmd, cmd);
|
||||||
|
return command;
|
||||||
|
}
|
||||||
|
|
||||||
|
void consolePrint(const char_t *fmt, ...) {
|
||||||
|
// Do we need to shift the log buffer?
|
||||||
|
if (CONSOLE.logCount == CONSOLE_LOG_COUNT_MAX) {
|
||||||
|
memcpy(
|
||||||
|
CONSOLE.log[0],
|
||||||
|
CONSOLE.log[1],
|
||||||
|
sizeof(char_t) *
|
||||||
|
CONSOLE_LOG_MESSAGE_LENGTH_MAX *
|
||||||
|
(CONSOLE_LOG_COUNT_MAX - 1)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print the message
|
||||||
|
va_list args;
|
||||||
|
va_start(args, fmt);
|
||||||
|
|
||||||
|
// buffer but don't let it overflow the string length.
|
||||||
|
int ret = vsnprintf(
|
||||||
|
CONSOLE.log[CONSOLE.logCount++],
|
||||||
|
CONSOLE_LOG_MESSAGE_LENGTH_MAX,
|
||||||
|
fmt,
|
||||||
|
args
|
||||||
|
);
|
||||||
|
|
||||||
|
assertTrue(ret >= 0, "Log message error.");
|
||||||
|
assertTrue(ret < CONSOLE_LOG_MESSAGE_LENGTH_MAX, "Log message overflow.");
|
||||||
|
|
||||||
|
printf("%s\n", CONSOLE.log[CONSOLE.logCount - 1]);
|
||||||
|
|
||||||
|
va_end(args);
|
||||||
|
}
|
43
src/dusk/console/console.h
Normal file
43
src/dusk/console/console.h
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "consolecmd.h"
|
||||||
|
|
||||||
|
#define CONSOLE_COMMAND_COUNT_MAX 64
|
||||||
|
#define CONSOLE_LOG_MESSAGE_LENGTH_MAX 1024
|
||||||
|
#define CONSOLE_LOG_COUNT_MAX 256
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
consolecmd_t commands[CONSOLE_COMMAND_COUNT_MAX];
|
||||||
|
uint8_t commandCount;
|
||||||
|
|
||||||
|
char_t log[CONSOLE_LOG_COUNT_MAX][CONSOLE_LOG_MESSAGE_LENGTH_MAX + 1];
|
||||||
|
uint8_t logCount;
|
||||||
|
} console_t;
|
||||||
|
|
||||||
|
extern console_t CONSOLE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the console.
|
||||||
|
*/
|
||||||
|
void consoleInit();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run a command against the console.
|
||||||
|
*
|
||||||
|
* @param cmd The command string to run.
|
||||||
|
*/
|
||||||
|
const consolecmd_t * consoleCommand(const char_t *cmd);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints a message to the console.
|
||||||
|
*
|
||||||
|
* @param fmt The format string.
|
||||||
|
* @param ... The format arguments.
|
||||||
|
*/
|
||||||
|
void consolePrint(const char_t *fmt, ...);
|
15
src/dusk/console/consolecmd.h
Normal file
15
src/dusk/console/consolecmd.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "dusk.h"
|
||||||
|
|
||||||
|
#define CONSOLE_CMD_LENGTH_MAX 256
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char_t cmd[CONSOLE_CMD_LENGTH_MAX + 1];
|
||||||
|
} consolecmd_t;
|
@ -13,6 +13,7 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
typedef bool bool_t;
|
typedef bool bool_t;
|
||||||
typedef char char_t;
|
typedef char char_t;
|
@ -6,12 +6,17 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
|
#include "gametime.h"
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
#include "console/console.h"
|
||||||
|
|
||||||
void gameInit() {
|
void gameInit() {
|
||||||
|
consoleInit();
|
||||||
|
gameTimeInit();
|
||||||
inputInit();
|
inputInit();
|
||||||
}
|
}
|
||||||
|
|
||||||
void gameUpdate() {
|
void gameUpdate(const float delta) {
|
||||||
|
gameTimeUpdate(delta);
|
||||||
inputUpdate();
|
inputUpdate();
|
||||||
}
|
}
|
@ -15,5 +15,7 @@ void gameInit();
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the game.
|
* Updates the game.
|
||||||
|
*
|
||||||
|
* @param delta Game time update delta since last tick.
|
||||||
*/
|
*/
|
||||||
void gameUpdate();
|
void gameUpdate(const float_t delta);
|
20
src/dusk/gametime.c
Normal file
20
src/dusk/gametime.c
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "gametime.h"
|
||||||
|
|
||||||
|
gametime_t GAME_TIME;
|
||||||
|
|
||||||
|
void gameTimeInit() {
|
||||||
|
memset(&GAME_TIME, 0, sizeof(gametime_t));
|
||||||
|
GAME_TIME.delta = GAME_TIME_STEP_DEFAULT;
|
||||||
|
}
|
||||||
|
|
||||||
|
void gameTimeUpdate(const float_t delta) {
|
||||||
|
GAME_TIME.delta = delta;
|
||||||
|
GAME_TIME.time += GAME_TIME.delta;
|
||||||
|
}
|
30
src/dusk/gametime.h
Normal file
30
src/dusk/gametime.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "dusk.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
float_t delta;
|
||||||
|
float_t time;
|
||||||
|
} gametime_t;
|
||||||
|
|
||||||
|
extern gametime_t GAME_TIME;
|
||||||
|
|
||||||
|
#define GAME_TIME_STEP_DEFAULT 0.016f
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the game time.
|
||||||
|
*/
|
||||||
|
void gameTimeInit();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the game time.
|
||||||
|
*
|
||||||
|
* @param delta The time since the last frame.
|
||||||
|
*/
|
||||||
|
void gameTimeUpdate(const float_t delta);
|
@ -17,7 +17,7 @@ typedef enum {
|
|||||||
INPUT_RIGHT,
|
INPUT_RIGHT,
|
||||||
INPUT_ACCEPT,
|
INPUT_ACCEPT,
|
||||||
INPUT_BACK,
|
INPUT_BACK,
|
||||||
INPUT_QUIT
|
INPUT_QUIT,
|
||||||
} inputbind_t;
|
} inputbind_t;
|
||||||
|
|
||||||
#define INPUT_BIND_COUNT INPUT_QUIT + 1
|
#define INPUT_BIND_COUNT INPUT_QUIT + 1
|
||||||
|
@ -7,11 +7,24 @@
|
|||||||
|
|
||||||
#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,10 +10,7 @@
|
|||||||
#include "display/render.h"
|
#include "display/render.h"
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include "asset.h"
|
#include "asset.h"
|
||||||
|
#include "console/console.h"
|
||||||
#define TIME_BETWEEN_UPDATES (1.0f / 60.0f)
|
|
||||||
|
|
||||||
float_t timeUntilNextUpdate;
|
|
||||||
|
|
||||||
char_t EXECUTABLE_PATH[FILENAME_MAX];
|
char_t EXECUTABLE_PATH[FILENAME_MAX];
|
||||||
char_t EXECUTABLE_DIRECTORY[FILENAME_MAX];
|
char_t EXECUTABLE_DIRECTORY[FILENAME_MAX];
|
||||||
@ -35,15 +32,20 @@ int32_t main(int32_t argc, char_t **argv) {
|
|||||||
if((ret = windowInit()) != 0) {
|
if((ret = windowInit()) != 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
gameInit();
|
||||||
|
|
||||||
|
// Print OS Information
|
||||||
|
const char* glfwVersionString = glfwGetVersionString();
|
||||||
|
consolePrint("GLFW: %s", glfwVersionString);
|
||||||
|
|
||||||
|
// Init asset and render systems.
|
||||||
assetInit();
|
assetInit();
|
||||||
renderInit();
|
renderInit();
|
||||||
gameInit();
|
|
||||||
|
|
||||||
// Prepare for time tracking
|
// Prepare for time tracking
|
||||||
double_t time, newTime;
|
double_t time, newTime;
|
||||||
float_t fDelta;
|
float_t fDelta;
|
||||||
int32_t updateResult;
|
int32_t updateResult;
|
||||||
timeUntilNextUpdate = 0.0f;
|
|
||||||
|
|
||||||
// Main loop
|
// Main loop
|
||||||
while(!windowShouldClose()) {
|
while(!windowShouldClose()) {
|
||||||
@ -51,13 +53,8 @@ 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;
|
||||||
timeUntilNextUpdate -= fDelta;
|
|
||||||
|
|
||||||
// Should game tick?
|
gameUpdate(fDelta);
|
||||||
if(timeUntilNextUpdate <= 0) {
|
|
||||||
gameUpdate();
|
|
||||||
timeUntilNextUpdate = TIME_BETWEEN_UPDATES;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw
|
// Draw
|
||||||
renderUpdate();
|
renderUpdate();
|
||||||
|
Reference in New Issue
Block a user