Commit 44fc035f authored by Johannes Braun's avatar Johannes Braun
Browse files

GLFW wrapper

parent 7181c324
#pragma once
#include <glfw/glfw3.h>
namespace glfw
{
enum class KeyCode
{
eUnknown = GLFW_KEY_UNKNOWN,
eSpace = GLFW_KEY_SPACE,
eApostrophe = GLFW_KEY_APOSTROPHE,
eComma = GLFW_KEY_COMMA,
eMinus = GLFW_KEY_MINUS,
ePeriod = GLFW_KEY_PERIOD,
eSlash = GLFW_KEY_SLASH,
e0 = GLFW_KEY_0,
e1 = GLFW_KEY_1,
e2 = GLFW_KEY_2,
e3 = GLFW_KEY_3,
e4 = GLFW_KEY_4,
e5 = GLFW_KEY_5,
e6 = GLFW_KEY_6,
e7 = GLFW_KEY_7,
e8 = GLFW_KEY_8,
e9 = GLFW_KEY_9,
eSemicolon = GLFW_KEY_SEMICOLON,
eEqual = GLFW_KEY_EQUAL,
eA = GLFW_KEY_A,
eB = GLFW_KEY_B,
eC = GLFW_KEY_C,
eD = GLFW_KEY_D,
eE = GLFW_KEY_E,
eF = GLFW_KEY_F,
eG = GLFW_KEY_G,
eH = GLFW_KEY_H,
eI = GLFW_KEY_I,
eJ = GLFW_KEY_J,
eK = GLFW_KEY_K,
eL = GLFW_KEY_L,
eM = GLFW_KEY_M,
eN = GLFW_KEY_N,
eO = GLFW_KEY_O,
eP = GLFW_KEY_P,
eQ = GLFW_KEY_Q,
eR = GLFW_KEY_R,
eS = GLFW_KEY_S,
eT = GLFW_KEY_T,
eU = GLFW_KEY_U,
eV = GLFW_KEY_V,
eW = GLFW_KEY_W,
eX = GLFW_KEY_X,
eY = GLFW_KEY_Y,
eZ = GLFW_KEY_Z,
eLeftBracket = GLFW_KEY_LEFT_BRACKET,
eBackslash = GLFW_KEY_BACKSLASH,
eRightBracket = GLFW_KEY_RIGHT_BRACKET,
eGraveAccent = GLFW_KEY_GRAVE_ACCENT,
eWorld1 = GLFW_KEY_WORLD_1,
eWorld2 = GLFW_KEY_WORLD_2,
eEscape = GLFW_KEY_ESCAPE,
eEnter = GLFW_KEY_ENTER,
eTab = GLFW_KEY_TAB,
eBackspace = GLFW_KEY_BACKSPACE,
eInsert = GLFW_KEY_INSERT,
eDelete = GLFW_KEY_DELETE,
eRight = GLFW_KEY_RIGHT,
eLeft = GLFW_KEY_LEFT,
eDown = GLFW_KEY_DOWN,
eUp = GLFW_KEY_UP,
ePageUp = GLFW_KEY_PAGE_UP,
ePageDown = GLFW_KEY_PAGE_DOWN,
eHome = GLFW_KEY_HOME,
eEnd = GLFW_KEY_END,
eCapsLock = GLFW_KEY_CAPS_LOCK,
eScrollLock = GLFW_KEY_SCROLL_LOCK,
eNumLock = GLFW_KEY_NUM_LOCK,
ePrintScreen = GLFW_KEY_PRINT_SCREEN,
ePause = GLFW_KEY_PAUSE,
eF1 = GLFW_KEY_F1,
eF2 = GLFW_KEY_F2,
eF3 = GLFW_KEY_F3,
eF4 = GLFW_KEY_F4,
eF5 = GLFW_KEY_F5,
eF6 = GLFW_KEY_F6,
eF7 = GLFW_KEY_F7,
eF8 = GLFW_KEY_F8,
eF9 = GLFW_KEY_F9,
eF10 = GLFW_KEY_F10,
eF11 = GLFW_KEY_F11,
eF12 = GLFW_KEY_F12,
eF13 = GLFW_KEY_F13,
eF14 = GLFW_KEY_F14,
eF15 = GLFW_KEY_F15,
eF16 = GLFW_KEY_F16,
eF17 = GLFW_KEY_F17,
eF18 = GLFW_KEY_F18,
eF19 = GLFW_KEY_F19,
eF20 = GLFW_KEY_F20,
eF21 = GLFW_KEY_F21,
eF22 = GLFW_KEY_F22,
eF23 = GLFW_KEY_F23,
eF24 = GLFW_KEY_F24,
eF25 = GLFW_KEY_F25,
eNumpad0 = GLFW_KEY_KP_0,
eNumpad1 = GLFW_KEY_KP_1,
eNumpad2 = GLFW_KEY_KP_2,
eNumpad3 = GLFW_KEY_KP_3,
eNumpad4 = GLFW_KEY_KP_4,
eNumpad5 = GLFW_KEY_KP_5,
eNumpad6 = GLFW_KEY_KP_6,
eNumpad7 = GLFW_KEY_KP_7,
eNumpad8 = GLFW_KEY_KP_8,
eNumpad9 = GLFW_KEY_KP_9,
eNumpadDecimal = GLFW_KEY_KP_DECIMAL,
eNumpadDivide = GLFW_KEY_KP_DIVIDE,
eNumpadMultiply = GLFW_KEY_KP_MULTIPLY,
eNumpadSubtract = GLFW_KEY_KP_SUBTRACT,
eNumpadAdd = GLFW_KEY_KP_ADD,
eNumpadEnter = GLFW_KEY_KP_ENTER,
eNumpadEqual = GLFW_KEY_KP_EQUAL,
eLeftShift = GLFW_KEY_LEFT_SHIFT,
eLeftControl = GLFW_KEY_LEFT_CONTROL,
eLeftAlt = GLFW_KEY_LEFT_ALT,
eLeftSuper = GLFW_KEY_LEFT_SUPER,
eRightShift = GLFW_KEY_RIGHT_SHIFT,
eRightControl = GLFW_KEY_RIGHT_CONTROL,
eRightAlt = GLFW_KEY_RIGHT_ALT,
eRightSuper = GLFW_KEY_RIGHT_SUPER,
eMenu = GLFW_KEY_MENU,
eLast = GLFW_KEY_LAST,
};
enum class KeyMods
{
eShift = GLFW_MOD_SHIFT,
eControl = GLFW_MOD_CONTROL,
eAlt = GLFW_MOD_ALT,
eSuper = GLFW_MOD_SUPER,
};
enum class KeyAction
{
ePress = GLFW_PRESS,
eRepeat = GLFW_REPEAT,
eRelease = GLFW_RELEASE,
};
}
\ No newline at end of file
#include "window.h"
namespace glfw
{
Window::Window(int width, int height, const std::string& title, const Monitor& monitor, const Window& share)
: window(glfwCreateWindow(width, height, title.c_str(), monitor, share.get()))
{
glfwSetWindowUserPointer(window, this);
glfwSetKeyCallback(window, [](GLFWwindow* w, int k, int sc, int ac, int md){
auto&& window = *reinterpret_cast<Window*>(glfwGetWindowUserPointer(w));
window.m_key_callback(static_cast<KeyCode>(k), static_cast<KeyAction>(ac), static_cast<KeyMods>(md));
});
glfwMakeContextCurrent(window);
glewExperimental = true;
glewInit();
}
Window::Window(nullptr_t)
: window(nullptr){}
Window::~Window()
{
m_destroy_callback();
glfwDestroyWindow(window);
glfwMakeContextCurrent(nullptr);
}
GLFWwindow* Window::get() const{
return window;
}
void Window::update()
{
if (window)
{
glfwMakeContextCurrent(window);
m_frame_callback();
}
}
void Window::close()
{
glfwSetWindowShouldClose(window, true);
m_key_callback = [](auto, auto, auto) {};
}
void Window::setDestroyCallback(FrameCallback callback)
{
m_destroy_callback = callback;
}
bool Window::shouldClose() const
{
return glfwWindowShouldClose(window);
}
void Window::makeCurrent() const
{
glfwMakeContextCurrent(window);
}
void Window::setFrameCallback(FrameCallback callback)
{
m_frame_callback = [this, callback](){
callback();
swapBuffers();
};
}
void Window::setKeyCallback(KeyCallback callback)
{
m_key_callback = callback;
}
void Window::swapBuffers() const
{
glfwSwapBuffers(window);
}
}
#pragma once
#include <functional>
#include <string>
#include <util/opengl.h>
#include <GLFW/glfw3.h>
#include "controls.h"
namespace glfw
{
using Monitor = GLFWmonitor*;
struct WindowManager;
struct Window
{
friend struct WindowManager;
using FrameCallback = std::function<void()>;
using DestroyCallback = std::function<void()>;
using KeyCallback = std::function<void(KeyCode key, KeyAction action, KeyMods mods)>;
Window(nullptr_t);
~Window();
GLFWwindow* get() const;
bool shouldClose() const;
void makeCurrent() const;
void setKeyCallback(KeyCallback callback);
void setFrameCallback(FrameCallback callback);
void setDestroyCallback(FrameCallback callback);
void swapBuffers() const;
void update();
void close();
protected:
Window(int width, int height, const std::string& title, const Monitor& monitor, const Window& share);
private:
GLFWwindow* window;
DestroyCallback m_destroy_callback = []() {};
FrameCallback m_frame_callback = [this](){ swapBuffers(); };
KeyCallback m_key_callback = [](auto, auto, auto){};
};
}
#include "window_manager.h"
namespace glfw
{
WindowManager::WindowManager()
{
glfwInit();
}
WindowManager::~WindowManager()
{
glfwTerminate();
}
void WindowManager::setHint(WindowHintInt hint, int value)
{
glfwWindowHint(static_cast<unsigned>(hint), value);
}
void WindowManager::setHint(WindowHintBool hint, bool value)
{
glfwWindowHint(static_cast<unsigned>(hint), value);
}
void WindowManager::setHint(ClientApi api)
{
glfwWindowHint(GLFW_CLIENT_API, static_cast<int>(api));
}
void WindowManager::setHint(ContextCreationApi api)
{
glfwWindowHint(GLFW_CONTEXT_CREATION_API, static_cast<int>(api));
}
void WindowManager::setHint(ContextRobustness robustness)
{
glfwWindowHint(GLFW_CONTEXT_ROBUSTNESS, static_cast<int>(robustness));
}
void WindowManager::setHint(ReleaseBehavior behavior)
{
glfwWindowHint(GLFW_CONTEXT_RELEASE_BEHAVIOR, static_cast<int>(behavior));
}
void WindowManager::setHint(OpenGLProfile profile)
{
glfwWindowHint(GLFW_OPENGL_PROFILE, static_cast<int>(profile));
}
void WindowManager::pollEvents() const
{
glfwPollEvents();
}
std::shared_ptr<Window> WindowManager::makeWindow(int width, int height, const std::string& title, const Monitor& monitor, const Window& share)
{
struct W : public Window {
W(int w, int h, const std::string& t, const Monitor& m, const Window& s)
: Window(w, h, t, m, s){}
};
m_windows.push_back(std::make_shared<W>(width, height, title, monitor, share));
return m_windows.back();
}
const std::vector<std::shared_ptr<glfw::Window>>& WindowManager::windows() const
{
return m_windows;
}
bool WindowManager::validateOpenWindows()
{
for (auto it = m_windows.begin(); it != m_windows.end();)
{
if ((*it)->shouldClose())
{
glfwMakeContextCurrent(it->get()->get());
it->get()->m_frame_callback = []() {};
it->reset();
it = m_windows.erase(it);
glfwMakeContextCurrent(nullptr);
}
else
{
++it;
}
}
return !m_windows.empty();
}
}
#pragma once
#include <memory>
#include <util/opengl.h>
#define GLFW_INCLUDE_GLEXT
#include <GLFW/glfw3.h>
#include "window.h"
namespace glfw
{
enum class WindowHintBool
{
eResizable = GLFW_RESIZABLE,
eVisible = GLFW_VISIBLE,
eDecorated = GLFW_DECORATED,
eFocused = GLFW_FOCUSED,
eAutoIconify = GLFW_AUTO_ICONIFY,
eFloating = GLFW_FLOATING,
eMaximized = GLFW_MAXIMIZED,
eStereo = GLFW_STEREO,
eSRGBCapable = GLFW_SRGB_CAPABLE,
eDoublebuffer = GLFW_DOUBLEBUFFER,
eOpenGLForwardCompat = GLFW_OPENGL_FORWARD_COMPAT,
eOpenGLDebugContext = GLFW_OPENGL_DEBUG_CONTEXT,
};
enum class ClientApi
{
eOpenGL = GLFW_OPENGL_API,
eOpenGLES = GLFW_OPENGL_ES_API,
eNone = GLFW_NO_API,
};
enum class ContextCreationApi
{
eNative = GLFW_NATIVE_CONTEXT_API,
eEGL = GLFW_EGL_CONTEXT_API,
};
enum class ContextRobustness
{
eNone = GLFW_NO_ROBUSTNESS,
eNoResetNotification = GLFW_NO_RESET_NOTIFICATION,
eLoseContextOnReset = GLFW_LOSE_CONTEXT_ON_RESET,
};
enum class ReleaseBehavior
{
eAny = GLFW_ANY_RELEASE_BEHAVIOR,
eFlush = GLFW_RELEASE_BEHAVIOR_FLUSH,
eNone = GLFW_RELEASE_BEHAVIOR_NONE,
};
enum class OpenGLProfile
{
eAny = GLFW_OPENGL_ANY_PROFILE,
eCompat = GLFW_OPENGL_COMPAT_PROFILE,
eCore = GLFW_OPENGL_CORE_PROFILE,
};
enum class WindowHint
{
eClientApi = GLFW_CLIENT_API,
eContextCreationApi = GLFW_CONTEXT_CREATION_API,
eContextRobustness = GLFW_CONTEXT_ROBUSTNESS,
eContextReleaseBehavior = GLFW_CONTEXT_RELEASE_BEHAVIOR,
eOpenGLProfile = GLFW_OPENGL_PROFILE,
};
enum class WindowHintInt
{
eRedBits = GLFW_RED_BITS,
eGreenBits = GLFW_GREEN_BITS,
eBlueBits = GLFW_BLUE_BITS,
eAlphaBits = GLFW_ALPHA_BITS,
eDepthBits = GLFW_DEPTH_BITS,
eStencilBits = GLFW_STENCIL_BITS,
eAccumRedBits = GLFW_ACCUM_RED_BITS,
eAccumGreenBits = GLFW_ACCUM_GREEN_BITS,
eAccumBlueBits = GLFW_ACCUM_BLUE_BITS,
eAccumAlphaBits = GLFW_ACCUM_ALPHA_BITS,
eAuxBuffers = GLFW_AUX_BUFFERS,
eSamples = GLFW_SAMPLES,
eRefreshRate = GLFW_REFRESH_RATE,
eContextVersionMajor = GLFW_CONTEXT_VERSION_MAJOR,
eContextVersionMinor = GLFW_CONTEXT_VERSION_MINOR,
};
struct WindowManager
{
WindowManager();
~WindowManager();
void setHint(WindowHintInt hint, int value);
void setHint(WindowHintBool hint, bool value);
void setHint(ClientApi api);
void setHint(ContextCreationApi api);
void setHint(ContextRobustness robustness);
void setHint(ReleaseBehavior behavior);
void setHint(OpenGLProfile profile);
void pollEvents() const;
std::shared_ptr<Window> makeWindow(int width, int height, const std::string& title, const Monitor& monitor, const Window& share);
bool validateOpenWindows();
const std::vector<std::shared_ptr<glfw::Window>>& windows() const;
private:
std::vector<std::shared_ptr<glfw::Window>> m_windows;
};
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment