Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6644065388 | ||
|
|
74e337a053 | ||
|
|
fa60274540 |
@@ -6,4 +6,3 @@ docs/*
|
|||||||
!docs/CMake*
|
!docs/CMake*
|
||||||
!docs/Doxyfile.in
|
!docs/Doxyfile.in
|
||||||
!.clangd
|
!.clangd
|
||||||
.glsld
|
|
||||||
@@ -76,8 +76,6 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
|
|||||||
set(NB_TARGET_LINUX ON)
|
set(NB_TARGET_LINUX ON)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
set(NB_GRAPHICS_API_OGL ON)
|
|
||||||
|
|
||||||
if(NB_BUILD_TESTS)
|
if(NB_BUILD_TESTS)
|
||||||
message(STATUS "Building tests")
|
message(STATUS "Building tests")
|
||||||
enable_testing()
|
enable_testing()
|
||||||
|
|||||||
@@ -13,10 +13,10 @@ toAbsolutePath(NB_CORE_SOURCE
|
|||||||
./src/Logger.cpp
|
./src/Logger.cpp
|
||||||
./src/Processes.cpp
|
./src/Processes.cpp
|
||||||
./src/StringUtils.cpp
|
./src/StringUtils.cpp
|
||||||
./src/TypeTraits.cpp
|
|
||||||
./src/Utils.cpp
|
./src/Utils.cpp
|
||||||
)
|
)
|
||||||
toAbsolutePath(NB_CORE_INCLUDE
|
toAbsolutePath(NB_CORE_INCLUDE
|
||||||
|
./include/NBCore/ANSITerm.hpp
|
||||||
./include/NBCore/DataSink.hpp
|
./include/NBCore/DataSink.hpp
|
||||||
./include/NBCore/Errors.hpp
|
./include/NBCore/Errors.hpp
|
||||||
./include/NBCore/ErrorsImpl.hpp
|
./include/NBCore/ErrorsImpl.hpp
|
||||||
|
|||||||
@@ -0,0 +1,214 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef _NB_ANSI_TERM
|
||||||
|
#define _NB_ANSI_TERM
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
/*
|
||||||
|
----------- TECH DEBT ------------
|
||||||
|
Idk wtf to do here. This was originally to allow me to print
|
||||||
|
to terminal easily, but it's becoming too much of an investment.
|
||||||
|
For the time being, drop it.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
template <typename ST, typename U>
|
||||||
|
struct SmartText {
|
||||||
|
using Type = ST;
|
||||||
|
using ValType = U;
|
||||||
|
static constexpr bool HasChild = Type::HasChild;
|
||||||
|
|
||||||
|
SmartText() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename Type, typename U>
|
||||||
|
struct StyleFormatter : SmartText<StyleFormatter<Type, U>, U> {
|
||||||
|
using Base = SmartText<StyleFormatter<Type, U>, U>;
|
||||||
|
using typename Base::ValType;
|
||||||
|
static constexpr bool HasChild = true;
|
||||||
|
|
||||||
|
StyleFormatter(const std::string& _fmt, const ValType& _val, const std::string& _closing) :
|
||||||
|
fmt{_fmt}, val{_val}, closing{_closing} {}
|
||||||
|
|
||||||
|
std::string fmt;
|
||||||
|
ValType val;
|
||||||
|
std::string closing;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename Child>
|
||||||
|
struct StyleFormatter<Child, void> : SmartText<StyleFormatter<Child, void>, void>{
|
||||||
|
using Base = SmartText<StyleFormatter<Child, void>, void>;
|
||||||
|
using Base::ValType;
|
||||||
|
static constexpr bool HasChild = false;
|
||||||
|
|
||||||
|
StyleFormatter(const std::string& _fmt) : fmt{_fmt} {}
|
||||||
|
|
||||||
|
std::string fmt;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename U=void>
|
||||||
|
struct ANSITextColor;
|
||||||
|
|
||||||
|
template <typename U>
|
||||||
|
struct ANSITextColor : StyleFormatter<ANSITextColor<U>, U> {
|
||||||
|
using Base = StyleFormatter<ANSITextColor, U>;
|
||||||
|
using typename Base::ValType;
|
||||||
|
using Base::HasChild;
|
||||||
|
|
||||||
|
template <typename = typename std::enable_if<HasChild, U>::type>
|
||||||
|
ANSITextColor(const U& _val) : Base{"\x1b[39m", _val, "\x1b[39m"} {}
|
||||||
|
template <typename = typename std::enable_if<!HasChild, U>>
|
||||||
|
ANSITextColor() : Base{"\x1b[92m"} {}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct ANSITextColor<std::string> : StyleFormatter<ANSITextColor<std::string>, std::string> {
|
||||||
|
using Base = StyleFormatter<ANSITextColor<std::string>, std::string>;
|
||||||
|
static constexpr bool HasChild = Base::HasChild;
|
||||||
|
|
||||||
|
template <std::size_t N>
|
||||||
|
ANSITextColor(char const(&_val) [N]) : Base{"\x1b[92m", _val, "\x1b[39m"} {}
|
||||||
|
ANSITextColor(const char* _val) : Base{"\x1b[92m", _val, "\x1b[39m"} {}
|
||||||
|
ANSITextColor(const std::string& _val) : Base{"\x1b[92m", _val, "\x1b[39m"} {}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace nb
|
||||||
|
|
||||||
|
template<typename Type, typename U>
|
||||||
|
std::ostream& operator<<(std::ostream& stream, const nb::StyleFormatter<Type, U>& msg) {
|
||||||
|
if (&stream == &std::cout) {
|
||||||
|
stream << msg.fmt;
|
||||||
|
if (msg.HasChild) {
|
||||||
|
stream << msg.val << msg.closing;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (msg.HasChild) { stream << msg.val; }
|
||||||
|
}
|
||||||
|
stream << std::flush;
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* struct CellBase {
|
||||||
|
uint8_t width;
|
||||||
|
uint8_t height;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct Cell : SmartText<Cell<T>, T> {
|
||||||
|
using Base = SmartText<Cell<T>, T>;
|
||||||
|
using typename Base::ValType;
|
||||||
|
using Base::HasChild;
|
||||||
|
|
||||||
|
Cell(const ValType& _val, const uint8_t& w, const uint8_t& h=1)
|
||||||
|
: val(_val), width(w), height(h) {}
|
||||||
|
|
||||||
|
ValType val;
|
||||||
|
uint8_t width;
|
||||||
|
|
||||||
|
uint8_t height;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct Cell<std::string> : SmartText<Cell<std::string>, std::string> {
|
||||||
|
using Base = SmartText<Cell<std::string>, std::string>;
|
||||||
|
using typename Base::ValType;
|
||||||
|
using Base::HasChild;
|
||||||
|
|
||||||
|
template <std::size_t N>
|
||||||
|
Cell( const(&_val) [N], const uint8_t& w, const uint8_t& h=0) : Cell(std::string(_val), w, h) {}
|
||||||
|
Cell(char* _val, const uint8_t& w, const uint8_t& h=0) : Cell(std::string(_val), w, h) {}
|
||||||
|
Cell(const ValType& _val, const uint8_t& w, const uint8_t& h=0)
|
||||||
|
: val(_val), width(w) {
|
||||||
|
unsigned int strsize = _val.size();
|
||||||
|
height = strsize / width;
|
||||||
|
height += (strsize % width) ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ValType val;
|
||||||
|
uint8_t width;
|
||||||
|
|
||||||
|
uint8_t height;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
std::ostream& operator<<(std::ostream& stream, const Cell<T>& cll) {
|
||||||
|
if (&stream == &std::cout) {
|
||||||
|
stream << "\x1b[s" << cll.val << "\x1b[u\x1b[" << 1+cll.width << "C\x1b[" << 1+cll.height;
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void ChangeCellHeight(int i, Cell<T>& cll, const unsigned int& h) {
|
||||||
|
if (h < cll.height) { cll.height=h; }
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void GetTallestCell(int i, Cell<T>& cll, unsigned int& h) {
|
||||||
|
if (cll.height > h) { h = cll.height; }
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... Pack>
|
||||||
|
struct TableRow {
|
||||||
|
using RowCells = std::tuple<Cell<Pack>...>;
|
||||||
|
using CellWidths = std::array<int, sizeof...(Pack)>;
|
||||||
|
using DelimiterArray = std::array<std::string, sizeof...(Pack)+1>;
|
||||||
|
|
||||||
|
TableRow(CellWidths&& widths_, DelimiterArray&& delims_, const Pack&... vals_)
|
||||||
|
: widths(widths_), delimiters(delims_), topBorder("") {
|
||||||
|
nb::ForEach(_cells, GetTallestCell, _height);
|
||||||
|
}
|
||||||
|
|
||||||
|
static constexpr int num_columns = sizeof...(Pack);
|
||||||
|
|
||||||
|
int height() const { return height; }
|
||||||
|
inline int height(const unsigned int newh) {
|
||||||
|
nb::ForEach(_cells, ChangeCellHeight, newh);
|
||||||
|
return (_height=newh);
|
||||||
|
}
|
||||||
|
|
||||||
|
const CellWidths widths;
|
||||||
|
const DelimiterArray delimiters;
|
||||||
|
const std::string topBorder;
|
||||||
|
const std::string bottomBorder;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
RowCells _cells;
|
||||||
|
int _height;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename... Pack>
|
||||||
|
std::ostream& operator<<(std::ostream& stream, const TableRow<Pack...>& row) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Pack>
|
||||||
|
class Table {
|
||||||
|
public:
|
||||||
|
Table(const Pack&... _vals) : row(std::array<int, sizeof...(Pack)>(10),
|
||||||
|
std::array<std::string, sizeof...(Pack)>(""), _vals...) {}
|
||||||
|
private:
|
||||||
|
TableRow<Pack...> row;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace nb
|
||||||
|
|
||||||
|
#ifdef _NB_TARGET_WINDOWS
|
||||||
|
template<typename... Types>
|
||||||
|
std::ostream& operator<<(std::ostream& stream, const nb::TableRow<Types...>& row) {
|
||||||
|
using PosType = decltype(stream.tellp());
|
||||||
|
constexpr int num_cells = sizeof...(Types);
|
||||||
|
|
||||||
|
int col_num = 0;
|
||||||
|
int col_start = 0;
|
||||||
|
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
#endif // _NB_TARGET_WINDOWS */
|
||||||
|
|
||||||
|
#endif // _NB_ANSI_TERM
|
||||||
@@ -2,8 +2,9 @@
|
|||||||
#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 <utility>
|
#include <vector>
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
@@ -30,17 +31,6 @@ namespace detail {
|
|||||||
};
|
};
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
template<typename K, typename V>
|
|
||||||
std::pair<K, V> KeyVal(K key, V val) { return std::pair(key, val); }
|
|
||||||
|
|
||||||
enum TypeCodes {
|
|
||||||
UINT8, UINT16, UINT32, UINT64, INT8, INT16, INT32, INT64,
|
|
||||||
FLOAT16, FLOAT32, DOUBLE64, BOOL, NONE
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
static constexpr TypeCodes GetTypeEnum = NONE;
|
|
||||||
|
|
||||||
struct NoneType{};
|
struct NoneType{};
|
||||||
|
|
||||||
template<typename T, unsigned int Arg>
|
template<typename T, unsigned int Arg>
|
||||||
@@ -100,6 +90,37 @@ struct RunAndOutput {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename A, typename B>
|
||||||
|
using SubtractType = decltype(std::declval<A>() - std::declval<B>());
|
||||||
|
|
||||||
|
template<typename A, typename B>
|
||||||
|
using MultiplyType = decltype(std::declval<A>() * std::declval<B>());
|
||||||
|
|
||||||
|
template<typename A, typename B>
|
||||||
|
using AdditionType = decltype(std::declval<A>() + std::declval<B>());
|
||||||
|
|
||||||
|
template<typename Func, std::size_t N=0, typename... Pack>
|
||||||
|
inline typename std::enable_if<N==sizeof...(Pack), void>::type
|
||||||
|
ForEach(std::tuple<Pack...>&, Func) {}
|
||||||
|
|
||||||
|
template<typename Func, std::size_t N=0, typename... Pack>
|
||||||
|
inline typename std::enable_if<N < sizeof...(Pack), void>::type
|
||||||
|
ForEach(std::tuple<Pack...>& tup, Func f) {
|
||||||
|
f(N, std::get<N>(tup));
|
||||||
|
ForEach<N+1, Func, Pack...>(tup, f);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<std::size_t N=0, typename Func, typename Args, typename... Pack>
|
||||||
|
inline typename std::enable_if<N==sizeof...(Pack), void>::type
|
||||||
|
ForEach(std::tuple<Pack...>&, Func, const Args&) {}
|
||||||
|
|
||||||
|
template<std::size_t N=0, typename Func, typename Args, typename... Pack>
|
||||||
|
inline typename std::enable_if<N < sizeof...(Pack), void>::type
|
||||||
|
ForEach(std::tuple<Pack...>& tup, Func f, const Args& arg) {
|
||||||
|
f(N, std::get<N>(tup), arg);
|
||||||
|
ForEach<N+1, Func, Args, Pack...>(tup, f, arg);
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T, typename... Pack>
|
template<typename T, typename... Pack>
|
||||||
struct Find;
|
struct Find;
|
||||||
|
|
||||||
@@ -135,6 +156,48 @@ struct Find<T, U> {
|
|||||||
>::value;
|
>::value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T, typename Pack1, typename... Pack>
|
||||||
|
struct RefPackToPtrVec {
|
||||||
|
RefPackToPtrVec(std::vector<T*> _vec, Pack1& p1, Pack&... packs)
|
||||||
|
: vec(_vec) {
|
||||||
|
static_assert(std::is_base_of<T, Pack1>::value);
|
||||||
|
vec.push_back(&p1);
|
||||||
|
vec = RefPackToPtrVec<T, Pack...>(vec, packs...).vec;
|
||||||
|
}
|
||||||
|
|
||||||
|
RefPackToPtrVec(Pack1& p1, Pack&... packs)
|
||||||
|
: RefPackToPtrVec({}, p1, packs...) {}
|
||||||
|
|
||||||
|
std::vector<T*> vec;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, typename Pack>
|
||||||
|
struct RefPackToPtrVec<T, Pack> {
|
||||||
|
RefPackToPtrVec(std::vector<T*> _vec, Pack& p1)
|
||||||
|
: vec(_vec) {
|
||||||
|
static_assert(std::is_base_of<T, Pack>::value);
|
||||||
|
vec.push_back(&p1);
|
||||||
|
}
|
||||||
|
|
||||||
|
RefPackToPtrVec(Pack& p1)
|
||||||
|
: RefPackToPtrVec({}, p1) {}
|
||||||
|
|
||||||
|
std::vector<T*> vec;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, typename... Pack>
|
||||||
|
struct PackIsSameType;
|
||||||
|
|
||||||
|
template <typename T, typename PackType>
|
||||||
|
struct PackIsSameType<T, PackType> {
|
||||||
|
const bool value = std::is_base_of<T, PackType>::value;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, typename FirstPack, typename... Pack>
|
||||||
|
struct PackIsSameType<T, FirstPack, Pack...> {
|
||||||
|
const bool value = std::is_base_of<T, FirstPack>::value && PackIsSameType<T, Pack...>::value;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
|
|
||||||
#endif // _NB_TYPE_TRAITS
|
#endif // _NB_TYPE_TRAITS
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
#ifndef _NB_CORE_TYPES
|
#ifndef _NB_CORE_TYPES
|
||||||
#define _NB_CORE_TYPES
|
#define _NB_CORE_TYPES
|
||||||
|
|
||||||
|
#include <exception>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
#include <NBCore/TypeTraits.hpp>
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
template<> constexpr TypeCodes GetTypeEnum<unsigned char> = TypeCodes::UINT8;
|
|
||||||
template<> constexpr TypeCodes GetTypeEnum<unsigned short> = TypeCodes::UINT16;
|
|
||||||
template<> constexpr TypeCodes GetTypeEnum<unsigned int> = TypeCodes::UINT32;
|
|
||||||
template<> constexpr TypeCodes GetTypeEnum<unsigned long int> = TypeCodes::UINT64;
|
|
||||||
template<> constexpr TypeCodes GetTypeEnum<char> = TypeCodes::INT8;
|
|
||||||
template<> constexpr TypeCodes GetTypeEnum<short> = TypeCodes::INT16;
|
|
||||||
template<> constexpr TypeCodes GetTypeEnum<int> = TypeCodes::INT32;
|
|
||||||
template<> constexpr TypeCodes GetTypeEnum<long> = TypeCodes::INT64;
|
|
||||||
template<> constexpr TypeCodes GetTypeEnum<float> = TypeCodes::FLOAT32;
|
|
||||||
template<> constexpr TypeCodes GetTypeEnum<double> = TypeCodes::DOUBLE64;
|
|
||||||
template<> constexpr TypeCodes GetTypeEnum<bool> = TypeCodes::BOOL;
|
|
||||||
|
|
||||||
} // namespace nb
|
|
||||||
@@ -56,18 +56,6 @@ toAbsolutePath(NB_GRAPHICS_INCLUDE
|
|||||||
set(NB_GRAPHICS_SOURCE ${NB_GRAPHICS_SOURCE} PARENT_SCOPE)
|
set(NB_GRAPHICS_SOURCE ${NB_GRAPHICS_SOURCE} PARENT_SCOPE)
|
||||||
set(NB_GRAPHICS_INCLUDE ${NB_GRAPHICS_INCLUDE} PARENT_SCOPE)
|
set(NB_GRAPHICS_INCLUDE ${NB_GRAPHICS_INCLUDE} PARENT_SCOPE)
|
||||||
|
|
||||||
if (NB_GRAPHICS_API_OGL)
|
|
||||||
toAbsolutePath(NB_GRAPHICS_API_OGL_SOURCE
|
|
||||||
./src/OGL/Buffers.cpp
|
|
||||||
)
|
|
||||||
toAbsolutePath(NB_GRAPHICS_API_OGL_INCLUDE
|
|
||||||
./include/NBGraphics/OGL/Buffers.hpp
|
|
||||||
)
|
|
||||||
set(NB_GRAPHICS_SOURCE ${NB_GRAPHICS_SOURCE} ${NB_GRAPHICS_API_OGL_SOURCE} PARENT_SCOPE)
|
|
||||||
set(NB_GRAPHICS_INCLUDE ${NB_GRAPHICS_INCLUDE} ${NB_GRAPHICS_API_OGL_SOURCE} PARENT_SCOPE)
|
|
||||||
add_library(NBGraphicsAPI_OGL ${NB_GRAPHICS_API_OGL_SOURCE})
|
|
||||||
endif()
|
|
||||||
|
|
||||||
add_library(NBGraphics
|
add_library(NBGraphics
|
||||||
${NB_GRAPHICS_SOURCE}
|
${NB_GRAPHICS_SOURCE}
|
||||||
${GLAD_PATH}/src/glad.c
|
${GLAD_PATH}/src/glad.c
|
||||||
@@ -75,11 +63,6 @@ add_library(NBGraphics
|
|||||||
add_library(NBEngine::Graphics ALIAS NBGraphics)
|
add_library(NBEngine::Graphics ALIAS NBGraphics)
|
||||||
add_dependencies(NBGraphics NBCore)
|
add_dependencies(NBGraphics NBCore)
|
||||||
add_dependencies(NBGraphics glfw)
|
add_dependencies(NBGraphics glfw)
|
||||||
|
|
||||||
if(NB_GRAPHICS_API_OGL)
|
|
||||||
add_dependencies(NBGraphics NBGraphicsAPI_OGL)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
target_link_libraries(NBGraphics
|
target_link_libraries(NBGraphics
|
||||||
PUBLIC glfw
|
PUBLIC glfw
|
||||||
PUBLIC NBCore
|
PUBLIC NBCore
|
||||||
|
|||||||
@@ -1,262 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_BUFFER
|
|
||||||
#define _NB_BUFFER
|
|
||||||
|
|
||||||
#include <NBGraphics/GLLoad.hpp>
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include <NBCore/Errors.hpp>
|
|
||||||
#include <NBCore/Logger.hpp>
|
|
||||||
#include <NBCore/Utils.hpp>
|
|
||||||
|
|
||||||
#include <NBGraphics/OGLObjects.hpp>
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
extern const ConstantMap <GLenum, std::string> BufferTypes;
|
|
||||||
|
|
||||||
template<GLenum N>
|
|
||||||
struct GLSLEnum;
|
|
||||||
template<typename T>
|
|
||||||
struct GLSLType;
|
|
||||||
|
|
||||||
#define GLSLTypeTraits(type_, size_, value_) \
|
|
||||||
template<>\
|
|
||||||
struct GLSLEnum<value_> {\
|
|
||||||
using type = type_;\
|
|
||||||
static constexpr size_t size = size_;\
|
|
||||||
static constexpr GLenum value=value_;\
|
|
||||||
};\
|
|
||||||
template<>\
|
|
||||||
struct GLSLType<type_> {\
|
|
||||||
using type = type_;\
|
|
||||||
static constexpr size_t size = size_;\
|
|
||||||
static constexpr GLenum value=value_;\
|
|
||||||
};
|
|
||||||
|
|
||||||
#define GLSLTypeEquivalence(original, newtype) \
|
|
||||||
template<>\
|
|
||||||
struct GLSLType<newtype> : public GLSLType<original> {};
|
|
||||||
|
|
||||||
GLSLTypeTraits(uint8_t, 1, GL_UNSIGNED_BYTE);
|
|
||||||
GLSLTypeTraits(uint16_t, 2, GL_UNSIGNED_SHORT);
|
|
||||||
GLSLTypeTraits(uint32_t, 4, GL_UNSIGNED_INT);
|
|
||||||
GLSLTypeTraits(int8_t, 1, GL_BYTE);
|
|
||||||
GLSLTypeTraits(int16_t, 2, GL_SHORT);
|
|
||||||
GLSLTypeTraits(int32_t, 4, GL_INT);
|
|
||||||
GLSLTypeTraits(float, 4, GL_FLOAT);
|
|
||||||
GLSLTypeTraits(double, 8, GL_DOUBLE);
|
|
||||||
|
|
||||||
class BufferError : public Error<BufferError> {
|
|
||||||
protected:
|
|
||||||
using Base = Error<BufferError>;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::Base;
|
|
||||||
enum Codes : unsigned int {
|
|
||||||
UNDEFINED,
|
|
||||||
DATA_OVERFLOW,
|
|
||||||
DATA_ERROR,
|
|
||||||
BAD_DATA_TYPE
|
|
||||||
};
|
|
||||||
static const std::string type;
|
|
||||||
static const ErrorCodeMap ErrorMessages;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Buffer : public OpenGLObject {
|
|
||||||
protected:
|
|
||||||
using Codes = BufferError::Codes;
|
|
||||||
using OpenGLObject::_id;
|
|
||||||
GLenum _usage = GL_STATIC_DRAW;
|
|
||||||
size_t _size;
|
|
||||||
virtual GLuint declare() override {
|
|
||||||
if (!_id) {
|
|
||||||
OPENGL_CALL(glGenBuffers(1, &_id));
|
|
||||||
}
|
|
||||||
this->bind();
|
|
||||||
return _id;
|
|
||||||
}
|
|
||||||
virtual void remove() override {
|
|
||||||
if (_id) {
|
|
||||||
this->unbind();
|
|
||||||
OPENGL_CALL(glDeleteBuffers(1, &_id));
|
|
||||||
_id = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
|
||||||
const GLenum Target;
|
|
||||||
using OpenGLObject::OpenGLObject;
|
|
||||||
Buffer(GLenum target);
|
|
||||||
Buffer(GLenum target, const ByteVector&, GLenum usage=GL_STATIC_DRAW);
|
|
||||||
Buffer(Buffer&& other);
|
|
||||||
Buffer& operator=(Buffer&& rhs);
|
|
||||||
|
|
||||||
virtual void bind() const override {
|
|
||||||
if (_id) {
|
|
||||||
OPENGL_CALL(glBindBuffer(Target, _id));
|
|
||||||
} else {
|
|
||||||
THROW(OpenGLObjectError(
|
|
||||||
OpenGLObjectError::Codes::INVALID_OBJECT,
|
|
||||||
"w/ BufferType " + BufferTypes[Target]
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
virtual void unbind() const override {
|
|
||||||
OPENGL_CALL(glBindBuffer(Target, 0));
|
|
||||||
}
|
|
||||||
GLenum usage() const;
|
|
||||||
size_t size() const;
|
|
||||||
ByteVector data() const;
|
|
||||||
|
|
||||||
void data(const ByteVector& data, GLenum usage=GL_STATIC_DRAW);
|
|
||||||
void data(const void* data, size_t size, GLenum usage=GL_STATIC_DRAW);
|
|
||||||
void subdata(const void* data, size_t size, GLintptr offset=0);
|
|
||||||
void subdata(const ByteVector& data_, GLintptr offset=0);
|
|
||||||
// virtual void clear() const { glClearBufferData(Target, ) }
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
/* template <typename BufferType>
|
|
||||||
class ImmutableBuffer : public virtual Buffer {
|
|
||||||
public:
|
|
||||||
ImmutableBuffer() = default;
|
|
||||||
ImmutableBuffer(
|
|
||||||
size_t size_,
|
|
||||||
GLenum usage_ = GL_STATIC_DRAW,
|
|
||||||
GLbitfield flags_=0x0
|
|
||||||
) : size(size_), Base::usage(usage_) {
|
|
||||||
glBufferStorage(Target, size, nullptr, flags_);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::weak_ptr<void> map(GLbitfield access_) {
|
|
||||||
if (_map && _mapAccess != access_) { unmap(); }
|
|
||||||
_mapAccess = access_;
|
|
||||||
if (!_map) {
|
|
||||||
bind();
|
|
||||||
glMapBuffer(Target, _mapAccess);
|
|
||||||
}
|
|
||||||
return _map;
|
|
||||||
}
|
|
||||||
void unmap() {
|
|
||||||
bind();
|
|
||||||
glUnmapBuffer(Target);
|
|
||||||
_map.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
const size_t size;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
std::shared_ptr<void> _map = nullptr;
|
|
||||||
GLbitfield _mapAccess = 0;
|
|
||||||
|
|
||||||
}; */
|
|
||||||
|
|
||||||
class UniformBuffer : public Buffer {
|
|
||||||
protected:
|
|
||||||
GLuint _index;
|
|
||||||
|
|
||||||
public:
|
|
||||||
UniformBuffer();
|
|
||||||
void bind() const override;
|
|
||||||
GLuint bufferIndex() const;
|
|
||||||
GLuint bufferIndex(GLuint);
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
struct IsValidGLElementIndexType {
|
|
||||||
static constexpr bool value = false;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
struct ValidGLElementIndexType {
|
|
||||||
static constexpr bool value = true;
|
|
||||||
static constexpr GLenum glsltype = GLSLType<T>::value;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
template<>
|
|
||||||
struct IsValidGLElementIndexType<uint8_t> : public ValidGLElementIndexType<uint8_t> {};
|
|
||||||
template<>
|
|
||||||
struct IsValidGLElementIndexType<uint16_t> : public ValidGLElementIndexType<uint16_t> {};
|
|
||||||
template<>
|
|
||||||
struct IsValidGLElementIndexType<uint32_t> : public ValidGLElementIndexType<uint32_t> {};
|
|
||||||
|
|
||||||
class ElementBuffer : Buffer {
|
|
||||||
private:
|
|
||||||
template<typename T>
|
|
||||||
inline void checkForElementDataType() {
|
|
||||||
static_assert(IsValidGLElementIndexType<T>::value,
|
|
||||||
"ElementBuffer data type must resolve to one of GL_UNSIGNED_BYTE,"
|
|
||||||
"GL_UNSIGNED_SHORT, or GL_UNSIGNED_INT"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
using Codes = BufferError::Codes;
|
|
||||||
using OpenGLObject::_id;
|
|
||||||
using Buffer::_usage;
|
|
||||||
using Buffer::_size;
|
|
||||||
using Buffer::declare;
|
|
||||||
using Buffer::remove;
|
|
||||||
GLenum _datatype;
|
|
||||||
template<typename T>
|
|
||||||
inline void checkDatatypeConsistency() {
|
|
||||||
if (_datatype != ValidGLElementIndexType<T>::value) {
|
|
||||||
THROW(BufferError(
|
|
||||||
Codes::BAD_DATA_TYPE,
|
|
||||||
GLenumTable[ValidGLElementIndexType<T>::value] + " not same as buffer "
|
|
||||||
"data type "+GLenumTable[_datatype]
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Buffer::Target;
|
|
||||||
using Buffer::OpenGLObject;
|
|
||||||
using Buffer::bind;
|
|
||||||
using Buffer::unbind;
|
|
||||||
using Buffer::usage;
|
|
||||||
using Buffer::size;
|
|
||||||
template<typename T>
|
|
||||||
ElementBuffer(const std::vector<T>& indices, GLenum usage=GL_STATIC_DRAW) : Buffer(GL_ELEMENT_ARRAY_BUFFER) {
|
|
||||||
data(indices, usage);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline GLenum datatype() { return _datatype; }
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void data(const std::vector<T>& data, GLenum usage=GL_STATIC_DRAW) {
|
|
||||||
checkForElementDataType<T>();
|
|
||||||
_datatype = ValidGLElementIndexType<T>::glsltype;
|
|
||||||
Buffer::data(vectorToBytes(data), usage);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void data(const T* data, size_t size, GLenum usage=GL_STATIC_DRAW) {
|
|
||||||
checkForElementDataType<T>();
|
|
||||||
_datatype = ValidGLElementIndexType<T>::glsltype;
|
|
||||||
Buffer::data((void*)data, size * sizeof(T), usage);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void subdata(const T* data, size_t size, GLintptr offset=0) {
|
|
||||||
checkForElementDataType<T>();
|
|
||||||
checkDatatypeConsistency<T>();
|
|
||||||
Buffer::subdata((void*)data, size * sizeof(T), offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void subdata(const std::vector<T>& data_, GLintptr offset=0) {
|
|
||||||
checkForElementDataType<T>();
|
|
||||||
checkDatatypeConsistency<T>();
|
|
||||||
Buffer::subdata(vectorToBytes(data_), offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace NB
|
|
||||||
#endif // _NB_BUFFER
|
|
||||||
@@ -6,11 +6,8 @@
|
|||||||
#include <NBGraphics/OGLObjects.hpp>
|
#include <NBGraphics/OGLObjects.hpp>
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <functional>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <type_traits>
|
|
||||||
#include <utility>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <NBCore/Errors.hpp>
|
#include <NBCore/Errors.hpp>
|
||||||
@@ -33,86 +30,6 @@ class GLFWError : public Error<GLFWError> {
|
|||||||
static const ErrorCodeMap ErrorMessages;
|
static const ErrorCodeMap ErrorMessages;
|
||||||
};
|
};
|
||||||
|
|
||||||
class OGLWindow;
|
|
||||||
|
|
||||||
enum WindowCallbacks {
|
|
||||||
KEY, CHAR, CHAR_MODS, MOUSE_BUTTON, CURSOR_POS, CURSOR_ENTER,
|
|
||||||
SCROLL, DROP, WINDOW_POS, WINDOW_SIZE, WINDOW_CLOSE, WINDOW_REFRESH,
|
|
||||||
WINDOW_FOCUS, WINDOW_ICONIFY, WINDOW_MAX, FRAMEBUFFER_SIZE, WINDOW_SCALE,
|
|
||||||
};
|
|
||||||
|
|
||||||
template<WindowCallbacks>
|
|
||||||
struct OGLWindowCallbacks;
|
|
||||||
|
|
||||||
template<>
|
|
||||||
struct OGLWindowCallbacks<WindowCallbacks::KEY> {
|
|
||||||
using type = typename std::function<void(OGLWindow*, int, int, int, int)>;
|
|
||||||
};
|
|
||||||
template<>
|
|
||||||
struct OGLWindowCallbacks<WindowCallbacks::CHAR> {
|
|
||||||
using type = typename std::function<void(OGLWindow*, unsigned int)>;
|
|
||||||
};
|
|
||||||
template<>
|
|
||||||
struct OGLWindowCallbacks<WindowCallbacks::CHAR_MODS> {
|
|
||||||
using type = typename std::function<void(OGLWindow*, unsigned int, int)>;
|
|
||||||
};
|
|
||||||
template<>
|
|
||||||
struct OGLWindowCallbacks<WindowCallbacks::CURSOR_ENTER> {
|
|
||||||
using type = typename std::function<void(OGLWindow*, int)>;
|
|
||||||
};
|
|
||||||
template<>
|
|
||||||
struct OGLWindowCallbacks<WindowCallbacks::CURSOR_POS> {
|
|
||||||
using type = typename std::function<void(OGLWindow*, double, double)>;
|
|
||||||
};
|
|
||||||
template<>
|
|
||||||
struct OGLWindowCallbacks<WindowCallbacks::MOUSE_BUTTON> {
|
|
||||||
using type = typename std::function<void(OGLWindow*, int, int, int)>;
|
|
||||||
};
|
|
||||||
template<>
|
|
||||||
struct OGLWindowCallbacks<WindowCallbacks::SCROLL> {
|
|
||||||
using type = typename std::function<void(OGLWindow*, double, double)>;
|
|
||||||
};
|
|
||||||
template<>
|
|
||||||
struct OGLWindowCallbacks<WindowCallbacks::DROP> {
|
|
||||||
using type = typename std::function<void(OGLWindow*, int, const char**)>;
|
|
||||||
};
|
|
||||||
template<>
|
|
||||||
struct OGLWindowCallbacks<WindowCallbacks::WINDOW_POS> {
|
|
||||||
using type = typename std::function<void(OGLWindow*, int, int)>;
|
|
||||||
};
|
|
||||||
template<>
|
|
||||||
struct OGLWindowCallbacks<WindowCallbacks::WINDOW_SIZE> {
|
|
||||||
using type = typename std::function<void(OGLWindow*, int, int)>;
|
|
||||||
};
|
|
||||||
template<>
|
|
||||||
struct OGLWindowCallbacks<WindowCallbacks::WINDOW_CLOSE> {
|
|
||||||
using type = typename std::function<void(OGLWindow*)>;
|
|
||||||
};
|
|
||||||
template<>
|
|
||||||
struct OGLWindowCallbacks<WindowCallbacks::WINDOW_REFRESH> {
|
|
||||||
using type = typename std::function<void(OGLWindow*)>;
|
|
||||||
};
|
|
||||||
template<>
|
|
||||||
struct OGLWindowCallbacks<WindowCallbacks::WINDOW_FOCUS> {
|
|
||||||
using type = typename std::function<void(OGLWindow*, int)>;
|
|
||||||
};
|
|
||||||
template<>
|
|
||||||
struct OGLWindowCallbacks<WindowCallbacks::WINDOW_ICONIFY> {
|
|
||||||
using type = typename std::function<void(OGLWindow*, int)>;
|
|
||||||
};
|
|
||||||
template<>
|
|
||||||
struct OGLWindowCallbacks<WindowCallbacks::WINDOW_MAX> {
|
|
||||||
using type = typename std::function<void(OGLWindow*, int)>;
|
|
||||||
};
|
|
||||||
template<>
|
|
||||||
struct OGLWindowCallbacks<WindowCallbacks::FRAMEBUFFER_SIZE> {
|
|
||||||
using type = typename std::function<void(OGLWindow*, int, int)>;
|
|
||||||
};
|
|
||||||
template<>
|
|
||||||
struct OGLWindowCallbacks<WindowCallbacks::WINDOW_SCALE> {
|
|
||||||
using type = typename std::function<void(OGLWindow*, int)>;
|
|
||||||
};
|
|
||||||
|
|
||||||
class OGLWindow {
|
class OGLWindow {
|
||||||
using GLFWHint = std::array<int, 2>;
|
using GLFWHint = std::array<int, 2>;
|
||||||
private:
|
private:
|
||||||
@@ -128,30 +45,32 @@ class OGLWindow {
|
|||||||
static thread_local GLFWwindow* _currWindow;
|
static thread_local GLFWwindow* _currWindow;
|
||||||
static thread_local const GLFWwindow* _currArena;
|
static thread_local const GLFWwindow* _currArena;
|
||||||
|
|
||||||
template<typename T>
|
static void SetHintInteger(const std::array<int, 2>&);
|
||||||
static void SetHint(std::pair<int, T> hint) {
|
static void SetHintString(const std::tuple<int, std::string>&);
|
||||||
if constexpr ( std::is_same_v<T, std::string> ) {
|
|
||||||
glfwWindowHintString(std::get<0>(hint), std::get<1>(hint).c_str());
|
|
||||||
} else if constexpr ( std::is_same_v<T, int> ) {
|
|
||||||
glfwWindowHint(std::get<0>(hint), std::get<1>(hint));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string _title;
|
std::string _title;
|
||||||
GLFWwindow* _window_ptr;
|
GLFWwindow* _window_ptr;
|
||||||
const GLFWwindow* _arena;
|
const GLFWwindow* _arena;
|
||||||
|
|
||||||
OGLWindow(
|
OGLWindow(
|
||||||
unsigned int x,
|
unsigned int,
|
||||||
unsigned int y,
|
unsigned int,
|
||||||
const std::string&,
|
const std::string&,
|
||||||
|
const std::vector<std::array<int, 2>>&,
|
||||||
|
const std::vector<std::tuple<int, std::string>>&,
|
||||||
GLFWwindow* sharingContext = NULL
|
GLFWwindow* sharingContext = NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
GLFWwindow* makeCurrent() const;
|
GLFWwindow* makeCurrent() const;
|
||||||
void detachWindow();
|
void detachWindow();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
enum Callbacks {
|
||||||
|
KEY, CHAR, CHAR_MODS, MOUSE_BUTTON, CURSOR_POS, CURSOR_ENTER,
|
||||||
|
SCROLL, DROP, WINDOW_POS, WINDOW_SIZE, WINDOW_CLOSE, WINDOW_REFRESH,
|
||||||
|
WINDOW_FOCUS, WINDOW_ICONIFY, WINDOW_MAX, FRAMEBUFFER_SIZE, WINDOW_SCALE,
|
||||||
|
};
|
||||||
|
template<typename... T>
|
||||||
|
using CallbackFunc = void(*)(OGLWindow*, T...);
|
||||||
|
|
||||||
static inline bool IsGLFWInitialized() { return _glfwInitialized; }
|
static inline bool IsGLFWInitialized() { return _glfwInitialized; }
|
||||||
static bool InitGLFW(const std::vector<GLFWHint>& hints={});
|
static bool InitGLFW(const std::vector<GLFWHint>& hints={});
|
||||||
@@ -161,38 +80,21 @@ class OGLWindow {
|
|||||||
static GLFWwindow* MakeCurrent(const std::shared_ptr<OGLWindow>&);
|
static GLFWwindow* MakeCurrent(const std::shared_ptr<OGLWindow>&);
|
||||||
static inline GLFWwindow* DropCurrent() { return MakeCurrent(nullptr); }
|
static inline GLFWwindow* DropCurrent() { return MakeCurrent(nullptr); }
|
||||||
static void DestroyWindow(std::shared_ptr<OGLWindow>&);
|
static void DestroyWindow(std::shared_ptr<OGLWindow>&);
|
||||||
template <typename... T>
|
|
||||||
static std::shared_ptr<OGLWindow> CreateWindow(
|
static std::shared_ptr<OGLWindow> CreateWindow(
|
||||||
unsigned int x,
|
unsigned int x,
|
||||||
unsigned int y,
|
unsigned int y,
|
||||||
const std::string& title,
|
const std::string& title,
|
||||||
const std::pair<int, T>&... windowHints
|
const std::vector<std::array<int, 2>>& integerHints={},
|
||||||
) {
|
const std::vector<std::tuple<int, std::string>>& stringHints={}
|
||||||
(SetHint(std::pair(windowHints)),... );
|
);
|
||||||
return std::shared_ptr<OGLWindow>(new OGLWindow(
|
|
||||||
x,
|
|
||||||
y,
|
|
||||||
title,
|
|
||||||
nullptr
|
|
||||||
) );
|
|
||||||
}
|
|
||||||
template<typename... T>
|
|
||||||
static std::shared_ptr<OGLWindow> CreateSharedWindow(
|
static std::shared_ptr<OGLWindow> CreateSharedWindow(
|
||||||
unsigned int x,
|
unsigned int x,
|
||||||
unsigned int y,
|
unsigned int y,
|
||||||
const std::string& title,
|
const std::string& title,
|
||||||
std::shared_ptr<OGLWindow> sharingWindow = NULL,
|
std::shared_ptr<OGLWindow> sharingWindow,
|
||||||
const std::pair<int, T>&... windowHints
|
const std::vector<std::array<int, 2>>& integerHints={},
|
||||||
) {
|
const std::vector<std::tuple<int, std::string>>& stringHints={}
|
||||||
(SetHint(std::pair(windowHints)),... );
|
);
|
||||||
return std::shared_ptr<OGLWindow>(new OGLWindow(
|
|
||||||
x,
|
|
||||||
y,
|
|
||||||
title,
|
|
||||||
sharingWindow->_window_ptr
|
|
||||||
) );
|
|
||||||
}
|
|
||||||
|
|
||||||
OGLWindow(OGLWindow&&) = delete;
|
OGLWindow(OGLWindow&&) = delete;
|
||||||
OGLWindow(const OGLWindow&) = delete;
|
OGLWindow(const OGLWindow&) = delete;
|
||||||
~OGLWindow();
|
~OGLWindow();
|
||||||
@@ -204,91 +106,14 @@ class OGLWindow {
|
|||||||
inline std::string getTitle() const { return _title; }
|
inline std::string getTitle() const { return _title; }
|
||||||
std::array<int, 2> getSize() const;
|
std::array<int, 2> getSize() const;
|
||||||
bool shouldClose() const;
|
bool shouldClose() const;
|
||||||
inline void pollEvents() const { CheckForMainThread(); glfwPollEvents(); }
|
inline void processEvents() const { CheckForMainThread(); glfwPollEvents(); }
|
||||||
inline void swapBuffers() const { glfwSwapBuffers(_window_ptr); }
|
inline void refresh() const { glfwSwapBuffers(_window_ptr); }
|
||||||
|
|
||||||
void setSize(unsigned int x, unsigned int y);
|
void setSize(unsigned int x, unsigned int y);
|
||||||
void setTitle(const std::string&);
|
void setTitle(const std::string&);
|
||||||
void shouldClose(bool);
|
void shouldClose(bool);
|
||||||
|
/* template<enum Callbacks, typename Func>
|
||||||
template<WindowCallbacks CallbackType, typename CallbackFunc>
|
void setCallBack(Func); */
|
||||||
void setCallback(CallbackFunc callback) {
|
|
||||||
CheckForMainThread();
|
|
||||||
auto tmp = makeCurrent();
|
|
||||||
static auto this_ptr = this;
|
|
||||||
static typename OGLWindowCallbacks<CallbackType>::type tmp_callback = callback;
|
|
||||||
if constexpr (CallbackType == KEY) {
|
|
||||||
glfwSetKeyCallback(_window_ptr, [](GLFWwindow*, int a, int b, int c, int d){
|
|
||||||
tmp_callback(this_ptr, a,b,c,d);
|
|
||||||
});
|
|
||||||
} else if constexpr (CallbackType == CHAR) {
|
|
||||||
glfwSetCharCallback(_window_ptr, [](GLFWwindow*, int a) {
|
|
||||||
tmp_callback(this_ptr, a);
|
|
||||||
});
|
|
||||||
} else if constexpr (CallbackType == CHAR_MODS) {
|
|
||||||
glfwSetCharModsCallback(_window_ptr, [](GLFWwindow*, unsigned int a, int b) {
|
|
||||||
tmp_callback(this_ptr, a, b);
|
|
||||||
});
|
|
||||||
} else if constexpr (CallbackType == CURSOR_ENTER) {
|
|
||||||
glfwSetCursorEnterCallback(_window_ptr, [](GLFWwindow*, int a) {
|
|
||||||
tmp_callback(this_ptr, a);
|
|
||||||
});
|
|
||||||
} else if constexpr (CallbackType == CURSOR_POS) {
|
|
||||||
glfwSetCursorPosCallback(_window_ptr, [](GLFWwindow*, double a, double b){
|
|
||||||
tmp_callback(this_ptr, a,b);
|
|
||||||
});
|
|
||||||
} else if constexpr (CallbackType == MOUSE_BUTTON) {
|
|
||||||
glfwSetMouseButtonCallback(_window_ptr, [](GLFWwindow*, int a, int b, int c){
|
|
||||||
tmp_callback(this_ptr, a,b,c);
|
|
||||||
});
|
|
||||||
} else if constexpr (CallbackType == SCROLL) {
|
|
||||||
glfwSetScrollCallback(_window_ptr, [](GLFWwindow*, double a, double b) {
|
|
||||||
tmp_callback(this_ptr, a,b);
|
|
||||||
});
|
|
||||||
} else if constexpr (CallbackType == DROP) {
|
|
||||||
glfwSetDropCallback(_window_ptr, [](GLFWwindow*, int a, const char** b) {
|
|
||||||
tmp_callback(this_ptr, a, b);
|
|
||||||
});
|
|
||||||
} else if constexpr (CallbackType == WINDOW_POS) {
|
|
||||||
glfwSetWindowPosCallback(_window_ptr, [](GLFWwindow*, int a, int b){
|
|
||||||
tmp_callback(this_ptr, a,b);
|
|
||||||
});
|
|
||||||
} else if constexpr (CallbackType == WINDOW_SIZE) {
|
|
||||||
glfwSetWindowSizeCallback(_window_ptr, [](GLFWwindow*, int a, int b){
|
|
||||||
tmp_callback(this_ptr, a,b);
|
|
||||||
});
|
|
||||||
} else if constexpr (CallbackType == WINDOW_CLOSE) {
|
|
||||||
glfwSetWindowCloseCallback(_window_ptr, [](GLFWwindow*){
|
|
||||||
tmp_callback(this_ptr);
|
|
||||||
});
|
|
||||||
} else if constexpr (CallbackType == WINDOW_REFRESH) {
|
|
||||||
glfwSetWindowRefreshCallback(_window_ptr, [](GLFWwindow*){
|
|
||||||
tmp_callback(this_ptr);
|
|
||||||
});
|
|
||||||
} else if constexpr (CallbackType == WINDOW_FOCUS) {
|
|
||||||
glfwSetWindowFocusCallback(_window_ptr, [](GLFWwindow*, int a) {
|
|
||||||
tmp_callback(this_ptr, a);
|
|
||||||
});
|
|
||||||
} else if constexpr (CallbackType == WINDOW_ICONIFY) {
|
|
||||||
glfwSetWindowIconifyCallback(_window_ptr, [](GLFWwindow*, int a) {
|
|
||||||
tmp_callback(this_ptr, a);
|
|
||||||
});
|
|
||||||
} else if constexpr (CallbackType == WINDOW_MAX) {
|
|
||||||
glfwSetWindowMaximizeCallback(_window_ptr, [](GLFWwindow*, int a) {
|
|
||||||
tmp_callback(this_ptr, a);
|
|
||||||
});
|
|
||||||
} else if constexpr (CallbackType == FRAMEBUFFER_SIZE) {
|
|
||||||
glfwSetFramebufferSizeCallback(_window_ptr, [](GLFWwindow*, int a, int b) {
|
|
||||||
tmp_callback(this_ptr, a, b);
|
|
||||||
});
|
|
||||||
} else if constexpr (CallbackType == WINDOW_SCALE) {
|
|
||||||
glfwSetWindowContentScaleCallback(_window_ptr, [](GLFWwindow*, int a, int b) {
|
|
||||||
tmp_callback(this_ptr, a, b);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
static_assert(false, "No matching callback.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -48,27 +48,79 @@ bool OGLWindow::TerminateGLFW() {
|
|||||||
return !IsGLFWInitialized();
|
return !IsGLFWInitialized();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void OGLWindow::SetHintInteger(const std::array<int, 2>& hint) {
|
||||||
|
CheckForMainThread();
|
||||||
|
glfwWindowHint(hint[0], hint[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OGLWindow::SetHintString(const std::tuple<int, std::string>& hint) {
|
||||||
|
CheckForMainThread();
|
||||||
|
glfwWindowHintString(std::get<0>(hint), std::get<1>(hint).c_str());
|
||||||
|
}
|
||||||
|
|
||||||
GLFWwindow* OGLWindow::MakeCurrent(const std::shared_ptr<OGLWindow>& window) {
|
GLFWwindow* OGLWindow::MakeCurrent(const std::shared_ptr<OGLWindow>& window) {
|
||||||
auto tmp = _currWindow;
|
auto tmp = _currWindow;
|
||||||
if (window) {
|
|
||||||
_currWindow = window->getWindowPtr();
|
_currWindow = window->getWindowPtr();
|
||||||
|
if (window) {
|
||||||
|
glfwMakeContextCurrent(_currWindow);
|
||||||
_currArena = window->_arena;
|
_currArena = window->_arena;
|
||||||
} else {
|
} else {
|
||||||
_currWindow = nullptr;
|
glfwMakeContextCurrent(nullptr);
|
||||||
_currArena = nullptr;
|
_currArena = nullptr;
|
||||||
}
|
}
|
||||||
glfwMakeContextCurrent(_currWindow);
|
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<OGLWindow> OGLWindow::CreateWindow(
|
||||||
|
unsigned int x,
|
||||||
|
unsigned int y,
|
||||||
|
const std::string& title,
|
||||||
|
const std::vector<std::array<int, 2>>& integerHints,
|
||||||
|
const std::vector<std::tuple<int, std::string>>& stringHints
|
||||||
|
) {
|
||||||
|
return std::shared_ptr<OGLWindow>( new OGLWindow(
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
title,
|
||||||
|
integerHints,
|
||||||
|
stringHints
|
||||||
|
) );
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<OGLWindow> OGLWindow::CreateSharedWindow(
|
||||||
|
unsigned int x,
|
||||||
|
unsigned int y,
|
||||||
|
const std::string& title,
|
||||||
|
std::shared_ptr<OGLWindow> sharingWindow,
|
||||||
|
const std::vector<std::array<int, 2>>& integerHints,
|
||||||
|
const std::vector<std::tuple<int, std::string>>& stringHints
|
||||||
|
) {
|
||||||
|
return std::shared_ptr<OGLWindow>( new OGLWindow(
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
title,
|
||||||
|
integerHints,
|
||||||
|
stringHints,
|
||||||
|
sharingWindow->getWindowPtr()
|
||||||
|
) );
|
||||||
|
}
|
||||||
|
|
||||||
OGLWindow::OGLWindow(
|
OGLWindow::OGLWindow(
|
||||||
unsigned int x,
|
unsigned int x,
|
||||||
unsigned int y,
|
unsigned int y,
|
||||||
const std::string&,
|
const std::string& title,
|
||||||
|
const std::vector<std::array<int, 2>>& integerHints,
|
||||||
|
const std::vector<std::tuple<int, std::string>>& stringHints,
|
||||||
GLFWwindow* sharingContext
|
GLFWwindow* sharingContext
|
||||||
) {
|
) : _title(title) {
|
||||||
CheckForMainThread();
|
CheckForMainThread();
|
||||||
InitGLFW();
|
InitGLFW();
|
||||||
|
for (const auto& hint : integerHints) {
|
||||||
|
SetHintInteger(hint);
|
||||||
|
}
|
||||||
|
for (const auto& hint : stringHints) {
|
||||||
|
SetHintString(hint);
|
||||||
|
}
|
||||||
_window_ptr = glfwCreateWindow(
|
_window_ptr = glfwCreateWindow(
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
@@ -148,4 +200,14 @@ void OGLWindow::setTitle(const std::string& name) {
|
|||||||
glfwSetWindowTitle(_window_ptr, _title.c_str());
|
glfwSetWindowTitle(_window_ptr, _title.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* template<> void OGLWindow::setCallBack<OGLWindow::KEY>( CallbackFunc<int,int,int,int> func) {
|
||||||
|
CheckForMainThread();
|
||||||
|
auto tmp = makeCurrent();
|
||||||
|
auto tmp_func =
|
||||||
|
[=](GLFWwindow* window, int a, int b, int c, int d){
|
||||||
|
func(this, a, b, c, d);
|
||||||
|
};
|
||||||
|
glfwSetKeyCallback(_window_ptr, tmp_func);
|
||||||
|
} */
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -3,7 +3,6 @@
|
|||||||
#include "stb_image.h"
|
#include "stb_image.h"
|
||||||
|
|
||||||
#include <NBCore/Errors.hpp>
|
#include <NBCore/Errors.hpp>
|
||||||
#include <NBCore/TypeTraits.hpp>
|
|
||||||
#include <NBGraphics/Image.hpp>
|
#include <NBGraphics/Image.hpp>
|
||||||
#include <NBGraphics/Mesh.hpp>
|
#include <NBGraphics/Mesh.hpp>
|
||||||
#include <NBGraphics/ProgramPipeline.hpp>
|
#include <NBGraphics/ProgramPipeline.hpp>
|
||||||
@@ -18,8 +17,8 @@ int main() {
|
|||||||
400,
|
400,
|
||||||
400,
|
400,
|
||||||
"Howdy!",
|
"Howdy!",
|
||||||
nb::KeyVal(GLFW_CONTEXT_VERSION_MAJOR, 3),
|
{{GLFW_CONTEXT_VERSION_MAJOR, 3},
|
||||||
nb::KeyVal(GLFW_CONTEXT_VERSION_MINOR, 3)
|
{GLFW_CONTEXT_VERSION_MINOR, 3}}
|
||||||
);
|
);
|
||||||
nb::OGLWindow::MakeCurrent(window);
|
nb::OGLWindow::MakeCurrent(window);
|
||||||
|
|
||||||
@@ -70,25 +69,13 @@ int main() {
|
|||||||
}}, indxs);
|
}}, indxs);
|
||||||
LOG(prog.log());
|
LOG(prog.log());
|
||||||
|
|
||||||
using nb::WindowCallbacks;
|
GLFWwindow* window_ptr = window->getWindowPtr();
|
||||||
|
while(!glfwWindowShouldClose(window_ptr)) {
|
||||||
window->setCallback<nb::KEY>(
|
|
||||||
[](nb::OGLWindow* w, int key, int, int, int)->void{
|
|
||||||
if (key == GLFW_KEY_Q) {
|
|
||||||
w->shouldClose(true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
window->setCallback<nb::CURSOR_POS>(
|
|
||||||
[](nb::OGLWindow* w, double x, double y)->void{
|
|
||||||
nb::logger.msg("Pos in x is " + std::to_string(x));
|
|
||||||
}
|
|
||||||
);
|
|
||||||
while(!window->shouldClose()) {
|
|
||||||
OPENGL_CALL(glClearColor(0.2f, 0.3f, 0.3f, 1.0f));
|
OPENGL_CALL(glClearColor(0.2f, 0.3f, 0.3f, 1.0f));
|
||||||
OPENGL_CALL(glClear(GL_COLOR_BUFFER_BIT));
|
OPENGL_CALL(glClear(GL_COLOR_BUFFER_BIT));
|
||||||
tri.draw();
|
tri.draw();
|
||||||
window->pollEvents();
|
glfwPollEvents();
|
||||||
window->swapBuffers();
|
glfwSwapBuffers(window_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user