Compare commits
5
Commits
06f261b793
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
97aed4e5ca | ||
|
|
416446206f | ||
|
|
c6ab0327c1 | ||
|
|
e6a5c9e805 | ||
|
|
b9e8144519 |
+4
-3
@@ -1,9 +1,10 @@
|
||||
build/
|
||||
.vscode/
|
||||
.*
|
||||
!.gitignore
|
||||
!.clangd
|
||||
CMakeFiles/
|
||||
CMakeKits.json
|
||||
CMakeCache.txt
|
||||
docs/*
|
||||
!docs/CMake*
|
||||
!docs/Doxyfile.in
|
||||
!.clangd
|
||||
.glsld
|
||||
+16
-29
@@ -28,45 +28,32 @@ function(toAbsolutePath SETVAR PATHS)
|
||||
set(${SETVAR} ${RET_VAL} PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
if (NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE "Debug")
|
||||
endif()
|
||||
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||
message(STATUS "Targeting Release build")
|
||||
add_compile_definitions(_NB_BUILD_RELEASE)
|
||||
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
message(STATUS "Targeting Debug build")
|
||||
set(NB_DEBUG_BUILD ON)
|
||||
add_compile_definitions(_NB_BUILD_DEBUG)
|
||||
endif()
|
||||
message(STATUS "Targeting ${CMAKE_BUILD_TYPE} build")
|
||||
|
||||
if (NB_DEBUG_BUILD)
|
||||
set(NB_LOGGING ON)
|
||||
set(NB_BUILD_TESTS ON)
|
||||
set(NB_BUILD_DOCS ON)
|
||||
set(NB_MONITOR_OPENGL_CALLS ON)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
|
||||
endif()
|
||||
|
||||
cmake_dependent_option(NB_LOGGING
|
||||
"Creates a default logger and automatically logs with code locations."
|
||||
ON
|
||||
NB_DEBUG_BUILD
|
||||
OFF
|
||||
)
|
||||
cmake_dependent_option(NB_BUILD_TESTS
|
||||
"Build unit tests"
|
||||
ON
|
||||
NB_DEBUG_BUILD
|
||||
OFF
|
||||
)
|
||||
cmake_dependent_option(NB_BUILD_DOCS
|
||||
"Build documentation"
|
||||
ON
|
||||
NB_DEBUG_BUILD
|
||||
OFF
|
||||
)
|
||||
cmake_dependent_option(NBENGINE_INSTALL
|
||||
"Install NBEngine"
|
||||
ON
|
||||
NB_DEBUG_BUILD
|
||||
OFF
|
||||
)
|
||||
cmake_dependent_option(NB_MONITOR_OPENGL_CALLS
|
||||
"Monitors every OpenGL call for errors"
|
||||
ON
|
||||
NB_DEBUG_BUILD
|
||||
OFF
|
||||
)
|
||||
|
||||
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
|
||||
message(STATUS "Building for Windows")
|
||||
@@ -106,13 +93,13 @@ endif()
|
||||
|
||||
get_filename_component(NBENGINE_ROOT ${CMAKE_CURRENT_LIST_DIR} ABSOLUTE)
|
||||
|
||||
add_subdirectory(./engine ${PROJECT_BINARY_DIR}/${CMAKE_BUILD_TYPE})
|
||||
add_subdirectory(./engine)
|
||||
|
||||
# Toggle
|
||||
set(NB_REBUILD_DOCS)
|
||||
|
||||
if (NB_BUILD_DOCS)
|
||||
add_subdirectory(./docs)
|
||||
add_subdirectory(./docs)
|
||||
endif()
|
||||
|
||||
include(CMakePackageConfigHelpers)
|
||||
|
||||
@@ -18,7 +18,8 @@ toAbsolutePath(NB_CORE_SOURCE
|
||||
)
|
||||
toAbsolutePath(NB_CORE_INCLUDE
|
||||
./include/NBCore/DataSink.hpp
|
||||
./include/NBCore/Errors.hpp
|
||||
./include/NBCore/DataTypes.hpp
|
||||
./include/NBCore/Errors.hpp
|
||||
./include/NBCore/ErrorsImpl.hpp
|
||||
./include/NBCore/Logger.hpp
|
||||
./include/NBCore/Printer.hpp
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
#include <cstring>
|
||||
#ifndef _NB_DATA_TYPES
|
||||
#define _NB_DATA_TYPES
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <memory>
|
||||
|
||||
namespace nb {
|
||||
|
||||
template<typename T, unsigned int N>
|
||||
class NDArray {
|
||||
protected:
|
||||
std::unique_ptr<T[]> _data;
|
||||
static unsigned int calculateTotalSize(const std::array<unsigned int, N>& dims) {
|
||||
unsigned int ret = 1;
|
||||
std::for_each(dims.begin(), dims.end(), [&](unsigned int n){
|
||||
ret *= n;
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
public:
|
||||
const std::array<unsigned int, N> dimensions;
|
||||
const unsigned int totalSize;
|
||||
static constexpr unsigned int NumberDimensions = N;
|
||||
NDArray(
|
||||
const std::array<unsigned int, N>& dimensions_,
|
||||
T* data_=nullptr
|
||||
) :
|
||||
dimensions(dimensions_),
|
||||
totalSize(calculateTotalSize(dimensions_))
|
||||
{
|
||||
if (data_) { data(data_); }
|
||||
}
|
||||
NDArray(
|
||||
const std::array<unsigned int, N>& dimensions_,
|
||||
const T* data_
|
||||
) : NDArray(dimensions_) { data(data_); }
|
||||
|
||||
unsigned int coordinateToIndex(const std::array<T, N>& coord) const {
|
||||
unsigned int blockSize = 1;
|
||||
unsigned int ret = 0;
|
||||
for (auto i = 0; i < dimensions.size(); ++i) {
|
||||
ret += coord[i] * blockSize;
|
||||
blockSize *= dimensions[i];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline const T& at(const std::array<T, N>& coord) const {
|
||||
return _data[coordinateToIndex(coord)];
|
||||
}
|
||||
inline std::shared_ptr<T> data() const { return _data; }
|
||||
|
||||
inline T& at(const std::array<T, N>& coord) {
|
||||
return _data[coordinateToIndex(coord)];
|
||||
}
|
||||
inline T* data(const T* data_) {
|
||||
_data = std::make_unique<T[]>(totalSize);
|
||||
std::memcpy(_data.get(), data_, totalSize * sizeof(T));
|
||||
return _data.get();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}; // namespace nb
|
||||
|
||||
}
|
||||
#endif // _NB_DATA_TYPES
|
||||
@@ -3,6 +3,7 @@
|
||||
#define _NB_TYPE_TRAITS
|
||||
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace nb {
|
||||
|
||||
@@ -29,6 +30,9 @@ 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
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
#ifndef _NB_CORE_TYPES
|
||||
#define _NB_CORE_TYPES
|
||||
#ifndef _NB_UTILTIES
|
||||
#define _NB_UTILTIES
|
||||
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
@@ -1,24 +1,23 @@
|
||||
cmake_minimum_required(VERSION 3.26.0)
|
||||
|
||||
if (NB_BUILD_TESTS)
|
||||
enable_testing()
|
||||
include(GoogleTest)
|
||||
add_executable(TestCore
|
||||
./testErrors.cpp
|
||||
#./testProcesses.cpp
|
||||
./testUtils.cpp
|
||||
)
|
||||
target_link_libraries(TestCore
|
||||
NBCore
|
||||
GTest::gtest_main
|
||||
)
|
||||
enable_testing()
|
||||
include(GoogleTest)
|
||||
add_executable(TestCore
|
||||
./testErrors.cpp
|
||||
#./testProcesses.cpp
|
||||
./testUtils.cpp
|
||||
)
|
||||
target_link_libraries(TestCore
|
||||
NBCore
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
add_executable(LoggerTest
|
||||
./testLogger.cpp
|
||||
)
|
||||
target_link_libraries(LoggerTest
|
||||
NBCore
|
||||
)
|
||||
add_executable(LoggerTest
|
||||
./testLogger.cpp
|
||||
)
|
||||
target_link_libraries(LoggerTest
|
||||
NBCore
|
||||
)
|
||||
|
||||
gtest_discover_tests(TestCore)
|
||||
|
||||
gtest_discover_tests(TestCore)
|
||||
endif()
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <string>
|
||||
#include <NBCore/DataTypes.hpp>
|
||||
#include <NBCore/TypeTraits.hpp>
|
||||
#include <NBCore/StringUtils.hpp>
|
||||
|
||||
@@ -22,7 +23,7 @@ TEST(UtilsTest, TestFindAndReplace) {
|
||||
|
||||
tmp = find_and_replace("aeiou", "a", "afa");
|
||||
tmp = find_and_replace(tmp, "e", "efe");
|
||||
tmp = find_and_replace(tmp, "i", "ifi");
|
||||
tmp = find_and_replace(tmp, "i", "ifi");
|
||||
tmp = find_and_replace(tmp, "o", "ofo");
|
||||
tmp = find_and_replace(tmp, "u", "ufu");
|
||||
ASSERT_STREQ(
|
||||
@@ -46,7 +47,6 @@ TEST(UtilsTest, TestFindAndReplaceWstr) {
|
||||
find_and_replace<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(),
|
||||
@@ -74,44 +74,6 @@ TEST(UtilsTest, TestFindAndReplaceWstr) {
|
||||
);
|
||||
}
|
||||
|
||||
/* TEST(UtilsTest, TestWstrToStr) {
|
||||
ASSERT_STREQ(
|
||||
nb::wstr_to_str(L"Hi!").c_str(),
|
||||
"Hi!"
|
||||
);
|
||||
ASSERT_STREQ(
|
||||
nb::wstr_to_str(L"Naif").c_str(),
|
||||
"Naif"
|
||||
);
|
||||
ASSERT_STREQ(
|
||||
nb::wstr_to_str(L"\r\r\r\n").c_str(),
|
||||
"\r\r\r\n"
|
||||
);
|
||||
ASSERT_STREQ(
|
||||
nb::wstr_to_str(L"Naif\ttalks\r\ra\t\nlot").c_str(),
|
||||
"Naif\ttalks\r\ra\t\nlot"
|
||||
);
|
||||
} */
|
||||
|
||||
/* TEST(UtilsTest, TestStrToWstr) {
|
||||
ASSERT_STREQ(
|
||||
nb::str_to_wstr("Hi!").c_str(),
|
||||
L"Hi!"
|
||||
);
|
||||
ASSERT_STREQ(
|
||||
nb::str_to_wstr("Naif").c_str(),
|
||||
L"Naif"
|
||||
);
|
||||
ASSERT_STREQ(
|
||||
nb::str_to_wstr("\r\r\r\n").c_str(),
|
||||
L"\r\r\r\n"
|
||||
);
|
||||
ASSERT_STREQ(
|
||||
nb::str_to_wstr("Naif\ttalks\r\ra\t\nlot").c_str(),
|
||||
L"Naif\ttalks\r\ra\t\nlot"
|
||||
);
|
||||
} */
|
||||
|
||||
struct A { bool x(); };
|
||||
struct B { bool y(); };
|
||||
struct C { bool x(); bool y; };
|
||||
@@ -145,4 +107,22 @@ TEST(UtilsTest, TestIsDetected) {
|
||||
ASSERT_TRUE(ret);
|
||||
}
|
||||
|
||||
TEST(DataTypesTests, TestNDArray) {
|
||||
float* data_f = new float[9] {0,1,2,3,4,5,6,7,8};
|
||||
nb::NDArray<float, 2> arr({3, 3}, data_f);
|
||||
ASSERT_FLOAT_EQ(data_f[4], arr.at({1,1}));
|
||||
arr.at({1,1}) = 0.5;
|
||||
ASSERT_FLOAT_EQ(0.5, arr.at({1,1}));
|
||||
delete[] data_f;
|
||||
|
||||
unsigned int* data_ui = new unsigned int[8] {0,1,2,3,4,5,6,7};
|
||||
nb::NDArray<unsigned int, 3> arr3d({2,2,2}, data_ui);
|
||||
ASSERT_EQ(data_ui[5], arr3d.at({1,0,1}));
|
||||
arr3d.at({0,1,1}) = 23;
|
||||
ASSERT_EQ(23, arr3d.at({0,1,1}));
|
||||
delete[] data_ui;
|
||||
|
||||
|
||||
}
|
||||
|
||||
} // namespace nb
|
||||
@@ -6,8 +6,11 @@
|
||||
#include <NBGraphics/OGLObjects.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <NBCore/Errors.hpp>
|
||||
@@ -30,6 +33,86 @@ class GLFWError : public Error<GLFWError> {
|
||||
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 {
|
||||
using GLFWHint = std::array<int, 2>;
|
||||
private:
|
||||
@@ -45,32 +128,30 @@ class OGLWindow {
|
||||
static thread_local GLFWwindow* _currWindow;
|
||||
static thread_local const GLFWwindow* _currArena;
|
||||
|
||||
static void SetHintInteger(const std::array<int, 2>&);
|
||||
static void SetHintString(const std::tuple<int, std::string>&);
|
||||
template<typename T>
|
||||
static void SetHint(std::pair<int, T> hint) {
|
||||
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;
|
||||
GLFWwindow* _window_ptr;
|
||||
const GLFWwindow* _arena;
|
||||
|
||||
OGLWindow(
|
||||
unsigned int,
|
||||
unsigned int,
|
||||
unsigned int x,
|
||||
unsigned int y,
|
||||
const std::string&,
|
||||
const std::vector<std::array<int, 2>>&,
|
||||
const std::vector<std::tuple<int, std::string>>&,
|
||||
GLFWwindow* sharingContext = NULL
|
||||
);
|
||||
|
||||
GLFWwindow* makeCurrent() const;
|
||||
void detachWindow();
|
||||
|
||||
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 bool InitGLFW(const std::vector<GLFWHint>& hints={});
|
||||
@@ -80,21 +161,38 @@ class OGLWindow {
|
||||
static GLFWwindow* MakeCurrent(const std::shared_ptr<OGLWindow>&);
|
||||
static inline GLFWwindow* DropCurrent() { return MakeCurrent(nullptr); }
|
||||
static void DestroyWindow(std::shared_ptr<OGLWindow>&);
|
||||
template <typename... T>
|
||||
static std::shared_ptr<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={}
|
||||
);
|
||||
const std::pair<int, T>&... windowHints
|
||||
) {
|
||||
(SetHint(std::pair(windowHints)),... );
|
||||
return std::shared_ptr<OGLWindow>(new OGLWindow(
|
||||
x,
|
||||
y,
|
||||
title,
|
||||
nullptr
|
||||
) );
|
||||
}
|
||||
template<typename... T>
|
||||
static std::shared_ptr<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={}
|
||||
);
|
||||
std::shared_ptr<OGLWindow> sharingWindow = NULL,
|
||||
const std::pair<int, T>&... windowHints
|
||||
) {
|
||||
(SetHint(std::pair(windowHints)),... );
|
||||
return std::shared_ptr<OGLWindow>(new OGLWindow(
|
||||
x,
|
||||
y,
|
||||
title,
|
||||
sharingWindow->_window_ptr
|
||||
) );
|
||||
}
|
||||
|
||||
OGLWindow(OGLWindow&&) = delete;
|
||||
OGLWindow(const OGLWindow&) = delete;
|
||||
~OGLWindow();
|
||||
@@ -106,14 +204,91 @@ class OGLWindow {
|
||||
inline std::string getTitle() const { return _title; }
|
||||
std::array<int, 2> getSize() const;
|
||||
bool shouldClose() const;
|
||||
inline void processEvents() const { CheckForMainThread(); glfwPollEvents(); }
|
||||
inline void refresh() const { glfwSwapBuffers(_window_ptr); }
|
||||
inline void pollEvents() const { CheckForMainThread(); glfwPollEvents(); }
|
||||
inline void swapBuffers() const { glfwSwapBuffers(_window_ptr); }
|
||||
|
||||
void setSize(unsigned int x, unsigned int y);
|
||||
void setTitle(const std::string&);
|
||||
void shouldClose(bool);
|
||||
/* template<enum Callbacks, typename Func>
|
||||
void setCallBack(Func); */
|
||||
|
||||
template<WindowCallbacks CallbackType, typename CallbackFunc>
|
||||
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,79 +48,27 @@ bool OGLWindow::TerminateGLFW() {
|
||||
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) {
|
||||
auto tmp = _currWindow;
|
||||
_currWindow = window->getWindowPtr();
|
||||
if (window) {
|
||||
glfwMakeContextCurrent(_currWindow);
|
||||
_currWindow = window->getWindowPtr();
|
||||
_currArena = window->_arena;
|
||||
} else {
|
||||
glfwMakeContextCurrent(nullptr);
|
||||
_currWindow = nullptr;
|
||||
_currArena = nullptr;
|
||||
}
|
||||
glfwMakeContextCurrent(_currWindow);
|
||||
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(
|
||||
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,
|
||||
const std::string&,
|
||||
GLFWwindow* sharingContext
|
||||
) : _title(title) {
|
||||
) {
|
||||
CheckForMainThread();
|
||||
InitGLFW();
|
||||
for (const auto& hint : integerHints) {
|
||||
SetHintInteger(hint);
|
||||
}
|
||||
for (const auto& hint : stringHints) {
|
||||
SetHintString(hint);
|
||||
}
|
||||
_window_ptr = glfwCreateWindow(
|
||||
x,
|
||||
y,
|
||||
@@ -200,14 +148,4 @@ void OGLWindow::setTitle(const std::string& name) {
|
||||
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,6 +3,7 @@
|
||||
#include "stb_image.h"
|
||||
|
||||
#include <NBCore/Errors.hpp>
|
||||
#include <NBCore/TypeTraits.hpp>
|
||||
#include <NBGraphics/Image.hpp>
|
||||
#include <NBGraphics/Mesh.hpp>
|
||||
#include <NBGraphics/ProgramPipeline.hpp>
|
||||
@@ -17,8 +18,8 @@ int main() {
|
||||
400,
|
||||
400,
|
||||
"Howdy!",
|
||||
{{GLFW_CONTEXT_VERSION_MAJOR, 3},
|
||||
{GLFW_CONTEXT_VERSION_MINOR, 3}}
|
||||
nb::KeyVal(GLFW_CONTEXT_VERSION_MAJOR, 3),
|
||||
nb::KeyVal(GLFW_CONTEXT_VERSION_MINOR, 3)
|
||||
);
|
||||
nb::OGLWindow::MakeCurrent(window);
|
||||
|
||||
@@ -69,13 +70,25 @@ int main() {
|
||||
}}, indxs);
|
||||
LOG(prog.log());
|
||||
|
||||
GLFWwindow* window_ptr = window->getWindowPtr();
|
||||
while(!glfwWindowShouldClose(window_ptr)) {
|
||||
using nb::WindowCallbacks;
|
||||
|
||||
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(glClear(GL_COLOR_BUFFER_BIT));
|
||||
tri.draw();
|
||||
glfwPollEvents();
|
||||
glfwSwapBuffers(window_ptr);
|
||||
window->pollEvents();
|
||||
window->swapBuffers();
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user