How did I get here?
This commit is contained in:
118
CMakeLists.txt
118
CMakeLists.txt
@@ -1,16 +1,124 @@
|
||||
# Copyright (c) 2022 Dominic Masters
|
||||
# Copyright (c) 2025 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Setup
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
add_library(dawn STATIC
|
||||
src/funcs.c
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
|
||||
|
||||
if(NOT DEFINED DAWN_TARGET_SYSTEM)
|
||||
set(DAWN_TARGET_SYSTEM "linux")
|
||||
# set(DAWN_TARGET_SYSTEM "psp")
|
||||
endif()
|
||||
|
||||
# Prep cache
|
||||
set(DAWN_CACHE_TARGET "dusk-target")
|
||||
|
||||
# Build variables
|
||||
set(DAWN_ROOT_DIR "${CMAKE_SOURCE_DIR}")
|
||||
set(DAWN_BUILD_DIR "${CMAKE_BINARY_DIR}")
|
||||
set(DAWN_SOURCES_DIR "${DAWN_ROOT_DIR}/src")
|
||||
set(DAWN_TEMP_DIR "${DAWN_BUILD_DIR}/temp")
|
||||
set(DAWN_TOOLS_DIR "${DAWN_ROOT_DIR}/tools")
|
||||
set(DAWN_DATA_DIR "${DAWN_ROOT_DIR}/data")
|
||||
set(DAWN_ASSETS_DIR "${DAWN_ROOT_DIR}/assets")
|
||||
set(DAWN_BUILT_ASSETS_DIR "${DAWN_BUILD_DIR}/built_assets" CACHE INTERNAL ${DAWN_CACHE_TARGET})
|
||||
set(DAWN_GENERATED_HEADERS_DIR "${DAWN_BUILD_DIR}/generated")
|
||||
set(DAWN_TARGET_NAME "Dusk" CACHE INTERNAL ${DAWN_CACHE_TARGET})
|
||||
set(DAWN_BUILD_BINARY ${DAWN_BUILD_DIR}/Dusk CACHE INTERNAL ${DAWN_CACHE_TARGET})
|
||||
set(DAWN_ASSETS "" CACHE INTERNAL ${DAWN_CACHE_TARGET})
|
||||
|
||||
# Create directories
|
||||
file(MAKE_DIRECTORY ${DAWN_GENERATED_HEADERS_DIR})
|
||||
|
||||
# Find packages
|
||||
find_package(Python3 COMPONENTS Interpreter REQUIRED)
|
||||
|
||||
# Toolchains
|
||||
if(DAWN_TARGET_SYSTEM STREQUAL "psp")
|
||||
find_package(pspsdk REQUIRED)
|
||||
endif()
|
||||
|
||||
# Init Project
|
||||
project(${DAWN_TARGET_NAME}
|
||||
VERSION 1.0.0
|
||||
LANGUAGES C CXX
|
||||
)
|
||||
|
||||
target_include_directories(dawn PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
|
||||
# Executable
|
||||
add_executable(${DAWN_TARGET_NAME})
|
||||
|
||||
# Add tools
|
||||
# add_subdirectory(tools)
|
||||
|
||||
# Assets
|
||||
# add_subdirectory(assets)
|
||||
|
||||
# Add libraries
|
||||
if(DAWN_TARGET_SYSTEM STREQUAL "linux")
|
||||
find_package(SDL2 REQUIRED)
|
||||
find_package(OpenGL REQUIRED)
|
||||
target_link_libraries(${DAWN_TARGET_NAME} PRIVATE
|
||||
SDL2
|
||||
OpenGL::GL
|
||||
GL
|
||||
)
|
||||
|
||||
elseif(DAWN_TARGET_SYSTEM STREQUAL "psp")
|
||||
find_package(SDL2 REQUIRED)
|
||||
find_package(OpenGL REQUIRED)
|
||||
target_link_libraries(${DAWN_TARGET_NAME} PUBLIC
|
||||
${SDL2_LIBRARIES}
|
||||
SDL2
|
||||
OpenGL::GL
|
||||
zip
|
||||
bz2
|
||||
z
|
||||
mbedtls
|
||||
mbedcrypto
|
||||
lzma
|
||||
)
|
||||
target_include_directories(${DAWN_TARGET_NAME} PRIVATE
|
||||
${SDL2_INCLUDE_DIRS}
|
||||
)
|
||||
endif()
|
||||
|
||||
# Add code
|
||||
add_subdirectory(src)
|
||||
|
||||
# Include generated headers
|
||||
target_include_directories(${DAWN_TARGET_NAME} PUBLIC
|
||||
${DAWN_GENERATED_HEADERS_DIR}
|
||||
)
|
||||
|
||||
# # Build assets
|
||||
# add_custom_target(DAWN_ASSETS_BUILT ALL
|
||||
# COMMAND
|
||||
# ${Python3_EXECUTABLE} ${DAWN_TOOLS_DIR}/assetstool/main.py
|
||||
# --assets ${DAWN_ASSETS_DIR}
|
||||
# --build-type wad
|
||||
# --output-assets ${DAWN_BUILT_ASSETS_DIR}
|
||||
# --output-file ${DAWN_BUILD_DIR}/dusk.dsk
|
||||
# --output-headers ${DAWN_GENERATED_HEADERS_DIR}/asset/assetbundle.h
|
||||
# --headers-dir ${DAWN_GENERATED_HEADERS_DIR}
|
||||
# --input ${DAWN_ASSETS}
|
||||
# )
|
||||
# add_dependencies(${DAWN_TARGET_NAME} DAWN_ASSETS_BUILT)
|
||||
|
||||
# # Postbuild
|
||||
# if(DAWN_TARGET_SYSTEM STREQUAL "psp")
|
||||
# create_pbp_file(
|
||||
# TARGET "${DAWN_TARGET_NAME}"
|
||||
# ICON_PATH NULL
|
||||
# BACKGROUND_PATH NULL
|
||||
# PREVIEW_PATH NULL
|
||||
# TITLE "${DAWN_TARGET_NAME}"
|
||||
# VERSION 01.00
|
||||
# )
|
||||
# endif()
|
14
cmake/modules/Findglm.cmake
Normal file
14
cmake/modules/Findglm.cmake
Normal file
@@ -0,0 +1,14 @@
|
||||
# Copyright (c) 2025 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
include(FetchContent)
|
||||
FetchContent_Declare(
|
||||
glm
|
||||
GIT_REPOSITORY https://github.com/g-truc/glm.git
|
||||
GIT_TAG 1.0.1
|
||||
)
|
||||
|
||||
FetchContent_MakeAvailable(glm)
|
||||
set(glm_FOUND ON)
|
106
cmake/modules/Findpspsdk.cmake
Normal file
106
cmake/modules/Findpspsdk.cmake
Normal file
@@ -0,0 +1,106 @@
|
||||
# Copyright (c) 2025 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
if(NOT TARGET pspsdk)
|
||||
message(STATUS "Looking for PSPSDK...")
|
||||
|
||||
set(PSPSDK_FOUND FALSE CACHE INTERNAL "PSPSDK found")
|
||||
set(PSPSDK_DOWNLOAD_DIR "${CMAKE_BINARY_DIR}/_pspsdk")
|
||||
set(PSPSDK_SEARCH_ROOTS
|
||||
"${PSPSDK_ROOT}"
|
||||
"$ENV{PSPDEV}"
|
||||
"$ENV{HOME}/pspdev"
|
||||
"/usr/local/pspdev"
|
||||
"/opt/pspdev"
|
||||
"/usr/pspdev"
|
||||
"${PSPSDK_DOWNLOAD_DIR}/pspdev"
|
||||
)
|
||||
|
||||
foreach(root IN LISTS PSPSDK_SEARCH_ROOTS)
|
||||
list(APPEND PSPSDK_BIN_HINTS "${root}/bin")
|
||||
list(APPEND PSPSDK_INCLUDE_HINTS "${root}/include")
|
||||
list(APPEND PSPSDK_LIB_HINTS "${root}/lib")
|
||||
endforeach()
|
||||
|
||||
# Find PSP GCC
|
||||
find_program(PSPSDK_PSP_GCC NAMES psp-gcc HINTS ${PSPSDK_BIN_HINTS})
|
||||
|
||||
# If we did not find it, download it.
|
||||
if(NOT PSPSDK_PSP_GCC)
|
||||
message(STATUS "psp-gcc not found in system paths. Downloading PSPSDK tarball...")
|
||||
file(DOWNLOAD
|
||||
"https://github.com/pspdev/pspdev/releases/latest/download/pspdev-ubuntu-latest-x86_64.tar.gz"
|
||||
"${CMAKE_BINARY_DIR}/pspsdk.tar.gz"
|
||||
EXPECTED_HASH SHA256=2befe2ad83afd88934c106dbe98a72a7ad5ede8d272b7f1e79fda256a22f1062
|
||||
SHOW_PROGRESS
|
||||
)
|
||||
|
||||
# Make output dir
|
||||
file(MAKE_DIRECTORY "${PSPSDK_DOWNLOAD_DIR}")
|
||||
|
||||
# Extract the tarball
|
||||
execute_process(
|
||||
COMMAND
|
||||
${CMAKE_COMMAND} -E tar xzf "${CMAKE_BINARY_DIR}/pspsdk.tar.gz"
|
||||
WORKING_DIRECTORY
|
||||
"${PSPSDK_DOWNLOAD_DIR}"
|
||||
RESULT_VARIABLE
|
||||
tar_result
|
||||
)
|
||||
if(NOT tar_result EQUAL 0)
|
||||
message(FATAL_ERROR "Failed to extract PSPSDK tarball")
|
||||
endif()
|
||||
|
||||
# Retry discovery with extracted fallback
|
||||
find_program(PSPSDK_PSP_GCC NAMES psp-gcc HINTS ${PSPSDK_BIN_HINTS})
|
||||
endif()
|
||||
|
||||
if(PSPSDK_PSP_GCC)
|
||||
get_filename_component(PSPSDK_BIN_ROOT "${PSPSDK_PSP_GCC}" DIRECTORY)
|
||||
get_filename_component(PSPSDK_ROOT "${PSPSDK_BIN_ROOT}" DIRECTORY)
|
||||
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
|
||||
set(ENV{PSPDEV} "${PSPSDK_ROOT}")
|
||||
set(CMAKE_TOOLCHAIN_FILE "${PSPSDK_ROOT}/psp/share/pspdev.cmake")
|
||||
set(BUILD_PRX ON CACHE BOOL "Build PRX modules")
|
||||
|
||||
include("${PSPSDK_ROOT}/psp/share/pspdev.cmake")
|
||||
set(CMAKE_C_COMPILER ${PSPSDK_BIN_ROOT}/psp-gcc)
|
||||
set(CMAKE_CXX_COMPILER ${PSPSDK_BIN_ROOT}/psp-g++)
|
||||
|
||||
if(NOT DEFINED PSP)
|
||||
message(FATAL_ERROR "PSP environment variable is not set correctly.")
|
||||
endif()
|
||||
|
||||
add_library(pspsdk INTERFACE IMPORTED)
|
||||
set_target_properties(pspsdk PROPERTIES
|
||||
INTERFACE_INCLUDE_DIRECTORIES "${PSPSDK_INCLUDE_DIR}"
|
||||
INTERFACE_LINK_DIRECTORIES "${PSPSDK_LIB_DIR}"
|
||||
)
|
||||
target_include_directories(pspsdk
|
||||
INTERFACE
|
||||
${PSPDEV}/psp/include
|
||||
${PSPDEV}/psp/sdk/include
|
||||
)
|
||||
target_link_directories(pspsdk
|
||||
INTERFACE
|
||||
${PSPDEV}/lib
|
||||
${PSPDEV}/psp/lib
|
||||
${PSPDEV}/psp/sdk/lib
|
||||
)
|
||||
target_link_libraries(pspsdk INTERFACE
|
||||
pspdebug
|
||||
pspdisplay
|
||||
pspge
|
||||
pspctrl
|
||||
pspgu
|
||||
pspaudio
|
||||
pspaudiolib
|
||||
psputility
|
||||
pspvfpu
|
||||
pspvram
|
||||
psphprm
|
||||
)
|
||||
endif()
|
||||
endif()
|
33
src/CMakeLists.txt
Normal file
33
src/CMakeLists.txt
Normal 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
85
src/assert/Assert.cpp
Normal 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
143
src/assert/Assert.hpp
Normal 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
10
src/assert/CMakeLists.txt
Normal 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
|
||||
)
|
10
src/console/CMakeLists.txt
Normal file
10
src/console/CMakeLists.txt
Normal 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
146
src/console/Console.cpp
Normal 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
72
src/console/Console.hpp
Normal 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
48
src/dawn.hpp
Normal 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
10
src/engine/CMakeLists.txt
Normal 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
32
src/engine/Engine.cpp
Normal 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
31
src/engine/Engine.hpp
Normal 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();
|
||||
};
|
||||
}
|
@@ -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);
|
||||
}
|
16
src/funcs.h
16
src/funcs.h
@@ -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
23
src/main.cpp
Normal 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
10
src/time/CMakeLists.txt
Normal 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
41
src/time/Time.cpp
Normal 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
35
src/time/Time.hpp
Normal 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);
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user