How did I get here?

This commit is contained in:
2025-09-19 22:01:48 -05:00
parent 2049a6f7b9
commit 96fcddea30
20 changed files with 962 additions and 30 deletions

33
src/CMakeLists.txt Normal file
View File

@@ -0,0 +1,33 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
find_package(glm REQUIRED)
find_package(libzip REQUIRED)
# Libs
target_link_libraries(${DAWN_TARGET_NAME}
PUBLIC
m
glm
zip
)
# Includes
target_include_directories(${DAWN_TARGET_NAME}
PRIVATE
${CMAKE_CURRENT_LIST_DIR}
)
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
main.cpp
)
# Subdirs
add_subdirectory(assert)
add_subdirectory(console)
add_subdirectory(engine)
add_subdirectory(time)

85
src/assert/Assert.cpp Normal file
View File

@@ -0,0 +1,85 @@
/**
* Copyright (c) 2023 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "Assert.hpp"
#ifndef ASSERTIONS_FAKED
void assertTrueImpl(
const char_t *file,
const int32_t line,
const bool_t x,
const char_t *message
) {
if(x != true) {
fprintf(
stderr,
"Assertion Failed in %s:%i\n\n%s\n",
file,
line,
message
);
abort();
}
}
void assertFalseImpl(
const char_t *file,
const int32_t line,
bool_t x,
const char_t *message
) {
assertTrueImpl(file, line, !x, message);
}
void assertUnreachableImpl(
const char_t *file,
const int32_t line,
const char_t *message
) {
assertTrueImpl(file, line, false, message);
}
void assertNotNullImpl(
const char_t *file,
const int32_t line,
const void *pointer,
const char_t *message
) {
assertTrueImpl(
file,
line,
pointer != NULL,
message
);
// Ensure we can touch it
volatile char_t temp;
temp = *((char_t*)pointer);
}
void assertNullImpl(
const char_t *file,
const int32_t line,
const void *pointer,
const char_t *message
) {
assertTrueImpl(
file,
line,
pointer == NULL,
message
);
}
void assertDeprecatedImpl(
const char_t *file,
const int32_t line,
const char_t *message
) {
assertUnreachableImpl(file, line, message);
}
#endif

143
src/assert/Assert.hpp Normal file
View File

@@ -0,0 +1,143 @@
/**
* Copyright (c) 2023 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dawn.hpp"
#ifndef ASSERTIONS_FAKED
/**
* Assert a given value to be true.
*
* @param file File that the assertion is being made from.
* @param line Line that the assertion is being made from.
* @param x Value to assert as true.
* @param message Message to throw against assertion failure.
*/
void assertTrueImpl(
const char_t *file,
const int32_t line,
const bool_t x,
const char_t *message
);
/**
* Asserts a given statement to be false.
*
* @param file File that the assertion is being made from.
* @param line Line that the assertion is being made from.
* @param x Value to assert as false.
* @param message Message to throw against assertion failure.
*/
void assertFalseImpl(
const char_t *file,
const int32_t line,
const bool_t x,
const char_t *message
);
/**
* Asserts that a given line of code is unreachable. Essentially a forced
* assertion failure, good for "edge cases"
*
* @param file File that the assertion is being made from.
* @param line Line that the assertion is being made from.
* @param message Message to throw against assertion failure.
*/
void assertUnreachableImpl(
const char_t *file,
const int32_t line,
const char_t *message
);
/**
* Assert a given pointer to not point to a null pointer.
*
* @param file File that the assertion is being made from.
* @param line Line that the assertion is being made from.
* @param pointer Pointer to assert is not a null pointer.
* @param message Message to throw against assertion failure.
*/
void assertNotNullImpl(
const char_t *file,
const int32_t line,
const void *pointer,
const char_t *message
);
/**
* Asserts a given pointer to be a nullptr.
*
* @param file File that the assertion is being made from.
* @param line Line that the assertion is being made from.
* @param pointer Pointer to assert is nullptr.
* @param message Message to throw against assertion failure.
*/
void assertNullImpl(
const char_t *file,
const int32_t line,
const void *pointer,
const char_t *message
);
/**
* Asserts a function as being deprecated.
*
* @param file File that the assertion is being made from.
* @param line Line that the assertion is being made from.
* @param message Message to throw against assertion failure.
*/
void assertDeprecatedImpl(
const char_t *file,
const int32_t line,
const char_t *message
);
void assertMemoryRangeMatchesImpl(
const char_t *file,
const int32_t line,
const void *start,
const void *end,
const size_t size,
const char_t *message
);
#define assertTrue(x, message) \
assertTrueImpl(__FILE__, __LINE__, x, message)
#define assertFalse(x, message) \
assertFalseImpl(__FILE__, __LINE__, x, message)
#define assertUnreachable(message) \
assertUnreachableImpl(__FILE__, __LINE__, message)
#define assertNotNull(pointer, message) \
assertNotNullImpl(__FILE__, __LINE__, pointer, message)
#define assertNull(pointer, message) \
assertNullImpl(__FILE__, __LINE__, pointer, message)
#define assertDeprecated(message) \
assertDeprecatedImpl(__FILE__, __LINE__, message)
#define assertStrLenMax(str, len, message) \
assertTrue(strlen(str) < len, message)
#define assertStrLenMin(str, len, message) \
assertTrue(strlen(str) >= len, message)
#else
// If assertions are faked, we define the macros to do nothing.
#define assertTrue(x, message) ((void)0)
#define assertFalse(x, message) ((void)0)
#define assertUnreachable(message) ((void)0)
#define assertNotNull(pointer, message) ((void)0)
#define assertNull(pointer, message) ((void)0)
#define assertDeprecated(message) ((void)0)
#define assertStrLenMax(str, len, message) ((void)0)
#define assertStrLenMin(str, len, message) ((void)0)
#endif

