AGL
A graphics library
window.h
1 // Copyright 2020, Savvy Sine, Aline Normoyle
2 
3 #ifndef AGL_WINDOW_H_
4 #define AGL_WINDOW_H_
5 
6 #include <string>
7 #include <map>
8 #include "agl/agl.h"
9 #include "agl/aglm.h"
10 #include "agl/renderer.h"
11 #include "agl/camera.h"
12 
13 namespace agl {
14 
21 class Window {
22  public:
30  Window();
31  virtual ~Window();
32 
43  void run();
44 
56  bool screenshot(const std::string& filename);
57 
58  protected:
61 
77  virtual void setup() {}
78 
84  virtual void draw() {}
85 
96  virtual void mouseMotion(int x, int y, int dx, int dy) {}
97 
107  virtual void mouseDown(int button, int mods) {}
108 
115  virtual void mouseUp(int button, int mods) {}
116 
125  virtual void scroll(float dx, float dy) {}
126 
138  virtual void keyUp(int key, int mods) {}
139 
149  virtual void keyDown(int key, int mods) {}
150 
158  virtual void resize(int width, int height) {}
160 
163 
172  bool keyIsDown(int key) const;
173 
182  bool mouseIsDown(int button) const;
183 
187  glm::vec2 mousePosition() const;
188 
195  float dt() const; // amount of time since last frame
196 
200  float elapsedTime() const; // amount of time since start (can be reset)
201 
205  float height() const;
206 
210  float width() const;
212 
219  void noLoop();
220 
227  void background(const glm::vec3& color);
228 
231 
238  void setWindowSize(int w, int h);
239 
249  void setupOrthoScene(const glm::vec3& center, const glm::vec3& dim);
250 
260  void setupPerspectiveScene(const glm::vec3& center, const glm::vec3& dim);
261 
273  void lookAt(const glm::vec3& camPos,
274  const glm::vec3& camLook,
275  const glm::vec3& up = glm::vec3(0, 1, 0));
276 
282  void perspective(float fovRadians, float aspect, float near, float far);
283 
289  void ortho(float minx, float maxx,
290  float miny, float maxy, float minz, float maxz);
291 
296  inline bool cameraEnabled() const { return _cameraEnabled; }
297 
302  inline void setCameraEnabled(bool on) { _cameraEnabled = on; }
304 
305  private:
306  void init();
307 
308  static void onScrollCb(GLFWwindow* w, double xoffset, double yoffset);
309  static void onMouseMotionCb(GLFWwindow* w, double x, double y);
310  static void onMouseButtonCb(GLFWwindow* w, int button, int action, int mods);
311  static void onResizeCb(GLFWwindow* w, int width, int height);
312  static void onKeyboardCb(GLFWwindow* w,
313  int key, int code, int action, int mods);
314 
315  void onMouseMotion(int x, int y);
316  void onMouseButton(int button, int action, int mods);
317  void onKeyboard(int key, int scancode, int action, int mods);
318  void onResize(int width, int height);
319  void onScroll(float xoffset, float yoffset);
320 
321  protected:
322  Renderer renderer;
323  Camera camera;
324 
325  private:
326  int _windowWidth, _windowHeight;
327  float _elapsedTime;
328  float _dt;
329  float _lastx, _lasty;
330  bool _cameraEnabled;
331  glm::vec3 _backgroundColor;
332  struct GLFWwindow* _window = 0;
333 };
334 
335 } // namespace agl
336 #endif // AGL_WINDOW_H_
agl::Window::screenshot
bool screenshot(const std::string &filename)
Save the current screen image to a file.
Definition: window.cpp:137
agl::Window::keyDown
virtual void keyDown(int key, int mods)
Override this method to respond to key presses (button down)
Definition: window.h:149
agl::Window::perspective
void perspective(float fovRadians, float aspect, float near, float far)
Set the current projection to a perspective view.
Definition: window.cpp:102
agl::Window::run
void run()
Opens the window and starts the main application loop.
Definition: window.cpp:113
agl::Window::resize
virtual void resize(int width, int height)
Override this method to respond to window resizing.
Definition: window.h:158
agl::Window::setWindowSize
void setWindowSize(int w, int h)
Set the size of the window in pixels.
Definition: window.cpp:198
agl::Window::keyIsDown
bool keyIsDown(int key) const
Query whether the given key is down @key The key to test.
Definition: window.cpp:188
agl::Window::scroll
virtual void scroll(float dx, float dy)
Override this method to respond to scrolling the middle mouse button.
Definition: window.h:125
agl::Window::ortho
void ortho(float minx, float maxx, float miny, float maxy, float minz, float maxz)
Set the current projection to an orthographic view.
Definition: window.cpp:97
agl::Window::setup
virtual void setup()
Override this method to perform setup before the main application loop.
Definition: window.h:77
agl::Window::cameraEnabled
bool cameraEnabled() const
Returns true if the camera controls are active; false otherwise.
Definition: window.h:296
agl::Window::background
void background(const glm::vec3 &color)
Set the background color.
Definition: window.cpp:39
agl::Window::noLoop
void noLoop()
Stop the main application loop.
Definition: window.cpp:45
agl::Window::draw
virtual void draw()
Override this method to draw.
Definition: window.h:84
agl::Window::setupOrthoScene
void setupOrthoScene(const glm::vec3 &center, const glm::vec3 &dim)
Initialize the projection and camera to fit the given dimensions and center using an orthographic pro...
Definition: window.cpp:49
agl::Window
Manages the window and user input.
Definition: window.h:21
agl::Window::Window
Window()
Constructor.
Definition: window.cpp:23
agl::Window::mouseIsDown
bool mouseIsDown(int button) const
Query whether the given mouse button is down.
Definition: window.cpp:193
agl::Window::lookAt
void lookAt(const glm::vec3 &camPos, const glm::vec3 &camLook, const glm::vec3 &up=glm::vec3(0, 1, 0))
Definition: window.cpp:107
agl::Window::setCameraEnabled
void setCameraEnabled(bool on)
Set whether the window's camera controls are active.
Definition: window.h:302
agl::Window::mouseUp
virtual void mouseUp(int button, int mods)
Override this method to respond to mouse press (button up)
Definition: window.h:115
agl::Camera
Implements orbit and pan camera movement.
Definition: camera.h:19
agl::Window::elapsedTime
float elapsedTime() const
Return the amount of time since the setup() was called (in seconds)
Definition: window.cpp:178
agl::Renderer
The Renderer class draws meshes to the screen using shaders.
Definition: renderer.h:37
agl::Window::keyUp
virtual void keyUp(int key, int mods)
Override this method to respond to key presses (button up)
Definition: window.h:138
agl::Window::height
float height() const
Return the window height in pixels.
Definition: window.cpp:166
aglm.h
Global utilities and common third party includes.
agl::Window::setupPerspectiveScene
void setupPerspectiveScene(const glm::vec3 &center, const glm::vec3 &dim)
Initialize the projection and camera to fit the given dimensions and center using a perspective proje...
Definition: window.cpp:60
agl::Window::mouseMotion
virtual void mouseMotion(int x, int y, int dx, int dy)
Override this method to respond to mouse movement.
Definition: window.h:96
agl::Window::width
float width() const
Return the window width in pixels.
Definition: window.cpp:170
agl::Window::dt
float dt() const
Return the amount of time since the previous frame (in seconds)
Definition: window.cpp:174
agl::Window::mouseDown
virtual void mouseDown(int button, int mods)
Override this method to respond to mouse press (button down)
Definition: window.h:107
agl::Window::mousePosition
glm::vec2 mousePosition() const
Return the current mouse position (in screen coordinates)
Definition: window.cpp:182