trase
BackendGL.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2018, University of Oxford.
3 All rights reserved.
4 
5 University of Oxford means the Chancellor, Masters and Scholars of the
6 University of Oxford, having an administrative office at Wellington
7 Square, Oxford OX1 2JD, UK.
8 
9 This file is part of the Oxford RSE C++ Template project.
10 
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are met:
13 * Redistributions of source code must retain the above copyright notice, this
14  list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16  this list of conditions and the following disclaimer in the documentation
17  and/or other materials provided with the distribution.
18 * Neither the name of the copyright holder nor the names of its
19  contributors may be used to endorse or promote products derived from
20  this software without specific prior written permission.
21 
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
35 
36 #ifndef BACKENDGL_H_
37 #define BACKENDGL_H_
38 
39 // forward declare GL and nanovg stuff (included in BackendGL.cpp)
40 struct NVGcontext;
41 struct GLFWwindow;
42 
43 #include <array>
44 #include <cstdio>
45 #include <iostream>
46 
47 #include "backend/Backend.hpp"
48 #include "util/BBox.hpp"
49 #include "util/Colors.hpp"
50 #include "util/Exception.hpp"
51 #include "util/Vector.hpp"
52 
53 namespace trase {
54 
55 class BackendGL : public Backend {
56  GLFWwindow *m_window;
57  NVGcontext *m_vg;
58  FontManager m_fm;
59  static bool m_lbutton_down;
60  static vfloat2_t m_lbutton_down_mouse_pos;
61 
62 public:
63  TRASE_BACKEND_VISITABLE()
64 
65 
66  void init(const vfloat2_t &pixels, const char *name);
69 
71  void finalise();
72 
76 
79  void end_frame();
80 
82  bool is_interactive();
83 
86  bool should_close();
87 
89  float get_time();
90 
91  static void set_mouse_down(const vfloat2_t &mouse_pos);
92  static void set_mouse_up();
93 
96 
98  bool mouse_dragging();
99 
102 
104  void mouse_drag_reset_delta();
105 
108  void scissor(const bfloat2_t &x);
109 
111  void reset_scissor();
112 
114  void rotate(const float angle);
115 
117  void translate(const vfloat2_t &v);
118 
120  void reset_transform();
121 
124  void begin_path();
125 
129  void move_to(const vfloat2_t &x);
130 
134  void line_to(const vfloat2_t &x);
135 
138  void stroke();
139 
142  void fill();
143 
146  void stroke_width(const float lw);
147 
150  void stroke_color(const RGBA &color);
151 
154  void fill_color(const RGBA &color);
155 
159  void rounded_rect(const bfloat2_t &x, const float r);
160 
163  void rect(const bfloat2_t &x);
164 
166  void circle(const vfloat2_t &centre, float radius);
167 
172  void text(const vfloat2_t &x, const char *string, const char *end);
173 
177  bfloat2_t text_bounds(const vfloat2_t &x, const char *string);
178 
181  void font_size(float size);
182 
185  void font_face(const char *face);
186 
189  void font_blur(const float blur);
190 
193  void text_align(const int align);
194 
195 private:
196  NVGcontext *init_nanovg(const vint2_t &pixels);
197  GLFWwindow *create_window(const vint2_t &pixels, const char *name);
198 };
199 
200 } // namespace trase
201 
202 #endif // BACKENDGL_H_
a base class for all the backends that support drawing a single frame
Definition: Backend.hpp:50
void font_size(float size)
Sets the current font height in pixels.
Definition: BackendGL.cpp:153
void stroke_color(const RGBA &color)
Set the current stroke color.
Definition: BackendGL.cpp:146
void reset_scissor()
Remove a previously call to scissor()
Definition: BackendGL.cpp:114
void rounded_rect(const bfloat2_t &x, const float r)
Draw a rectangle with rounded corners.
Definition: BackendGL.cpp:123
void fill_color(const RGBA &color)
Set the current fill color.
Definition: BackendGL.cpp:169
void text(const vfloat2_t &x, const char *string, const char *end)
Draw the given text to the screen.
Definition: BackendGL.cpp:173
float get_time()
Get the current time.
Definition: BackendGL.cpp:79
void rect(const bfloat2_t &x)
Draw a rectangle.
Definition: BackendGL.cpp:130
void move_to(const vfloat2_t &x)
Extends the current path.
Definition: BackendGL.cpp:144
bool mouse_dragging()
Returns true if the mouse button is clicked and the mouse is dragging.
Definition: BackendGL.cpp:93
void mouse_drag_reset_delta()
Reset drag delta to zero.
Definition: BackendGL.cpp:104
An N-dimensional vector class.
Definition: Vector.hpp:59
void scissor(const bfloat2_t &x)
All subsequent drawing calls will be masked to only show within the bounding box x.
Definition: BackendGL.cpp:108
void line_to(const vfloat2_t &x)
Extends the current path.
Definition: BackendGL.cpp:145
void rotate(const float angle)
Apply a rotation of angle to the current drawing transform.
Definition: BackendGL.cpp:116
Definition: BackendGL.hpp:55
void stroke()
Draw a line along the completed path.
Definition: BackendGL.cpp:151
bool is_interactive()
Return true if this backend has mouse interaction.
Definition: BackendGL.cpp:77
vfloat2_t mouse_drag_delta()
Current delta position of the dragging mouse.
Definition: BackendGL.cpp:97
void end_frame()
End the current frame.
Definition: BackendGL.cpp:225
Colour class with red, green, blue and alpha in range 0 to 255.
Definition: Colors.hpp:48
void stroke_width(const float lw)
Set the current stroke width.
Definition: BackendGL.cpp:150
Definition: Backend.hpp:171
void begin_path()
Begin a path.
Definition: BackendGL.cpp:122
void translate(const vfloat2_t &v)
Apply a translation of v to the current drawing transform.
Definition: BackendGL.cpp:117
void circle(const vfloat2_t &centre, float radius)
Draw a circle with a given centre and radius.
Definition: BackendGL.cpp:138
void text_align(const int align)
Sets the alignment of the text to its position.
Definition: BackendGL.cpp:168
bfloat2_t text_bounds(const vfloat2_t &x, const char *string)
Returns the bounding box covering the given text.
Definition: BackendGL.cpp:177
void font_blur(const float blur)
Sets the amount of blur to apply to the drawn text.
Definition: BackendGL.cpp:167
void reset_transform()
Reset the drawing transform to the identity matrix.
Definition: BackendGL.cpp:120
bool should_close()
Returns true if the current window should close (e.g.
Definition: BackendGL.cpp:188
void font_face(const char *face)
Sets the current font name.
Definition: BackendGL.cpp:154
void finalise()
Finalise this backend. Call after all drawing finished.
Definition: BackendGL.cpp:192
Definition: Backend.cpp:39
vfloat2_t get_mouse_pos()
Get the current position of the mouse in pixel units.
Definition: BackendGL.cpp:87
vfloat2_t begin_frame()
Begin a new frame.
Definition: BackendGL.cpp:200
void init(const vfloat2_t &pixels, const char *name)
Initialise this backend.
Definition: BackendGL.cpp:70
void fill()
Draw a line along the completed path and fill it in.
Definition: BackendGL.cpp:152