From 97aed4e5ca5ebbef416d230e0c1982c181ace0cc Mon Sep 17 00:00:00 2001 From: NaifBanana <30419422+NaifBanana@users.noreply.github.com> Date: Mon, 19 Oct 2026 03:09:35 -0500 Subject: [PATCH] Honestly fuck if i know atp --- .gitignore | 7 ++- CMakeLists.txt | 45 +++++--------- engine/NBCore/CMakeLists.txt | 5 +- engine/NBCore/include/NBCore/DataTypes.hpp | 71 ++++++++++++++++++++++ engine/NBCore/include/NBCore/Utils.hpp | 6 +- engine/NBCore/tests/CMakeLists.txt | 39 ++++++------ engine/NBCore/tests/testUtils.cpp | 62 +++++++------------ 7 files changed, 137 insertions(+), 98 deletions(-) create mode 100644 engine/NBCore/include/NBCore/DataTypes.hpp diff --git a/.gitignore b/.gitignore index 93e3b49..90e6a2c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,10 @@ build/ -.vscode/ +.* +!.gitignore +!.clangd CMakeFiles/ +CMakeKits.json CMakeCache.txt docs/* !docs/CMake* !docs/Doxyfile.in -!.clangd -.glsld \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f84f4e..2eafbf5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,45 +28,32 @@ function(toAbsolutePath SETVAR PATHS) set(${SETVAR} ${RET_VAL} PARENT_SCOPE) endfunction() +if (NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE "Debug") +endif() + if(CMAKE_BUILD_TYPE STREQUAL "Release") - message(STATUS "Targeting Release build") + add_compile_definitions(_NB_BUILD_RELEASE) elseif(CMAKE_BUILD_TYPE STREQUAL "Debug") - message(STATUS "Targeting Debug build") set(NB_DEBUG_BUILD ON) add_compile_definitions(_NB_BUILD_DEBUG) endif() +message(STATUS "Targeting ${CMAKE_BUILD_TYPE} build") + +if (NB_DEBUG_BUILD) + set(NB_LOGGING ON) + set(NB_BUILD_TESTS ON) + set(NB_BUILD_DOCS ON) + set(NB_MONITOR_OPENGL_CALLS ON) + set(CMAKE_EXPORT_COMPILE_COMMANDS 1) +endif() -cmake_dependent_option(NB_LOGGING - "Creates a default logger and automatically logs with code locations." - ON - NB_DEBUG_BUILD - OFF -) -cmake_dependent_option(NB_BUILD_TESTS - "Build unit tests" - ON - NB_DEBUG_BUILD - OFF -) -cmake_dependent_option(NB_BUILD_DOCS - "Build documentation" - ON - NB_DEBUG_BUILD - OFF -) cmake_dependent_option(NBENGINE_INSTALL "Install NBEngine" ON NB_DEBUG_BUILD OFF ) -cmake_dependent_option(NB_MONITOR_OPENGL_CALLS - "Monitors every OpenGL call for errors" - ON - NB_DEBUG_BUILD - OFF -) - if(CMAKE_SYSTEM_NAME STREQUAL "Windows") message(STATUS "Building for Windows") @@ -106,13 +93,13 @@ endif() get_filename_component(NBENGINE_ROOT ${CMAKE_CURRENT_LIST_DIR} ABSOLUTE) -add_subdirectory(./engine ${PROJECT_BINARY_DIR}/${CMAKE_BUILD_TYPE}) +add_subdirectory(./engine) # Toggle set(NB_REBUILD_DOCS) if (NB_BUILD_DOCS) - add_subdirectory(./docs) + add_subdirectory(./docs) endif() include(CMakePackageConfigHelpers) diff --git a/engine/NBCore/CMakeLists.txt b/engine/NBCore/CMakeLists.txt index c73ff1e..a25b7f3 100644 --- a/engine/NBCore/CMakeLists.txt +++ b/engine/NBCore/CMakeLists.txt @@ -18,7 +18,8 @@ toAbsolutePath(NB_CORE_SOURCE ) toAbsolutePath(NB_CORE_INCLUDE ./include/NBCore/DataSink.hpp - ./include/NBCore/Errors.hpp + ./include/NBCore/DataTypes.hpp + ./include/NBCore/Errors.hpp ./include/NBCore/ErrorsImpl.hpp ./include/NBCore/Logger.hpp ./include/NBCore/Printer.hpp @@ -83,4 +84,4 @@ endif() if (NB_BUILD_TESTS) add_subdirectory(./tests ) -endif() \ No newline at end of file +endif() diff --git a/engine/NBCore/include/NBCore/DataTypes.hpp b/engine/NBCore/include/NBCore/DataTypes.hpp new file mode 100644 index 0000000..df6640e --- /dev/null +++ b/engine/NBCore/include/NBCore/DataTypes.hpp @@ -0,0 +1,71 @@ +#pragma once +#include +#ifndef _NB_DATA_TYPES +#define _NB_DATA_TYPES + +#include +#include +#include + +namespace nb { + +template +class NDArray { + protected: + std::unique_ptr _data; + static unsigned int calculateTotalSize(const std::array& dims) { + unsigned int ret = 1; + std::for_each(dims.begin(), dims.end(), [&](unsigned int n){ + ret *= n; + }); + return ret; + } + + public: + const std::array dimensions; + const unsigned int totalSize; + static constexpr unsigned int NumberDimensions = N; + NDArray( + const std::array& dimensions_, + T* data_=nullptr + ) : + dimensions(dimensions_), + totalSize(calculateTotalSize(dimensions_)) + { + if (data_) { data(data_); } + } + NDArray( + const std::array& dimensions_, + const T* data_ + ) : NDArray(dimensions_) { data(data_); } + + unsigned int coordinateToIndex(const std::array& coord) const { + unsigned int blockSize = 1; + unsigned int ret = 0; + for (auto i = 0; i < dimensions.size(); ++i) { + ret += coord[i] * blockSize; + blockSize *= dimensions[i]; + } + return ret; + } + + inline const T& at(const std::array& coord) const { + return _data[coordinateToIndex(coord)]; + } + inline std::shared_ptr data() const { return _data; } + + inline T& at(const std::array& coord) { + return _data[coordinateToIndex(coord)]; + } + inline T* data(const T* data_) { + _data = std::make_unique(totalSize); + std::memcpy(_data.get(), data_, totalSize * sizeof(T)); + return _data.get(); + } + + + +}; // namespace nb + +} +#endif // _NB_DATA_TYPES diff --git a/engine/NBCore/include/NBCore/Utils.hpp b/engine/NBCore/include/NBCore/Utils.hpp index 19e046a..e0c8e1d 100644 --- a/engine/NBCore/include/NBCore/Utils.hpp +++ b/engine/NBCore/include/NBCore/Utils.hpp @@ -1,6 +1,6 @@ #pragma once -#ifndef _NB_CORE_TYPES -#define _NB_CORE_TYPES +#ifndef _NB_UTILTIES +#define _NB_UTILTIES #include #include @@ -131,4 +131,4 @@ class ThreadsafeObjectLock { }; */ } // namespace nb -#endif // _NB_CORE_TYPES \ No newline at end of file +#endif // _NB_CORE_TYPES diff --git a/engine/NBCore/tests/CMakeLists.txt b/engine/NBCore/tests/CMakeLists.txt index 34476ff..030154d 100644 --- a/engine/NBCore/tests/CMakeLists.txt +++ b/engine/NBCore/tests/CMakeLists.txt @@ -1,24 +1,23 @@ cmake_minimum_required(VERSION 3.26.0) -if (NB_BUILD_TESTS) - enable_testing() - include(GoogleTest) - add_executable(TestCore - ./testErrors.cpp - #./testProcesses.cpp - ./testUtils.cpp - ) - target_link_libraries(TestCore - NBCore - GTest::gtest_main - ) +enable_testing() +include(GoogleTest) +add_executable(TestCore + ./testErrors.cpp + #./testProcesses.cpp + ./testUtils.cpp +) +target_link_libraries(TestCore + NBCore + GTest::gtest_main +) - add_executable(LoggerTest - ./testLogger.cpp - ) - target_link_libraries(LoggerTest - NBCore - ) +add_executable(LoggerTest + ./testLogger.cpp +) +target_link_libraries(LoggerTest + NBCore +) + +gtest_discover_tests(TestCore) - gtest_discover_tests(TestCore) -endif() \ No newline at end of file diff --git a/engine/NBCore/tests/testUtils.cpp b/engine/NBCore/tests/testUtils.cpp index 0244428..2d927f4 100644 --- a/engine/NBCore/tests/testUtils.cpp +++ b/engine/NBCore/tests/testUtils.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -22,7 +23,7 @@ TEST(UtilsTest, TestFindAndReplace) { tmp = find_and_replace("aeiou", "a", "afa"); tmp = find_and_replace(tmp, "e", "efe"); - tmp = find_and_replace(tmp, "i", "ifi"); + tmp = find_and_replace(tmp, "i", "ifi"); tmp = find_and_replace(tmp, "o", "ofo"); tmp = find_and_replace(tmp, "u", "ufu"); ASSERT_STREQ( @@ -46,7 +47,6 @@ TEST(UtilsTest, TestFindAndReplaceWstr) { find_and_replace(L"Jeff", L"e", L"efe").c_str(), L"Jefeff" ); - std::wstring tmp = find_and_replace(L"Naif", L"a", L"afa"); ASSERT_STREQ( find_and_replace(tmp, L"i", L"ifi").c_str(), @@ -74,44 +74,6 @@ TEST(UtilsTest, TestFindAndReplaceWstr) { ); } -/* TEST(UtilsTest, TestWstrToStr) { - ASSERT_STREQ( - nb::wstr_to_str(L"Hi!").c_str(), - "Hi!" - ); - ASSERT_STREQ( - nb::wstr_to_str(L"Naif").c_str(), - "Naif" - ); - ASSERT_STREQ( - nb::wstr_to_str(L"\r\r\r\n").c_str(), - "\r\r\r\n" - ); - ASSERT_STREQ( - nb::wstr_to_str(L"Naif\ttalks\r\ra\t\nlot").c_str(), - "Naif\ttalks\r\ra\t\nlot" - ); -} */ - -/* TEST(UtilsTest, TestStrToWstr) { - ASSERT_STREQ( - nb::str_to_wstr("Hi!").c_str(), - L"Hi!" - ); - ASSERT_STREQ( - nb::str_to_wstr("Naif").c_str(), - L"Naif" - ); - ASSERT_STREQ( - nb::str_to_wstr("\r\r\r\n").c_str(), - L"\r\r\r\n" - ); - ASSERT_STREQ( - nb::str_to_wstr("Naif\ttalks\r\ra\t\nlot").c_str(), - L"Naif\ttalks\r\ra\t\nlot" - ); -} */ - struct A { bool x(); }; struct B { bool y(); }; struct C { bool x(); bool y; }; @@ -145,4 +107,22 @@ TEST(UtilsTest, TestIsDetected) { ASSERT_TRUE(ret); } -} // namespace nb \ No newline at end of file +TEST(DataTypesTests, TestNDArray) { + float* data_f = new float[9] {0,1,2,3,4,5,6,7,8}; + nb::NDArray arr({3, 3}, data_f); + ASSERT_FLOAT_EQ(data_f[4], arr.at({1,1})); + arr.at({1,1}) = 0.5; + ASSERT_FLOAT_EQ(0.5, arr.at({1,1})); + delete[] data_f; + + unsigned int* data_ui = new unsigned int[8] {0,1,2,3,4,5,6,7}; + nb::NDArray arr3d({2,2,2}, data_ui); + ASSERT_EQ(data_ui[5], arr3d.at({1,0,1})); + arr3d.at({0,1,1}) = 23; + ASSERT_EQ(23, arr3d.at({0,1,1})); + delete[] data_ui; + + +} + +} // namespace nb