Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c6ab0327c1 | ||
|
|
e6a5c9e805 | ||
|
|
b9e8144519 |
@@ -6,6 +6,7 @@
|
|||||||
#include <NBGraphics/OGLObjects.hpp>
|
#include <NBGraphics/OGLObjects.hpp>
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -30,6 +31,86 @@ 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:
|
||||||
@@ -56,21 +137,14 @@ class OGLWindow {
|
|||||||
unsigned int,
|
unsigned int,
|
||||||
unsigned int,
|
unsigned int,
|
||||||
const std::string&,
|
const std::string&,
|
||||||
const std::vector<std::array<int, 2>>&,
|
const std::vector<std::array<int, 2>>& integerWindowHints,
|
||||||
const std::vector<std::tuple<int, std::string>>&,
|
const std::vector<std::tuple<int, std::string>>& stringWindowHints={},
|
||||||
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={});
|
||||||
@@ -95,6 +169,7 @@ class OGLWindow {
|
|||||||
const std::vector<std::array<int, 2>>& integerHints={},
|
const std::vector<std::array<int, 2>>& integerHints={},
|
||||||
const std::vector<std::tuple<int, std::string>>& stringHints={}
|
const std::vector<std::tuple<int, std::string>>& stringHints={}
|
||||||
);
|
);
|
||||||
|
|
||||||
OGLWindow(OGLWindow&&) = delete;
|
OGLWindow(OGLWindow&&) = delete;
|
||||||
OGLWindow(const OGLWindow&) = delete;
|
OGLWindow(const OGLWindow&) = delete;
|
||||||
~OGLWindow();
|
~OGLWindow();
|
||||||
@@ -106,14 +181,91 @@ 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 processEvents() const { CheckForMainThread(); glfwPollEvents(); }
|
inline void pollEvents() const { CheckForMainThread(); glfwPollEvents(); }
|
||||||
inline void refresh() const { glfwSwapBuffers(_window_ptr); }
|
inline void swapBuffers() 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>
|
|
||||||
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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -60,14 +60,14 @@ void OGLWindow::SetHintString(const std::tuple<int, std::string>& hint) {
|
|||||||
|
|
||||||
GLFWwindow* OGLWindow::MakeCurrent(const std::shared_ptr<OGLWindow>& window) {
|
GLFWwindow* OGLWindow::MakeCurrent(const std::shared_ptr<OGLWindow>& window) {
|
||||||
auto tmp = _currWindow;
|
auto tmp = _currWindow;
|
||||||
_currWindow = window->getWindowPtr();
|
|
||||||
if (window) {
|
if (window) {
|
||||||
glfwMakeContextCurrent(_currWindow);
|
_currWindow = window->getWindowPtr();
|
||||||
_currArena = window->_arena;
|
_currArena = window->_arena;
|
||||||
} else {
|
} else {
|
||||||
glfwMakeContextCurrent(nullptr);
|
_currWindow = nullptr;
|
||||||
_currArena = nullptr;
|
_currArena = nullptr;
|
||||||
}
|
}
|
||||||
|
glfwMakeContextCurrent(_currWindow);
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,14 +200,4 @@ 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);
|
|
||||||
} */
|
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -69,13 +69,25 @@ int main() {
|
|||||||
}}, indxs);
|
}}, indxs);
|
||||||
LOG(prog.log());
|
LOG(prog.log());
|
||||||
|
|
||||||
GLFWwindow* window_ptr = window->getWindowPtr();
|
using nb::WindowCallbacks;
|
||||||
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();
|
||||||
glfwPollEvents();
|
window->pollEvents();
|
||||||
glfwSwapBuffers(window_ptr);
|
window->swapBuffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user