10
src/assert/CMakeLists.txt Normal file
View File

@@ -0,0 +1,10 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
Assert.cpp
)

View File

@@ -0,0 +1,10 @@
# 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.cpp
)

146
src/console/Console.cpp Normal file
View File

@@ -0,0 +1,146 @@
// Copyright (c) 2025 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Console.hpp"
#include "assert/Assert.hpp"
using namespace Dawn;
Console::Console(void) :
commands(),
variables(),
buffer()
{
}
void Console::registerCommand(
const std::string &cmd,
const std::function<void(std::vector<std::string>&)> &callback
) {
assertTrue(cmd.length() > 0, "cmd length must be > 0");
assertTrue(callback != nullptr, "callback must not be null");
this->commands[cmd] = callback;
}
template<typename T>
void Console::registerVariable(
const std::string &var,
const std::function<T()> &getter,
const std::function<void(const T &)> &setter
) {
assertTrue(var.length() > 0, "var length must be > 0");
assertTrue(getter != nullptr, "getter must not be null");
assertTrue(setter != nullptr, "setter must not be null");
this->variables[var] = std::make_pair(getter, setter);
}
void Console::update() {
enum class ParseState { FIND_CMD, FIND_ARG, COMMAND, ARG, ARG_QUOTED };
for (const auto &cmd : buffer) {
std::string cmdName;
std::vector<std::string> args;
ParseState state = ParseState::FIND_CMD;
std::string buff;
for(size_t i = 0; i < cmd.length(); i++) {
char_t c = cmd[i];
switch(state) {
case ParseState::FIND_CMD:
if(isspace(c)) continue;
state = ParseState::COMMAND;
buff += c;
break;
case ParseState::FIND_ARG:
if(isspace(c)) continue;
if(c == '\"') {
state = ParseState::ARG_QUOTED;
break;
}
state = ParseState::ARG;
buff += c;
break;
case ParseState::COMMAND:
if(isspace(c)) {
cmdName = buff;
buff.clear();
state = ParseState::FIND_ARG;
break;
}
buff += c;
break;
case ParseState::ARG:
if(isspace(c)) {
args.push_back(buff);
buff.clear();
state = ParseState::FIND_ARG;
break;
}
buff += c;
break;
case ParseState::ARG_QUOTED:
if(c == '\"') {
args.push_back(buff);
buff.clear();
state = ParseState::FIND_ARG;
break;
}
buff += c;
break;
}
}
if(buff.length() > 0) {
if(state == ParseState::COMMAND) {
cmdName = buff;
} else {
args.push_back(buff);
}
}
// Find command
auto itCmd = commands.find(cmdName);
if(itCmd == commands.end()) {
// Find variable
auto itVar = variables.find(cmdName);
if(itVar == variables.end()) {
std::cout << "Console: Unknown command or variable '" << cmdName << "'" << std::endl;
continue;
}
// Variable get/set
printf("Console: Variable '%s' ", cmdName.c_str());
continue;
}
itCmd->second(args);
}
buffer.clear();
}
void Console::exec(const std::string &str) {
// Split strings by command seperator
std::istringstream iss(str);
std::string token;
while(std::getline(iss, token, COMMAND_SEPARATOR)) {
// Seperate by newlines too
std::istringstream lineIss(token);
std::string lineToken;
while(std::getline(lineIss, lineToken)) {
if(lineToken.length() == 0) continue;
buffer.push_back(lineToken);
}
}
}
Console::~Console(void) {
}

72
src/console/Console.hpp Normal file
View File

@@ -0,0 +1,72 @@
// Copyright (c) 2025 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawn.hpp"
namespace Dawn {
struct Console {
private:
std::map<
std::string, std::function<void(std::vector<std::string>&)>
> commands;
std::map<std::string, std::pair<
std::function<std::any()>, std::function<void(std::any&)>
>> variables;
std::vector<std::string> buffer;
public:
static constexpr char_t COMMAND_SEPARATOR = ';';
/**
* Constructs the console.
*/
Console(void);
/**
* Registers a command with the console.
*
* @param cmd The command string.
* @param callback The callback function for the command.
*/
void registerCommand(
const std::string &cmd,
const std::function<void(std::vector<std::string>&)> &callback
);
/**
* Registers a variable with the console.
*
* @param var The variable name.
* @param getter The getter function for the variable.
* @param setter The setter function for the variable.
*/
template<typename T>
void registerVariable(
const std::string &var,
const std::function<T()> &getter,
const std::function<void(const T &)> &setter
);
/**
* Updates the console state, this will exec the command buffer.
*/
void update();
/**
* Executes a command string.
*
* @param str The command string to execute.
*/
void exec(const std::string &str);
/**
* Destructs the console.
*/
~Console(void);
};
}

