Skip to content

Commit

Permalink
Wrapped some glfw calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brotcrunsher committed Nov 24, 2023
1 parent ab5008c commit faa1038
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
[Bb]uildEmscripten/
[Bb]uildPortaudio/
[Bb]uildLinuxNull/
[Bb]uildVulkan/
bld/
[Bb]in/
[Oo]bj/
Expand Down
6 changes: 6 additions & 0 deletions BrotBoxEngine/BBE/glfwWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,11 @@ namespace bbe
void glfwGetWindowPos(GLFWwindow* handle, int* xpos, int* ypos);
const char* glfwGetClipboardString(GLFWwindow* handle);
void glfwSetClipboardString(GLFWwindow* handle, const char* string);
void glfwSetWindowShouldClose(GLFWwindow* window, int value);
void glfwShowWindow(GLFWwindow* window);
void glfwHideWindow(GLFWwindow* window);
int glfwGetWindowAttrib(GLFWwindow* window, int attrib);
void glfwSetWindowUserPointer(GLFWwindow* window, void* pointer);
void* glfwGetWindowUserPointer(GLFWwindow* window);
}
}
30 changes: 15 additions & 15 deletions BrotBoxEngine/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ bbe::Window::Window(int width, int height, const char * title, uint32_t major, u
#endif

m_pwindow = glfwWrapper::glfwCreateWindow(width, height, title, nullptr, nullptr);
glfwSetWindowUserPointer(m_pwindow, this);
glfwWrapper::glfwSetWindowUserPointer(m_pwindow, this);
if (m_pwindow == nullptr)
{
bbe::INTERNAL::triggerFatalError("Could not create window!");
Expand Down Expand Up @@ -239,17 +239,17 @@ bbe::WindowCloseMode bbe::Window::getWindowCloseMode() const

void bbe::Window::showWindow()
{
glfwShowWindow(m_pwindow);
glfwWrapper::glfwShowWindow(m_pwindow);
}

void bbe::Window::hideWindow()
{
glfwHideWindow(m_pwindow);
glfwWrapper::glfwHideWindow(m_pwindow);
}

bool bbe::Window::isShown() const
{
int visible = glfwGetWindowAttrib(m_pwindow, GLFW_VISIBLE);
int visible = glfwWrapper::glfwGetWindowAttrib(m_pwindow, GLFW_VISIBLE);
return visible;
}

Expand Down Expand Up @@ -287,7 +287,7 @@ void bbe::Window::setVideoRenderingMode(const char* path)

void bbe::Window::close()
{
glfwSetWindowShouldClose(m_pwindow, GLFW_TRUE);
glfwWrapper::glfwSetWindowShouldClose(m_pwindow, GLFW_TRUE);
}

void bbe::Window::registerCloseListener(const std::function<void()>& listener)
Expand Down Expand Up @@ -336,11 +336,11 @@ void bbe::INTERNAL_keyCallback(GLFWwindow * window, int keyCode, int scanCode, i
if (ImGui::GetIO().WantCaptureKeyboard) return;
if (action == GLFW_PRESS)
{
((bbe::Window*)glfwGetWindowUserPointer(window))->INTERNAL_keyboard.INTERNAL_press((bbe::Key)keyCode);
((bbe::Window*)glfwWrapper::glfwGetWindowUserPointer(window))->INTERNAL_keyboard.INTERNAL_press((bbe::Key)keyCode);
}
else if (action == GLFW_RELEASE)
{
((bbe::Window*)glfwGetWindowUserPointer(window))->INTERNAL_keyboard.INTERNAL_release((bbe::Key)keyCode);
((bbe::Window*)glfwWrapper::glfwGetWindowUserPointer(window))->INTERNAL_keyboard.INTERNAL_release((bbe::Key)keyCode);
}
#endif
}
Expand All @@ -360,12 +360,12 @@ void bbe::INTERNAL_cursorPosCallback(GLFWwindow * window, double xpos, double yp
float windowXScale = 0;
float windowYScale = 0;
glfwWrapper::glfwGetWindowContentScale(window, &windowXScale, &windowYScale);
((bbe::Window*)glfwGetWindowUserPointer(window))->INTERNAL_mouse.INTERNAL_moveMouse((float)(xpos / windowXScale), (float)(ypos / windowYScale));
((bbe::Window*)glfwWrapper::glfwGetWindowUserPointer(window))->INTERNAL_mouse.INTERNAL_moveMouse((float)(xpos / windowXScale), (float)(ypos / windowYScale));
}

void bbe::INTERNAL_windowResizeCallback(GLFWwindow * window, int width, int height)
{
((bbe::Window*)glfwGetWindowUserPointer(window))->INTERNAL_resize(width, height);
((bbe::Window*)glfwWrapper::glfwGetWindowUserPointer(window))->INTERNAL_resize(width, height);
}

void bbe::INTERNAL_mouseButtonCallback(GLFWwindow * window, int button, int action, int mods)
Expand All @@ -377,11 +377,11 @@ void bbe::INTERNAL_mouseButtonCallback(GLFWwindow * window, int button, int acti

if (action == GLFW_PRESS)
{
((bbe::Window*)glfwGetWindowUserPointer(window))->INTERNAL_mouse.INTERNAL_press((bbe::MouseButton)button);
((bbe::Window*)glfwWrapper::glfwGetWindowUserPointer(window))->INTERNAL_mouse.INTERNAL_press((bbe::MouseButton)button);
}
else if (action == GLFW_RELEASE)
{
((bbe::Window*)glfwGetWindowUserPointer(window))->INTERNAL_mouse.INTERNAL_release((bbe::MouseButton)button);
((bbe::Window*)glfwWrapper::glfwGetWindowUserPointer(window))->INTERNAL_mouse.INTERNAL_release((bbe::MouseButton)button);
}
}

Expand All @@ -391,19 +391,19 @@ void bbe::INTERNAL_mouseScrollCallback(GLFWwindow * window, double xoffset, doub
ImGui_ImplGlfw_ScrollCallback(window, xoffset, yoffset);
if (ImGui::GetIO().WantCaptureMouse) return;
#endif
((bbe::Window*)glfwGetWindowUserPointer(window))->INTERNAL_mouse.INTERNAL_scroll(static_cast<float>(xoffset), static_cast<float>(yoffset));
((bbe::Window*)glfwWrapper::glfwGetWindowUserPointer(window))->INTERNAL_mouse.INTERNAL_scroll(static_cast<float>(xoffset), static_cast<float>(yoffset));
}

void bbe::INTERNAL_windowCloseCallback(GLFWwindow* window)
{
switch (((bbe::Window*)glfwGetWindowUserPointer(window))->getWindowCloseMode())
switch (((bbe::Window*)glfwWrapper::glfwGetWindowUserPointer(window))->getWindowCloseMode())
{
case bbe::WindowCloseMode::CLOSE:
// Do nothing
break;
case bbe::WindowCloseMode::HIDE:
glfwSetWindowShouldClose(window, GLFW_FALSE);
glfwHideWindow(window);
glfwWrapper::glfwSetWindowShouldClose(window, GLFW_FALSE);
glfwWrapper::glfwHideWindow(window);
break;
default:
throw bbe::IllegalStateException();
Expand Down
46 changes: 46 additions & 0 deletions BrotBoxEngine/glfwWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,49 @@ void bbe::glfwWrapper::glfwSetClipboardString(GLFWwindow* handle, const char* st
::glfwSetClipboardString(handle, string);
#endif
}

void bbe::glfwWrapper::glfwSetWindowShouldClose(GLFWwindow* window, int value)
{
#ifndef BBE_RENDERER_NULL
::glfwSetWindowShouldClose(window, value);
#endif
}

void bbe::glfwWrapper::glfwShowWindow(GLFWwindow* window)
{
#ifndef BBE_RENDERER_NULL
::glfwShowWindow(window);
#endif
}

void bbe::glfwWrapper::glfwHideWindow(GLFWwindow* window)
{
#ifndef BBE_RENDERER_NULL
::glfwHideWindow(window);
#endif
}

int bbe::glfwWrapper::glfwGetWindowAttrib(GLFWwindow* window, int attrib)
{
#ifndef BBE_RENDERER_NULL
return ::glfwGetWindowAttrib(window, attrib);
#else
return 0;
#endif
}

void bbe::glfwWrapper::glfwSetWindowUserPointer(GLFWwindow* window, void* pointer)
{
#ifndef BBE_RENDERER_NULL
return ::glfwSetWindowUserPointer(window, pointer);
#endif
}

void* bbe::glfwWrapper::glfwGetWindowUserPointer(GLFWwindow* window)
{
#ifndef BBE_RENDERER_NULL
return ::glfwGetWindowUserPointer(window);
#else
return nullptr;
#endif
}

0 comments on commit faa1038

Please sign in to comment.