Loading [MathJax]/extensions/TeX/AMSmath.js
Radium Engine  1.5.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
OpenGLContext.hpp
1 #pragma once
2 #include <Core/Utils/Observable.hpp>
3 #include <Headless/RaHeadless.hpp>
4 
5 #include <array>
6 #include <functional>
7 #include <string>
8 
9 #include <glbinding-aux/ContextInfo.h>
10 #include <glbinding/Version.h>
11 #include <glbinding/gl/gl.h>
12 
13 namespace Ra {
14 namespace Headless {
15 
22 class HEADLESS_API OpenGLContext
23 {
24  public:
41  explicit OpenGLContext( const glbinding::Version& /*glVersion*/ = { 4, 1 },
42  const std::array<int, 2>& /*size*/ = { { 1, 1 } } ) {};
44  virtual ~OpenGLContext() = default;
46  virtual void makeCurrent() const = 0;
48  virtual void doneCurrent() const = 0;
50  virtual bool isValid() const = 0;
51 
53  virtual bool isWindow() const { return false; }
54 
56  [[nodiscard]] virtual std::string getInfo() const;
67  enum class EventMode : int { POLL = 0, WAIT, TIMEOUT, NUM_MODES };
69  virtual void show( EventMode /*mode*/, float /*delay*/ ) {};
71  virtual void hide() {};
73  virtual void resize( const std::array<int, 2>& /*size*/ ) {};
75  virtual void renderLoop( std::function<void( float )> /*render*/ ) {};
76 
84 
85  // TODO : give access to the DPI ratio
86  // https://www.glfw.org/docs/latest/window_guide.html#window_scale
87 
95  return m_keyboardObservers;
96  }
97 
107  return m_mouseObservers;
108  }
109 
117  Ra::Core::Utils::Observable<int, int>& mouseMoveListener() { return m_mouseMoveObservers; }
118 
125  Ra::Core::Utils::Observable<int, int>& scrollListener() { return m_scrollObservers; }
126 
128  protected:
133  virtual bool processEvents() { return true; };
134 
136  void resizeFrameBuffer( int width, int height );
139 
141  void keyboardEventCallback( int key, int scancode, int action, int mods );
144 
146  void mouseEventCallback( int button, int action, int mods, int x, int y );
149 
151  void mouseMoveEventCallback( int x, int y );
154 
156  void scrollEventCallback( int xoffset, int yoffset );
159 
161  EventMode m_mode { EventMode::POLL };
163  float m_delay { 1.f / 60.f };
165 };
166 
167 inline std::string OpenGLContext::getInfo() const {
168  std::stringstream infoText;
169  using ContextInfo = glbinding::aux::ContextInfo;
170  makeCurrent();
171  infoText << "*** OffScreen OpenGL context ***" << std::endl;
172  infoText << "Renderer (glbinding) : " << ContextInfo::renderer() << "\n";
173  infoText << "Vendor (glbinding) : " << ContextInfo::vendor() << "\n";
174  infoText << "OpenGL (glbinding) : " << ContextInfo::version().toString() << "\n";
175  infoText << "GLSL : " << gl::glGetString( gl::GL_SHADING_LANGUAGE_VERSION )
176  << "\n";
177  doneCurrent();
178 
179  return infoText.str();
180 }
181 
182 inline void OpenGLContext::resizeFrameBuffer( int width, int height ) {
183  gl::glViewport( 0, 0, width, height );
184  m_resizers.notify( width, height );
185 }
186 
187 inline void OpenGLContext::keyboardEventCallback( int key, int scancode, int action, int mods ) {
188  m_keyboardObservers.notify( key, scancode, action, mods );
189 }
190 
191 inline void OpenGLContext::mouseEventCallback( int button, int action, int mods, int x, int y ) {
192  m_mouseObservers.notify( button, action, mods, x, y );
193 }
194 
195 inline void OpenGLContext::mouseMoveEventCallback( int x, int y ) {
197 }
198 
199 inline void OpenGLContext::scrollEventCallback( int xoffset, int yoffset ) {
200  m_scrollObservers.notify( xoffset, yoffset );
201 }
202 
203 } // namespace Headless
204 } // namespace Ra
void notify(Args... p) const
Notify (i.e. call) each attached observer with argument p.
Definition: Observable.hpp:67
virtual ~OpenGLContext()=default
destructor
virtual void doneCurrent() const =0
make the context inactive
virtual void makeCurrent() const =0
make the context active
virtual std::string getInfo() const
Return a string identifying the openGL Context and its supported versions.
OpenGLContext(const glbinding::Version &={ 4, 1 }, const std::array< int, 2 > &={ { 1, 1 } })
virtual bool isValid() const =0
Check for validity of the context.
virtual bool isWindow() const
Check if the context is associated to a window.
Ra::Core::Utils::Observable< int, int > m_scrollObservers
Scroll event observable.
void scrollEventCallback(int xoffset, int yoffset)
Scroll callback.
void mouseEventCallback(int button, int action, int mods, int x, int y)
Mouse callback.
virtual void resize(const std::array< int, 2 > &)
Resize the window.
Ra::Core::Utils::Observable< int, int, int, int > & keyboardListener()
Ra::Core::Utils::Observable< int, int > m_resizers
Resize event observable.
Ra::Core::Utils::Observable< int, int > & resizeListener()
virtual void hide()
Hide the window.
Ra::Core::Utils::Observable< int, int > m_mouseMoveObservers
Mouse move event observable.
virtual void renderLoop(std::function< void(float)>)
loop on events and execute the functor render after each event
void mouseMoveEventCallback(int x, int y)
Mouse move callback.
Ra::Core::Utils::Observable< int, int > & scrollListener()
Ra::Core::Utils::Observable< int, int, int, int, int > & mouseListener()
void resizeFrameBuffer(int width, int height)
Resize callback.
void keyboardEventCallback(int key, int scancode, int action, int mods)
Keyboard callback.
Ra::Core::Utils::Observable< int, int, int, int > m_keyboardObservers
Keyboard event observable.
Ra::Core::Utils::Observable< int, int, int, int, int > m_mouseObservers
Mouse event observable.
virtual bool processEvents()
Process the pending events according to the window show mode.
virtual void show(EventMode, float)
Show the window.
Ra::Core::Utils::Observable< int, int > & mouseMoveListener()
Definition: Cage.cpp:3