48
src/dawn.hpp Normal file
View File

@@ -0,0 +1,48 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
extern "C" {
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <errno.h>
#include <ctype.h>
#include <stdarg.h>
#include <float.h>
// #include <cglm/cglm.h>
// #include <cglm/types.h>
// #include <cglm/vec2.h>
#if PSP
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <pspctrl.h>
#include <psphprm.h>
#endif
typedef bool bool_t;
typedef int int_t;
typedef float float_t;
typedef char char_t;
}
#include <memory>
#include <string>
#include <vector>
#include <functional>
#include <map>
#include <array>
#include <any>
#include <iostream>
#include <sstream>

10
src/engine/CMakeLists.txt Normal file
View File

@@ -0,0 +1,10 @@
# 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
Engine.cpp
)

32
src/engine/Engine.cpp Normal file
View File

@@ -0,0 +1,32 @@
// Copyright (c) 2025 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Engine.hpp"
using namespace Dawn;
Engine::Engine() :
time(),
console()
{
// console.registerCommand("echo", [](std::vector<std::string> &args) {
// if(args.size() == 0) {
// std::cout << "Usage: echo <message>" << std::endl;
// return;
// }
// std::cout << args[0] << std::endl;
// });
// console.exec("echo");
}
void Engine::update(void) {
time.update();
console.update();
}
Engine::~Engine() {
}

31
src/engine/Engine.hpp Normal file
View File

@@ -0,0 +1,31 @@
// Copyright (c) 2025 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "time/Time.hpp"
#include "console/Console.hpp"
namespace Dawn {
struct Engine {
public:
Time time;
Console console;
/**
* Constructor for the Dawn engine.
*/
Engine();
/**
* Update the engine state.
*/
void update(void);
/**
* Destructor for the Dawn engine.
*/
~Engine();
};
}

View File

@@ -1,9 +0,0 @@
#include "funcs.h"
void doSomething(int32_t a, int32_t b) {
someNotImplementedFunction(a, b);
}
void addNumbers(int32_t l, int32_t r) {
doSomething(l + r, 32);
}

View File

@@ -1,16 +0,0 @@
#ifndef FUNC_H
#define FUNC_H
typedef int int32_t;
void doSomething(int32_t a, int32_t b);
void someNotImplementedFunction(int32_t x, int32_t y);
void addNumbers(int32_t l, int32_t r);
void someHeaderImplementedFunction(int32_t a, int32_t b) {
doSomething(a, b);
}
#endif // FUNC_H

23
src/main.cpp Normal file
View File

@@ -0,0 +1,23 @@
// Copyright (c) 2025 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "dawn.hpp"
#include "engine/Engine.hpp"
using namespace Dawn;
static std::shared_ptr<Engine> engine;
int main(int argc, char **argv) {
engine = std::make_shared<Engine>();
while(1) {
engine->update();
break;
}
engine = nullptr;
return 0;
}

10
src/time/CMakeLists.txt Normal file
View File

@@ -0,0 +1,10 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
Time.cpp
)

41
src/time/Time.cpp Normal file
View File

@@ -0,0 +1,41 @@
// Copyright (c) 2025 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Time.hpp"
#include "assert/Assert.hpp"
using namespace Dawn;
Time::Time(void) :
delta(Time::FIXED_STEP),
time(0.0f),
fixedDelta(Time::FIXED_STEP),
fixedTime(0.0f),
isFixedUpdate(false)
{
}
void Time::update(void) {
float_t delta;
#if TIME_SDL2
delta = (float_t)SDL_GetTicks() / 1000.0f - this->time;
#else
#endif
this->delta = delta;
assertTrue(this->delta >= 0.0f, "Delta time is negative");
this->time += delta;
this->isFixedUpdate = this->time - this->fixedTime >= Time::FIXED_STEP;
if (this->isFixedUpdate) {
this->fixedDelta = Time::FIXED_STEP;
this->fixedTime += this->fixedDelta;
} else {
this->fixedDelta = 0.0f;
this->fixedTime += this->fixedDelta;
}
}

35
src/time/Time.hpp Normal file
View File

@@ -0,0 +1,35 @@
// Copyright (c) 2025 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawn.hpp"
namespace Dawn {
struct Time {
private:
bool_t isFixedUpdate;
public:
static constexpr float_t FIXED_STEP = 1.0f / 60.0f;
#if TIME_FIXED
static const float_t FIXED_PLATFORM_STEP = TIME_PLATFORM_STEP;
#endif
float_t delta;
float_t time;
float_t fixedDelta;
float_t fixedTime;
/**
* The time in seconds since the application started.
*/
Time(void);
/**
* Update the time values.
*/
void update(void);
};
}