Browse Source

Abstract GLFW away from main loop close

master
Riyyi 4 years ago
parent
commit
349257eba0
  1. 24
      inferno/src/inferno/application.cpp
  2. 2
      inferno/src/inferno/application.h
  3. 10
      inferno/src/inferno/input.cpp
  4. 3
      inferno/src/inferno/input.h
  5. 9
      inferno/src/inferno/window.cpp
  6. 2
      inferno/src/inferno/window.h

24
inferno/src/inferno/application.cpp

@ -5,6 +5,7 @@
#include "inferno/core.h" #include "inferno/core.h"
#include "inferno/event/applicationevent.h" #include "inferno/event/applicationevent.h"
#include "inferno/event/event.h" #include "inferno/event/event.h"
#include "inferno/event/keyevent.h"
#include "inferno/input.h" #include "inferno/input.h"
#include "inferno/log.h" #include "inferno/log.h"
#include "inferno/settings.h" #include "inferno/settings.h"
@ -103,11 +104,7 @@ namespace Inferno {
{ {
dbg() << "Application startup"; dbg() << "Application startup";
while(!glfwWindowShouldClose(m_window->getWindow())) { while(!m_window->shouldClose()) {
if(glfwGetKey(m_window->getWindow(), GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(m_window->getWindow(), GL_TRUE);
}
Command::clearColor({ 0.2f, 0.3f, 0.3f, 1.0f }); Command::clearColor({ 0.2f, 0.3f, 0.3f, 1.0f });
Command::clear(); Command::clear();
@ -137,6 +134,7 @@ namespace Inferno {
EventDispatcher dispatcher(e); EventDispatcher dispatcher(e);
dispatcher.dispatch<WindowCloseEvent>(NF_BIND_EVENT(Application::onWindowClose)); dispatcher.dispatch<WindowCloseEvent>(NF_BIND_EVENT(Application::onWindowClose));
dispatcher.dispatch<WindowResizeEvent>(NF_BIND_EVENT(Application::onWindowResize)); dispatcher.dispatch<WindowResizeEvent>(NF_BIND_EVENT(Application::onWindowResize));
dispatcher.dispatch<KeyPressEvent>(NF_BIND_EVENT(Application::onKeyPress));
} }
bool Application::onWindowClose(WindowCloseEvent& e) bool Application::onWindowClose(WindowCloseEvent& e)
@ -163,4 +161,20 @@ namespace Inferno {
return true; return true;
} }
bool Application::onKeyPress(KeyPressEvent& e)
{
// Suppress unused warning
(void)e;
dbgln(Log::Info, "KeyPressEvent {} ({}) triggered",
Input::getKeyName(e.getKey()),
e.getKey());
// Stop the main loop on 'Escape' keypress
if (e.getKey() == GLFW_KEY_ESCAPE) {
m_window->setShouldClose(true);
}
return true;
}
} }

2
inferno/src/inferno/application.h

@ -6,6 +6,7 @@
namespace Inferno { namespace Inferno {
class Event; class Event;
class KeyPressEvent;
class Texture; class Texture;
class TextureManager; class TextureManager;
class Window; class Window;
@ -25,6 +26,7 @@ namespace Inferno {
void onEvent(Event& e); void onEvent(Event& e);
bool onWindowClose(WindowCloseEvent& e); bool onWindowClose(WindowCloseEvent& e);
bool onWindowResize(WindowResizeEvent& e); bool onWindowResize(WindowResizeEvent& e);
bool onKeyPress(KeyPressEvent& e);
// ----------------------------------------- // -----------------------------------------

10
inferno/src/inferno/input.cpp

@ -68,4 +68,14 @@ namespace Inferno {
return getMousePosition().second; return getMousePosition().second;
} }
const char* Input::getKeyName(int key)
{
return glfwGetKeyName(key, getKeyScancode(key));
}
int Input::getKeyScancode(int key)
{
return glfwGetKeyScancode(key);
}
} }

3
inferno/src/inferno/input.h

@ -19,6 +19,9 @@ namespace Inferno {
static float getMouseX(); static float getMouseX();
static float getMouseY(); static float getMouseY();
static const char* getKeyName(int key);
static int getKeyScancode(int key);
// ----------------------------------------- // -----------------------------------------
static inline float getXOffset() { return m_xOffset; } static inline float getXOffset() { return m_xOffset; }

9
inferno/src/inferno/window.cpp

@ -209,4 +209,13 @@ namespace Inferno {
glfwSetWindowMonitor(m_window, monitor, xPos, yPos, width, height, refresh); glfwSetWindowMonitor(m_window, monitor, xPos, yPos, width, height, refresh);
} }
bool Window::shouldClose() const {
return glfwWindowShouldClose(m_window);
}
void Window::setShouldClose(bool close) const
{
glfwSetWindowShouldClose(m_window, close ? GL_TRUE : GL_FALSE);
}
} }

2
inferno/src/inferno/window.h

@ -33,6 +33,8 @@ namespace Inferno {
// ----------------------------------------- // -----------------------------------------
void setWindowMonitor(); void setWindowMonitor();
bool shouldClose() const;
void setShouldClose(bool close) const;
inline int getWidth() const { return m_windowProperties.width; } inline int getWidth() const { return m_windowProperties.width; }
inline int getHeight() const { return m_windowProperties.height; } inline int getHeight() const { return m_windowProperties.height; }

Loading…
Cancel
Save