Compare commits
14 Commits
684d09a09e
...
1efe2a0a7c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1efe2a0a7c | ||
|
|
ea632614b0 | ||
|
|
3048d524b7 | ||
|
|
7648a77e1a | ||
|
|
cff7164d27 | ||
|
|
13ef940b2d | ||
|
|
58121bdf28 | ||
|
|
0f62aecfa3 | ||
|
|
40cf079e9d | ||
|
|
fb695798e7 | ||
|
|
e1c456bfe4 | ||
|
|
e23e062e58 | ||
|
|
fd7d36e4be | ||
|
|
0b473c0f8a |
@ -48,13 +48,12 @@ if(NB_BUILD_TESTS)
|
|||||||
include(FetchContent)
|
include(FetchContent)
|
||||||
FetchContent_Declare(
|
FetchContent_Declare(
|
||||||
gtest
|
gtest
|
||||||
URL https://github.com/google/googletest/archive/refs/tags/release-1.12.1.zip
|
URL https://github.com/google/googletest/archive/refs/heads/main.zip
|
||||||
)
|
)
|
||||||
if (WIN32)
|
if (WIN32)
|
||||||
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
|
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
|
||||||
FetchContent_MakeAvailable(gtest)
|
FetchContent_MakeAvailable(gtest)
|
||||||
endif()
|
endif()
|
||||||
include(GoogleTest)
|
|
||||||
set(GTEST_COLOR ON)
|
set(GTEST_COLOR ON)
|
||||||
set(GLFW_BUILD_TESTS ON CACHE BOOL "" FORCE)
|
set(GLFW_BUILD_TESTS ON CACHE BOOL "" FORCE)
|
||||||
endif()
|
endif()
|
||||||
@ -62,6 +61,7 @@ endif()
|
|||||||
if (NB_LOGGING)
|
if (NB_LOGGING)
|
||||||
message(STATUS "Building with automatic logging")
|
message(STATUS "Building with automatic logging")
|
||||||
add_compile_definitions(_NB_AUTOLOG)
|
add_compile_definitions(_NB_AUTOLOG)
|
||||||
|
add_compile_definitions(_NB_CODE_ERROR_LOCATIONS)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if (NB_TARGET_WINDOWS)
|
if (NB_TARGET_WINDOWS)
|
||||||
|
|||||||
@ -2,16 +2,10 @@
|
|||||||
#ifndef _NB_ANSI_TERM
|
#ifndef _NB_ANSI_TERM
|
||||||
#define _NB_ANSI_TERM
|
#define _NB_ANSI_TERM
|
||||||
|
|
||||||
#include <array>
|
|
||||||
#include <cmath>
|
|
||||||
#include <iomanip>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <tuple>
|
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
#include "TypeTraits.hpp"
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
----------- TECH DEBT ------------
|
----------- TECH DEBT ------------
|
||||||
Idk wtf to do here. This was originally to allow me to print
|
Idk wtf to do here. This was originally to allow me to print
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
include_directories(./.)
|
|
||||||
|
|
||||||
toAbsolutePath(NB_CORE_SOURCE
|
toAbsolutePath(NB_CORE_SOURCE
|
||||||
./src/Errors.cpp
|
./src/Errors.cpp
|
||||||
./src/Processes.cpp
|
|
||||||
./src/Logger.cpp
|
./src/Logger.cpp
|
||||||
|
./src/Processes.cpp
|
||||||
|
./src/StringUtils.cpp
|
||||||
|
./src/Utils.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
toAbsolutePath(NB_CORE_INCLUDE
|
toAbsolutePath(NB_CORE_INCLUDE
|
||||||
@ -12,9 +12,11 @@ toAbsolutePath(NB_CORE_INCLUDE
|
|||||||
./Errors.hpp
|
./Errors.hpp
|
||||||
./Logger.hpp
|
./Logger.hpp
|
||||||
./Processes.hpp
|
./Processes.hpp
|
||||||
|
./StringUtils.hpp
|
||||||
./ThreadsafeQueue.hpp
|
./ThreadsafeQueue.hpp
|
||||||
./Types.hpp
|
./Types.hpp
|
||||||
./TypeTraits.hpp
|
./TypeTraits.hpp
|
||||||
|
./Utils.hpp
|
||||||
)
|
)
|
||||||
|
|
||||||
set(NB_CORE_SOURCE ${NB_CORE_SOURCE} PARENT_SCOPE)
|
set(NB_CORE_SOURCE ${NB_CORE_SOURCE} PARENT_SCOPE)
|
||||||
@ -22,6 +24,8 @@ set(NB_CORE_INCLUDE ${NB_CORE_INCLUDE} PARENT_SCOPE)
|
|||||||
|
|
||||||
add_library(NBCore ${NB_CORE_SOURCE})
|
add_library(NBCore ${NB_CORE_SOURCE})
|
||||||
|
|
||||||
|
target_include_directories(NBCore PUBLIC ./.)
|
||||||
|
|
||||||
if (NB_BUILD_TESTS)
|
if (NB_BUILD_TESTS)
|
||||||
add_subdirectory(./tests)
|
add_subdirectory(./tests)
|
||||||
endif()
|
endif()
|
||||||
@ -2,6 +2,7 @@
|
|||||||
#ifndef _NB_DATASINK
|
#ifndef _NB_DATASINK
|
||||||
#define _NB_DATASINK
|
#define _NB_DATASINK
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
#include "ThreadSafeQueue.hpp"
|
#include "ThreadSafeQueue.hpp"
|
||||||
@ -34,9 +35,9 @@ class BufferedDataProcessor : public DataSink<DataType> {
|
|||||||
public:
|
public:
|
||||||
using Base::Base;
|
using Base::Base;
|
||||||
|
|
||||||
bool stop() noexcept { return type_ptr->stop(); }
|
virtual bool stop() noexcept override { return type_ptr->stop(); }
|
||||||
bool run() { return type_ptr->run(); }
|
virtual bool run() override { return type_ptr->run(); }
|
||||||
bool in(const DataType& val) { return type_ptr->in(val); }
|
virtual bool in(const DataType& val) override { return type_ptr->in(val); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
unsigned int count() const {
|
unsigned int count() const {
|
||||||
@ -77,7 +78,7 @@ public:
|
|||||||
bool isRunning() const noexcept override {
|
bool isRunning() const noexcept override {
|
||||||
return this->_running && (_runningThread!=nullptr);
|
return this->_running && (_runningThread!=nullptr);
|
||||||
}
|
}
|
||||||
bool run() {
|
bool run() override {
|
||||||
if (!type_ptr->isRunning()) {
|
if (!type_ptr->isRunning()) {
|
||||||
this->_running = true;
|
this->_running = true;
|
||||||
_runningThread = std::make_shared<std::thread>([&]{
|
_runningThread = std::make_shared<std::thread>([&]{
|
||||||
|
|||||||
@ -8,132 +8,305 @@
|
|||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
#ifndef THROW_WITH_INFO
|
#include "Logger.hpp"
|
||||||
#ifdef CODE_ERROR_LOCATIONS
|
#include "StringUtils.hpp"
|
||||||
#define THROW_WITH_INFO(type, ...) throw type(__VA_ARGS__, __LINE__, __FILE__)
|
#include "TypeTraits.hpp"
|
||||||
#else
|
|
||||||
#define THROW_WITH_INFO(type, ...) throw type(__VA_ARGS__)
|
|
||||||
#endif // CODE_ERROR_LOCATIONS
|
|
||||||
#endif // THROW_WITH_INFO
|
|
||||||
|
|
||||||
#ifndef THROW
|
|
||||||
#ifdef LOGGING
|
|
||||||
#define THROW_WTIH_INFO(type, ...) throw type(__VA_ARGS__, __LINE__, __FILE__)
|
|
||||||
#else
|
|
||||||
#define THROW(...) THROW_WITH_INFO(__VA_ARGS__)
|
|
||||||
#endif // LOGGING
|
|
||||||
#endif // THROW
|
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
typedef std::unordered_map<unsigned int, const char*> ErrorCodeMap;
|
typedef std::unordered_map<unsigned int, const char*> ErrorCodeMap;
|
||||||
|
|
||||||
template<class ErrorType>
|
template<typename T>
|
||||||
class ErrorBase : public std::exception {
|
constexpr bool IsValidException_v = std::is_base_of_v<std::exception, T>;
|
||||||
public:
|
template <typename T>
|
||||||
ErrorBase(
|
using IsValidException = std::enable_if_t<IsValidException_v<T>, bool>;
|
||||||
const unsigned int code,
|
|
||||||
unsigned int line=0,
|
|
||||||
std::string filename=""
|
|
||||||
) noexcept : ErrorBase(
|
|
||||||
code,
|
|
||||||
ErrorBase<ErrorType>::lookup(code),
|
|
||||||
line,
|
|
||||||
filename
|
|
||||||
) {}
|
|
||||||
ErrorBase(
|
|
||||||
const unsigned int code,
|
|
||||||
const std::exception& trace,
|
|
||||||
unsigned int line=0,
|
|
||||||
std::string filename=""
|
|
||||||
) noexcept : ErrorBase(
|
|
||||||
code,
|
|
||||||
ErrorBase<ErrorType>::lookup(code),
|
|
||||||
trace,
|
|
||||||
line,
|
|
||||||
filename
|
|
||||||
) {}
|
|
||||||
|
|
||||||
static std::string lookup(unsigned int);
|
template<class ErrorType=NoneType>
|
||||||
unsigned int code() const noexcept {
|
class ErrorBase;
|
||||||
return static_cast<const ErrorType*>(this)->_code;
|
|
||||||
};
|
template <class ErrorType=NoneType>
|
||||||
virtual const char* what() const noexcept override final { return _msg.c_str(); };
|
class Error;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool IsValidNBError_v = std::is_base_of_v<ErrorBase<NoneType>, T>;
|
||||||
|
template <typename T>
|
||||||
|
using IsValidNBError = std::enable_if_t<IsValidNBError_v<T>, bool>;
|
||||||
|
|
||||||
|
|
||||||
|
template<>
|
||||||
|
class ErrorBase<NoneType> {
|
||||||
|
public:
|
||||||
|
const unsigned int code;
|
||||||
|
const std::string msg;
|
||||||
|
const std::string type;
|
||||||
|
const std::shared_ptr<const void> trace;
|
||||||
|
const bool traceIsNBError;
|
||||||
|
|
||||||
|
virtual std::string what() const noexcept = 0;
|
||||||
|
virtual std::shared_ptr<ErrorBase> make_shared() const noexcept = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
ErrorBase() = default;
|
template<typename T=NoneType>
|
||||||
|
ErrorBase(const ErrorBase<T>& err) : ErrorBase(std::move(err)) {}
|
||||||
|
|
||||||
|
template<typename T=NoneType>
|
||||||
|
ErrorBase(ErrorBase<T>&& err) : ErrorBase(
|
||||||
|
err.code,
|
||||||
|
err.msg,
|
||||||
|
err.type,
|
||||||
|
(err.traceIsNBError)
|
||||||
|
? static_cast<const ErrorBase*>(err.trace.get())
|
||||||
|
: static_cast<const std::exception*>(err.trace.get())
|
||||||
|
) {}
|
||||||
|
|
||||||
ErrorBase(
|
ErrorBase(
|
||||||
const unsigned int code,
|
unsigned int code_,
|
||||||
std::string msg,
|
std::string msg_,
|
||||||
unsigned int line=0,
|
std::string type_,
|
||||||
std::string filename=""
|
const std::exception* trace_
|
||||||
) noexcept : _code{code}, _msg{""} {
|
) noexcept :
|
||||||
|
code(code_),
|
||||||
|
msg(msg_),
|
||||||
|
type(type_),
|
||||||
|
trace{ trace_ ?
|
||||||
|
std::static_pointer_cast<const void>(
|
||||||
|
std::make_shared<Error<NoneType>>(*trace_))
|
||||||
|
: nullptr,
|
||||||
|
},
|
||||||
|
traceIsNBError(false)
|
||||||
|
{}
|
||||||
|
|
||||||
|
template<typename T, std::enable_if_t<IsValidNBError_v<T>, bool> = true>
|
||||||
|
ErrorBase(
|
||||||
|
unsigned int code_,
|
||||||
|
std::string msg_,
|
||||||
|
std::string type_,
|
||||||
|
const T* trace_
|
||||||
|
) noexcept :
|
||||||
|
code(code_),
|
||||||
|
msg(msg_),
|
||||||
|
type(type_),
|
||||||
|
trace{trace_ ? trace_->make_shared() : nullptr},
|
||||||
|
traceIsNBError(true)
|
||||||
|
{}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template <class ErrorType>
|
||||||
|
class ErrorBase : public ErrorBase<NoneType> {
|
||||||
|
private:
|
||||||
|
using Base = ErrorBase<NoneType>;
|
||||||
|
void inline check_asserts() {
|
||||||
static_assert(std::is_same<const std::unordered_map<unsigned int, const char*>, decltype(ErrorType::ErrorMessages)>::value,
|
static_assert(std::is_same<const std::unordered_map<unsigned int, const char*>, decltype(ErrorType::ErrorMessages)>::value,
|
||||||
"const std::unordered_map<unsigned int, const char*> ErrorMessages must be "
|
"const std::unordered_map<unsigned int, const char*> ErrorMessages must be "
|
||||||
"a class member."
|
"a class member."
|
||||||
);
|
);
|
||||||
static_assert(std::is_enum_v<typename ErrorType::ErrorCodes>, "enum ErrorCodes must be a class member.");
|
static_assert(std::is_enum_v<typename ErrorType::Codes>, "enum Codes must be a class member.");
|
||||||
static_assert(std::is_same<std::underlying_type_t<typename ErrorType::ErrorCodes>, unsigned int>::value,
|
static_assert(std::is_same<std::underlying_type_t<typename ErrorType::Codes>, unsigned int>::value,
|
||||||
"enum ErrorCodes must be of underlying type unsigned int."
|
"enum Codes must be of underlying type unsigned int."
|
||||||
);
|
);
|
||||||
static_assert(std::is_same<const std::string, decltype(ErrorType::type)>::value,
|
static_assert(std::is_same<const std::string, decltype(ErrorType::type)>::value,
|
||||||
"const std::string type must be a class member."
|
"const std::string type must be a class member."
|
||||||
);
|
);
|
||||||
|
|
||||||
_msg += std::string(ErrorType::type);
|
|
||||||
_msg += "[" + std::to_string(_code) + "]";
|
|
||||||
if (line && filename.size()) {
|
|
||||||
_msg += " in \'" + filename + "\':" + std::to_string(line);
|
|
||||||
}
|
|
||||||
_msg += "\n " + msg;
|
|
||||||
}
|
|
||||||
ErrorBase(
|
|
||||||
const unsigned int code,
|
|
||||||
std::string msg,
|
|
||||||
const std::exception& trace,
|
|
||||||
unsigned int line=0,
|
|
||||||
std::string filename=""
|
|
||||||
) noexcept : ErrorBase(code, msg, line, filename) {
|
|
||||||
std::string what_msg(trace.what());
|
|
||||||
std::string::size_type newline_pos=-1;
|
|
||||||
while((newline_pos=what_msg.find("\n", newline_pos+1))!= std::string::npos) {
|
|
||||||
what_msg.replace(newline_pos, 1, "\n ");
|
|
||||||
}
|
|
||||||
_msg += "\n Trace - " + what_msg;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int _code;
|
|
||||||
std::string _msg;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Error : public ErrorBase<Error> {
|
|
||||||
public:
|
public:
|
||||||
enum ErrorCodes : unsigned int {
|
template <typename T>
|
||||||
GENERAL, UNDEFINED, BADERRORCODE
|
ErrorBase(const ErrorBase<T>& cpy) : Base(cpy) { check_asserts(); }
|
||||||
};
|
|
||||||
|
|
||||||
using ErrorBase<Error>::ErrorBase;
|
virtual std::shared_ptr<Base> make_shared() const noexcept override {
|
||||||
|
return std::static_pointer_cast<Base>(
|
||||||
|
std::make_shared<ErrorBase>(*this)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
friend ErrorBase<Error>;
|
virtual std::string what() const noexcept override {
|
||||||
|
std::string ret = msg;
|
||||||
|
if (trace) {
|
||||||
|
std::string trace_msg;
|
||||||
|
if (traceIsNBError) {
|
||||||
|
trace_msg = std::static_pointer_cast<const Base>(trace)->what();
|
||||||
|
} else {
|
||||||
|
trace_msg = std::string(std::static_pointer_cast<const Base>(trace)->what());
|
||||||
|
}
|
||||||
|
ret += nb::NEWLINE + indent_strblock(
|
||||||
|
trace_msg,
|
||||||
|
nb::TABOVER,
|
||||||
|
nb::TABOVER+"Trace: "
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
|
||||||
static const std::string type;
|
static const std::string type;
|
||||||
static const ErrorCodeMap ErrorMessages;
|
static const ErrorCodeMap ErrorMessages;
|
||||||
|
|
||||||
|
using Base::code;
|
||||||
|
using Base::msg;
|
||||||
|
using Base::trace;
|
||||||
|
using Base::traceIsNBError;
|
||||||
|
|
||||||
|
friend ErrorType;
|
||||||
|
friend Error<ErrorType>;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
using Base::Base;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
ErrorBase(
|
||||||
|
unsigned int code_,
|
||||||
|
std::string msg_,
|
||||||
|
std::string type_,
|
||||||
|
const T* trace_
|
||||||
|
) : Base(
|
||||||
|
code_,
|
||||||
|
msg_,
|
||||||
|
type_,
|
||||||
|
trace_
|
||||||
|
) { check_asserts(); }
|
||||||
};
|
};
|
||||||
|
template <typename ErrorType>
|
||||||
|
const std::string ErrorBase<ErrorType>::type = ErrorType::type;
|
||||||
|
template <typename ErrorType>
|
||||||
|
const ErrorCodeMap ErrorBase<ErrorType>::ErrorMessages = ErrorType::ErrorMessages;
|
||||||
|
|
||||||
|
|
||||||
template <class ErrorType>
|
template <class ErrorType>
|
||||||
std::string ErrorBase<ErrorType>::lookup(unsigned int code) {
|
class Error : public ErrorBase<ErrorType> {
|
||||||
for (auto kv : ErrorType::ErrorMessages) {
|
using Base = ErrorBase<ErrorType>;
|
||||||
if (kv.first==code) {
|
public:
|
||||||
return std::string(kv.second);
|
template <typename T>
|
||||||
}
|
Error(
|
||||||
}
|
unsigned int code_,
|
||||||
|
const T& trace_
|
||||||
|
) noexcept : Base(
|
||||||
|
code_,
|
||||||
|
ErrorType::ErrorMessages.at(code_),
|
||||||
|
ErrorType::type,
|
||||||
|
&trace_
|
||||||
|
) {}
|
||||||
|
|
||||||
throw Error(Error::ErrorCodes::BADERRORCODE);
|
template<typename ET>
|
||||||
}
|
Error(
|
||||||
|
std::string msg_,
|
||||||
|
const ErrorBase<ET>& trace_
|
||||||
|
) noexcept : Base(
|
||||||
|
0,
|
||||||
|
msg_,
|
||||||
|
ErrorType::type,
|
||||||
|
&trace_
|
||||||
|
) {}
|
||||||
|
|
||||||
|
Error(
|
||||||
|
std::string msg_,
|
||||||
|
const std::exception& trace_
|
||||||
|
) noexcept : Base(
|
||||||
|
0,
|
||||||
|
msg_,
|
||||||
|
ErrorType::type,
|
||||||
|
&trace_
|
||||||
|
) {}
|
||||||
|
|
||||||
|
Error(unsigned int code_) noexcept : Base(
|
||||||
|
code_,
|
||||||
|
ErrorType::ErrorMessages.at(code_),
|
||||||
|
ErrorType::type,
|
||||||
|
NULLPTR<std::exception>
|
||||||
|
) {}
|
||||||
|
|
||||||
|
Error(std::string msg_) noexcept : Base(
|
||||||
|
0,
|
||||||
|
msg_,
|
||||||
|
ErrorType::type,
|
||||||
|
NULLPTR<std::exception>
|
||||||
|
) {}
|
||||||
|
|
||||||
|
using Base::what;
|
||||||
|
using Base::code;
|
||||||
|
using Base::msg;
|
||||||
|
using Base::trace;
|
||||||
|
using Base::traceIsNBError;
|
||||||
|
using Base::type;
|
||||||
|
using Base::ErrorMessages;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
using Base::Base;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template<>
|
||||||
|
class Error<NoneType> : public ErrorBase<Error<NoneType>> {
|
||||||
|
using Base = ErrorBase<Error<NoneType>>;
|
||||||
|
public:
|
||||||
|
using Base::Base;
|
||||||
|
|
||||||
|
Error(unsigned int code_=1) noexcept : Base(
|
||||||
|
code_,
|
||||||
|
ErrorMessages.at(code_),
|
||||||
|
type,
|
||||||
|
NULLPTR<std::exception>
|
||||||
|
) {}
|
||||||
|
|
||||||
|
Error(const std::exception& err) : Base(
|
||||||
|
Codes::STANDARD,
|
||||||
|
std::string(err.what()),
|
||||||
|
"std::exception",
|
||||||
|
nullptr
|
||||||
|
) {}
|
||||||
|
|
||||||
|
Error(std::string msg_) noexcept : Base(
|
||||||
|
Codes::UNDEFINED,
|
||||||
|
msg_,
|
||||||
|
type,
|
||||||
|
NULLPTR<std::exception>
|
||||||
|
) {}
|
||||||
|
|
||||||
|
enum Codes : unsigned int {
|
||||||
|
STANDARD, UNDEFINED
|
||||||
|
};
|
||||||
|
|
||||||
|
using Base::what;
|
||||||
|
|
||||||
|
using Base::code;
|
||||||
|
using Base::msg;
|
||||||
|
using Base::trace;
|
||||||
|
using Base::traceIsNBError;
|
||||||
|
static const std::string type;
|
||||||
|
static const ErrorCodeMap ErrorMessages;
|
||||||
|
};
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
|
|
||||||
|
#ifdef _NB_AUTOLOG
|
||||||
|
#ifdef _NB_CODE_ERROR_LOCATIONS
|
||||||
|
#ifndef LOG
|
||||||
|
#define LOG(args...) nb::logger.log(args, __FILE__, __LINE__)
|
||||||
|
#endif // LOG
|
||||||
|
#ifndef WARN
|
||||||
|
#define WARN(args...) nb::logger.warn(args, __FILE__, __LINE__)
|
||||||
|
#endif // WARN
|
||||||
|
#ifndef ERROR
|
||||||
|
#define ERROR(args...) nb::logger.error(args, __FILE__, __LINE__)
|
||||||
|
#endif // ERROR
|
||||||
|
#else
|
||||||
|
#ifndef LOG
|
||||||
|
#define LOG(args...) nb::logger.log(args)
|
||||||
|
#endif // LOG
|
||||||
|
#ifndef WARN
|
||||||
|
#define WARN(args...) nb::logger.warn(args)
|
||||||
|
#endif // WARN
|
||||||
|
#ifndef ERROR
|
||||||
|
#define ERROR(args...) nb::logger.error(args)
|
||||||
|
#endif // ERROR
|
||||||
|
#endif // _NB_CODE_ERROR_LOCATIONS
|
||||||
|
#endif // _NB_AUTOLOG
|
||||||
|
|
||||||
|
#ifndef THROW
|
||||||
|
#ifdef _NB_AUTOLOG
|
||||||
|
#define THROW(args...) ERROR(args); throw args
|
||||||
|
#else
|
||||||
|
#define THROW(args...) throw args
|
||||||
|
#endif // _NB_CODE_ERROR_LOCATIONS
|
||||||
|
#endif // THROW
|
||||||
|
|
||||||
#endif // _NB_ERROR
|
#endif // _NB_ERROR
|
||||||
@ -2,12 +2,9 @@
|
|||||||
#ifndef _NB_LOGGER
|
#ifndef _NB_LOGGER
|
||||||
#define _NB_LOGGER
|
#define _NB_LOGGER
|
||||||
|
|
||||||
#include <atomic>
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <iomanip>
|
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <type_traits>
|
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -18,19 +15,14 @@
|
|||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class ErrorBase;
|
||||||
|
|
||||||
typedef std::chrono::time_point<
|
typedef std::chrono::time_point<
|
||||||
std::chrono::system_clock,
|
std::chrono::system_clock,
|
||||||
std::chrono::nanoseconds
|
std::chrono::nanoseconds
|
||||||
> LoggerTimePoint;
|
> LoggerTimePoint;
|
||||||
|
|
||||||
struct LogEvent{
|
|
||||||
const LoggerTimePoint time;
|
|
||||||
const unsigned char lvl;
|
|
||||||
const std::string msg;
|
|
||||||
const std::thread::id tid;
|
|
||||||
const uint64_t pid;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef std::string (*LogProcessFunction)(const LoggerTimePoint&, const std::string&);
|
typedef std::string (*LogProcessFunction)(const LoggerTimePoint&, const std::string&);
|
||||||
typedef std::unordered_map<uint8_t, LogProcessFunction> LogProcessFunctionMap;
|
typedef std::unordered_map<uint8_t, LogProcessFunction> LogProcessFunctionMap;
|
||||||
|
|
||||||
@ -41,25 +33,32 @@ class LoggerBase
|
|||||||
using LoggerType = Logger;
|
using LoggerType = Logger;
|
||||||
using Base = MultithreadedDataProcessor<LogType, LoggerType>;
|
using Base = MultithreadedDataProcessor<LogType, LoggerType>;
|
||||||
public:
|
public:
|
||||||
bool run() {
|
bool run() override {
|
||||||
if (!type_ptr->isRunning()) {
|
if (!static_cast<LoggerType*>(this)->isRunning()) {
|
||||||
this->_running = true;
|
this->_running = true;
|
||||||
this->_runningThread = std::make_shared<std::thread>([&]{
|
this->_runningThread = std::make_shared<std::thread>([&]{
|
||||||
while(type_ptr->isRunning()) {
|
while(static_cast<LoggerType*>(this)->isRunning()) {
|
||||||
type_ptr->flush();
|
static_cast<LoggerType*>(this)->flush();
|
||||||
}
|
}
|
||||||
type_ptr->flush();
|
static_cast<LoggerType*>(this)->flush();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return type_ptr->isRunning();
|
return static_cast<LoggerType*>(this)->isRunning();
|
||||||
}
|
}
|
||||||
protected:
|
protected:
|
||||||
LoggerBase() = default;
|
LoggerBase() = default;
|
||||||
|
|
||||||
StreamType _ostream;
|
StreamType _ostream;
|
||||||
|
};
|
||||||
|
|
||||||
private:
|
struct LogEvent{
|
||||||
LoggerType* type_ptr = static_cast<LoggerType*>(this);
|
const LoggerTimePoint time;
|
||||||
|
const unsigned char lvl;
|
||||||
|
const std::string msg;
|
||||||
|
const std::thread::id tid;
|
||||||
|
const uint64_t pid;
|
||||||
|
const std::string file="";
|
||||||
|
const unsigned int line=0;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename LT>
|
template <typename LT>
|
||||||
@ -72,39 +71,76 @@ public:
|
|||||||
template<typename... ST>
|
template<typename... ST>
|
||||||
DebugLogger(ST&... streams) : _ostream(nb::RefPackToPtrVec<std::ostream, ST...>(streams...).vec) {}
|
DebugLogger(ST&... streams) : _ostream(nb::RefPackToPtrVec<std::ostream, ST...>(streams...).vec) {}
|
||||||
|
|
||||||
~DebugLogger() { type_ptr->stop(); }
|
~DebugLogger() { static_cast<LoggerType*>(this)->stop(); }
|
||||||
|
|
||||||
void log(const std::string& msg, const uint8_t& lvl=0xFF) {
|
void log(
|
||||||
type_ptr->push(LogEvent{
|
std::string msg,
|
||||||
|
uint8_t lvl=0x00,
|
||||||
|
std::string file="",
|
||||||
|
unsigned int line=0
|
||||||
|
) {
|
||||||
|
static_cast<LoggerType*>(this)->push(LogEvent{
|
||||||
std::chrono::system_clock::now(),
|
std::chrono::system_clock::now(),
|
||||||
lvl,
|
lvl,
|
||||||
msg,
|
msg,
|
||||||
std::this_thread::get_id(),
|
std::this_thread::get_id(),
|
||||||
GetPID(),
|
GetPID(),
|
||||||
|
file,
|
||||||
|
line
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
template <size_t N>
|
template <size_t N>
|
||||||
void log(char const(&msg) [N], const uint8_t& lvl=0xFF) {
|
void log(
|
||||||
type_ptr->log(std::string(msg), lvl);
|
char const(&msg) [N],
|
||||||
|
uint8_t lvl=0x00,
|
||||||
|
std::string file="",
|
||||||
|
unsigned int line=0
|
||||||
|
) {
|
||||||
|
static_cast<LoggerType*>(this)->log(std::string(msg), lvl, file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
void log(const std::exception& err, const uint8_t& lvl=0xFF) {
|
template <typename T>
|
||||||
type_ptr->log(err.what(), lvl);
|
void log(
|
||||||
|
const ErrorBase<T>& err,
|
||||||
|
uint8_t lvl=0xFF,
|
||||||
|
std::string file="",
|
||||||
|
unsigned int line=0
|
||||||
|
) {
|
||||||
|
static_cast<LoggerType*>(this)->log(err.what(), lvl, file, line);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
std::enable_if_t<std::is_base_of_v<std::exception, T>, void> log(
|
||||||
|
const T& err,
|
||||||
|
uint8_t lvl=0xFF,
|
||||||
|
std::string file="",
|
||||||
|
unsigned int line=0
|
||||||
|
) {
|
||||||
|
static_cast<LoggerType*>(this)->log(std::string(err.what()), lvl, file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename U>
|
template<typename U>
|
||||||
void warn(const U& val, const uint8_t& lvl=0x01) { type_ptr->log(val, lvl); }
|
void warn(
|
||||||
|
U val,
|
||||||
|
uint8_t lvl=0x01,
|
||||||
|
std::string file="",
|
||||||
|
unsigned int line=0
|
||||||
|
) { static_cast<LoggerType*>(this)->log(val, lvl, file, line); }
|
||||||
|
|
||||||
template<typename U>
|
template<typename U>
|
||||||
void error(const U& val) { type_ptr->log(val, 0xFF); }
|
void error(
|
||||||
|
U val,
|
||||||
|
std::string file="",
|
||||||
|
unsigned int line=0
|
||||||
|
) {
|
||||||
|
static_cast<LoggerType*>(this)->log(val, 0xFF, file, line);
|
||||||
|
static_cast<LoggerType*>(this)->flush();
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::vector<std::ostream*> _ostream;
|
std::vector<std::ostream*> _ostream;
|
||||||
|
|
||||||
private:
|
|
||||||
LoggerType* type_ptr = static_cast<LoggerType*>(this);
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
|
class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
|
||||||
@ -127,17 +163,15 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
using Base::_ostream;
|
using Base::_ostream;
|
||||||
|
|
||||||
bool process(const LogEvent& msg) {
|
bool process(const LogEvent& msg);
|
||||||
for (const auto os : this->_ostream) {
|
|
||||||
*os << msg.lvl << "\t|\t" << msg.msg << "\n";
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#ifndef _NB_NO_LOGGER
|
||||||
extern DefaultDebugLogger logger;
|
extern DefaultDebugLogger logger;
|
||||||
|
#endif // _NB_NO_LOGGER
|
||||||
|
|
||||||
// Taking Charge of Adult ADHD by Russell Barkley
|
// Taking Charge of Adult ADHD by Russell Barkley
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
|
|
||||||
#endif // _NB_LOGGER
|
#endif // _NB_LOGGER
|
||||||
@ -7,6 +7,8 @@
|
|||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
uint64_t GetPID();
|
uint64_t GetPID();
|
||||||
|
uint64_t getTID();
|
||||||
|
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
|
|
||||||
|
|||||||
79
engine/NBCore/StringUtils.hpp
Normal file
79
engine/NBCore/StringUtils.hpp
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef _NB_STRING_UTILS
|
||||||
|
#define _NB_STRING_UTILS
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
#include "TypeTraits.hpp"
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
#ifdef _NB_TARGET_WINDOWS
|
||||||
|
const std::string NEWLINE = "\n";
|
||||||
|
const std::wstring WNEWLINE = L"\n";
|
||||||
|
#endif // _NB_TARGET_WINDOWS
|
||||||
|
#ifdef _NB_TARGET_LINUX
|
||||||
|
const std::string NEWLINE = "\n";
|
||||||
|
const std::wstring WNEWLINE = L"\n";
|
||||||
|
#endif // _NB_TARGET_LINUX
|
||||||
|
|
||||||
|
const std::string TABOVER = " ";
|
||||||
|
|
||||||
|
// std::wstring str_to_wstr(std::string in);
|
||||||
|
|
||||||
|
// std::string wstr_to_str(std::wstring in);
|
||||||
|
|
||||||
|
template <typename Stream, typename... Args>
|
||||||
|
void stream(const Stream& s, Args&&... args);
|
||||||
|
|
||||||
|
template <typename Stream, typename... Args>
|
||||||
|
void stream(const Stream& s, Args&&... args) {
|
||||||
|
(s << ... << args);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
void term(Args&&... args) { stream(std::cout, args..., nb::NEWLINE); }
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
void wterm(Args&&... args) { stream(std::wcout, args..., nb::WNEWLINE); }
|
||||||
|
|
||||||
|
template <typename T = char>
|
||||||
|
std::basic_string<T> find_and_replace(
|
||||||
|
ExplicitType_t<std::basic_string<T>> original,
|
||||||
|
ExplicitType_t<std::basic_string_view<T>> find,
|
||||||
|
ExplicitType_t<std::basic_string_view<T>> replace
|
||||||
|
) {
|
||||||
|
using StringType = std::basic_string<T>;
|
||||||
|
|
||||||
|
StringType ret(original);
|
||||||
|
|
||||||
|
std::size_t find_len = find.length();
|
||||||
|
std::size_t replace_len = replace.length();
|
||||||
|
std::size_t currpos = 0;
|
||||||
|
while(true) {
|
||||||
|
currpos = ret.find(find, currpos);
|
||||||
|
if (currpos == StringType::npos) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ret = ret.erase(currpos, find_len);
|
||||||
|
ret = ret.insert(currpos, replace);
|
||||||
|
currpos += replace_len;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string indent_strblock(
|
||||||
|
std::string block,
|
||||||
|
std::string prepend,
|
||||||
|
std::string topIndent
|
||||||
|
);
|
||||||
|
|
||||||
|
std::string indent_strblock(
|
||||||
|
std::string block,
|
||||||
|
std::string prepend
|
||||||
|
);
|
||||||
|
|
||||||
|
} // namespace nb
|
||||||
|
#endif // _NB_STRING_UTILS
|
||||||
@ -2,15 +2,96 @@
|
|||||||
#ifndef _NB_TYPE_TRAITS
|
#ifndef _NB_TYPE_TRAITS
|
||||||
#define _NB_TYPE_TRAITS
|
#define _NB_TYPE_TRAITS
|
||||||
|
|
||||||
|
#include <tuple>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
template<std::size_t N=0, typename Func, typename... Pack>
|
namespace detail {
|
||||||
|
template <
|
||||||
|
class Default,
|
||||||
|
class AlwaysVoid,
|
||||||
|
template <class...> class Op,
|
||||||
|
class... Args
|
||||||
|
>
|
||||||
|
struct detector {
|
||||||
|
using value = std::false_type;
|
||||||
|
using type = Default;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <
|
||||||
|
class Default,
|
||||||
|
template <class...> class Op,
|
||||||
|
class... Args
|
||||||
|
>
|
||||||
|
struct detector<Default, std::void_t<Op<Args...>>, Op, Args...> {
|
||||||
|
using value = std::true_type;
|
||||||
|
using type = Op<Args...>;
|
||||||
|
};
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
struct NoneType{};
|
||||||
|
|
||||||
|
template <
|
||||||
|
template <class...> class Op,
|
||||||
|
class... Args
|
||||||
|
>
|
||||||
|
using is_detected = typename detail::detector<NoneType, void, Op, Args...>::value;
|
||||||
|
|
||||||
|
template <typename... Types>
|
||||||
|
struct ValidConversion;
|
||||||
|
|
||||||
|
template <typename To, typename A, typename B, typename... Rest>
|
||||||
|
struct ValidConversion<To, A, B, Rest...> {
|
||||||
|
typedef std::conjunction<
|
||||||
|
std::is_constructible<To, A>, std::is_constructible<To, A>, std::conjunction<std::is_constructible<To, Rest>...>
|
||||||
|
> value_type;
|
||||||
|
static constexpr bool value = value_type::value;
|
||||||
|
typedef std::enable_if_t<value, To> to;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename To, typename From>
|
||||||
|
struct ValidConversion<To, From> {
|
||||||
|
typedef std::is_constructible<To, From> value_type;
|
||||||
|
static constexpr bool value = value_type::value;
|
||||||
|
typedef std::enable_if_t<value, To> to;
|
||||||
|
typedef std::enable_if_t<value, From> from;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename... Types>
|
||||||
|
using ValidConversion_v = typename ValidConversion<Types...>::value;
|
||||||
|
|
||||||
|
template<typename... Types>
|
||||||
|
using ValidConversion_to = typename ValidConversion<Types...>::to;
|
||||||
|
|
||||||
|
template<typename To, typename From>
|
||||||
|
using ValidConversion_from = typename ValidConversion<To, From>::from;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct ExplicitType { using type=T; };
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
using ExplicitType_t = typename ExplicitType<T>::type;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
T* NULLPTR = static_cast<T*>(nullptr);
|
||||||
|
|
||||||
|
template <typename Func, typename T>
|
||||||
|
struct RunAndOutput {
|
||||||
|
using type = T;
|
||||||
|
T value;
|
||||||
|
RunAndOutput(Func func, T val) : value(val) {
|
||||||
|
func();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename Func, std::size_t N=0, typename... Pack>
|
||||||
inline typename std::enable_if<N==sizeof...(Pack), void>::type
|
inline typename std::enable_if<N==sizeof...(Pack), void>::type
|
||||||
ForEach(std::tuple<Pack...>&, Func) {}
|
ForEach(std::tuple<Pack...>&, Func) {}
|
||||||
|
|
||||||
template<std::size_t N=0, typename Func, typename... Pack>
|
template<typename Func, std::size_t N=0, typename... Pack>
|
||||||
inline typename std::enable_if<N < sizeof...(Pack), void>::type
|
inline typename std::enable_if<N < sizeof...(Pack), void>::type
|
||||||
ForEach(std::tuple<Pack...>& tup, Func f) {
|
ForEach(std::tuple<Pack...>& tup, Func f) {
|
||||||
f(N, std::get<N>(tup));
|
f(N, std::get<N>(tup));
|
||||||
|
|||||||
@ -1,25 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_CORE_TYPES
|
|
||||||
#define _NB_CORE_TYPES
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
T swapEndian(const T& val) {
|
|
||||||
T ret;
|
|
||||||
const int size = sizeof(T);
|
|
||||||
auto retLoc = static_cast<void*>(&ret);
|
|
||||||
auto valLoc = static_cast<const void*>(&val);
|
|
||||||
|
|
||||||
for (int i = 0; i < size; ++i) {
|
|
||||||
memcpy(retLoc+i, valLoc+(size-i-1), 1);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
using ByteVector = std::vector<uint8_t>;
|
|
||||||
|
|
||||||
} // namespace nb
|
|
||||||
#endif // _NB_CORE_TYPES
|
|
||||||
80
engine/NBCore/Utils.hpp
Normal file
80
engine/NBCore/Utils.hpp
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef _NB_CORE_TYPES
|
||||||
|
#define _NB_CORE_TYPES
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "Errors.hpp"
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
using ByteVector = std::vector<uint8_t>;
|
||||||
|
|
||||||
|
class ObjectManagerError : public Error<ObjectManagerError> {
|
||||||
|
using Base = Error<ObjectManagerError>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
using Base::Base;
|
||||||
|
|
||||||
|
enum Codes : unsigned int {
|
||||||
|
UNDEFINED, NO_MANAGER, MANAGER_MISMATCH, LOCK_OVERFLOW, BAD_THREAD
|
||||||
|
};
|
||||||
|
|
||||||
|
static const std::string type;
|
||||||
|
static const ErrorCodeMap ErrorMessages;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class ThreadsafeObjectLock;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class ThreadsafeObject {
|
||||||
|
using Codes = ObjectManagerError::Codes;
|
||||||
|
|
||||||
|
public:
|
||||||
|
ThreadsafeObject(T&& obj)
|
||||||
|
: _obj(std::make_shared<T>(obj)) {}
|
||||||
|
|
||||||
|
ThreadsafeObjectLock<T> lock() {
|
||||||
|
return ThreadsafeObjectLock<T>(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
friend ThreadsafeObjectLock<T>;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::shared_ptr<T> _obj;
|
||||||
|
mutable std::recursive_mutex _mutex;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class ThreadsafeObjectLock {
|
||||||
|
public:
|
||||||
|
ThreadsafeObjectLock(const ThreadsafeObjectLock&) = delete;
|
||||||
|
ThreadsafeObjectLock operator=(const ThreadsafeObjectLock&) = delete;
|
||||||
|
~ThreadsafeObjectLock() {
|
||||||
|
_manager->_mutex.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
T* operator->() {
|
||||||
|
return _manager->_obj.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
friend ThreadsafeObject<T>;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
ThreadsafeObjectLock(
|
||||||
|
ThreadsafeObject<T>* const manager_
|
||||||
|
) : _manager(manager_) {
|
||||||
|
if (!_manager) {
|
||||||
|
using Codes = ObjectManagerError::Codes;
|
||||||
|
THROW(ObjectManagerError(Codes::NO_MANAGER));
|
||||||
|
}
|
||||||
|
_manager->_mutex.lock();
|
||||||
|
}
|
||||||
|
ThreadsafeObject<T>* const _manager;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace nb
|
||||||
|
#endif // _NB_CORE_TYPES
|
||||||
@ -1,12 +1,13 @@
|
|||||||
#include "Errors.hpp"
|
#include "Errors.hpp"
|
||||||
|
#include "TypeTraits.hpp"
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
const std::string Error::type = "Error";
|
const std::string Error<>::type = "nb::Error";
|
||||||
const ErrorCodeMap Error::ErrorMessages = {
|
|
||||||
{ErrorCodes::GENERAL, "General std::exception."},
|
const ErrorCodeMap Error<nb::NoneType>::ErrorMessages = {
|
||||||
{ErrorCodes::UNDEFINED, "Undefined / general error."},
|
{Error::Codes::STANDARD, "std::exception"},
|
||||||
{ErrorCodes::BADERRORCODE, "Unrecognized error code."}
|
{Error::Codes::UNDEFINED, "Error"}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,10 +1,33 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "Logger.hpp"
|
#include "Logger.hpp"
|
||||||
|
#include "StringUtils.hpp"
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
|
#ifndef _NB_NO_LOGGER
|
||||||
nb::DefaultDebugLogger logger(std::cout);
|
nb::DefaultDebugLogger logger(std::cout);
|
||||||
|
#endif // _NB_NO_LOGGER
|
||||||
|
|
||||||
|
bool nb::DefaultDebugLogger::process(const LogEvent& msg) {
|
||||||
|
constexpr size_t level_field_width = 5;
|
||||||
|
constexpr size_t msg_prompt_length = level_field_width+7;
|
||||||
|
for (const auto os : this->_ostream) {
|
||||||
|
*os << "[";
|
||||||
|
os->width(level_field_width);
|
||||||
|
*os << std::to_string(msg.lvl) << "] :: ";
|
||||||
|
std::string fileloc="";
|
||||||
|
if (!msg.file.empty()) {
|
||||||
|
fileloc += "(" + msg.file + ":" + std::to_string(msg.line) + ") - ";
|
||||||
|
}
|
||||||
|
*os << indent_strblock(
|
||||||
|
fileloc + msg.msg,
|
||||||
|
std::string(20 - msg_prompt_length, ' '),
|
||||||
|
""
|
||||||
|
) << nb::NEWLINE;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
static bool RUN_LOGGER(nb::DefaultDebugLogger& log) {
|
static bool RUN_LOGGER(nb::DefaultDebugLogger& log) {
|
||||||
return log.run();
|
return log.run();
|
||||||
|
|||||||
@ -7,7 +7,6 @@
|
|||||||
#endif // _NB_TARGET_LINUX
|
#endif // _NB_TARGET_LINUX
|
||||||
|
|
||||||
#include "Processes.hpp"
|
#include "Processes.hpp"
|
||||||
#include "Types.hpp"
|
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
@ -16,7 +15,14 @@ uint64_t GetPID() {
|
|||||||
return GetCurrentProcessId();
|
return GetCurrentProcessId();
|
||||||
}
|
}
|
||||||
#endif // _NB_TARGET_WINDOWS
|
#endif // _NB_TARGET_WINDOWS
|
||||||
|
#ifdef _NB_TARGET_LINUX
|
||||||
|
#endif // _NB_TARGET_LINUX
|
||||||
|
|
||||||
|
#ifdef _NB_TARGET_WINDOWS
|
||||||
|
uint64_t GetTID() {
|
||||||
|
return GetCurrentThreadId();
|
||||||
|
}
|
||||||
|
#endif // _NB_TARGET_WINDOWS
|
||||||
#ifdef _NB_TARGET_LINUX
|
#ifdef _NB_TARGET_LINUX
|
||||||
#endif // _NB_TARGET_LINUX
|
#endif // _NB_TARGET_LINUX
|
||||||
|
|
||||||
|
|||||||
29
engine/NBCore/src/StringUtils.cpp
Normal file
29
engine/NBCore/src/StringUtils.cpp
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#include "StringUtils.hpp"
|
||||||
|
|
||||||
|
/* std::string nb::wstr_to_str(std::wstring in) {
|
||||||
|
std::size_t wstrlen = in.length();
|
||||||
|
char* c_str= new char[wstrlen];
|
||||||
|
std::wcstombs(c_str, in.c_str(), wstrlen);
|
||||||
|
std::string ret(c_str, wstrlen);
|
||||||
|
delete[] c_str;
|
||||||
|
return ret;
|
||||||
|
} */
|
||||||
|
|
||||||
|
std::string nb::indent_strblock(
|
||||||
|
std::string block,
|
||||||
|
std::string prepend,
|
||||||
|
std::string topIndent
|
||||||
|
) {
|
||||||
|
return topIndent + nb::find_and_replace(
|
||||||
|
block,
|
||||||
|
nb::NEWLINE,
|
||||||
|
nb::NEWLINE + prepend
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string nb::indent_strblock(
|
||||||
|
std::string block,
|
||||||
|
std::string prepend
|
||||||
|
) {
|
||||||
|
return nb::indent_strblock(block, prepend, prepend);
|
||||||
|
}
|
||||||
27
engine/NBCore/src/Utils.cpp
Normal file
27
engine/NBCore/src/Utils.cpp
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#include "Utils.hpp"
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
using ObjectManagerCodes = ObjectManagerError::Codes;
|
||||||
|
const std::string ObjectManagerError::type = "nb::ObjectManagerError";
|
||||||
|
const ErrorCodeMap ObjectManagerError::ErrorMessages({
|
||||||
|
{ObjectManagerCodes::UNDEFINED, "Error"},
|
||||||
|
{
|
||||||
|
ObjectManagerCodes::NO_MANAGER,
|
||||||
|
"Attempting to create object lock without lock manager"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ObjectManagerCodes::MANAGER_MISMATCH,
|
||||||
|
"Attempting to delete object lock from mismatched lock manager"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ObjectManagerCodes::LOCK_OVERFLOW,
|
||||||
|
"Too many object locks allocated"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ObjectManagerCodes::BAD_THREAD,
|
||||||
|
"Attempting operation from a bad thread"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}; // namespace nb
|
||||||
@ -4,12 +4,21 @@ if (NB_BUILD_TESTS)
|
|||||||
enable_testing()
|
enable_testing()
|
||||||
include(GoogleTest)
|
include(GoogleTest)
|
||||||
add_executable(TestCore
|
add_executable(TestCore
|
||||||
testErrors.cpp
|
./testErrors.cpp
|
||||||
testProcesses.cpp
|
#./testProcesses.cpp
|
||||||
|
./testUtils.cpp
|
||||||
)
|
)
|
||||||
target_link_libraries(TestCore
|
target_link_libraries(TestCore
|
||||||
NBCore
|
NBCore
|
||||||
GTest::gtest_main
|
GTest::gtest_main
|
||||||
)
|
)
|
||||||
|
|
||||||
|
add_executable(LoggerTest
|
||||||
|
./testLogger.cpp
|
||||||
|
)
|
||||||
|
target_link_libraries(LoggerTest
|
||||||
|
NBCore
|
||||||
|
)
|
||||||
|
|
||||||
gtest_discover_tests(TestCore)
|
gtest_discover_tests(TestCore)
|
||||||
endif()
|
endif()
|
||||||
@ -1,38 +1,39 @@
|
|||||||
#define CODE_ERROR_LOCATIONS
|
#define CODE_ERROR_LOCATIONS
|
||||||
|
|
||||||
#include "Errors.hpp"
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <iostream>
|
|
||||||
#include <sstream>
|
#include "Errors.hpp"
|
||||||
#include "Logger.hpp"
|
#include <string>
|
||||||
|
|
||||||
using namespace nb;
|
using namespace nb;
|
||||||
|
|
||||||
class TestError : public ErrorBase<TestError> {
|
class TestError : public Error<TestError> {
|
||||||
|
using Base = Error<TestError>;
|
||||||
public:
|
public:
|
||||||
using ErrorBase<TestError>::ErrorBase;
|
using Base::Base;
|
||||||
|
using Base::what;
|
||||||
|
using Base::code;
|
||||||
|
using Base::msg;
|
||||||
|
using Base::trace;
|
||||||
|
|
||||||
enum ErrorCodes : unsigned int {
|
enum Codes : unsigned int {
|
||||||
A, B, C, D
|
A, B, C, D
|
||||||
};
|
};
|
||||||
|
|
||||||
static const std::string type;
|
static const std::string type;
|
||||||
static const ErrorCodeMap ErrorMessages;
|
static const nb::ErrorCodeMap ErrorMessages;
|
||||||
};
|
};
|
||||||
|
|
||||||
const std::string TestError::type="TestError";
|
const std::string TestError::type="TestError";
|
||||||
const ErrorCodeMap TestError::ErrorMessages{
|
const ErrorCodeMap TestError::ErrorMessages{
|
||||||
{TestError::ErrorCodes::A, "Hey!"},
|
{TestError::Codes::A, "Hey!"},
|
||||||
{TestError::ErrorCodes::B, "How"},
|
{TestError::Codes::B, "How"},
|
||||||
{TestError::ErrorCodes::C, "You"},
|
{TestError::Codes::C, "You"},
|
||||||
{TestError::ErrorCodes::D, "Doin"}
|
{TestError::Codes::D, "Doin"}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
TEST(ErrorTest, Test) {
|
TEST(ErrorTest, Test) {
|
||||||
EXPECT_EQ(1, 1);
|
auto err = TestError(0, TestError(1, TestError(2, TestError(3, TestError(2)))));
|
||||||
|
ASSERT_STREQ(err.what().c_str(), "Hey!\n Trace: How\n Trace: You\n Trace: Doin\n Trace: You");
|
||||||
std::stringstream sstream;
|
|
||||||
|
|
||||||
ASSERT_TRUE(nb::logger.isRunning());
|
|
||||||
nb::logger.log("Hey!");
|
|
||||||
}
|
}
|
||||||
46
engine/NBCore/tests/testLogger.cpp
Normal file
46
engine/NBCore/tests/testLogger.cpp
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#include <exception>
|
||||||
|
|
||||||
|
#include "Errors.hpp"
|
||||||
|
#include "Logger.hpp"
|
||||||
|
|
||||||
|
class TestError : public nb::Error<TestError> {
|
||||||
|
using Base = Error<TestError>;
|
||||||
|
public:
|
||||||
|
using Base::Base;
|
||||||
|
using Base::what;
|
||||||
|
using Base::code;
|
||||||
|
using Base::msg;
|
||||||
|
using Base::trace;
|
||||||
|
|
||||||
|
enum Codes : unsigned int {
|
||||||
|
A, B, C, D
|
||||||
|
};
|
||||||
|
|
||||||
|
static const std::string type;
|
||||||
|
static const nb::ErrorCodeMap ErrorMessages;
|
||||||
|
};
|
||||||
|
|
||||||
|
const std::string TestError::type="TestError";
|
||||||
|
const nb::ErrorCodeMap TestError::ErrorMessages{
|
||||||
|
{TestError::Codes::A, "Hey!"},
|
||||||
|
{TestError::Codes::B, "How"},
|
||||||
|
{TestError::Codes::C, "You"},
|
||||||
|
{TestError::Codes::D, "Doin"}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
nb::logger.log("Whoop!");
|
||||||
|
try {
|
||||||
|
THROW(TestError(0, TestError(1, TestError(2, TestError(3, TestError(2))))));
|
||||||
|
} catch (TestError e){
|
||||||
|
nb::logger.log("nb::Error was thrown!");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
THROW(std::exception());
|
||||||
|
} catch (std::exception e){
|
||||||
|
nb::logger.log("std::exception was thrown!");
|
||||||
|
}
|
||||||
|
ERROR("hello there!");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@ -1,8 +1,6 @@
|
|||||||
#define CODE_ERROR_LOCATIONS
|
#define CODE_ERROR_LOCATIONS
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include "Processes.hpp"
|
#include "Processes.hpp"
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
|
|
||||||
|
|||||||
148
engine/NBCore/tests/testUtils.cpp
Normal file
148
engine/NBCore/tests/testUtils.cpp
Normal file
@ -0,0 +1,148 @@
|
|||||||
|
#define CODE_ERROR_LOCATIONS
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include "TypeTraits.hpp"
|
||||||
|
#include "StringUtils.hpp"
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
TEST(UtilsTest, TestFindAndReplace) {
|
||||||
|
ASSERT_STREQ(
|
||||||
|
find_and_replace("Jeff", "e", "efe").c_str(),
|
||||||
|
"Jefeff"
|
||||||
|
);
|
||||||
|
|
||||||
|
std::string tmp = find_and_replace("Naif", "a", "afa");
|
||||||
|
ASSERT_STREQ(
|
||||||
|
find_and_replace(tmp, "i", "ifi").c_str(),
|
||||||
|
"Nafaifif"
|
||||||
|
);
|
||||||
|
|
||||||
|
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, "o", "ofo");
|
||||||
|
tmp = find_and_replace(tmp, "u", "ufu");
|
||||||
|
ASSERT_STREQ(
|
||||||
|
tmp.c_str(),
|
||||||
|
"afaefeifiofoufu"
|
||||||
|
);
|
||||||
|
|
||||||
|
tmp = find_and_replace(tmp, "afa", "a");
|
||||||
|
tmp = find_and_replace(tmp, "efe", "e");
|
||||||
|
tmp = find_and_replace(tmp, "ifi", "i");
|
||||||
|
tmp = find_and_replace(tmp, "ofo", "o");
|
||||||
|
tmp = find_and_replace(tmp, "ufu", "u");
|
||||||
|
ASSERT_STREQ(
|
||||||
|
tmp.c_str(),
|
||||||
|
"aeiou"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(UtilsTest, TestFindAndReplaceWstr) {
|
||||||
|
ASSERT_STREQ(
|
||||||
|
find_and_replace<wchar_t>(L"Jeff", L"e", L"efe").c_str(),
|
||||||
|
L"Jefeff"
|
||||||
|
);
|
||||||
|
|
||||||
|
std::wstring tmp = find_and_replace<wchar_t>(L"Naif", L"a", L"afa");
|
||||||
|
ASSERT_STREQ(
|
||||||
|
find_and_replace<wchar_t>(tmp, L"i", L"ifi").c_str(),
|
||||||
|
L"Nafaifif"
|
||||||
|
);
|
||||||
|
|
||||||
|
tmp = find_and_replace<wchar_t>(L"aeiou", L"a", L"afa");
|
||||||
|
tmp = find_and_replace<wchar_t>(tmp, L"e", L"efe");
|
||||||
|
tmp = find_and_replace<wchar_t>(tmp, L"i", L"ifi");
|
||||||
|
tmp = find_and_replace<wchar_t>(tmp, L"o", L"ofo");
|
||||||
|
tmp = find_and_replace<wchar_t>(tmp, L"u", L"ufu");
|
||||||
|
ASSERT_STREQ(
|
||||||
|
tmp.c_str(),
|
||||||
|
L"afaefeifiofoufu"
|
||||||
|
);
|
||||||
|
|
||||||
|
tmp = find_and_replace<wchar_t>(tmp, L"afa", L"a");
|
||||||
|
tmp = find_and_replace<wchar_t>(tmp, L"efe", L"e");
|
||||||
|
tmp = find_and_replace<wchar_t>(tmp, L"ifi", L"i");
|
||||||
|
tmp = find_and_replace<wchar_t>(tmp, L"ofo", L"o");
|
||||||
|
tmp = find_and_replace<wchar_t>(tmp, L"ufu", L"u");
|
||||||
|
ASSERT_STREQ(
|
||||||
|
tmp.c_str(),
|
||||||
|
L"aeiou"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 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; };
|
||||||
|
struct D : C {};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
using has_x = decltype(T::x);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
using has_y = decltype(T::y);
|
||||||
|
|
||||||
|
TEST(UtilsTest, TestIsDetected) {
|
||||||
|
auto ret = is_detected<has_x, A>::value;
|
||||||
|
ASSERT_TRUE(ret);
|
||||||
|
ret = is_detected<has_y, A>::value;
|
||||||
|
ASSERT_FALSE(ret);
|
||||||
|
|
||||||
|
ret = is_detected<has_y, B>::value;
|
||||||
|
ASSERT_TRUE(ret);
|
||||||
|
ret = is_detected<has_x, B>::value;
|
||||||
|
ASSERT_FALSE(ret);
|
||||||
|
|
||||||
|
ret = is_detected<has_x, C>::value;
|
||||||
|
ASSERT_TRUE(ret);
|
||||||
|
ret = is_detected<has_y, C>::value;
|
||||||
|
ASSERT_TRUE(ret);
|
||||||
|
|
||||||
|
ret = is_detected<has_x, D>::value;
|
||||||
|
ASSERT_TRUE(ret);
|
||||||
|
ret = is_detected<has_y, D>::value;
|
||||||
|
ASSERT_TRUE(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace nb
|
||||||
@ -4,16 +4,18 @@
|
|||||||
|
|
||||||
#include "GLLoad.hpp"
|
#include "GLLoad.hpp"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <NBCore/Errors.hpp>
|
#include <NBCore/Errors.hpp>
|
||||||
#include <NBCore/Types.hpp>
|
#include <NBCore/Logger.hpp>
|
||||||
|
#include <NBCore/Utils.hpp>
|
||||||
|
|
||||||
|
#include "OGLObjects.hpp"
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
typedef std::vector<unsigned char> RawVec;
|
|
||||||
|
|
||||||
static uint8_t GLSLTypeSize(GLenum type) {
|
static uint8_t GLSLTypeSize(GLenum type) {
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case GL_SHORT:
|
case GL_SHORT:
|
||||||
@ -35,51 +37,30 @@ static uint8_t GLSLTypeSize(GLenum type) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct VertexAttributePointer {
|
|
||||||
GLuint buffer = 0;
|
|
||||||
int32_t offset = 0;
|
|
||||||
GLsizei stride = -1;
|
|
||||||
GLuint divisor = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct VertexAttribute {
|
|
||||||
GLint GLSLSize;
|
|
||||||
GLenum GLSLType;
|
|
||||||
GLboolean GLSLNormalization;
|
|
||||||
VertexAttributePointer ptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
static bool isIndexInVertexAttribute(int i, const VertexAttribute& va) {
|
|
||||||
if ((i -= va.ptr.offset) < 0) { return false; }
|
|
||||||
return (i%va.ptr.stride) < GLSLTypeSize(va.GLSLType) * va.GLSLSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef std::vector<VertexAttribute> VertexAttributeList;
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
RawVec vectorToRaw(const std::vector<T>& vec) {
|
ByteVector vectorToRaw(const std::vector<T>& vec) {
|
||||||
unsigned int num_bytes = vec.size() * sizeof(T);
|
unsigned int num_bytes = vec.size() * sizeof(T);
|
||||||
RawVec ret(num_bytes);
|
ByteVector ret(num_bytes);
|
||||||
memcpy(ret.data(), vec.data(), num_bytes);
|
memcpy(ret.data(), vec.data(), num_bytes);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, typename S>
|
template<typename T, typename S>
|
||||||
RawVec concatVectorsToRaw(const std::vector<T>& vec1, const std::vector<S>& vec2) {
|
ByteVector concatVectorsToRaw(const std::vector<T>& vec1, const std::vector<S>& vec2) {
|
||||||
RawVec vec1_raw = vectorToRaw<T>(vec1);
|
ByteVector vec1_raw = vectorToRaw<T>(vec1);
|
||||||
RawVec vec2_raw = vectorToRaw<S>(vec2);
|
ByteVector vec2_raw = vectorToRaw<S>(vec2);
|
||||||
unsigned int vec1_raw_size = vec1_raw.size();
|
unsigned int vec1_raw_size = vec1_raw.size();
|
||||||
unsigned int vec2_raw_size = vec2_raw.size();
|
unsigned int vec2_raw_size = vec2_raw.size();
|
||||||
RawVec ret(vec1_raw_size + vec2_raw_size);
|
ByteVector ret(vec1_raw_size + vec2_raw_size);
|
||||||
memcpy(ret.data(), vec1_raw.data(), vec1_raw_size);
|
memcpy(ret.data(), vec1_raw.data(), vec1_raw_size);
|
||||||
memcpy(ret.data() + vec1_raw_size, vec2_raw.data(), vec2_raw_size);
|
memcpy(ret.data() + vec1_raw_size, vec2_raw.data(), vec2_raw_size);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::vector<T> rawToVector(const RawVec& vec) {
|
std::vector<T> rawToVector(const ByteVector& vec) {
|
||||||
if (vec.size() % sizeof(T) != 0) {
|
if (vec.size() % sizeof(T) != 0) {
|
||||||
throw std::runtime_error("Data size does not align to std::vector<" + std::string(typeid(T).name()) + ">.");
|
THROW(std::runtime_error("Data size does not align to std::vector<" + std::string(typeid(T).name()) + ">."));
|
||||||
}
|
}
|
||||||
unsigned int num_elmts = vec.size() / sizeof(T);
|
unsigned int num_elmts = vec.size() / sizeof(T);
|
||||||
std::vector<T> ret(num_elmts);
|
std::vector<T> ret(num_elmts);
|
||||||
@ -88,278 +69,206 @@ std::vector<T> rawToVector(const RawVec& vec) {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
class BufferError : public std::runtime_error {
|
class BufferError : public Error<BufferError> {
|
||||||
|
using Base = Error<BufferError>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
const bool error;
|
|
||||||
BufferError(const std::string& msg, const std::string& file="", int line=-1)
|
|
||||||
: std::runtime_error(NB::formatDebugString(msg, file, line)), error(true) {}
|
|
||||||
BufferError(bool isError=true) : std::runtime_error(""), error(isError) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
template <typename ObjectType>
|
|
||||||
class OpenGLObject {
|
|
||||||
public:
|
|
||||||
OpenGLObject(OpenGLObject&&) = default;
|
|
||||||
OpenGLObject& operator=(OpenGLObject&&) = default;
|
|
||||||
~OpenGLObject() { remove(); }
|
|
||||||
|
|
||||||
virtual void bind() const = 0;
|
|
||||||
virtual void unbind() const = 0;
|
|
||||||
virtual bool isInitialized() const = 0;
|
|
||||||
GLuint id() const { return _id; }
|
|
||||||
|
|
||||||
protected:
|
|
||||||
OpenGLObject() = default;
|
|
||||||
OpenGLObject(const OpenGLObject&) = delete;
|
|
||||||
OpenGLObject& operator=(const OpenGLObject&) = delete;
|
|
||||||
|
|
||||||
virtual void remove() const = 0;
|
|
||||||
|
|
||||||
GLuint _id;
|
|
||||||
};
|
|
||||||
|
|
||||||
class VAO : public virtual OpenGLObject<VAO> {
|
|
||||||
public:
|
|
||||||
using Base = OpenGLObject<VAO>;
|
|
||||||
using Base::Base;
|
using Base::Base;
|
||||||
|
|
||||||
VAO() { glGenVertexArrays(1, &_id); }
|
enum Codes : unsigned int {
|
||||||
|
UNDEFINED, DATA_OVERFLOW, INVALID_BUFFER, HANDLE_OVERWRITE
|
||||||
|
};
|
||||||
|
|
||||||
void bind() const { glBindVertexArray(_id); }
|
static const std::string type;
|
||||||
void unbind() const { glBindVertexArray(0); }
|
static const ErrorCodeMap ErrorMessages;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename BufferType>
|
||||||
|
class Buffer : public OpenGLObject {
|
||||||
|
using Codes = BufferError::Codes;
|
||||||
|
|
||||||
|
public:
|
||||||
|
using OpenGLObject::OpenGLObject;
|
||||||
|
|
||||||
|
Buffer() = default;
|
||||||
|
Buffer(Buffer&& other) {
|
||||||
|
*this = std::move(other);
|
||||||
|
}
|
||||||
|
Buffer& operator=(Buffer&& rhs) {
|
||||||
|
if (_id) { THROW(BufferError(Codes::HANDLE_OVERWRITE)); }
|
||||||
|
_id = rhs._id;
|
||||||
|
_usage = rhs._usage;
|
||||||
|
_size = rhs._size;
|
||||||
|
|
||||||
|
rhs._id = 0;
|
||||||
|
rhs._usage = GL_STATIC_DRAW;
|
||||||
|
rhs._size = 0;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void bind() const override {
|
||||||
|
if (_id) {
|
||||||
|
glBindBuffer(Target, _id);
|
||||||
|
}
|
||||||
|
THROW(BufferError(Codes::INVALID_BUFFER));
|
||||||
|
}
|
||||||
|
virtual void unbind() const override { glBindBuffer(Target, 0); }
|
||||||
|
virtual GLenum usage() const { return _usage; }
|
||||||
|
virtual GLenum usage(GLenum usage_) {
|
||||||
|
_usage = usage_;
|
||||||
|
data(data(), _usage);
|
||||||
|
return _usage;
|
||||||
|
}
|
||||||
|
virtual size_t size() const { return _size; }
|
||||||
|
virtual ByteVector data() const {
|
||||||
|
bind();
|
||||||
|
ByteVector ret(_size);
|
||||||
|
glGetBufferSubData(Target, 0, _size, ret.data());
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
virtual void data(
|
||||||
|
const void* data_,
|
||||||
|
GLsizei size_,
|
||||||
|
GLenum usage_=GL_STATIC_DRAW
|
||||||
|
) {
|
||||||
|
declare();
|
||||||
|
bind();
|
||||||
|
glBufferData(Target, size_, data_, usage_);
|
||||||
|
_size = size_;
|
||||||
|
_usage = usage_;
|
||||||
|
}
|
||||||
|
virtual void data(const ByteVector& data_, GLenum usage_=GL_STATIC_DRAW) {
|
||||||
|
data(data_.data(), data_.size(), usage_);
|
||||||
|
|
||||||
|
}
|
||||||
|
virtual void subdata(void* data_, GLsizeiptr size_, GLintptr offset_=0) {
|
||||||
|
bind();
|
||||||
|
if (offset_+size_ <= _size) {
|
||||||
|
THROW(BufferError(BufferError::Codes::DATA_OVERFLOW));
|
||||||
|
}
|
||||||
|
glBufferSubData(Target, offset_, size_, data_);
|
||||||
|
}
|
||||||
|
virtual void subdata(ByteVector& data_, GLintptr offset_=0) {
|
||||||
|
bind();
|
||||||
|
size_t size_ = data_.size();
|
||||||
|
if (offset_+size_ <= _size) {
|
||||||
|
THROW(BufferError(BufferError::Codes::DATA_OVERFLOW));
|
||||||
|
}
|
||||||
|
glBufferSubData(Target, offset_, data_.size(), data_.data());
|
||||||
|
}
|
||||||
|
// virtual void clear() const { glClearBufferData(Target, ) }
|
||||||
|
static const GLenum Target;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
using Base::_id;
|
using OpenGLObject::_id;
|
||||||
const
|
GLenum _usage = GL_STATIC_DRAW;
|
||||||
|
size_t _size;
|
||||||
void remove() { glDeleteVertexArrays(1, &_id); }
|
virtual GLuint declare() override {
|
||||||
|
if (!_id) {
|
||||||
|
glGenBuffers(1, &_id);
|
||||||
|
}
|
||||||
|
return _id;
|
||||||
|
}
|
||||||
|
virtual void remove() override {
|
||||||
|
if (_id) {
|
||||||
|
glDeleteBuffers(1, &_id);
|
||||||
|
_id = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename BufferType>
|
template<typename BufferType>
|
||||||
class Buffer : public virtual OpenGLObject<Buffer<BufferType>> {
|
const GLenum Buffer<BufferType>::Target = BufferType::Target;
|
||||||
|
|
||||||
|
template <typename BufferType>
|
||||||
|
class ImmutableBuffer : public virtual Buffer<BufferType> {
|
||||||
public:
|
public:
|
||||||
using Base = OpenGLObject<Buffer>;
|
using Base = Buffer<BufferType>;
|
||||||
using Base::Base;
|
using Base::Target;
|
||||||
|
using Base::bind;
|
||||||
|
|
||||||
Buffer(GLenum usage_ = GL_STATIC_DRAW) : usage(usage_) { glGenBuffers(1, &_id); }
|
ImmutableBuffer() = default;
|
||||||
|
ImmutableBuffer(size_t size_, GLenum usage_ = GL_STATIC_DRAW, GLbitfield flags_=0x0) : size(size_), Base::usage(usage_) {
|
||||||
|
glBufferStorage(Target, size, nullptr, flags_);
|
||||||
|
}
|
||||||
|
|
||||||
void bind() const { glBindBuffer(GLTarget, _id); }
|
std::weak_ptr<void> map(GLbitfield access_) {
|
||||||
void unbind() const { glBindBuffer(GLTarget, 0); }
|
if (_map && _mapAccess != access_) { unmap(); }
|
||||||
void data(void* data_, size_t size) const { glBufferData(GLTarget, size, data_, usage); }
|
_mapAccess = access_;
|
||||||
void data(nb::ByteVector& data_) const { glBufferData(GLTarget, data_.size(), data_.data(), usage); }
|
if (!_map) {
|
||||||
void invalidate() const { glInvalidateBufferData(_id); }
|
bind();
|
||||||
|
glMapBuffer(Target, _mapAccess);
|
||||||
|
}
|
||||||
|
return _map;
|
||||||
|
}
|
||||||
|
void unmap() {
|
||||||
|
bind();
|
||||||
|
glUnmapBuffer(Target);
|
||||||
|
_map.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
const size_t size;
|
||||||
const GLenum GLTarget = BufferType::GLTarget;
|
|
||||||
GLenum usage;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
using Base::_id;
|
using Base::_id;
|
||||||
void remove() { glDeleteBuffers(1, &_id); }
|
std::shared_ptr<void> _map = nullptr;
|
||||||
|
GLbitfield _mapAccess = 0;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class VertexBuffer : public virtual Buffer<VertexBuffer> {
|
template<bool Immutable=false>
|
||||||
public:
|
class ArrayBuffer : public Buffer<ArrayBuffer<false>> {
|
||||||
|
using Base = Buffer<ArrayBuffer<false>>;
|
||||||
|
using BufferType = Base;
|
||||||
|
|
||||||
|
public:
|
||||||
|
using Base::Base;
|
||||||
|
using Base::data;
|
||||||
|
ArrayBuffer(const ByteVector&, GLenum usage_=GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
static const GLenum Target = GL_ARRAY_BUFFER;
|
||||||
|
|
||||||
static const GLenum GLTarget = GL_ARRAY_BUFFER;
|
|
||||||
protected:
|
protected:
|
||||||
|
using Base::_usage;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
class ArrayBuffer<true>
|
||||||
|
: public virtual ArrayBuffer<false>, public virtual ImmutableBuffer<ArrayBuffer<true>> {
|
||||||
|
using Base = ArrayBuffer<false>;
|
||||||
|
using BufferType = ImmutableBuffer<ArrayBuffer<true>>;
|
||||||
|
using BufferType::BufferType;
|
||||||
|
|
||||||
|
public:
|
||||||
|
using Base::Target;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class ElementBuffer : public virtual Buffer<ElementBuffer> {
|
template<bool Immutable=false>
|
||||||
public:
|
class ElementBuffer : public virtual Buffer<ElementBuffer<false>> {
|
||||||
|
using Base = Buffer<ElementBuffer<false>>;
|
||||||
|
using BufferType = Base;
|
||||||
|
|
||||||
static const GLenum GLTarget = GL_ELEMENT_ARRAY_BUFFER;
|
public:
|
||||||
|
using Base::Base;
|
||||||
|
static const GLenum Target = GL_ELEMENT_ARRAY_BUFFER;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
template <>
|
||||||
class Buffer : public OpenGLObject {
|
class ElementBuffer<true>
|
||||||
|
: public virtual ElementBuffer<false>, public virtual ImmutableBuffer<ElementBuffer<true>> {
|
||||||
|
using Base = ElementBuffer<false>;
|
||||||
|
using BufferType = ImmutableBuffer<ElementBuffer<true>>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Buffer(
|
using BufferType::BufferType;
|
||||||
GLenum buffer_type,
|
using Base::Target;
|
||||||
GLenum usage=GL_STATIC_DRAW
|
|
||||||
) : _type(buffer_type), _usage(usage) {
|
|
||||||
glGenBuffers(1, &_id);
|
|
||||||
}
|
|
||||||
Buffer(
|
|
||||||
const RawVec& init_data,
|
|
||||||
GLenum buffer_type,
|
|
||||||
GLenum usage=GL_STATIC_DRAW
|
|
||||||
) : _type{buffer_type}, _usage{usage} { data(init_data); }
|
|
||||||
Buffer(
|
|
||||||
unsigned int size,
|
|
||||||
GLenum buffer_type,
|
|
||||||
GLenum usage=GL_STATIC_DRAW
|
|
||||||
) : Buffer(RawVec(size), buffer_type, usage) {}
|
|
||||||
|
|
||||||
Buffer(Buffer&& rhs) { *this = std::move(rhs); }
|
|
||||||
Buffer& operator=(Buffer&& rhs) {
|
|
||||||
remove();
|
|
||||||
_usage = rhs._usage;
|
|
||||||
_id = rhs._id;
|
|
||||||
rhs._id = 0;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
GLenum usage() const { return _usage; }
|
|
||||||
unsigned int id() const { return _id; }
|
|
||||||
GLuint size() const { return _size; }
|
|
||||||
bool isInitialized() const override { return _id && glIsBuffer(_id) && bool(_size); }
|
|
||||||
RawVec data() const {
|
|
||||||
RawVec ret(_size);
|
|
||||||
bind();
|
|
||||||
glGetBufferSubData(_type, 0, _size, ret.data());
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
void bind() const override { glBindBuffer(_type, _id); }
|
|
||||||
void unbind() const override { glBindBuffer(_type, 0); }
|
|
||||||
|
|
||||||
GLenum usage(GLenum usage) {
|
|
||||||
_usage=usage;
|
|
||||||
data(data());
|
|
||||||
return _usage;
|
|
||||||
}
|
|
||||||
void data(const RawVec& set_data) {
|
|
||||||
if (!glfwGetCurrentContext()) {
|
|
||||||
THROW_BUFFER_ERROR("No OpenGL context.");
|
|
||||||
}
|
|
||||||
bind();
|
|
||||||
glBufferData(_type, set_data.size(), set_data.data(), _usage);
|
|
||||||
_size = set_data.size();
|
|
||||||
}
|
|
||||||
void data(const RawVec& newData, unsigned int offset) {
|
|
||||||
if (newData.size()+offset > _size) {
|
|
||||||
THROW_BUFFER_ERROR("Attempting to overflow buffer of capacity " + std::to_string(_size) + ".");
|
|
||||||
}
|
|
||||||
bind();
|
|
||||||
glBufferSubData(_type, offset, newData.size(), newData.data());
|
|
||||||
}
|
|
||||||
void data(void* src, unsigned int offset, unsigned int size) {
|
|
||||||
if (size+offset > _size) {
|
|
||||||
THROW_BUFFER_ERROR("Attempting to overflow buffer of capacity " + std::to_string(_size) + ".");
|
|
||||||
}
|
|
||||||
bind();
|
|
||||||
glBufferSubData(_type, offset, size, src);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
virtual void remove() const override {
|
|
||||||
unbind();
|
|
||||||
if (_id) { glDeleteBuffers(1, &_id); }
|
|
||||||
}
|
|
||||||
|
|
||||||
using OpenGLObject::_id;
|
|
||||||
GLenum _type;
|
|
||||||
GLenum _usage;
|
|
||||||
unsigned int _size = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
class Texture : public OpenGLObject {
|
|
||||||
public:
|
|
||||||
Texture(GLenum tar=GL_TEXTURE_2D) : _target(tar) { glGenTextures(1, &_id); }
|
|
||||||
|
|
||||||
Texture(Texture&& rhs) { *this=std::move(rhs); }
|
|
||||||
Texture& operator=(Texture&& rhs) {
|
|
||||||
_target = rhs._target;
|
|
||||||
_id = rhs._id;
|
|
||||||
rhs._id = 0;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
void bind() const override { glBindTexture(_target, _id); }
|
|
||||||
void unbind() const override { glBindTexture(_target, 0); }
|
|
||||||
bool isInitialized() const override { return _id && glIsTexture(_id); }
|
|
||||||
|
|
||||||
protected:
|
|
||||||
using OpenGLObject::_id;
|
|
||||||
GLenum _target;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Framebuffer : public OpenGLObject {
|
|
||||||
public:
|
|
||||||
|
|
||||||
Framebuffer(GLenum tar=GL_FRAMEBUFFER) : _target(tar) { glGenFramebuffers(1, &_id); }
|
|
||||||
|
|
||||||
Framebuffer(Framebuffer&& rhs) { *this=std::move(rhs); }
|
|
||||||
Framebuffer& operator=(Framebuffer&& rhs) {
|
|
||||||
remove();
|
|
||||||
_target = rhs._target;
|
|
||||||
_id = rhs._id;
|
|
||||||
rhs._id = 0;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
void bind() const override { glBindFramebuffer(_target, _id); }
|
|
||||||
void unbind() const override { glBindFramebuffer(_target, 0); }
|
|
||||||
bool isInitialized() const override { return _id && glIsFramebuffer(_id); }
|
|
||||||
GLenum status() const {
|
|
||||||
bind();
|
|
||||||
return glCheckFramebufferStatus(_target);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected:
|
|
||||||
virtual void remove() const override {
|
|
||||||
unbind();
|
|
||||||
if (_id) { glDeleteFramebuffers(1, &_id); }
|
|
||||||
}
|
|
||||||
using OpenGLObject::_id;
|
|
||||||
GLenum _target;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Renderbuffer : public OpenGLObject {
|
|
||||||
public:
|
|
||||||
Renderbuffer(unsigned int x, unsigned int y, GLenum format=GL_RGBA32F, unsigned int multi=0) : Renderbuffer() {
|
|
||||||
storage(x, y, format, multi);
|
|
||||||
}
|
|
||||||
Renderbuffer() { glGenRenderbuffers(1, &_id); }
|
|
||||||
|
|
||||||
|
|
||||||
Renderbuffer(Renderbuffer&& rhs) { *this = std::move(rhs); }
|
|
||||||
Renderbuffer& operator=(Renderbuffer&& rhs) {
|
|
||||||
_format = rhs._format;
|
|
||||||
_id = rhs._id;
|
|
||||||
rhs._id = 0;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void bind() const override { glBindRenderbuffer(GL_RENDERBUFFER, _id); }
|
|
||||||
virtual void unbind() const override { glBindRenderbuffer(GL_RENDERBUFFER, _id); }
|
|
||||||
virtual bool isInitialized() const override { return _id && glIsRenderbuffer(_id); }
|
|
||||||
|
|
||||||
void storage(unsigned int x, unsigned int y, GLenum format=GL_RGBA32F, unsigned int multi=0) {
|
|
||||||
_sizex = x;
|
|
||||||
_sizey = y;
|
|
||||||
_format = format;
|
|
||||||
_multisample = multi;
|
|
||||||
bind();
|
|
||||||
glRenderbufferStorageMultisample(GL_RENDERBUFFER, _multisample, _format, _sizex, _sizey);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected:
|
|
||||||
virtual void remove() const override {
|
|
||||||
unbind();
|
|
||||||
if (_id) { glDeleteRenderbuffers(1, &_id); }
|
|
||||||
}
|
|
||||||
|
|
||||||
using OpenGLObject::_id;
|
|
||||||
GLenum _format;
|
|
||||||
unsigned int _sizex{0};
|
|
||||||
unsigned int _sizey{0};
|
|
||||||
unsigned int _multisample{0};
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
} // namespace NB
|
} // namespace NB
|
||||||
#endif // _NB_BUFFER
|
#endif // _NB_BUFFER
|
||||||
|
|||||||
@ -1,5 +1,3 @@
|
|||||||
include_directories(./.)
|
|
||||||
|
|
||||||
find_package(OpenGL)
|
find_package(OpenGL)
|
||||||
add_subdirectory(${GLFW_PATH} ${GLFW_PATH}/build)
|
add_subdirectory(${GLFW_PATH} ${GLFW_PATH}/build)
|
||||||
|
|
||||||
@ -10,8 +8,11 @@ set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
|
|||||||
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
|
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
|
||||||
|
|
||||||
toAbsolutePath(NB_GRAPHICS_SOURCE
|
toAbsolutePath(NB_GRAPHICS_SOURCE
|
||||||
./src/Window.cpp
|
|
||||||
./src/Buffers.cpp
|
./src/Buffers.cpp
|
||||||
|
./src/OGLObjects.cpp
|
||||||
|
./src/Textures.cpp
|
||||||
|
./src/VertexArray.cpp
|
||||||
|
./src/Window.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
toAbsolutePath(NB_GRAPHICS_INCLUDE
|
toAbsolutePath(NB_GRAPHICS_INCLUDE
|
||||||
@ -19,8 +20,10 @@ toAbsolutePath(NB_GRAPHICS_INCLUDE
|
|||||||
./Camera.hpp
|
./Camera.hpp
|
||||||
./Draw.hpp
|
./Draw.hpp
|
||||||
./GLLoad.hpp
|
./GLLoad.hpp
|
||||||
|
./OGLObjects.hpp
|
||||||
./shader.hpp
|
./shader.hpp
|
||||||
./VAOManager.hpp
|
./Textures.hpp
|
||||||
|
./VertexArray.hpp
|
||||||
./Window.hpp
|
./Window.hpp
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -31,4 +34,11 @@ add_library(NBGraphics
|
|||||||
${NB_GRAPHICS_SOURCE}
|
${NB_GRAPHICS_SOURCE}
|
||||||
${GLAD_PATH}/src/glad.c
|
${GLAD_PATH}/src/glad.c
|
||||||
)
|
)
|
||||||
target_link_libraries(NBGraphics glfw3)
|
target_link_libraries(NBGraphics glfw)
|
||||||
|
|
||||||
|
target_include_directories(NBGraphics PUBLIC ./.)
|
||||||
|
|
||||||
|
if (NB_BUILD_TESTS)
|
||||||
|
add_subdirectory(./tests)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
|||||||
@ -2,13 +2,14 @@
|
|||||||
#ifndef _NB_CAMERA
|
#ifndef _NB_CAMERA
|
||||||
#define _NB_CAMERA
|
#define _NB_CAMERA
|
||||||
|
|
||||||
#include <GLLoad.h>
|
#include "GLLoad.hpp"
|
||||||
|
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <glm/gtc/matrix_inverse.hpp>
|
#include <glm/gtc/matrix_inverse.hpp>
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
#include <glm/gtc/type_ptr.hpp>
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
|
|
||||||
namespace NB {
|
namespace nb {
|
||||||
|
|
||||||
class Camera {
|
class Camera {
|
||||||
public:
|
public:
|
||||||
|
|||||||
@ -5,31 +5,4 @@
|
|||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace NB {
|
|
||||||
|
|
||||||
static unsigned int __NB_GL_DEBUG_ERROR_CODE__;
|
|
||||||
|
|
||||||
static std::string formatDebugString(const std::string& msg, const std::string& file="", int line=-1) {
|
|
||||||
std::string ret = "";
|
|
||||||
if (file != "") {
|
|
||||||
ret += "In file " + file;
|
|
||||||
if (line >= 0) {
|
|
||||||
ret += " at line " + std::to_string(line);
|
|
||||||
}
|
|
||||||
ret += ":\n\t";
|
|
||||||
}
|
|
||||||
return ret + msg;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#define NB_GL_DEBUG_THROW(cmd, when) while(NB::__NB_GL_DEBUG_ERROR_CODE__=glGetError()){\
|
|
||||||
std::cout << "[GL ERROR]: " << NB::__NB_GL_DEBUG_ERROR_CODE__;\
|
|
||||||
std::cout << " " << when << " " << cmd << "\n";\
|
|
||||||
}
|
|
||||||
#define NB_GL_DEBUG(cmd) NB_GL_DEBUG_THROW(#cmd, "before"); cmd; NB_GL_DEBUG_THROW(#cmd, "after");
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
45
engine/NBGraphics/OGLObjects.hpp
Normal file
45
engine/NBGraphics/OGLObjects.hpp
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef _NB_OGL_OBJECTS
|
||||||
|
#define _NB_OGL_OBJECTS
|
||||||
|
|
||||||
|
#include "GLLoad.hpp"
|
||||||
|
|
||||||
|
#include <NBCore/Errors.hpp>
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
class OGLError : public Error<OGLError> {
|
||||||
|
using Base = Error<OGLError>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
using Base::Base;
|
||||||
|
|
||||||
|
enum Codes : unsigned int {
|
||||||
|
UNDEFINED, HANGING_OBJECT
|
||||||
|
};
|
||||||
|
|
||||||
|
static const std::string type;
|
||||||
|
static const ErrorCodeMap ErrorMessages;
|
||||||
|
};
|
||||||
|
|
||||||
|
class OpenGLObject {
|
||||||
|
public:
|
||||||
|
OpenGLObject(const OpenGLObject&) = delete;
|
||||||
|
OpenGLObject& operator=(const OpenGLObject&) = delete;
|
||||||
|
~OpenGLObject() { remove(); }
|
||||||
|
|
||||||
|
virtual void bind() const = 0;
|
||||||
|
virtual void unbind() const = 0;
|
||||||
|
GLuint id() const { return _id; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
OpenGLObject() = default;
|
||||||
|
|
||||||
|
virtual GLuint declare() = 0;
|
||||||
|
virtual void remove() { this->remove(); };
|
||||||
|
|
||||||
|
GLuint _id;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace nb
|
||||||
|
#endif // _NB_OGL_OBJECTS
|
||||||
61
engine/NBGraphics/Textures.hpp
Normal file
61
engine/NBGraphics/Textures.hpp
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef _NB_TEXTURES
|
||||||
|
#define _NB_TEXTURES
|
||||||
|
|
||||||
|
#include "Buffers.hpp"
|
||||||
|
#include "OGLObjects.hpp"
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
template<bool Immutable=false>
|
||||||
|
class TextureBuffer : public virtual Buffer<TextureBuffer<false>> {
|
||||||
|
using Base = Buffer<TextureBuffer<false>>;
|
||||||
|
using BufferType = Base;
|
||||||
|
|
||||||
|
public:
|
||||||
|
using Base::Base;
|
||||||
|
static const GLenum Target = GL_TEXTURE_BUFFER;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
class TextureBuffer<true>
|
||||||
|
: public virtual TextureBuffer<false>, public virtual ImmutableBuffer<TextureBuffer<true>> {
|
||||||
|
using Base = TextureBuffer<false>;
|
||||||
|
using BufferType = ImmutableBuffer<TextureBuffer<true>>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
using BufferType::BufferType;
|
||||||
|
using Base::Target;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Texture : public OpenGLObject {
|
||||||
|
using Base = OpenGLObject;
|
||||||
|
public:
|
||||||
|
using Base::Base;
|
||||||
|
using Base::id;
|
||||||
|
|
||||||
|
Texture(GLenum);
|
||||||
|
|
||||||
|
virtual void bind() const override { glBindTexture(target, _id); }
|
||||||
|
virtual void unbind() const override { glBindTexture(target, 0); }
|
||||||
|
|
||||||
|
const GLenum target;
|
||||||
|
|
||||||
|
friend Base;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
using Base::_id;
|
||||||
|
|
||||||
|
virtual void remove() override {
|
||||||
|
if (_id) {
|
||||||
|
glDeleteTextures(0, &_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace nb
|
||||||
|
#endif // _NB_TEXTURES
|
||||||
@ -1,75 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_VAO_MANAGER
|
|
||||||
#define _NB_VAO_MANAGER
|
|
||||||
|
|
||||||
#include <GLLoad.h>
|
|
||||||
|
|
||||||
#include <exception>
|
|
||||||
#include <memory>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "Buffers.h"
|
|
||||||
|
|
||||||
#define THROW_VAO_ERROR(msg) throw VAOError(msg, __FILE__, __LINE__);
|
|
||||||
|
|
||||||
namespace NB {
|
|
||||||
|
|
||||||
class VAOError : public std::runtime_error {
|
|
||||||
public:
|
|
||||||
const bool error;
|
|
||||||
VAOError(const std::string& msg, const std::string& file="", int line=-1)
|
|
||||||
: std::runtime_error(NB::formatDebugString(msg, file, line)), error(true) {}
|
|
||||||
VAOError(bool isError=true) : std::runtime_error(""), error(isError) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
class VAOManager : public OpenGLObject {
|
|
||||||
public:
|
|
||||||
typedef std::shared_ptr<Buffer> BufferManagerPointer;
|
|
||||||
//typedef BufferManager BufferManagerPointer;
|
|
||||||
|
|
||||||
VAOManager();
|
|
||||||
VAOManager(
|
|
||||||
std::vector<BufferManagerPointer>,
|
|
||||||
BufferManagerPointer elmt_buf = nullptr,
|
|
||||||
const VertexAttributeList& vert_attrs = {}
|
|
||||||
);
|
|
||||||
VAOManager(
|
|
||||||
BufferManagerPointer vert_bufs,
|
|
||||||
BufferManagerPointer elmt_buf = nullptr,
|
|
||||||
const VertexAttributeList& vert_attrs = {}
|
|
||||||
) : VAOManager(std::vector<BufferManagerPointer>(1, vert_bufs), elmt_buf, vert_attrs) {}
|
|
||||||
|
|
||||||
VAOManager(VAOManager&& rhs);
|
|
||||||
VAOManager& operator=(VAOManager&& rhs);
|
|
||||||
|
|
||||||
VertexAttributeList getLayout() const;
|
|
||||||
GLuint id() const;
|
|
||||||
unsigned int vertSize(GLuint) const;
|
|
||||||
std::vector<BufferManagerPointer> getVertexBuffers() const;
|
|
||||||
bool isInitialized() const override;
|
|
||||||
RawVec attributeData(unsigned int);
|
|
||||||
|
|
||||||
RawVec attributeData(unsigned int, const RawVec&);
|
|
||||||
void bind() const;
|
|
||||||
void unbind() const;
|
|
||||||
void addVBO(BufferManagerPointer);
|
|
||||||
void changeEBO(BufferManagerPointer);
|
|
||||||
VertexAttributeList addVertexAttributes(const VertexAttributeList&);
|
|
||||||
VertexAttributeList addVertexAttributes(VertexAttribute);
|
|
||||||
VertexAttributeList generate();
|
|
||||||
VertexAttributeList generate(VertexAttributeList);
|
|
||||||
VertexAttributeList changeLayout(unsigned int, VertexAttributePointer);
|
|
||||||
|
|
||||||
VAOError checkValid(const VertexAttributeList&);
|
|
||||||
|
|
||||||
private:
|
|
||||||
void remove() const override;
|
|
||||||
|
|
||||||
using OpenGLObject::_id;
|
|
||||||
std::map<GLuint, BufferManagerPointer> _vert_buffers;
|
|
||||||
BufferManagerPointer _elmt_buffer = nullptr;
|
|
||||||
VertexAttributeList _vert_attrs;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace NB
|
|
||||||
#endif // _NB_VAO_MANAGER
|
|
||||||
177
engine/NBGraphics/VertexArray.hpp
Normal file
177
engine/NBGraphics/VertexArray.hpp
Normal file
@ -0,0 +1,177 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef _NB_VERTEX_ARRAY
|
||||||
|
#define _NB_VERTEX_ARRAY
|
||||||
|
|
||||||
|
#include "GLLoad.hpp"
|
||||||
|
|
||||||
|
#include <NBCore/Errors.hpp>
|
||||||
|
|
||||||
|
#include "Buffers.hpp"
|
||||||
|
#include "OGLObjects.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
struct VertexAttributeLayout {
|
||||||
|
int32_t offset = 0;
|
||||||
|
GLsizei stride = 0;
|
||||||
|
GLuint divisor = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VertexAttribute {
|
||||||
|
GLint GLSLSize;
|
||||||
|
GLenum GLSLType;
|
||||||
|
GLboolean GLSLNormalization;
|
||||||
|
VertexAttributeLayout layout;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VertexAttributePointer {
|
||||||
|
VertexAttribute attribute;
|
||||||
|
GLuint buffer;
|
||||||
|
GLuint index;
|
||||||
|
};
|
||||||
|
|
||||||
|
static bool isIndexInVertexAttribute(int i, const VertexAttribute& va) {
|
||||||
|
if ((i -= va.layout.offset) < 0) { return false; }
|
||||||
|
return (i%va.layout.stride) < GLSLTypeSize(va.GLSLType) * va.GLSLSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef std::vector<VertexAttribute> VertexAttributeList;
|
||||||
|
typedef std::vector<VertexAttributePointer> VertexAttributePointerList;
|
||||||
|
|
||||||
|
class VAOError : public Error<VAOError> {
|
||||||
|
using Base = Error<VAOError>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
using Base::Base;
|
||||||
|
|
||||||
|
enum Codes : unsigned int {
|
||||||
|
UNDEFINED, INVALID_ATTRIBUTE, INVALID_VAO
|
||||||
|
};
|
||||||
|
|
||||||
|
static const std::string type;
|
||||||
|
static const ErrorCodeMap ErrorMessages;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <bool Immutable=false>
|
||||||
|
class VertexBuffer : public ArrayBuffer<false>{
|
||||||
|
using Base = ArrayBuffer<false>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
template<typename... Args>
|
||||||
|
VertexBuffer(const VertexAttributeList& vas={}, Args... args)
|
||||||
|
: attributes(vas) {
|
||||||
|
if (sizeof...(Args)) {
|
||||||
|
data(args...);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
VertexBuffer(VertexBuffer&& rhs)
|
||||||
|
: attributes(rhs.attributes), Base(std::move(rhs)){}
|
||||||
|
|
||||||
|
|
||||||
|
using Base::Target;
|
||||||
|
const VertexAttributeList attributes;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
using Base::_usage;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
class VertexBuffer<true>
|
||||||
|
: public virtual VertexBuffer<false>, public virtual ImmutableBuffer<ArrayBuffer<true>> {
|
||||||
|
using Base = ArrayBuffer<false>;
|
||||||
|
using BufferType = ImmutableBuffer<ArrayBuffer<true>>;
|
||||||
|
using BufferType::BufferType;
|
||||||
|
|
||||||
|
public:
|
||||||
|
using Base::Target;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
class VAO : public OpenGLObject {
|
||||||
|
using Base = OpenGLObject;
|
||||||
|
using Codes = VAOError::Codes;
|
||||||
|
|
||||||
|
public:
|
||||||
|
using Base::Base;
|
||||||
|
VAO() = default;
|
||||||
|
VAO(const VertexAttributePointerList&);
|
||||||
|
VAO(VAO&&);
|
||||||
|
|
||||||
|
VAO& operator=(VAO&&);
|
||||||
|
|
||||||
|
virtual void bind() const override {
|
||||||
|
if(_id) {
|
||||||
|
glBindVertexArray(_id);
|
||||||
|
} else {
|
||||||
|
THROW(VAOError(Codes::INVALID_VAO));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
virtual void unbind() const override { glBindVertexArray(0); }
|
||||||
|
|
||||||
|
VertexAttributePointerList attributes() const;
|
||||||
|
VertexAttributePointerList attributes(const VertexAttributePointerList&);
|
||||||
|
VertexAttributePointer attr(GLuint) const;
|
||||||
|
void enable() const;
|
||||||
|
void enable(GLuint) const;
|
||||||
|
void disable() const;
|
||||||
|
void disable(GLuint) const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
using Base::_id;
|
||||||
|
VertexAttributePointerList _attrs;
|
||||||
|
|
||||||
|
virtual void remove() override {
|
||||||
|
if(_id) {
|
||||||
|
disable();
|
||||||
|
bind();
|
||||||
|
glDeleteVertexArrays(1, &_id);
|
||||||
|
_id = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
virtual GLuint declare() override {
|
||||||
|
if (!_id) {
|
||||||
|
glGenVertexArrays(1, &_id);
|
||||||
|
}
|
||||||
|
return _id;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
class VertexGroup {
|
||||||
|
public:
|
||||||
|
VertexGroup(VertexGroup&&);
|
||||||
|
VertexGroup& operator=(VertexGroup&&);
|
||||||
|
|
||||||
|
void bind() const {
|
||||||
|
_elmt_buf.bind();
|
||||||
|
_vao.bind();
|
||||||
|
}
|
||||||
|
void unbind() const {
|
||||||
|
_vao.bind();
|
||||||
|
_elmt_buf.unbind();
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexBuffer<false>& buffer(size_t);
|
||||||
|
size_t buffer(VertexBuffer<false>&&);
|
||||||
|
std::vector<std::shared_ptr<VertexBuffer<false>>> buffers();
|
||||||
|
size_t buffers(std::vector<VertexBuffer<false>>&&);
|
||||||
|
VertexBuffer<false>&& dropBuffer(size_t);
|
||||||
|
|
||||||
|
VertexAttributePointer attribute(size_t) const;
|
||||||
|
void enableAttr(GLuint);
|
||||||
|
void disableAttr(GLuint);
|
||||||
|
void enableBuffer(size_t);
|
||||||
|
void disableBuffer(size_t);
|
||||||
|
void enable();
|
||||||
|
void disable();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::vector<std::shared_ptr<VertexBuffer<false>>> _vertex_data;
|
||||||
|
ElementBuffer<false> _elmt_buf;
|
||||||
|
VAO _vao;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace nb
|
||||||
|
#endif // _NB_VERTEX_ARRAY
|
||||||
@ -4,12 +4,14 @@
|
|||||||
|
|
||||||
#include "GLLoad.hpp"
|
#include "GLLoad.hpp"
|
||||||
|
|
||||||
#include <atomic>
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include <NBCore/Errors.hpp>
|
||||||
|
#include <NBCore/Utils.hpp>
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
class GLError : public std::runtime_error {
|
class GLError : public std::runtime_error {
|
||||||
@ -17,9 +19,32 @@ public:
|
|||||||
GLError(const std::string&);
|
GLError(const std::string&);
|
||||||
};
|
};
|
||||||
|
|
||||||
class WindowError : public std::runtime_error {
|
class OpenGLError : public Error<OpenGLError> {
|
||||||
|
using Base = Error<OpenGLError>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
WindowError(const std::string&);
|
using Base::Base;
|
||||||
|
|
||||||
|
enum Codes : unsigned int {
|
||||||
|
UNDEFINED, INIT_FAILED, GLFW_INTIALIZED, GLAD_FAILED
|
||||||
|
};
|
||||||
|
|
||||||
|
static const std::string type;
|
||||||
|
static const ErrorCodeMap ErrorMessages;
|
||||||
|
};
|
||||||
|
|
||||||
|
class WindowError : public Error<WindowError> {
|
||||||
|
using Base = Error<WindowError>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
using Base::Base;
|
||||||
|
|
||||||
|
enum Codes : unsigned int {
|
||||||
|
UNDEFINED, INITIALIZED_WINDOW, NO_GLFW, INIT_FAILED
|
||||||
|
};
|
||||||
|
|
||||||
|
static const std::string type;
|
||||||
|
static const ErrorCodeMap ErrorMessages;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Window {
|
class Window {
|
||||||
|
|||||||
@ -2,4 +2,20 @@
|
|||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
|
using BufferErrorCodes = BufferError::Codes;
|
||||||
|
const std::string BufferError::type = "nb::BufferError";
|
||||||
|
const ErrorCodeMap BufferError::ErrorMessages = {
|
||||||
|
{ BufferErrorCodes::UNDEFINED, "Error" },
|
||||||
|
{ BufferErrorCodes::DATA_OVERFLOW, "Attempting buffer overflow" },
|
||||||
|
{ BufferErrorCodes::INVALID_BUFFER, "Attempting operation on invalid buffer" },
|
||||||
|
{BufferErrorCodes::HANDLE_OVERWRITE, "Attempting to overwrite active buffer"}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
ArrayBuffer<false>::ArrayBuffer(const ByteVector& data_, GLenum usage_)
|
||||||
|
: BufferType() {
|
||||||
|
data(data_, usage_);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,5 +1,6 @@
|
|||||||
#include "Camera.h"
|
#include "Camera.hpp"
|
||||||
namespace NB {
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
// Camera class
|
// Camera class
|
||||||
Camera::Camera(const Vec3& pos, const Vec3& tar, const Vec3& up) {
|
Camera::Camera(const Vec3& pos, const Vec3& tar, const Vec3& up) {
|
||||||
|
|||||||
11
engine/NBGraphics/src/OGLObjects.cpp
Normal file
11
engine/NBGraphics/src/OGLObjects.cpp
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#include "OGLObjects.hpp"
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
const std::string OGLError::type = "nb::OGLError";
|
||||||
|
const ErrorCodeMap OGLError::ErrorMessages = {
|
||||||
|
{ OGLError::Codes::UNDEFINED, "Error" },
|
||||||
|
{OGLError::Codes::HANGING_OBJECT, "Attempting to leave a hanging OpenGL object"}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace nb
|
||||||
10
engine/NBGraphics/src/Textures.cpp
Normal file
10
engine/NBGraphics/src/Textures.cpp
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#include "Textures.hpp"
|
||||||
|
|
||||||
|
namespace nb{
|
||||||
|
|
||||||
|
Texture::Texture(GLenum target_) : target(target_) {
|
||||||
|
glGenTextures(1, &_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace nb
|
||||||
@ -1,225 +0,0 @@
|
|||||||
#include "VAOManager.hpp"
|
|
||||||
|
|
||||||
namespace NB {
|
|
||||||
|
|
||||||
using BufferManagerPointer = std::shared_ptr<Buffer>;
|
|
||||||
|
|
||||||
VAOManager::VAOManager() { glGenVertexArrays(1, &_id); }
|
|
||||||
|
|
||||||
VAOManager::VAOManager(
|
|
||||||
std::vector<BufferManagerPointer> vert_bufs,
|
|
||||||
BufferManagerPointer elmt_buf,
|
|
||||||
const VertexAttributeList& vert_attrs
|
|
||||||
) : VAOManager() {
|
|
||||||
_elmt_buffer = elmt_buf;
|
|
||||||
for (BufferManagerPointer vb : vert_bufs) {
|
|
||||||
_vert_buffers[vb->id()] = vb;
|
|
||||||
}
|
|
||||||
if (vert_attrs.size()!=0) {
|
|
||||||
generate(vert_attrs);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
VAOManager::VAOManager(VAOManager&& rhs) { *this = std::move(rhs); }
|
|
||||||
|
|
||||||
VAOManager& VAOManager::operator=(VAOManager&& rhs) {
|
|
||||||
remove();
|
|
||||||
_vert_buffers = rhs._vert_buffers;
|
|
||||||
_elmt_buffer = rhs._elmt_buffer;
|
|
||||||
_vert_attrs = rhs._vert_attrs;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexAttributeList VAOManager::getLayout() const { return _vert_attrs; }
|
|
||||||
|
|
||||||
GLuint VAOManager::id() const { return _id; }
|
|
||||||
|
|
||||||
bool VAOManager::isInitialized() const {
|
|
||||||
return _id && glIsVertexArray(_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
void VAOManager::remove() const {
|
|
||||||
bind();
|
|
||||||
for (int i{0}; i < _vert_attrs.size(); ++i) {
|
|
||||||
glDisableVertexAttribArray(i);
|
|
||||||
}
|
|
||||||
unbind();
|
|
||||||
glDisableVertexAttribArray(_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int VAOManager::vertSize(GLuint id) const {
|
|
||||||
unsigned int size = 0;
|
|
||||||
for (const VertexAttribute& va : _vert_attrs) {
|
|
||||||
if (va.ptr.buffer == id) {
|
|
||||||
size += va.GLSLSize * GLSLTypeSize(va.GLSLType);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<BufferManagerPointer> VAOManager::getVertexBuffers() const {
|
|
||||||
std::vector<BufferManagerPointer> ret;
|
|
||||||
for (const auto& i : _vert_buffers) {
|
|
||||||
ret.emplace_back(i.second);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
RawVec VAOManager::attributeData(unsigned int i) {
|
|
||||||
if (i <= _vert_attrs.size()) {
|
|
||||||
throw std::out_of_range("No vertex attribute exists for specified index");
|
|
||||||
}
|
|
||||||
VertexAttribute attr = _vert_attrs[i];
|
|
||||||
BufferManagerPointer buffer = _vert_buffers[attr.ptr.buffer];
|
|
||||||
RawVec data = buffer->data();
|
|
||||||
unsigned int attr_size = attr.GLSLSize*GLSLTypeSize(attr.GLSLType);
|
|
||||||
RawVec ret((buffer->size()-attr.ptr.offset-attr_size)*attr_size/attr.ptr.stride);
|
|
||||||
int data_size = data.size();
|
|
||||||
for (int i{0}; i < data_size; ++i) {
|
|
||||||
if (isIndexInVertexAttribute(i, attr)) {
|
|
||||||
ret[i] = data[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
RawVec VAOManager::attributeData(unsigned int i, const RawVec& new_data) {
|
|
||||||
if (i >= _vert_attrs.size()) {
|
|
||||||
throw std::out_of_range("No vertex attribute exists for specified index.");
|
|
||||||
}
|
|
||||||
VertexAttribute attr = _vert_attrs[i];
|
|
||||||
BufferManagerPointer buffer = _vert_buffers[attr.ptr.buffer];
|
|
||||||
unsigned int attr_size = attr.GLSLSize*GLSLTypeSize(attr.GLSLType);
|
|
||||||
unsigned int offset = attr.ptr.offset;
|
|
||||||
unsigned int num_verts = buffer->size() / vertSize(buffer->id());
|
|
||||||
if (num_verts*attr_size != new_data.size()) {
|
|
||||||
// WHY HERE????
|
|
||||||
THROW_VAO_ERROR(
|
|
||||||
"Input data size of " + std::to_string(new_data.size())
|
|
||||||
+ " does not match data size for requested vertex attribute of total size " + std::to_string(num_verts*attr_size)
|
|
||||||
+ "."
|
|
||||||
);
|
|
||||||
}
|
|
||||||
uint64_t pos = uint64_t(new_data.data());
|
|
||||||
for (int i{0}; i<num_verts; i++) {
|
|
||||||
buffer->data((void*)(pos+i*attr_size), offset+i*attr.ptr.stride, attr_size);
|
|
||||||
}
|
|
||||||
return RawVec(new_data);
|
|
||||||
}
|
|
||||||
|
|
||||||
void VAOManager::bind() const {
|
|
||||||
glBindVertexArray(_id);
|
|
||||||
if (_elmt_buffer) { _elmt_buffer->bind(); }
|
|
||||||
}
|
|
||||||
|
|
||||||
void VAOManager::unbind() const {
|
|
||||||
glBindVertexArray(0);
|
|
||||||
if (_elmt_buffer) { _elmt_buffer->unbind(); }
|
|
||||||
}
|
|
||||||
|
|
||||||
void VAOManager::addVBO(BufferManagerPointer vert_buf) {
|
|
||||||
GLuint vert_id = vert_buf->id();
|
|
||||||
if (_vert_buffers.find(vert_id) == _vert_buffers.end()) {
|
|
||||||
_vert_buffers[vert_buf->id()] = vert_buf;
|
|
||||||
} else {
|
|
||||||
THROW_VAO_ERROR("Attempting to add identical VBO id of " + std::to_string(vert_id) + ".");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void VAOManager::changeEBO(BufferManagerPointer ebo) {
|
|
||||||
_elmt_buffer = ebo;
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexAttributeList VAOManager::addVertexAttributes(const VertexAttributeList& vert_attrs) {
|
|
||||||
VertexAttributeList curr_vas = _vert_attrs;
|
|
||||||
curr_vas.insert(curr_vas.end(), vert_attrs.begin(), vert_attrs.end());
|
|
||||||
try {
|
|
||||||
throw checkValid(curr_vas);
|
|
||||||
} catch (VAOError vaoe) {
|
|
||||||
if (vaoe.error) {
|
|
||||||
THROW_VAO_ERROR(vaoe.what());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_vert_attrs = curr_vas;
|
|
||||||
generate();
|
|
||||||
return _vert_attrs;
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexAttributeList VAOManager::addVertexAttributes(VertexAttribute vert_attr) {
|
|
||||||
return addVertexAttributes({vert_attr});
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexAttributeList VAOManager::generate() {
|
|
||||||
bind();
|
|
||||||
unsigned int num_attrs = _vert_attrs.size();
|
|
||||||
VertexAttribute* va;
|
|
||||||
for (int i = 0; i < num_attrs; ++i) {
|
|
||||||
va = &(_vert_attrs[i]);
|
|
||||||
_vert_buffers[va->ptr.buffer]->bind();
|
|
||||||
glVertexAttribPointer(
|
|
||||||
i,
|
|
||||||
va->GLSLSize,
|
|
||||||
va->GLSLType,
|
|
||||||
va->GLSLNormalization,
|
|
||||||
va->ptr.stride,
|
|
||||||
(void*)va->ptr.offset
|
|
||||||
);
|
|
||||||
glVertexAttribDivisor(i, va->ptr.divisor);
|
|
||||||
glEnableVertexAttribArray(i);
|
|
||||||
}
|
|
||||||
if (_elmt_buffer != nullptr) {
|
|
||||||
_elmt_buffer->bind();
|
|
||||||
}
|
|
||||||
unbind();
|
|
||||||
return _vert_attrs;
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexAttributeList VAOManager::generate(VertexAttributeList vert_attrs) {
|
|
||||||
try {
|
|
||||||
throw checkValid(vert_attrs);
|
|
||||||
} catch (VAOError vaoe) {
|
|
||||||
if (vaoe.error) {
|
|
||||||
THROW_VAO_ERROR(vaoe.what());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_vert_attrs.swap(vert_attrs);
|
|
||||||
return generate();
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexAttributeList VAOManager::changeLayout(unsigned int i, VertexAttributePointer vap) {
|
|
||||||
_vert_attrs[i].ptr = vap;
|
|
||||||
bind();
|
|
||||||
const VertexAttribute* va = &(_vert_attrs[i]);
|
|
||||||
glDisableVertexAttribArray(i);
|
|
||||||
_vert_buffers[va->ptr.buffer]->bind();
|
|
||||||
glVertexAttribPointer(
|
|
||||||
_id,
|
|
||||||
va->GLSLSize,
|
|
||||||
va->GLSLType,
|
|
||||||
va->GLSLNormalization,
|
|
||||||
va->ptr.stride,
|
|
||||||
(void*)va->ptr.offset
|
|
||||||
);
|
|
||||||
glVertexAttribDivisor(i, va->ptr.divisor);
|
|
||||||
glEnableVertexAttribArray(i);
|
|
||||||
unbind();
|
|
||||||
return _vert_attrs;
|
|
||||||
}
|
|
||||||
|
|
||||||
VAOError VAOManager::checkValid(const VertexAttributeList& vert_attrs) {
|
|
||||||
GLuint va_id;
|
|
||||||
unsigned int num_attrs = vert_attrs.size();
|
|
||||||
for (int i = 0; i < num_attrs; ++i) {
|
|
||||||
va_id = vert_attrs[i].ptr.buffer;
|
|
||||||
if (_vert_buffers.find(va_id) == _vert_buffers.cend()) {
|
|
||||||
return VAOError("Attempting to point to unknown VBO of id " + std::to_string(va_id) +
|
|
||||||
" at Vertex Attribute " + std::to_string(i) + ".");
|
|
||||||
}
|
|
||||||
if (vert_attrs[i].ptr.stride<0) {
|
|
||||||
return VAOError("Invalid stride value at Vertex Attribute " + std::to_string(i) + ".");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return VAOError(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace NB
|
|
||||||
225
engine/NBGraphics/src/VertexArray.cpp
Normal file
225
engine/NBGraphics/src/VertexArray.cpp
Normal file
@ -0,0 +1,225 @@
|
|||||||
|
#include "VertexArray.hpp"
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
using VAOErrorCodes = VAOError::Codes;
|
||||||
|
const std::string VAOError::type = "nb::VAOError";
|
||||||
|
const ErrorCodeMap VAOError::ErrorMessages = {
|
||||||
|
{ VAOErrorCodes::UNDEFINED, "Error" },
|
||||||
|
{ VAOErrorCodes::INVALID_ATTRIBUTE, "Targeting invalid attribute"},
|
||||||
|
{ VAOErrorCodes::INVALID_VAO, "Targeting invalid VAO" }
|
||||||
|
};
|
||||||
|
|
||||||
|
VAO::VAO(const VertexAttributePointerList& attr_ptrs) {
|
||||||
|
attributes(attr_ptrs);
|
||||||
|
}
|
||||||
|
|
||||||
|
VAO::VAO(VAO&& other) {
|
||||||
|
*this = std::move(other);
|
||||||
|
}
|
||||||
|
|
||||||
|
VAO& VAO::operator=(VAO&& rhs) {
|
||||||
|
_id = rhs._id;
|
||||||
|
_attrs = rhs._attrs;
|
||||||
|
|
||||||
|
rhs._id = 0;
|
||||||
|
rhs._attrs = {};
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexAttributePointerList VAO::attributes() const {
|
||||||
|
return _attrs;
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexAttributePointerList VAO::attributes(const VertexAttributePointerList& attrs_) {
|
||||||
|
declare();
|
||||||
|
disable();
|
||||||
|
_attrs = attrs_;
|
||||||
|
bind();
|
||||||
|
for (auto attr_ptr : attrs_) {
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, attr_ptr.buffer);
|
||||||
|
GLuint idx = attr_ptr.index;
|
||||||
|
glVertexAttribPointer(
|
||||||
|
idx,
|
||||||
|
attr_ptr.attribute.GLSLSize,
|
||||||
|
attr_ptr.attribute.GLSLType,
|
||||||
|
attr_ptr.attribute.GLSLNormalization,
|
||||||
|
attr_ptr.attribute.layout.stride,
|
||||||
|
(void*)attr_ptr.attribute.layout.offset
|
||||||
|
);
|
||||||
|
glEnableVertexAttribArray(idx);
|
||||||
|
}
|
||||||
|
unbind();
|
||||||
|
return _attrs;
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexAttributePointer VAO::attr(GLuint idx) const {
|
||||||
|
for (auto attr_ptr : _attrs) {
|
||||||
|
if (idx == attr_ptr.index) {
|
||||||
|
return attr_ptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
THROW(BufferError(Codes::INVALID_ATTRIBUTE));
|
||||||
|
}
|
||||||
|
|
||||||
|
void VAO::enable() const {
|
||||||
|
if (_id) {
|
||||||
|
bind();
|
||||||
|
for(auto attr_ptr : _attrs) {
|
||||||
|
glEnableVertexAttribArray(attr_ptr.index);
|
||||||
|
}
|
||||||
|
unbind();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void VAO::enable(GLuint idx) const {
|
||||||
|
if(_id) {
|
||||||
|
bind();
|
||||||
|
glEnableVertexAttribArray(attr(idx).index);
|
||||||
|
unbind();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void VAO::disable() const {
|
||||||
|
if(_id) {
|
||||||
|
bind();
|
||||||
|
for(auto attr_ptr : _attrs) {
|
||||||
|
glDisableVertexAttribArray(attr_ptr.index);
|
||||||
|
}
|
||||||
|
unbind();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void VAO::disable(GLuint idx) const {
|
||||||
|
if(_id) {
|
||||||
|
bind();
|
||||||
|
glDisableVertexAttribArray(attr(idx).index);
|
||||||
|
unbind();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexGroup::VertexGroup(VertexGroup&& other) {
|
||||||
|
*this = std::move(other);
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexGroup& VertexGroup::operator=(VertexGroup&& rhs) {
|
||||||
|
_vertex_data = rhs._vertex_data;
|
||||||
|
_elmt_buf = std::move(rhs._elmt_buf);
|
||||||
|
_vao = std::move(rhs._vao);
|
||||||
|
|
||||||
|
rhs._vertex_data = {};
|
||||||
|
rhs._elmt_buf = ElementBuffer<false>();
|
||||||
|
rhs._vao = VAO();
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t VertexGroup::buffer(VertexBuffer<false>&& buf_) {
|
||||||
|
_vertex_data.emplace_back(
|
||||||
|
std::make_shared<VertexBuffer<false>>(std::move(buf_))
|
||||||
|
);
|
||||||
|
VertexBuffer<false>* buf_ptr = _vertex_data.back().get();
|
||||||
|
VertexAttributePointerList attrs = _vao.attributes();
|
||||||
|
GLuint idx = attrs.size();
|
||||||
|
for (auto attr : buf_ptr->attributes) {
|
||||||
|
attrs.emplace_back(
|
||||||
|
VertexAttributePointer{attr, buf_ptr->id(),idx}
|
||||||
|
);
|
||||||
|
idx++;
|
||||||
|
}
|
||||||
|
_vao.attributes(attrs);
|
||||||
|
return _vertex_data.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexBuffer<false>& VertexGroup::buffer(size_t idx) {
|
||||||
|
return *_vertex_data[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::shared_ptr<VertexBuffer<false>>> VertexGroup::buffers() {
|
||||||
|
return _vertex_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t VertexGroup::buffers(std::vector<VertexBuffer<false>>&& bufs_) {
|
||||||
|
_vertex_data.clear();
|
||||||
|
for (auto& buf : bufs_) {
|
||||||
|
_vertex_data.emplace_back(
|
||||||
|
std::make_shared<VertexBuffer<false>>(std::move(buf))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
VertexAttributePointerList attr_ptrs;
|
||||||
|
GLuint idx = 0;
|
||||||
|
for (auto buf : _vertex_data) {
|
||||||
|
for (auto attr : buf->attributes) {
|
||||||
|
attr_ptrs.emplace_back(
|
||||||
|
VertexAttributePointer{attr, buf->id(), idx}
|
||||||
|
);
|
||||||
|
idx++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_vao.attributes(attr_ptrs);
|
||||||
|
return _vertex_data.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexBuffer<false>&& VertexGroup::dropBuffer(size_t idx) {
|
||||||
|
if (idx >= _vertex_data.size()) {
|
||||||
|
THROW(BufferError(BufferError::Codes::INVALID_BUFFER));
|
||||||
|
}
|
||||||
|
std::vector<VertexBuffer<false>> tmp;
|
||||||
|
std::shared_ptr<VertexBuffer<false>> ret_ptr;
|
||||||
|
size_t i = 0;
|
||||||
|
for (auto buf : _vertex_data) {
|
||||||
|
if (i == idx) {
|
||||||
|
ret_ptr = std::make_shared<VertexBuffer<false>>(std::move(tmp[idx]));
|
||||||
|
} else {
|
||||||
|
tmp.emplace_back(std::move(*buf));
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
buffers(std::move(tmp));
|
||||||
|
return std::move(*ret_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexAttributePointer VertexGroup::attribute(size_t idx_) const {
|
||||||
|
return _vao.attr(idx_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VertexGroup::enableAttr(GLuint idx) {
|
||||||
|
bind();
|
||||||
|
_vao.enable(idx);
|
||||||
|
unbind();
|
||||||
|
}
|
||||||
|
|
||||||
|
void VertexGroup::disableAttr(GLuint idx) {
|
||||||
|
bind();
|
||||||
|
_vao.disable(idx);
|
||||||
|
unbind();
|
||||||
|
}
|
||||||
|
|
||||||
|
void VertexGroup::disableBuffer(size_t idx) {
|
||||||
|
auto buf_idx = _vertex_data[idx]->id();
|
||||||
|
bind();
|
||||||
|
for (auto attr : _vao.attributes()) {
|
||||||
|
if (attr.buffer == buf_idx) {
|
||||||
|
_vao.disable(attr.index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unbind();
|
||||||
|
}
|
||||||
|
|
||||||
|
void VertexGroup::enableBuffer(size_t idx) {
|
||||||
|
auto buf_idx = _vertex_data[idx]->id();
|
||||||
|
bind();
|
||||||
|
for (auto attr : _vao.attributes()) {
|
||||||
|
if (attr.buffer == buf_idx) {
|
||||||
|
_vao.enable(attr.index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unbind();
|
||||||
|
}
|
||||||
|
|
||||||
|
void VertexGroup::enable() { _vao.enable(); }
|
||||||
|
|
||||||
|
void VertexGroup::disable() { _vao.disable(); }
|
||||||
|
|
||||||
|
} // namespace nb
|
||||||
@ -16,9 +16,25 @@ static std::map<int, int> defailt_window_hints = {
|
|||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
GLError::GLError(const std::string& msg) : std::runtime_error(msg) {}
|
using OpenGLErrorCodes = OpenGLError::Codes;
|
||||||
|
const std::string OpenGLError::type = "nb::OpenGLError";
|
||||||
|
const ErrorCodeMap OpenGLError::ErrorMessages = {
|
||||||
|
{OpenGLErrorCodes::UNDEFINED, "Error"},
|
||||||
|
{OpenGLErrorCodes::INIT_FAILED, "GLFW initialization failed"},
|
||||||
|
{OpenGLErrorCodes::GLFW_INTIALIZED, "GLFW has already been initialized"},
|
||||||
|
{OpenGLErrorCodes::GLAD_FAILED, "GLAD initialization failed"}
|
||||||
|
};
|
||||||
|
|
||||||
WindowError::WindowError(const std::string &msg) : std::runtime_error(msg) {}
|
using WindowErrorCodes = WindowError::Codes;
|
||||||
|
const std::string WindowError::type = "nb::WindowError";
|
||||||
|
const ErrorCodeMap WindowError::ErrorMessages = {
|
||||||
|
{WindowErrorCodes::UNDEFINED, "Error"},
|
||||||
|
{WindowErrorCodes::INITIALIZED_WINDOW, "Window already initialized"},
|
||||||
|
{WindowErrorCodes::NO_GLFW, "GLFW has not been initialized"},
|
||||||
|
{WindowErrorCodes::INIT_FAILED, "Could not intialized window"}
|
||||||
|
};
|
||||||
|
|
||||||
|
GLError::GLError(const std::string& msg) : std::runtime_error(msg) {}
|
||||||
|
|
||||||
int Window::getGLFWHint(int hint_key) {
|
int Window::getGLFWHint(int hint_key) {
|
||||||
if (GLFWHints.find(hint_key) == GLFWHints.end()) {
|
if (GLFWHints.find(hint_key) == GLFWHints.end()) {
|
||||||
@ -30,7 +46,7 @@ int Window::getGLFWHint(int hint_key) {
|
|||||||
|
|
||||||
int Window::setGLFWHint(int hint_key, int hint_val) {
|
int Window::setGLFWHint(int hint_key, int hint_val) {
|
||||||
if (Window::_glfw_init) {
|
if (Window::_glfw_init) {
|
||||||
throw GLError("Cannot set GLFW hint after window is initialized.");
|
THROW(OpenGLError(OpenGLErrorCodes::GLFW_INTIALIZED));
|
||||||
} else {
|
} else {
|
||||||
GLFWHints[hint_key] = hint_val;
|
GLFWHints[hint_key] = hint_val;
|
||||||
}
|
}
|
||||||
@ -60,7 +76,7 @@ Window::Window(const uint16_t x, const uint16_t y, const char* initName, GLFWmon
|
|||||||
Window::_glfw_init = true;
|
Window::_glfw_init = true;
|
||||||
} else {
|
} else {
|
||||||
if (Window::StrictInitialization) {
|
if (Window::StrictInitialization) {
|
||||||
throw GLError("Failed to load GLFW with glfwInit()="+std::to_string(glfwResponse)+".");
|
THROW(OpenGLError(OpenGLErrorCodes::INIT_FAILED));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -103,7 +119,7 @@ int Window::getWindowHint(int hint_key) const {
|
|||||||
|
|
||||||
int Window::setWindowHint(int hint_key, int hint_val) {
|
int Window::setWindowHint(int hint_key, int hint_val) {
|
||||||
if (_init) {
|
if (_init) {
|
||||||
throw WindowError("Cannot set window hint after window is initialized.");
|
THROW(WindowError(WindowErrorCodes::INITIALIZED_WINDOW));
|
||||||
} else {
|
} else {
|
||||||
windowHints[hint_key] = hint_val;
|
windowHints[hint_key] = hint_val;
|
||||||
}
|
}
|
||||||
@ -112,7 +128,7 @@ int Window::setWindowHint(int hint_key, int hint_val) {
|
|||||||
|
|
||||||
int Window::init() {
|
int Window::init() {
|
||||||
if (!_glfw_init) {
|
if (!_glfw_init) {
|
||||||
throw GLError("GLFW has not been initialized.");
|
THROW(WindowError(WindowErrorCodes::NO_GLFW));
|
||||||
}
|
}
|
||||||
for (const auto& hint : windowHints) {
|
for (const auto& hint : windowHints) {
|
||||||
glfwWindowHint(hint.first, hint.second);
|
glfwWindowHint(hint.first, hint.second);
|
||||||
@ -123,7 +139,7 @@ int Window::init() {
|
|||||||
Window::WindowContexts.erase(window);
|
Window::WindowContexts.erase(window);
|
||||||
if (Window::WindowContexts.size()==0 && Window::_glfw_init) { glfwTerminate(); }
|
if (Window::WindowContexts.size()==0 && Window::_glfw_init) { glfwTerminate(); }
|
||||||
if (Window::StrictInitialization) {
|
if (Window::StrictInitialization) {
|
||||||
throw WindowError("Could not create window in glfwCreateWindow().");
|
THROW(WindowError(WindowErrorCodes::INIT_FAILED));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
glfwMakeContextCurrent(window);
|
glfwMakeContextCurrent(window);
|
||||||
@ -133,10 +149,7 @@ int Window::init() {
|
|||||||
if (!gladResponse) {
|
if (!gladResponse) {
|
||||||
Window::checkKillGLFW();
|
Window::checkKillGLFW();
|
||||||
if (Window::StrictInitialization) {
|
if (Window::StrictInitialization) {
|
||||||
throw GLError("Failed to load GLAD with \
|
THROW(OpenGLError(OpenGLErrorCodes::GLAD_FAILED));
|
||||||
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)="+\
|
|
||||||
std::to_string(gladResponse)+"."
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
16
engine/NBGraphics/tests/CMakeLists.txt
Normal file
16
engine/NBGraphics/tests/CMakeLists.txt
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.26.0)
|
||||||
|
|
||||||
|
if (NB_BUILD_TESTS)
|
||||||
|
enable_testing()
|
||||||
|
include(GoogleTest)
|
||||||
|
add_executable(TestWindow
|
||||||
|
./TestWindow.cpp
|
||||||
|
)
|
||||||
|
target_link_libraries(TestWindow
|
||||||
|
NBCore
|
||||||
|
NBGraphics
|
||||||
|
# GTest::gtest_main
|
||||||
|
)
|
||||||
|
# gtest_discover_tests(TestWindow)
|
||||||
|
|
||||||
|
endif()
|
||||||
18
engine/NBGraphics/tests/TestWindow.cpp
Normal file
18
engine/NBGraphics/tests/TestWindow.cpp
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#include "GLLoad.hpp"
|
||||||
|
// #define _NB_AUTOLOG
|
||||||
|
#include "VertexArray.hpp"
|
||||||
|
#include "Window.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
nb::logger.log("Howdy!");
|
||||||
|
nb::Window window(200, 200, "Hello!");
|
||||||
|
window.setWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
|
window.setWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
|
window.init();
|
||||||
|
|
||||||
|
GLFWwindow* window_ptr = window.getWindow();
|
||||||
|
while(!glfwWindowShouldClose(window_ptr)) {}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user